From 96c2495a8bb970f367586a4c6cb7a1d4b178eb41 Mon Sep 17 00:00:00 2001 From: bartool Date: Tue, 14 Oct 2025 08:49:20 +0200 Subject: [PATCH] refactor(MainController): Simplify camera button signal handling Refactor the signal handling for the camera start/stop button in `MainController` to simplify logic and improve reliability. - Replaced the dynamic connect/disconnect pattern with a single, persistent signal connection to `_on_start_button_clicked`. - Centralized UI state updates (e.g., button text) into a new `_update_start_button_state` method. - This eliminates potential errors from mismatched signal connections and makes the control flow easier to follow. - Also fixes major indentation errors caused by a previous faulty replacement. --- controllers/main_controller.py | 45 +++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/controllers/main_controller.py b/controllers/main_controller.py index 59c304a..5afb9a5 100644 --- a/controllers/main_controller.py +++ b/controllers/main_controller.py @@ -49,7 +49,7 @@ class MainController: # UI control signals self.photo_button.clicked.connect(self.take_photo) - self.welcome_view.camera_start_btn.clicked.connect(self.camera_detect) + self.welcome_view.camera_start_btn.clicked.connect(self._on_start_button_clicked) self.live_view.rotateCW.connect(self.camera_manager.rotate_right) self.live_view.rotateCCW.connect(self.camera_manager.rotate_left) @@ -63,6 +63,15 @@ class MainController: self.camera_manager.shutdown() self.db.disconnect() + def _update_start_button_state(self): + """Updates the text and state of the start/stop button.""" + if self.camera_manager.get_active_camera_info(): + self.welcome_view.set_button_text("Zatrzymaj kamerÄ™") + elif self.camera_manager.get_detected_cameras(): + self.welcome_view.set_button_text("Uruchom kamerÄ™") + else: + self.welcome_view.set_button_text("Wykryj kamery") + # --- Slots for Database/Media --- @Slot(str) @@ -93,16 +102,8 @@ class MainController: """Handles the list of detected cameras.""" print("Detected cameras:", cameras) self.welcome_view.set_info_text(f"Detected {len(cameras)} cameras.") + self._update_start_button_state() - self.welcome_view.camera_start_btn.clicked.disconnect() - - if len(cameras) == 0: - self.welcome_view.set_button_text("Wykryj kamery") - self.welcome_view.camera_start_btn.clicked.connect(self.camera_detect) - else: - self.welcome_view.set_button_text("Uruchom kamere") - self.welcome_view.camera_start_btn.clicked.connect(self.start_liveview) - # Populate a combobox in the UI here # self.view.camera_combobox.clear() # for camera in cameras: @@ -118,29 +119,33 @@ class MainController: """Shows an error message from the camera.""" print(f"Camera Error: {error_message}") self.welcome_view.set_error_text(error_message) + self._update_start_button_state() @Slot() def on_camera_started(self): """Updates UI when the camera stream starts.""" self.split_view.toggle_live_view() - self.welcome_view.set_button_text("Stop Camera") - # Re-route button click to stop the camera - self.welcome_view.camera_start_btn.clicked.disconnect() - self.welcome_view.camera_start_btn.clicked.connect(self.stop_liveview) - + self._update_start_button_state() @Slot() def on_camera_stopped(self): """Updates UI when the camera stream stops.""" - # self.split_view.show_placeholder() - self.welcome_view.set_button_text("Start Camera") - # Re-route button click to start the camera - self.welcome_view.camera_start_btn.clicked.disconnect() - self.welcome_view.camera_start_btn.clicked.connect(self.start_liveview) + self.split_view.toggle_live_view() + self._update_start_button_state() # --- UI Actions --- + def _on_start_button_clicked(self): + """Handles clicks on the main start/stop/detect button.""" + if self.camera_manager.get_active_camera_info(): + self.stop_liveview() + elif self.camera_manager.get_detected_cameras(): + self.start_liveview() + else: + self.camera_detect() + def camera_detect(self): + self.welcome_view.set_info_text("Wykrywanie kamer...") self.camera_manager.detect_cameras() def start_liveview(self):