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

251 lines
9.9 KiB
Python

from core.models.Result import Result
from gui.v2.actions.profile_actions import create_profile
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, FAST_REGISTER_ONE, FAST_REGISTER_TWO
from gui.v2.ui.pages.Page import Page
from gui.v2.ui.pages.profile_creation.resolution_widget import CustomResolutionDialog
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):
"""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()
# 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):
"""Create Image/Value Select Boxes of a particular style"""
def __init__(self, parent, title, category, state):
super().__init__(parent)
self.category = category
self.state = state
# Tell the state: "When this category changes, run my update_display method"
self.state.register_listener(category, self.update_display)
# Ensure the group widget itself is wide enough to hold the layout
# (Legacy specs were: 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):
"""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)
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.button_back.clicked.connect(self.reverse)
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)
# 3. adding groups in rows of 2 items each:
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)
# 4. Custom options, such as Resolution:
custom_button = QPushButton("Custom Res", self)
custom_button.setGeometry(435, 430, 185, 30)
custom_button.setStyleSheet(FAST_REGISTER_ONE)
custom_button.clicked.connect(self.show_custom_res_dialog)
custom_button.show()
self.buttons.append(custom_button)
def show_custom_res_dialog(self):
# Shows the popup asking for the custom choice:
dialog = CustomResolutionDialog(
style_sheet=FAST_REGISTER_TWO,
parent=self
)
# if it passes the validation screen inside it, then update state:
dialog.resolution_selected.connect(self.handle_resolution_update)
dialog.exec()
def handle_resolution_update(self, resolution):
"""Update State with the selected Resolution"""
success = self.state.set_selection("Resolution", resolution)
if success:
print(f"Resolution updated to {resolution}")
else:
print("Custom resolution failed validation.")
def after_submission(self, result: Result):
"""Navigate to the homepage after the completion of the registration"""
if result.valid:
self.custom_window.navigator.navigate("menu")
def submit_registration(self):
"""Get current choices from state, and submit."""
final_choices = self.state.get_final_choices()
input_text = self.group_name.text_input.text()
if not input_text:
self.show_a_popup("Pick a name in the top right box.")
return
final_choices["Name"] = input_text
print(f"Submitting choices: {final_choices}")
create_profile(potential_data=final_choices, ui_caller=self.after_submission)
def reverse(self):
self.custom_window.navigator.navigate("menu")