from core.services.profiles.filter_protocols import protocol_matches_profile_type 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.protocol_data import get_protocol_button_data from gui.v2.presenters.data.resolution_data import get_resolution_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 from pathlib import Path class RegistrationState: def __init__(self, btn_path): self.btn_path = btn_path self.all_options = {} # Stores the Path (static) or Dict (dynamic) self.selections = {} # Stores current ID self.order_map = {} # Stores ID order self.pixmap_cache = {} # Caches generated QPixmaps to prevent lag # Filters where one data category affects another one. (in this case Protocol is affected by Type) self._filter_map = { "Protocol": self.is_protocol_valid, } self._load_all_data() def _register_category(self, category, data, default_idx=0): """Helper to handle the boilerplate of setting up a category.""" # If data is a list (like resolutions), convert to a dict where keys are the values if isinstance(data, list): data = {item: item for item in data} self.all_options[category] = data self.order_map[category] = list(data.keys()) if not self.order_map[category]: self.selections[category] = None return if default_idx < len(self.order_map[category]): self.selections[category] = self.order_map[category][default_idx] else: self.selections[category] = self.order_map[category][0] def _load_all_data(self): """Fetch the data via external functions, and then register it for the UI""" load_config = { "Type": (get_profile_type_data, 0), "Protocol": (get_protocol_button_data, 0), "Resolution": (get_resolution_data, 0), "Location": (get_location_display_data, 0), "Browser": (get_app_display_data, 0), } for category, (loader_func, default_idx) in load_config.items(): data = loader_func(self.btn_path) self._register_category(category, data, default_idx) def get_current_image(self, category): """Use the data to create an image. It could be either static or dynamic.""" current_id = self.selections[category] if current_id is None: return None data = self.all_options[category][current_id] # Special case for Resolution: use the template path and the ID (the resolution string) if category == "Resolution": cache_key = f"{category}_{current_id}" if cache_key in self.pixmap_cache: return self.pixmap_cache[cache_key] # Construct the template path here instead of in the loader template_path = f"{self.btn_path}/resolution_template.png" pixmap = create_resolution_button_image( btn_path=template_path, resolution_text=str(current_id) ) self.pixmap_cache[cache_key] = pixmap return pixmap if data is None: return None # Static string of image path: if isinstance(data, Path): return data cache_key = f"{category}_{current_id}" if cache_key in self.pixmap_cache: return self.pixmap_cache[cache_key] # dynamic creation of the image with the nested dict: if isinstance(data, dict): pixmap = create_button_image( first_line=data.get('name', ''), fallback_image_path=data.get('path'), second_line=data.get('second_line'), existing_icon_path=data.get('existing_icon') ) self.pixmap_cache[cache_key] = pixmap return pixmap def is_protocol_valid(self, protocol_id) -> bool: """ Checks if a specific protocol ID is compatible with the current Type. Returns True if valid, False if it should be skipped. """ current_type = self.selections.get("Type") if current_type is None: return True # Default to valid if type isn't set yet return protocol_matches_profile_type(protocol=protocol_id, profile_type=current_type) def _ensure_protocol_valid(self): """Checks current protocol and resets it to the first valid one if invalid.""" current_proto = self.selections.get("Protocol") if not self.is_protocol_valid(current_proto): # Find the first valid protocol available for the new type ids = self.order_map.get("Protocol", []) for proto_id in ids: if self.is_protocol_valid(proto_id): self.selections["Protocol"] = proto_id break def cycle_selection(self, category, direction): ids = self.order_map.get(category, []) if not ids: return None current_id = self.selections.get(category) current_index = ids.index(current_id) if current_id in ids else 0 num_ids = len(ids) # Get the filter function for this category (if one exists) filter_func = self._filter_map.get(category) for i in range(1, num_ids + 1): # Calculate the next index while ensuring it "wraps around" the list boundaries, using Modulo next_index = (current_index + (i * direction)) % num_ids candidate_id = ids[next_index] # If a filter exists for this category, use it. # If no filter exists, filter_func is None, so we just accept the candidate. if filter_func is None or filter_func(candidate_id): self.selections[category] = candidate_id break else: print(f"No valid options found for {category}.") return None # After the selection is updated, if we just changed the Type, # we must ensure the current Protocol is still valid. if category == "Type": self._ensure_protocol_valid() return self.get_current_image(category) def get_final_choices(self): return self.selections