hydraveil-gui/gui/v2/ui/pages/fast7b.py

206 lines
8.2 KiB
Python

from gui.v2.presenters.state.fast_registration_state import RegistrationState
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.pages.Page import Page
from pathlib import Path
from PyQt6 import QtWidgets, QtCore, QtGui
from PyQt6.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QGridLayout, QSizePolicy, QLineEdit
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt, pyqtSignal
class TextInputGroup(QWidget):
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()
# Set initial value from state if it exists
# initial_text = self.state.get_value(self.category) if hasattr(self.state, 'get_value') else ""
# self.text_input.setText(initial_text)
# 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)
# Update state in real-time as user types
self.text_input.textChanged.connect(self.update_state)
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)
def update_state(self, text):
if hasattr(self.state, 'set_value'):
self.state.set_value(self.category, text)
class SelectionGroup(QWidget):
# Define a signal that tells the world this widget's value changed
valueChanged = pyqtSignal(str) # Pass the category name
def __init__(self, parent, title, category, state):
super().__init__(parent)
self.category = category
self.state = state
# Ensure the group widget itself is wide enough to hold the layout
# (30px prev + 185px box + 30px next + spacing)
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)
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) # Add a little gap between buttons and box
self.btn_prev = QPushButton("<")
self.btn_prev.setFixedWidth(30)
self.btn_prev.clicked.connect(lambda: self.change_image(-1))
self.display_box = QLabel()
self.display_box.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
# --- STRICT SIZE POLICY ---
self.display_box.setFixedSize(185, 75)
# widget MUST be exactly this size
self.display_box.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
self.display_box.setStyleSheet(DISPLAY_BOX_STYLE)
self.btn_next = QPushButton(">")
self.btn_next.setFixedWidth(30)
self.btn_next.clicked.connect(lambda: self.change_image(1))
# Add widgets without stretch factors to maintain the fixed-size integrity
self.row_layout.addWidget(self.btn_prev)
self.row_layout.addWidget(self.display_box)
self.row_layout.addWidget(self.btn_next)
self.main_layout.addLayout(self.row_layout)
def showEvent(self, event):
super().showEvent(event)
self.update_display()
def change_image(self, direction):
new_path = self.state.cycle_selection(self.category, direction)
self.set_image(new_path)
# Emit the signal so the parent knows something happened
self.valueChanged.emit(self.category)
def update_display(self):
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 we 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)
class FastRegistrationPage(Page):
def __init__(self, page_stack, main_window, prepared=None):
super().__init__("FastRegistration", page_stack, main_window)
# 1. Initialize State
self.state = RegistrationState(btn_path=self.btn_path)
# Banner temp disabled:
# self.title.setGeometry(550, 40, 250, 40)
# self.title.setText("Fast Registration")
self.button_apply.setVisible(True)
self.button_back.setVisible(True)
self.button_apply.clicked.connect(self.submit_registration)
self.setFixedSize(800, 600)
self.content_area = QWidget(self)
self.content_area.setGeometry(100, 60, 600, 370)
self.grid = QGridLayout(self.content_area)
self.grid.setSpacing(30)
self.grid.setContentsMargins(0, 0, 0, 0)
# 2. Create groups, passing the category string and the state object
self.group_type = SelectionGroup(self.content_area, "Profile Type", "Type", self.state)
self.group_name = TextInputGroup(self.content_area, "Name", "Name", self.state)
self.group_proto = SelectionGroup(self.content_area, "Protocol", "Protocol", self.state)
self.group_loc = SelectionGroup(self.content_area, "Location", "Location", self.state)
self.group_browser = SelectionGroup(self.content_area, "Browser", "Browser", self.state)
self.group_res = SelectionGroup(self.content_area, "Resolution", "Resolution", self.state)
# Connect the signal: When Type changes, tell Protocol to refresh
self.group_type.valueChanged.connect(self.handle_type_change)
self.grid.addWidget(self.group_type, 0, 0, Qt.AlignmentFlag.AlignTop)
self.grid.addWidget(self.group_name, 0, 1, Qt.AlignmentFlag.AlignTop)
self.grid.addWidget(self.group_proto, 1, 0, Qt.AlignmentFlag.AlignTop)
self.grid.addWidget(self.group_loc, 1, 1, Qt.AlignmentFlag.AlignTop)
self.grid.addWidget(self.group_browser, 2, 0, Qt.AlignmentFlag.AlignTop)
self.grid.addWidget(self.group_res, 2, 1, Qt.AlignmentFlag.AlignTop)
self.grid.setRowStretch(3, 1)
def handle_type_change(self, category):
"""Redraw other relevant widgets that may be affected by a change"""
if category == "Type":
# Protocol is affected by Type
self.group_proto.update_display()
def submit_registration(self):
"""Get current choices from state, and submit."""
final_choices = self.state.get_final_choices()
print(f"Submitting choices: {final_choices}")