forked from Support/sp-hydra-veil-gui
Improved Edit page state management
This commit is contained in:
parent
fb477baa4b
commit
5f0db4d521
5 changed files with 138 additions and 58 deletions
|
|
@ -1,6 +1,8 @@
|
|||
from core.services.profiles.update_profile import update_profile
|
||||
from core.controllers.ProfileController import ProfileController
|
||||
from gui.v2.infrastructure.take_plumbing_action import take_action
|
||||
from gui.v2.infrastructure.setup_observers import profile_observer
|
||||
from core.utils.type_checker import type_checker
|
||||
|
||||
from typing import Callable
|
||||
|
||||
|
|
@ -12,3 +14,13 @@ def create_profile(potential_data: dict, ui_caller: Callable):
|
|||
potential_data=potential_data,
|
||||
profile_observer=profile_observer
|
||||
)
|
||||
|
||||
@type_checker
|
||||
def edit_profile(profile_id: int, potential_data: dict, ui_caller: Callable):
|
||||
"""Pass the data through the plumbing, to edit a profile."""
|
||||
take_action(
|
||||
action=update_profile,
|
||||
ui_caller=ui_caller,
|
||||
profile_id=profile_id,
|
||||
new_choices=potential_data
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import importlib
|
||||
import time
|
||||
import traceback
|
||||
import inspect
|
||||
|
||||
from PyQt6.QtCore import QObject, QTimer, pyqtSignal
|
||||
|
||||
|
|
@ -47,11 +48,31 @@ class Navigator(QObject):
|
|||
module = importlib.import_module(module_path)
|
||||
return getattr(module, class_name)
|
||||
|
||||
def _instantiate(self, name, prepared=None):
|
||||
|
||||
def _instantiate(self, name, prepared=None, profile_id=None):
|
||||
cls = self.get_class(name)
|
||||
if name in ASYNC_PREPARE:
|
||||
return cls(self.page_stack, self.custom_window, prepared=prepared)
|
||||
return cls(self.page_stack, self.custom_window)
|
||||
|
||||
# 1. Inspect the constructor's parameters
|
||||
sig = inspect.signature(cls.__init__)
|
||||
params = sig.parameters
|
||||
|
||||
# 2. Build a dynamic arguments dictionary (always pass in the basics first)
|
||||
kwargs = {
|
||||
"page_stack": self.page_stack,
|
||||
"main_window": self.custom_window
|
||||
}
|
||||
|
||||
# Only add profile_id if the class accepts it
|
||||
if "profile_id" in params:
|
||||
kwargs["profile_id"] = profile_id
|
||||
|
||||
# Only add prepared if the class accepts it
|
||||
if "prepared" in params:
|
||||
kwargs["prepared"] = prepared
|
||||
|
||||
# 3. Instantiate using the filtered dictionary
|
||||
return cls(**kwargs)
|
||||
|
||||
|
||||
def register_instance(self, name, page):
|
||||
self.page_stack.addWidget(page)
|
||||
|
|
@ -64,18 +85,16 @@ class Navigator(QObject):
|
|||
if name not in PAGE_REGISTRY:
|
||||
self._warn_not_migrated(name)
|
||||
return None
|
||||
page = self._instantiate(name)
|
||||
page = self._instantiate(name, profile_id=profile_id)
|
||||
self.register_instance(name, page)
|
||||
|
||||
# Inject variables directly into the object
|
||||
# Inject profile_id directly into the object, in-case it's cached with stale data or init doesn't run:
|
||||
if profile_id is not None:
|
||||
logger.info(f"Navigator is setting profile_id data of {profile_id}")
|
||||
page.profile_id = profile_id
|
||||
|
||||
|
||||
# Make it visible:
|
||||
self.page_stack.setCurrentIndex(self.page_stack.indexOf(page))
|
||||
|
||||
return page
|
||||
|
||||
def get_cached(self, name):
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ class RegistrationState:
|
|||
self.order_map = {}
|
||||
self.pixmap_cache = {}
|
||||
self.profile_id = profile_id
|
||||
self.original_choices = {}
|
||||
|
||||
# Maps a category to the function that validates its current selection.
|
||||
self._filter_map = {
|
||||
|
|
@ -42,6 +43,7 @@ class RegistrationState:
|
|||
if self.profile_id is not None:
|
||||
logger.info(f"RegistrationState got a profile id of {self.profile_id}")
|
||||
default_data = ProfileController.get_default_state_dict(profile_id)
|
||||
self.original_choices = default_data
|
||||
else:
|
||||
default_data = None
|
||||
|
||||
|
|
@ -236,9 +238,6 @@ class RegistrationState:
|
|||
# plus size filter:
|
||||
return validate_selected_resolution(resolution_choice)
|
||||
|
||||
def get_final_choices(self):
|
||||
return self.selections
|
||||
|
||||
def set_selection(self, category, value):
|
||||
"""Manually set a selection, validate it, and notify listeners."""
|
||||
|
||||
|
|
@ -260,3 +259,45 @@ class RegistrationState:
|
|||
# 4. Notify the UI for this category
|
||||
self._notify(category)
|
||||
return True
|
||||
|
||||
|
||||
def get_final_choices(self):
|
||||
return self.selections
|
||||
|
||||
def whats_different(self):
|
||||
"""Creates a dictionary of the difference between the current selections and the original choices."""
|
||||
different = {}
|
||||
|
||||
if not self.original_choices:
|
||||
return self.selections
|
||||
|
||||
for category, value in self.selections.items():
|
||||
|
||||
# wasn't in the original, but it's in the new selections:
|
||||
try:
|
||||
if not self.original_choices[category]:
|
||||
different[category] = value
|
||||
continue
|
||||
except KeyError:
|
||||
different[category] = value
|
||||
continue
|
||||
|
||||
# not in the new choices, but it is in the original:
|
||||
if not self.selections[category] and self.original_choices[category]:
|
||||
different[category] = None
|
||||
continue
|
||||
|
||||
# actually different:
|
||||
if self.original_choices[category] != self.selections[category]:
|
||||
different[category] = value
|
||||
|
||||
return different
|
||||
|
||||
def is_anything_different(self) -> bool:
|
||||
different = self.whats_different()
|
||||
if different:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
|
|
@ -168,3 +168,6 @@ class Page(QWidget):
|
|||
else:
|
||||
self.show_a_popup(error_msg)
|
||||
self.update_status.update_status(status_update)
|
||||
|
||||
# def update_bottom_status(self, text_content: str):
|
||||
# self.update_status.update_status(text_content)
|
||||
|
|
@ -3,7 +3,7 @@ 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.actions.profile_actions import edit_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
|
||||
|
|
@ -18,29 +18,37 @@ 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
|
||||
|
||||
# 1. Permanent Structure: Created once in init
|
||||
self.content_area = QWidget()
|
||||
self.grid = QGridLayout(self.content_area) # Set layout once here
|
||||
self.grid.setSpacing(30)
|
||||
self.grid.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
self.setFixedSize(800, 600)
|
||||
|
||||
self.main_layout = QVBoxLayout(self)
|
||||
self.main_layout.setContentsMargins(40, 60, 40, 60)
|
||||
|
||||
# Bottom right buttons:
|
||||
# Add content_area to main_layout once here
|
||||
self.main_layout.addWidget(self.content_area)
|
||||
|
||||
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 clear_ui(self):
|
||||
"""Removes all widgets from the grid layout and deletes them"""
|
||||
while self.grid.count():
|
||||
item = self.grid.takeAt(0)
|
||||
widget = item.widget()
|
||||
if widget:
|
||||
widget.deleteLater()
|
||||
|
||||
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)
|
||||
|
|
@ -51,14 +59,12 @@ class EditPage(Page):
|
|||
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
|
||||
# 2. State Reset: Wipe old widgets and old state
|
||||
self.clear_ui()
|
||||
self.state = RegistrationState(btn_path=self.btn_path, profile_id=profile_id)
|
||||
print("Created a new RegistrationState")
|
||||
|
||||
# 2. Create groups, passing the category string and the state object
|
||||
# 3. 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)
|
||||
|
|
@ -66,6 +72,7 @@ class EditPage(Page):
|
|||
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)
|
||||
|
|
@ -78,20 +85,45 @@ class EditPage(Page):
|
|||
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)
|
||||
# 5. Custom Buttons:
|
||||
custom_button = QPushButton("Custom Res", self.content_area) # Parent to content_area
|
||||
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:
|
||||
# 6. UI State updates:
|
||||
Notifications.subscribe(EventType.SYNC_COMPLETE, self.update_post_sync)
|
||||
|
||||
def after_submission(self, result: Result):
|
||||
if result.valid:
|
||||
print(f"Updated Profile {self.profile_id}")
|
||||
self.created_display = False # Allow showEvent to trigger create_ui next time
|
||||
self.custom_window.navigator.navigate("menu")
|
||||
|
||||
def submit_edits(self):
|
||||
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
|
||||
|
||||
self.state.set_selection(category="name", value=input_text)
|
||||
different_content = self.state.whats_different()
|
||||
|
||||
edit_profile(
|
||||
profile_id=self.profile_id,
|
||||
potential_data=different_content,
|
||||
ui_caller=self.after_submission
|
||||
)
|
||||
self.created_display = False
|
||||
|
||||
def reverse(self):
|
||||
if self.state.is_anything_different():
|
||||
print("unsubmitted changes")
|
||||
return
|
||||
|
||||
self.created_display = False
|
||||
self.custom_window.navigator.navigate("menu")
|
||||
|
||||
def update_post_sync(self, string_notification: str):
|
||||
"""At the completion of the sync, update the button styles"""
|
||||
|
|
@ -117,30 +149,3 @@ class EditPage(Page):
|
|||
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")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue