hydraveil-gui/gui/v2/ui/pages/create_edit/edit_page.py

146 lines
6.1 KiB
Python

from core.observers.Notifications import Notifications, EventType
from core.models.Result import Result
from core.errors.logger import logger
from gui.v2.ui.pages.create_edit.components import SelectionGroup, TextInputGroup
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 FAST_REGISTER_ONE, FAST_REGISTER_TWO
from gui.v2.ui.pages.Page import Page
from gui.v2.ui.pages.create_edit.resolution_widget import CustomResolutionDialog
from PyQt6.QtWidgets import QWidget, QPushButton, QGridLayout, QVBoxLayout
from PyQt6.QtCore import Qt
from typing import Optional
class EditPage(Page):
def __init__(self, page_stack, main_window, prepared=None, profile_id: Optional[int] = None):
super().__init__("EditPage", page_stack, main_window)
# Navigator passes in profile id:
self.profile_id = profile_id
self.content_area = QWidget()
self.setFixedSize(800, 600)
self.main_layout = QVBoxLayout(self)
self.main_layout.setContentsMargins(40, 60, 40, 60)
# Bottom right buttons:
self.button_apply.setVisible(True)
self.button_back.setVisible(True)
self.button_apply.clicked.connect(self.submit_edits)
self.button_back.clicked.connect(self.reverse)
# Flag for the showEvent not repeating redraws:
self.created_display = False
def showEvent(self, event):
"""Refresh data. This runs every time the widget is displayed"""
if not self.created_display and self.profile_id is not None:
logger.info(f"Recreating UI for profile {self.profile_id}")
self.create_ui(self.profile_id)
self.created_display = True
def create_ui(self, profile_id: int):
if profile_id is None:
logger.error("No profile id to edit.")
return
self.grid = QGridLayout(self.content_area)
self.grid.setSpacing(30)
self.grid.setContentsMargins(0, 0, 0, 0)
# 1. Initialize State
self.state = RegistrationState(btn_path=self.btn_path, profile_id=profile_id)
# 2. Create groups, passing the category string and the state object
self.group_type = SelectionGroup(self.content_area, title="Profile Type", category="type", state=self.state, sync_required=False, btn_path=self.btn_path)
self.group_name = TextInputGroup(self.content_area, title="Name", category="name", state=self.state)
self.group_proto = SelectionGroup(self.content_area, title="Protocol", category="protocol", state=self.state, sync_required=False, btn_path=self.btn_path)
self.group_loc = SelectionGroup(self.content_area, title="Location", category="location", state=self.state, sync_required=True, btn_path=self.btn_path)
self.group_browser = SelectionGroup(self.content_area, title="Browser", category="browser", state=self.state, sync_required=True, btn_path=self.btn_path)
self.group_res = SelectionGroup(self.content_area, title="Resolution", category="resolution", state=self.state, sync_required=False, btn_path=self.btn_path)
# 3. Quality Control: Force columns to share width equally
self.grid.setColumnStretch(0, 1)
self.grid.setColumnStretch(1, 1)
# 4. adding groups in rows of 2 items each:
self.grid.addWidget(self.group_type, 0, 0)
self.grid.addWidget(self.group_name, 0, 1)
self.grid.addWidget(self.group_proto, 1, 0)
self.grid.addWidget(self.group_loc, 1, 1)
self.grid.addWidget(self.group_browser, 2, 0)
self.grid.addWidget(self.group_res, 2, 1)
# Add the content area to the main window layout
self.main_layout.addWidget(self.content_area)
# 5. Custom options, such as Resolution:
custom_button = QPushButton("Custom Res", self)
custom_button.setStyleSheet(FAST_REGISTER_ONE)
custom_button.clicked.connect(self.show_custom_res_dialog)
self.grid.addWidget(custom_button, 3, 1, Qt.AlignmentFlag.AlignCenter)
self.grid.setRowStretch(4, 1)
# 6. Subscribe to state change on sync:
Notifications.subscribe(EventType.SYNC_COMPLETE, self.update_post_sync)
def update_post_sync(self, string_notification: str):
"""At the completion of the sync, update the button styles"""
buttons_required_sync = [self.group_loc, self.group_browser]
for each_button in buttons_required_sync:
each_button.apply_styles_to_buttons(sync_required=False)
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_edits(self):
self.created_display = False # reset for next time
print(f"hit button. self.profile_id is {self.profile_id}")
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}")
# swap for edit:
# create_profile(potential_data=final_choices, ui_caller=self.after_submission)
def reverse(self):
self.created_display = False # reset for next time
self.custom_window.navigator.navigate("menu")