Refactor camera control logic in `MainController` to use the State design pattern. - Create a new `core/camera/states.py` module with state classes (`NoCamerasState`, `DetectingState`, `ReadyToStreamState`, `StreamingState`). - `MainController` now acts as a context, delegating actions to the current state object. - This replaces complex conditional logic with a robust, scalable, and maintainable state machine, making it easier to manage camera behavior and add new states in the future.
58 lines
2.3 KiB
Python
58 lines
2.3 KiB
Python
from __future__ import annotations
|
|
from abc import ABC, abstractmethod
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from controllers.main_controller import MainController
|
|
|
|
class CameraState(ABC):
|
|
"""Abstract base class for all camera states."""
|
|
|
|
def enter_state(self, controller: MainController):
|
|
"""Called upon entering the state, e.g., to update the UI."""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def handle_start_button(self, controller: MainController):
|
|
"""Handles the main camera button click."""
|
|
pass
|
|
|
|
class NoCamerasState(CameraState):
|
|
def enter_state(self, controller: MainController):
|
|
controller.welcome_view.set_button_text("Wykryj kamery")
|
|
controller.welcome_view.set_info_text("Nie wykryto kamer. Kliknij, aby rozpocząć.")
|
|
controller.welcome_view.camera_start_btn.setEnabled(True)
|
|
|
|
def handle_start_button(self, controller: MainController):
|
|
controller.camera_detect()
|
|
|
|
class DetectingState(CameraState):
|
|
def enter_state(self, controller: MainController):
|
|
controller.welcome_view.set_button_text("Wykrywanie...")
|
|
controller.welcome_view.set_info_text("Trwa wykrywanie kamer...")
|
|
controller.welcome_view.camera_start_btn.setEnabled(False)
|
|
|
|
def handle_start_button(self, controller: MainController):
|
|
# Do nothing while detecting
|
|
pass
|
|
|
|
class ReadyToStreamState(CameraState):
|
|
def enter_state(self, controller: MainController):
|
|
cameras = controller.camera_manager.get_detected_cameras()
|
|
controller.welcome_view.set_button_text("Uruchom kamerę")
|
|
controller.welcome_view.set_info_text(f"Wykryto {len(cameras)} kamer(y).")
|
|
controller.welcome_view.camera_start_btn.setEnabled(True)
|
|
|
|
def handle_start_button(self, controller: MainController):
|
|
controller.start_liveview()
|
|
|
|
class StreamingState(CameraState):
|
|
def enter_state(self, controller: MainController):
|
|
controller.welcome_view.set_button_text("Zatrzymaj kamerę")
|
|
controller.welcome_view.camera_start_btn.setEnabled(True)
|
|
if controller.split_view.stack.currentWidget() != controller.live_view:
|
|
controller.split_view.toggle_live_view()
|
|
|
|
def handle_start_button(self, controller: MainController):
|
|
controller.stop_liveview()
|