Location filter on protocol fast creation, as well as resolution options now correctly offered for custom

This commit is contained in:
SimplifiedPrivacy 2026-11-05 15:35:08 -05:00
parent e224c12c7a
commit 9d636681b1
5 changed files with 200 additions and 19 deletions

View file

@ -1,5 +1,5 @@
from core.services.profiles.filter_protocols import get_filtered_protocol_list from core.services.profiles.attribute_filters import get_filtered_protocol_list
from core.models.enum_models import ProtocolChoice from core.models.enum_models import ProtocolChoice
from pathlib import Path from pathlib import Path

View file

@ -1,5 +1,6 @@
from gui.v2.infrastructure.screen_size import get_max_screensize from gui.v2.infrastructure.screen_size import get_max_screensize
from gui.v2.actions.config import get_registrations from gui.v2.actions.config import get_registrations
from core.errors.logger import logger
PRE_MADE_RESOLUTIONS = [ PRE_MADE_RESOLUTIONS = [
@ -13,7 +14,7 @@ def are_dynamic_larger_profiles_enabled():
return registration_settings.get("dynamic_larger_profiles", False) return registration_settings.get("dynamic_larger_profiles", False)
def _get_dynamic_resolution_list(host_width: int, host_height: int, full_btn_path: str) -> list: def _get_dynamic_resolution_list(host_width: int, host_height: int) -> list:
"""Get a list of resolutions slightly smaller than the host, counting down by incriments of 50, after adjusting for heights too long""" """Get a list of resolutions slightly smaller than the host, counting down by incriments of 50, after adjusting for heights too long"""
adjusted_host_height = round(host_height * 0.8) adjusted_host_height = round(host_height * 0.8)
@ -25,7 +26,6 @@ def _get_dynamic_resolution_list(host_width: int, host_height: int, full_btn_pat
resolution_as_string = f"{str(new_height)}x{str(new_width)}" resolution_as_string = f"{str(new_height)}x{str(new_width)}"
dynamic_resolutions.append(resolution_as_string) dynamic_resolutions.append(resolution_as_string)
# print(f"dynamic_resolutions are {dynamic_resolutions}")
return dynamic_resolutions return dynamic_resolutions
@ -49,12 +49,10 @@ def _get_filtered_premade_list(host_width: int, host_height: int) -> list:
premade_resolutions.append(each_pre_made) premade_resolutions.append(each_pre_made)
# print(f"final premade_resolutions {premade_resolutions}")
return premade_resolutions return premade_resolutions
def get_resolution_data(btn_path: str) -> list: def get_resolution_data(btn_path: str) -> list:
max_screen_size = get_max_screensize() max_screen_size = get_max_screensize()
# print(f"max_screen_size is {max_screen_size}")
host_w, host_h = max_screen_size.split("x") host_w, host_h = max_screen_size.split("x")
host_width = int(host_w) host_width = int(host_w)
@ -67,3 +65,39 @@ def get_resolution_data(btn_path: str) -> list:
return _get_filtered_premade_list(host_width=host_width, host_height=host_height) return _get_filtered_premade_list(host_width=host_width, host_height=host_height)
def get_host_resolution_limits():
"""Returns the (width, height) maximums allowed (including buffer)."""
max_screen_size = get_max_screensize()
w, h = map(int, max_screen_size.split("x"))
return w - 50, h - 50
def process_custom_resolution(res_string: str):
"""
Validates and clamps a resolution string to host limits.
Returns: (final_res_string, was_adjusted, is_valid)
"""
if not res_string or "x" not in res_string:
return None, False, False
try:
w, h = map(int, res_string.split("x"))
except ValueError:
return None, False, False
limit_w, limit_h = get_host_resolution_limits()
was_adjusted = False
if w > limit_w:
w = limit_w
was_adjusted = True
if h > limit_h:
h = limit_h
was_adjusted = True
return f"{w}x{h}", was_adjusted, True
def validate_selected_resolution(resolution_choice: str) -> bool:
"""Given a user picked resolution, is it valid? (called by GUI's state)"""
_, _, is_valid = process_custom_resolution(resolution_choice)
return is_valid

View file

@ -1,11 +1,11 @@
from core.services.profiles.filter_protocols import protocol_matches_profile_type, app_matches_profile_type from core.services.profiles.attribute_filters import protocol_matches_profile_type, app_or_display_matches_profile_type
from core.controllers.profile_state.data_filters import protocol_is_supported_for_location
from gui.v2.presenters.data.location_data import get_location_display_data from gui.v2.presenters.data.location_data import get_location_display_data
from gui.v2.presenters.data.app_data import get_app_display_data from gui.v2.presenters.data.app_data import get_app_display_data
from gui.v2.presenters.data.protocol_data import get_protocol_button_data from gui.v2.presenters.data.protocol_data import get_protocol_button_data
from gui.v2.presenters.data.resolution_data import get_resolution_data from gui.v2.presenters.data.resolution_data import get_resolution_data, validate_selected_resolution
from gui.v2.presenters.data.type_data import get_profile_type_data from gui.v2.presenters.data.type_data import get_profile_type_data
from gui.v2.presenters.image_tools.button_factory import create_button_image, create_resolution_button_image, blank_button, blank_button_path from gui.v2.presenters.image_tools.button_factory import create_button_image, create_resolution_button_image, blank_button
from pathlib import Path from pathlib import Path
@ -22,11 +22,13 @@ class RegistrationState:
self._filter_map = { self._filter_map = {
"Protocol": self.is_protocol_valid, "Protocol": self.is_protocol_valid,
"Browser": self.is_app_valid, "Browser": self.is_app_valid,
"Resolution": self.is_resolution_valid,
} }
# Maps a "Trigger" category to the categories it affects. # Maps a "Trigger" category to the categories it affects.
self._dependency_map = { self._dependency_map = {
"Type": ["Protocol", "Browser"], "Type": ["Protocol", "Browser", "Resolution"],
"Location": ["Protocol"],
} }
# Callbacks to notify the UI. Store functions here: { "Category": callback_func } # Callbacks to notify the UI. Store functions here: { "Category": callback_func }
@ -125,6 +127,7 @@ class RegistrationState:
if category in self.listeners: if category in self.listeners:
self.listeners[category]() self.listeners[category]()
def get_current_image(self, category): def get_current_image(self, category):
"""Returns an image or a path to one depending on the category and data format""" """Returns an image or a path to one depending on the category and data format"""
@ -137,10 +140,8 @@ class RegistrationState:
if cache_key in self.pixmap_cache: if cache_key in self.pixmap_cache:
return self.pixmap_cache[cache_key] return self.pixmap_cache[cache_key]
# 3. IMAGE DISPATCH: sorts based on data type # 1. HANDLE DYNAMIC CATEGORIES FIRST
data = self.all_options[category][current_id] # These categories build images from the ID itself and don't need a lookup table
# Special Case: Resolution
if category == "Resolution": if category == "Resolution":
res_img = create_resolution_button_image( res_img = create_resolution_button_image(
btn_path=f"{self.btn_path}/resolution_template.png", btn_path=f"{self.btn_path}/resolution_template.png",
@ -149,6 +150,14 @@ class RegistrationState:
self.pixmap_cache[cache_key] = res_img self.pixmap_cache[cache_key] = res_img
return res_img return res_img
# 2. HANDLE DATA-DRIVEN CATEGORIES
# Only perform the lookup for categories that actually use the all_options dictionary
try:
data = self.all_options[category][current_id]
except KeyError:
print(f"Error: {current_id} not found in {category} options. Returning blank.")
return blank_button(self.btn_path)
if data is None: if data is None:
return blank_button(self.btn_path) return blank_button(self.btn_path)
@ -164,19 +173,55 @@ class RegistrationState:
) )
self.pixmap_cache[cache_key] = pixmap self.pixmap_cache[cache_key] = pixmap
return pixmap return pixmap
else: else:
print("Error with Display State, invalid choice. But returning a blank image,")
return blank_button(self.btn_path) return blank_button(self.btn_path)
# Validation logic, used by the generic _ensure_valid # Validation logic, used by the generic _ensure_valid
def is_app_valid(self, app_choice): def is_app_valid(self, app_choice):
current_type = self.selections.get("Type") current_type = self.selections.get("Type")
return app_matches_profile_type(app_choice=app_choice, profile_type=current_type) return app_or_display_matches_profile_type(app_choice=app_choice, profile_type=current_type)
def is_protocol_valid(self, protocol_id): def is_protocol_valid(self, protocol_id):
"""Checks if a protocol is valid by the other choices of type and location."""
current_type = self.selections.get("Type") current_type = self.selections.get("Type")
return True if current_type is None else protocol_matches_profile_type(protocol=protocol_id, profile_type=current_type) current_location = self.selections.get("Location")
if not protocol_matches_profile_type(protocol=protocol_id, profile_type=current_type):
return False
return protocol_is_supported_for_location(location_id=current_location, protocol_choice=protocol_id)
def is_resolution_valid(self, resolution_choice):
current_type = self.selections.get("Type")
# same criteria as app:
if not app_or_display_matches_profile_type(app_choice=resolution_choice, profile_type=current_type):
return False
# plus size filter:
return validate_selected_resolution(resolution_choice)
def get_final_choices(self): def get_final_choices(self):
return self.selections return self.selections
def set_selection(self, category, value):
"""Manually set a selection, validate it, and notify listeners."""
# 1. Validate the choice using the existing filter map
filter_func = self._filter_map.get(category)
if filter_func and not filter_func(value):
print(f"Invalid selection for {category}: {value}")
return False
# 2. Update the selection
self.selections[category] = value
# 3. Trigger reactive updates for dependents (e.g., if Type changed)
dependents = self._dependency_map.get(category, [])
for dep in dependents:
self._ensure_valid(dep)
self._notify(dep)
# 4. Notify the UI for this category
self._notify(category)
return True

View file

@ -3,8 +3,9 @@ from core.models.Result import Result
from gui.v2.actions.profile_actions import create_profile from gui.v2.actions.profile_actions import create_profile
from gui.v2.presenters.state.fast_registration_state import RegistrationState 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.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.Page import Page
from gui.v2.ui.pages.profile_creation.resolution_widget import CustomResolutionDialog
from pathlib import Path from pathlib import Path
@ -13,6 +14,7 @@ from PyQt6.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayo
from PyQt6.QtGui import QPixmap from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtCore import Qt, pyqtSignal
class TextInputGroup(QWidget): class TextInputGroup(QWidget):
"""Create Text Input Boxes of a particular style""" """Create Text Input Boxes of a particular style"""
def __init__(self, parent, title, category, state): def __init__(self, parent, title, category, state):
@ -190,6 +192,7 @@ class FastRegistrationPage(Page):
self.group_browser = SelectionGroup(self.content_area, "Browser", "Browser", self.state) self.group_browser = SelectionGroup(self.content_area, "Browser", "Browser", self.state)
self.group_res = SelectionGroup(self.content_area, "Resolution", "Resolution", 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_type, 0, 0, Qt.AlignmentFlag.AlignTop)
self.grid.addWidget(self.group_name, 0, 1, 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_proto, 1, 0, Qt.AlignmentFlag.AlignTop)
@ -198,6 +201,32 @@ class FastRegistrationPage(Page):
self.grid.addWidget(self.group_res, 2, 1, Qt.AlignmentFlag.AlignTop) self.grid.addWidget(self.group_res, 2, 1, Qt.AlignmentFlag.AlignTop)
self.grid.setRowStretch(3, 1) 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): def after_submission(self, result: Result):
"""Navigate to the homepage after the completion of the registration""" """Navigate to the homepage after the completion of the registration"""

View file

@ -0,0 +1,73 @@
from gui.v2.presenters.data.resolution_data import process_custom_resolution, get_host_resolution_limits
from PyQt6.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox
from PyQt6.QtCore import pyqtSignal
class CustomResolutionDialog(QDialog):
"""Get custom resolution input from the user, and check it's validity with external functions"""
# Signal emits the final resolution string (e.g., "1920x1080")
resolution_selected = pyqtSignal(str)
def __init__(self, style_sheet, parent=None):
super().__init__(parent)
self.setWindowTitle("Custom Resolution")
self.setFixedSize(300, 150)
self.setModal(True)
self.setStyleSheet(style_sheet)
self.init_ui()
def init_ui(self):
# Get the max allowed values
limit_w, limit_h = get_host_resolution_limits()
layout = QVBoxLayout(self)
# Input Row
input_layout = QHBoxLayout()
self.width_input = QLineEdit()
self.width_input.setText(str(limit_w)) # Default to max allowed
self.height_input = QLineEdit()
self.height_input.setText(str(limit_h)) # Default to max allowed
input_layout.addWidget(QLabel("Width:"))
input_layout.addWidget(self.width_input)
input_layout.addWidget(QLabel("Height:"))
input_layout.addWidget(self.height_input)
# Button Row
button_layout = QHBoxLayout()
ok_button = QPushButton("OK")
cancel_button = QPushButton("Cancel")
button_layout.addWidget(cancel_button)
button_layout.addWidget(ok_button)
layout.addLayout(input_layout)
layout.addLayout(button_layout)
# Connections
ok_button.clicked.connect(self.validate_and_accept)
cancel_button.clicked.connect(self.reject)
self.width_input.returnPressed.connect(self.validate_and_accept)
self.height_input.returnPressed.connect(self.validate_and_accept)
def validate_and_accept(self):
# 1. Collect input
res_string = f"{self.width_input.text()}x{self.height_input.text()}"
# 2. Validate:
final_res, was_adjusted, is_valid = process_custom_resolution(res_string)
if not is_valid:
QMessageBox.information(self, "Notice", "Invalid format")
return
# 3. notification and the emission
if was_adjusted:
QMessageBox.information(self, "Notice", "Adjusted to fit host size")
self.resolution_selected.emit(final_res)
self.accept()