from core.infrastructure.state_management.global_state_controller import is_synced from gui.v2.actions.menu_coordinator import sync_now from core.models.Result import Result from gui.v2.presenters.state.arrow_sync_state import apply_arrow_state from gui.v2.ui.styles.css.fast_register import ENTER_NAME_TEXT, NAME_TEXT_STYLE, GROUP_TITLE_LABEL, DISPLAY_BOX_STYLE from gui.v2.ui.styles.css.editor_css import PRE_SYNC_LAYOUT from pathlib import Path from PyQt6 import QtCore from PyQt6.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QSizePolicy, QLineEdit from PyQt6.QtGui import QPixmap from PyQt6.QtCore import Qt from PyQt6.QtGui import QIcon from PyQt6.QtCore import QSize class TextInputGroup(QWidget): """Create Text Input Boxes of a particular style""" def __init__(self, parent, title, category, state): super().__init__(parent) self.category = category self.state = state # Match the width of the SelectionGroup self.setMinimumWidth(260) self.main_layout = QVBoxLayout(self) self.main_layout.setContentsMargins(0, 0, 0, 0) self.main_layout.setSpacing(5) self.main_layout.setAlignment(Qt.AlignmentFlag.AlignTop) # Same title style as SelectionGroup self.title_label = QLabel(title) self.title_label.setStyleSheet(NAME_TEXT_STYLE) self.main_layout.addWidget(self.title_label) # Row layout to hold the input field self.row_layout = QHBoxLayout() self.row_layout.setSpacing(10) self.text_input = QLineEdit() # Match the display_box styling self.text_input.setFixedSize(240, 35) # Slightly shorter than the image box for better text look self.text_input.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) self.text_input.setStyleSheet(ENTER_NAME_TEXT) self.row_layout.addWidget(self.text_input) # Center the input field within the row layout self.row_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) self.main_layout.addLayout(self.row_layout) # DEFAULT OPTIONS: current_state = self.state.get_final_choices() if current_state[category] is not None: self.text_input.setText(current_state[category]) class SelectionGroup(QWidget): def __init__(self, parent, title, category, state, sync_required: bool = False, btn_path = None): super().__init__(parent) self.category = category self.state = state self.btn_path = btn_path self.sync_required = sync_required # Tell the state: "When this category changes, run my update_display method" self.state.register_listener(category, self.update_display) self.main_layout = QVBoxLayout(self) self.main_layout.setContentsMargins(0, 0, 0, 0) self.main_layout.setSpacing(5) self.main_layout.setAlignment(Qt.AlignmentFlag.AlignTop) self.title_label = QLabel(title) self.title_label.setStyleSheet(GROUP_TITLE_LABEL) self.main_layout.addWidget(self.title_label) self.row_layout = QHBoxLayout() self.row_layout.setSpacing(10) self.btn_prev = QPushButton() self.btn_prev.clicked.connect(lambda: self.sync_or_continue(-1)) self.display_box = QLabel() self.display_box.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter) self.display_box.setFixedSize(185, 75) self.display_box.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) self.display_box.setStyleSheet(DISPLAY_BOX_STYLE) self.btn_next = QPushButton() self.btn_next.clicked.connect(lambda: self.sync_or_continue(1)) self.apply_styles_to_buttons(sync_required=self.sync_required) # These stretchers force buttons to the edges and image to the center self.row_layout.addWidget(self.btn_prev) self.row_layout.addStretch(1) self.row_layout.addWidget(self.display_box) self.row_layout.addStretch(1) self.row_layout.addWidget(self.btn_next) self.main_layout.addLayout(self.row_layout) self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred) self.setMinimumHeight(110) def showEvent(self, event): super().showEvent(event) self.update_display() def apply_styles_to_buttons(self, sync_required: bool): buttons = { self.btn_next: "right", self.btn_prev: "left" } for each_button in buttons: apply_arrow_state(sync_required=sync_required, button=each_button, direction=buttons[each_button], button_path=self.btn_path) def after_sync(self, result: Result): if result.valid: self.apply_styles_to_buttons(sync_required=False) def sync_or_continue(self, direction: int): if not self.sync_required: self.change_image(direction) return is_it_synced = is_synced() if is_it_synced: self.change_image(direction) else: sync_now(ui_caller=self.after_sync) def change_image(self, direction): """Toggle the next image change by category and direction""" self.state.cycle_selection(self.category, direction) def update_display(self): """Get and Change the currently displayed image. This function is passed as a callback to the state""" path = self.state.get_current_image(self.category) self.set_image(path) def set_image(self, image_source): # 1. Convert source to QPixmap regardless of what it is if isinstance(image_source, Path): pixmap = QPixmap(str(image_source)) elif isinstance(image_source, QPixmap): pixmap = image_source else: return if pixmap.isNull(): return # 2. Apply the scaling logic, built in the previous step box_width = self.display_box.width() box_height = self.display_box.height() if pixmap.width() > box_width or pixmap.height() > box_height: scaled_pixmap = pixmap.scaled( box_width, box_height, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.SmoothTransformation ) self.display_box.setPixmap(scaled_pixmap) else: self.display_box.setPixmap(pixmap)