forked from Support/sp-hydra-veil-gui
Big bang refactor of menu page population of profile data. The new refactor has serialized profile data, that's disconnected from core's objects or SQL threads. This allows for a complete separation of display data from SQL threading. With the goal of allowing sync to wipe data, and the UI to refresh it, without SQL threading issues.
This commit is contained in:
parent
a50c9dc709
commit
5cb567c385
13 changed files with 449 additions and 232 deletions
|
|
@ -80,6 +80,7 @@ from gui.v2.actions.ticket_failure import (
|
|||
from gui.v2.actions.should_be_synchronized import should_be_synchronized
|
||||
from gui.v2.ui.presenters.main_presenters import disconnect_message
|
||||
from gui.v2.ui.popups.confirmation_popup import ConfirmationPopup
|
||||
from gui.v2.infrastructure.folders import FolderStash
|
||||
|
||||
|
||||
class CustomWindow(QMainWindow):
|
||||
|
|
@ -91,6 +92,11 @@ class CustomWindow(QMainWindow):
|
|||
gui_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
font_path = os.path.join(gui_dir, 'resources', 'fonts')
|
||||
|
||||
folder_stash = FolderStash()
|
||||
folder_stash.gui_dir = gui_dir
|
||||
folder_stash.font_path = font_path
|
||||
folder_stash.image_path = os.path.join(gui_dir, 'resources', 'images')
|
||||
|
||||
retro_gaming_path = os.path.join(font_path, 'retro-gaming.ttf')
|
||||
font_id = QFontDatabase.addApplicationFont(retro_gaming_path)
|
||||
font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
|
||||
|
|
@ -124,10 +130,10 @@ class CustomWindow(QMainWindow):
|
|||
setup_gui_directory(
|
||||
self.gui_cache_home, self.gui_config_home, self.gui_config_file)
|
||||
|
||||
self.btn_path = os.getenv('BTN_PATH', os.path.join(
|
||||
gui_dir, 'resources', 'images'))
|
||||
self.css_path = os.getenv('CSS_PATH', os.path.join(
|
||||
gui_dir, 'resources', 'styles'))
|
||||
self.btn_path = os.path.join(
|
||||
gui_dir, 'resources', 'images')
|
||||
self.css_path = os.path.join(
|
||||
gui_dir, 'resources', 'styles')
|
||||
|
||||
self.force_sync = force_sync
|
||||
self.is_downloading = False
|
||||
|
|
|
|||
BIN
gui/resources/images/systemwide.png
(Stored with Git LFS)
Executable file
BIN
gui/resources/images/systemwide.png
(Stored with Git LFS)
Executable file
Binary file not shown.
BIN
gui/resources/images/tor_mini.png
(Stored with Git LFS)
Executable file
BIN
gui/resources/images/tor_mini.png
(Stored with Git LFS)
Executable file
Binary file not shown.
|
|
@ -190,23 +190,26 @@ def load_menu_profiles(
|
|||
|
||||
state_change = state_change or {}
|
||||
changed_id = state_change.get("profile_id")
|
||||
enabled_kinds = {
|
||||
profile_id: kind
|
||||
for profile_id, profile in profiles.items()
|
||||
if (kind := _enabled_profile_kind(
|
||||
profile, state_change.get("is_enabled") if profile_id == changed_id else None)) is not None
|
||||
}
|
||||
# enabled_kinds = {
|
||||
# profile_id: kind
|
||||
# for profile_id, profile in profiles.items()
|
||||
# if (kind := _enabled_profile_kind(
|
||||
# profile, state_change.get("is_enabled") if profile_id == changed_id else None)) is not None
|
||||
# }
|
||||
|
||||
enabled_kinds = {}
|
||||
summaries = match_core_profiles(profiles, gui_config_file)
|
||||
|
||||
selected = profiles.get(selected_id)
|
||||
details = None if selected is None else _profile_detail_data(
|
||||
selected, summaries[f"Profile_{selected_id}"], selected_id in enabled_kinds)
|
||||
return {
|
||||
return_data = {
|
||||
"display_profiles": summaries,
|
||||
"selected_details": details,
|
||||
"background": "background_connected" if "system" in enabled_kinds.values() else "original",
|
||||
"enabled_kinds": enabled_kinds,
|
||||
}
|
||||
return return_data
|
||||
|
||||
|
||||
def _profile_detail_data(profile: BaseProfile | None, summary: Mapping[str, Any], is_connected: bool) -> dict[str, Any]:
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from gui.v2.presenters.data.profile_data import convert_to_dto
|
||||
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
|
||||
|
|
@ -28,6 +29,26 @@ def edit_profile(profile_id: int, location_down: bool, potential_data: dict, ui_
|
|||
|
||||
def get_all_profiles(ui_caller: Callable):
|
||||
take_action(
|
||||
action=ProfileController.get_all,
|
||||
action=ProfileController.get_all_serialized,
|
||||
ui_caller=ui_caller
|
||||
)
|
||||
|
||||
def format_to_dtos(all_profile_data: dict) -> dict:
|
||||
"""Convert a dict with multiple profiles into a dict with DTOs"""
|
||||
converted_dict = {}
|
||||
for key, original_dict in all_profile_data.items():
|
||||
converted_data = convert_to_dto(original_dict)
|
||||
converted_dict[key] = converted_data
|
||||
|
||||
return converted_dict
|
||||
|
||||
|
||||
def get_one_serialized_profile(profile_id: int, ui_caller: Callable):
|
||||
"""Fetch from core a single serialized DTO for one profile id"""
|
||||
take_action(
|
||||
action=ProfileController.get_ONE_serialized,
|
||||
profile_id=profile_id,
|
||||
ui_caller=ui_caller
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
14
gui/v2/infrastructure/folders.py
Normal file
14
gui/v2/infrastructure/folders.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
|
||||
class FolderStash:
|
||||
"""Singleton to avoid having to data tramp the folder path"""
|
||||
|
||||
_instance = None
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = super(FolderStash, cls).__new__(cls)
|
||||
cls._instance.gui_dir = None
|
||||
cls._instance.font_path = None
|
||||
return cls._instance
|
||||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
from core.models.orm_calls.application_version_calls import get_all_supported_apps
|
||||
from core.models.BaseProfile import ProfileType
|
||||
from core.errors.logger import logger
|
||||
|
||||
from pathlib import Path
|
||||
|
|
@ -39,4 +40,17 @@ def get_app_display_data(button_path: str) -> dict:
|
|||
return display_dto
|
||||
|
||||
|
||||
def get_app_icon_path(image_path: Path, profile_type: ProfileType, app_name: str) -> Path:
|
||||
"""Return a Path for the app's icon, taking into account that systemwide profiles don't have one"""
|
||||
|
||||
if profile_type == ProfileType.SYSTEM:
|
||||
icon_image_path = "systemwide.png"
|
||||
else:
|
||||
icon_image_path = f"{app_name} latest_mini.png"
|
||||
|
||||
full_image_path = Path(image_path, icon_image_path)
|
||||
|
||||
if full_image_path.exists():
|
||||
return full_image_path
|
||||
else:
|
||||
return Path(image_path, "default_browser_mini.png")
|
||||
|
|
|
|||
|
|
@ -48,3 +48,19 @@ def get_location_display_data(button_path: str) -> dict|None:
|
|||
|
||||
return location_display_dto
|
||||
|
||||
|
||||
def get_location_icon_path(image_path: Path, country_code: str, code: str, larger_version: bool = False):
|
||||
"""Return a Path for the location's icon, with the default if not found."""
|
||||
|
||||
if larger_version:
|
||||
icon_image_path = f"icon_{country_code}_{code}.png"
|
||||
else:
|
||||
icon_image_path = f"icon_mini_{country_code}_{code}.png"
|
||||
|
||||
print(f"icon_image_path is {icon_image_path}")
|
||||
full_image_path = Path(image_path, icon_image_path)
|
||||
|
||||
if full_image_path.exists():
|
||||
return full_image_path
|
||||
else:
|
||||
return Path(image_path, "default_location_mini.png")
|
||||
|
|
|
|||
141
gui/v2/presenters/data/profile_data.py
Normal file
141
gui/v2/presenters/data/profile_data.py
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
from core.models.enum_models import ProtocolChoice
|
||||
from core.models.BaseProfile import ProfileType
|
||||
from gui.v2.infrastructure.folders import FolderStash
|
||||
from gui.v2.presenters.data.location_data import get_location_icon_path
|
||||
from gui.v2.presenters.data.app_data import get_app_icon_path
|
||||
from gui.v2.presenters.data.protocol_data import get_protocol_icon_path
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
@dataclass
|
||||
class ProfileDto():
|
||||
profile_id: int
|
||||
profile_name: str
|
||||
location_icon_path: Path
|
||||
app_icon_path: Path
|
||||
protocol_icon_path: Path
|
||||
needs_warning: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class RenderProfileDto():
|
||||
profile_id: int
|
||||
profile_type: ProfileType
|
||||
location_text: str
|
||||
operator_name: str
|
||||
nostr_public_key: str
|
||||
detail_image: Path
|
||||
primary_asset: Path
|
||||
|
||||
|
||||
def convert_one_render_dto(raw_dict: dict) -> Optional[RenderProfileDto]:
|
||||
"""Get a DTO for the left side of the menu's rendering."""
|
||||
|
||||
if not raw_dict or not isinstance(raw_dict, dict):
|
||||
return None
|
||||
|
||||
image_path = Path(FolderStash().image_path)
|
||||
if not image_path:
|
||||
return None
|
||||
|
||||
profile_id = raw_dict.get("id")
|
||||
profile_type = raw_dict.get("type")
|
||||
location = raw_dict.get("location")
|
||||
|
||||
if profile_type == ProfileType.SESSION:
|
||||
primary_asset = Path(image_path, "browser only.png")
|
||||
if not primary_asset.exists():
|
||||
primary_asset = None
|
||||
else:
|
||||
primary_asset = None
|
||||
|
||||
if not location:
|
||||
print(raw_dict)
|
||||
print("Couldn't find a location in the above dict")
|
||||
return None
|
||||
|
||||
detail_image = get_location_icon_path(
|
||||
image_path=image_path,
|
||||
country_code=location.get("country_code", "na") if location else "na",
|
||||
code=location.get("code", "na") if location else "na",
|
||||
larger_version=True
|
||||
)
|
||||
|
||||
country_name = location.get("country_name", "N/A")
|
||||
country_city = location.get("name", "N/A")
|
||||
location_text = f"{country_city}, {country_name}"
|
||||
|
||||
operator = location.get("operator")
|
||||
|
||||
if not operator:
|
||||
print(location)
|
||||
print("Couldn't find the operator in the above location")
|
||||
return None
|
||||
|
||||
nostr_public_key = operator.get("nostr_public_key", "Nostr not setup yet.")
|
||||
operator_name = operator.get("name", "N/A")
|
||||
|
||||
return RenderProfileDto(
|
||||
profile_id=profile_id,
|
||||
profile_type=profile_type,
|
||||
location_text=location_text,
|
||||
operator_name=operator_name,
|
||||
nostr_public_key=nostr_public_key,
|
||||
detail_image=detail_image,
|
||||
primary_asset=primary_asset
|
||||
)
|
||||
|
||||
|
||||
def convert_to_dto(raw_dict: dict) -> ProfileDto:
|
||||
"""Convert a profile dictionary into a DTO with image paths"""
|
||||
|
||||
if not raw_dict or not isinstance(raw_dict, dict):
|
||||
return None
|
||||
|
||||
image_path = Path(FolderStash().image_path)
|
||||
if not image_path:
|
||||
return None
|
||||
|
||||
profile_id = raw_dict.get("id")
|
||||
name = raw_dict.get("name", f"Profile {str(profile_id)}")
|
||||
profile_type = raw_dict.get("type", ProfileType.SYSTEM)
|
||||
location = raw_dict.get("location")
|
||||
|
||||
location_icon_path = get_location_icon_path(
|
||||
image_path=image_path,
|
||||
country_code=location.get("country_code", "na") if location else "na",
|
||||
code=location.get("code", "na") if location else "na",
|
||||
larger_version=False
|
||||
)
|
||||
|
||||
application = raw_dict.get("application_version")
|
||||
|
||||
app_icon_path = get_app_icon_path(
|
||||
image_path=image_path,
|
||||
profile_type=profile_type,
|
||||
app_name=application.get('application_code', "na") if application else "na"
|
||||
)
|
||||
|
||||
format_revision = raw_dict.get("format_revision", 1)
|
||||
|
||||
if profile_type == ProfileType.SYSTEM or format_revision == 2:
|
||||
needs_warning = False
|
||||
else:
|
||||
needs_warning = True
|
||||
|
||||
protocol_icon_path = get_protocol_icon_path(
|
||||
image_path=image_path,
|
||||
protocol=raw_dict.get("protocol")
|
||||
)
|
||||
|
||||
return ProfileDto(
|
||||
profile_id=profile_id,
|
||||
profile_name=name,
|
||||
location_icon_path=location_icon_path,
|
||||
app_icon_path=app_icon_path,
|
||||
protocol_icon_path=protocol_icon_path,
|
||||
needs_warning=needs_warning
|
||||
)
|
||||
|
||||
|
|
@ -18,3 +18,20 @@ def get_protocol_button_data(button_path: str) -> dict|None:
|
|||
|
||||
return protocol_display_dto
|
||||
|
||||
|
||||
def get_protocol_icon_path(image_path: Path, protocol: ProtocolChoice) -> Path:
|
||||
"""Return a Path for the location's icon, with the default if not found."""
|
||||
|
||||
if protocol is None:
|
||||
return Path(button_path, "browsers_mini.png")
|
||||
|
||||
elif isinstance(protocol, ProtocolChoice):
|
||||
protocol = protocol.value
|
||||
|
||||
icon_image_path = f"{protocol}_mini.png"
|
||||
full_image_path = Path(image_path, icon_image_path)
|
||||
|
||||
if full_image_path.exists():
|
||||
return full_image_path
|
||||
else:
|
||||
return Path(button_path, "browsers_mini.png")
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from core.infrastructure.global_state_coordinator import evaluate_state_on_start
|
|||
from core.services.networking.general_connection_tools.disconnect import kill_profile_from_grave
|
||||
from gui.v2.infrastructure.take_plumbing_action import take_action
|
||||
from gui.v2.actions.error_handlers import post_state_evaluation
|
||||
from gui.v2.actions.profile_actions import get_all_profiles
|
||||
from gui.v2.actions.profile_actions import get_all_profiles, format_to_dtos, get_one_serialized_profile
|
||||
|
||||
from core.controllers.ProfileController import ProfileController
|
||||
from core.models.BaseProfile import ProfileType
|
||||
|
|
@ -13,10 +13,11 @@ from core.observers.Notifications import Notifications, EventType
|
|||
import functools
|
||||
import os
|
||||
from itertools import chain
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt6 import QtCore, QtGui
|
||||
from PyQt6 import QtCore, QtGui, QtWidgets
|
||||
from PyQt6.QtCore import Qt, QSize, pyqtSlot
|
||||
from PyQt6.QtGui import QIcon, QPainter, QPixmap
|
||||
from PyQt6.QtGui import QIcon, QPainter, QPixmap, QPainterPath, QIcon, QColor
|
||||
from PyQt6.QtWidgets import (
|
||||
QApplication, QButtonGroup, QFrame, QHBoxLayout, QLabel, QLineEdit,
|
||||
QMessageBox, QPushButton, QScrollArea, QTextEdit, QVBoxLayout, QWidget,
|
||||
|
|
@ -33,7 +34,7 @@ from gui.v2.actions.menu_results import MenuResult
|
|||
from gui.v2.ui.pages.Page import Page
|
||||
from gui.v2.ui.pages.location_verification_page import LocationVerificationPage
|
||||
from gui.v2.ui.styles.styles import SCROLLBAR_CYAN_QSS
|
||||
from gui.v2.ui.styles.css.menu_css import CREATE_BUTTON_STYLE
|
||||
from gui.v2.ui.styles.css.menu_css import CREATE_BUTTON_STYLE, verification_style_one, VERIFICATION_STYLE_TWO
|
||||
from gui.v2.ui.popups.confirmation_popup import ConfirmationPopup
|
||||
from gui.v2.ui.popups.endpoint_verification_popup import EndpointVerificationPopup
|
||||
from gui.v2.ui.popups.ticket_data_loss_popup import TicketDataLossPopup
|
||||
|
|
@ -42,6 +43,7 @@ from gui.v2.infrastructure.setup_observers import (
|
|||
application_version_observer, connection_observer,
|
||||
profile_observer
|
||||
)
|
||||
from gui.v2.presenters.data.profile_data import ProfileDto, RenderProfileDto, convert_one_render_dto
|
||||
|
||||
from gui.v2.infrastructure.thread_singleton import get_main_thread_dispatcher
|
||||
mainthread = get_main_thread_dispatcher()
|
||||
|
|
@ -234,50 +236,145 @@ class MenuPage(Page):
|
|||
|
||||
@mainthread
|
||||
def refresh_menu_buttons(self, state_change=None):
|
||||
|
||||
"""Trigger a fetch of all profile data, to be loaded after."""
|
||||
print("refresh_menu_buttons triggered. First, getting all profiles..")
|
||||
get_all_profiles(ui_caller=self.actually_load_profiles)
|
||||
|
||||
|
||||
def actually_load_profiles(self, result: Result):
|
||||
"""Recieve data for all profiles, and hydrate all the profile buttons with it."""
|
||||
|
||||
if not result.valid:
|
||||
print("Result is invalid, but core handled the error on it's own.")
|
||||
return
|
||||
|
||||
profiles = result.data
|
||||
print("Got the data. Loading all menu profiles.")
|
||||
original_data = result.data
|
||||
|
||||
menu_data = load_menu_profiles(
|
||||
profiles=profiles,
|
||||
gui_config_file=getattr(self.update_status, 'gui_config_file', None),
|
||||
on_error=self.update_status.update_status,
|
||||
selected_id=self.reverse_id,
|
||||
state_change=None, # legacy variable to be refactored out
|
||||
)
|
||||
self.profiles_data = menu_data["display_profiles"]
|
||||
self.profile_info = self.profiles_data
|
||||
self.number_of_profiles = len(self.profiles_data)
|
||||
self.connection_manager.set_connected_profiles(menu_data["enabled_kinds"])
|
||||
icon_paths = {"session_tor": self.connected_tor_profile,
|
||||
"session": self.connected_profile, "system": self.system_wide_connected_profile}
|
||||
self.button_states = {profile_id: icon_paths[kind] for profile_id, kind in menu_data["enabled_kinds"].items()}
|
||||
# profiles is now {id: ProfileDto}
|
||||
profiles = format_to_dtos(original_data)
|
||||
|
||||
self.profiles_data = profiles
|
||||
self.profile_info = profiles
|
||||
self.number_of_profiles = len(profiles)
|
||||
|
||||
# Cleanup widgets
|
||||
for button in getattr(self, 'buttons', []):
|
||||
button.parent().deleteLater()
|
||||
if hasattr(self, 'button_group'):
|
||||
self.button_group.deleteLater()
|
||||
|
||||
self.profile_button_map.clear()
|
||||
self.button_group = QButtonGroup(self)
|
||||
self.buttons = []
|
||||
for profile_name, profile_value in self.profiles_data.items():
|
||||
self.create_profile_widget(profile_name, profile_value)
|
||||
|
||||
# Iterate directly over the DTO objects
|
||||
for profile_id, dto in profiles.items():
|
||||
self.create_profile_widget(dto)
|
||||
|
||||
self.update_scroll_widget_size()
|
||||
if menu_data["selected_details"] is not None:
|
||||
self._render_profile_details(menu_data["selected_details"])
|
||||
return
|
||||
self.clear_profile_details()
|
||||
|
||||
def create_profile_widget(self, dto: ProfileDto):
|
||||
parent_label = self.create_parent_label(dto.profile_id)
|
||||
button = self.create_profile_button(parent_label, dto.profile_id)
|
||||
|
||||
self.profile_button_map[dto.profile_id] = button
|
||||
|
||||
self.create_profile_name_label(parent_label, dto.profile_name)
|
||||
self.create_profile_icons(parent_label, dto)
|
||||
|
||||
def create_parent_label(self, profile_id):
|
||||
"""Create a label for multiple profiles"""
|
||||
parent_label = QLabel(self.scroll_widget)
|
||||
|
||||
# Calculate position based on the order of IDs in the dict
|
||||
profile_ids = list(self.profiles_data.keys())
|
||||
index = profile_ids.index(profile_id)
|
||||
row, column = divmod(index, 2)
|
||||
|
||||
parent_label.setGeometry(column * 179, row * 120, 175, 100)
|
||||
parent_label.setVisible(True)
|
||||
return parent_label
|
||||
|
||||
def create_profile_button(self, parent_label, profile_id):
|
||||
"""Return a clickable profile button, which is tied to a function to display the details for it."""
|
||||
button = QPushButton(parent_label)
|
||||
button.setGeometry(0, 0, 175, 60)
|
||||
button.setStyleSheet(CREATE_BUTTON_STYLE)
|
||||
|
||||
button.setIcon(QIcon(self.not_connected_profile))
|
||||
button.setIconSize(QtCore.QSize(175, 60))
|
||||
|
||||
button.show()
|
||||
button.setCheckable(True)
|
||||
self.button_group.addButton(button)
|
||||
self.buttons.append(button)
|
||||
|
||||
# Signal now passes the clean integer ID
|
||||
button.clicked.connect(
|
||||
lambda checked, pid=profile_id: self.print_profile_details(pid))
|
||||
|
||||
return button
|
||||
|
||||
def create_profile_name_label(self, parent_label, name):
|
||||
"""Create a label for the profile's name"""
|
||||
child_label = QLabel(parent_label)
|
||||
child_label.setGeometry(0, 65, 175, 30)
|
||||
child_label.setText(str(name or "Unnamed Profile"))
|
||||
child_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
child_label.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
|
||||
child_label.show()
|
||||
|
||||
|
||||
def create_profile_icons(self, parent_label, dto: ProfileDto):
|
||||
"""Create different types of icons for a single profile, based on the DTO data"""
|
||||
icons_to_render = [
|
||||
(dto.protocol_icon_path, 'protocol'),
|
||||
(dto.location_icon_path, 'location'),
|
||||
(dto.app_icon_path, 'browser')
|
||||
]
|
||||
|
||||
for index, (path, kind) in enumerate(icons_to_render):
|
||||
child_label = QLabel(parent_label)
|
||||
child_label.setObjectName("label_profiles")
|
||||
child_label.setGeometry(3 + index * 60, 5, 50, 50)
|
||||
child_label.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
|
||||
child_label.setStyleSheet("background-color: rgba(128, 128, 128, 0.5); border-radius: 25px;")
|
||||
|
||||
# Load the image exactly as the file exists on disk
|
||||
pixmap = self._load_profile_icon_pixmap(path, kind)
|
||||
|
||||
# Apply warning overlay only to the browser icon
|
||||
if kind == 'browser' and dto.needs_warning:
|
||||
pixmap = self._add_browser_warning(pixmap)
|
||||
|
||||
child_label.setPixmap(pixmap)
|
||||
child_label.show()
|
||||
|
||||
def _load_profile_icon_pixmap(self, image_path, label_name):
|
||||
"""Load a single profile's image path icon"""
|
||||
|
||||
# Load directly from path without any scaling
|
||||
pixmap = QPixmap(str(image_path))
|
||||
if not pixmap.isNull():
|
||||
return pixmap
|
||||
|
||||
# placeholder if it fails
|
||||
placeholder = QPixmap(50, 50)
|
||||
placeholder.fill(QtGui.QColor(200, 200, 200))
|
||||
return placeholder
|
||||
|
||||
def _add_browser_warning(self, pixmap):
|
||||
"""Draw a warning dot on top of the image"""
|
||||
painter = QPainter(pixmap)
|
||||
warning_pix = QPixmap(os.path.join(self.btn_path, 'warning.png'))
|
||||
# Draw at the bottom right of the existing pixmap
|
||||
painter.drawPixmap(pixmap.width() - 22, pixmap.height() - 21, warning_pix)
|
||||
painter.end()
|
||||
return pixmap
|
||||
|
||||
|
||||
def update_scroll_widget_size(self):
|
||||
"""Update the size of the scrolling widget, based on the quantity of profiles."""
|
||||
if self.number_of_profiles == 0:
|
||||
self.scroll_widget.setFixedSize(370, 0)
|
||||
return
|
||||
|
|
@ -286,6 +383,7 @@ class MenuPage(Page):
|
|||
height = rows * 120
|
||||
self.scroll_widget.setFixedSize(370, height)
|
||||
|
||||
|
||||
def clear_profile_details(self):
|
||||
print("clear_profile_details triggered")
|
||||
|
||||
|
|
@ -307,118 +405,6 @@ class MenuPage(Page):
|
|||
button.setChecked(False)
|
||||
self.button_group.setExclusive(True)
|
||||
|
||||
def create_profile_widget(self, key, value):
|
||||
profile_id = value['id']
|
||||
|
||||
parent_label = self.create_parent_label(profile_id)
|
||||
button = self.create_profile_button(parent_label, profile_id, key)
|
||||
|
||||
self.profile_button_map[profile_id] = button
|
||||
|
||||
self.create_profile_name_label(parent_label, value["name"])
|
||||
self.create_profile_icons(parent_label, value)
|
||||
|
||||
def create_parent_label(self, profile_id):
|
||||
parent_label = QLabel(self.scroll_widget)
|
||||
profile_list = list(self.profiles_data.keys())
|
||||
index = profile_list.index(f'Profile_{profile_id}')
|
||||
row, column = divmod(index, 2)
|
||||
|
||||
parent_label.setGeometry(column * 179, row * 120, 175, 100)
|
||||
parent_label.setVisible(True)
|
||||
return parent_label
|
||||
|
||||
|
||||
def create_profile_button(self, parent_label, profile_id, key):
|
||||
button = QPushButton(parent_label)
|
||||
button.setGeometry(0, 0, 175, 60)
|
||||
button.setStyleSheet(CREATE_BUTTON_STYLE)
|
||||
|
||||
button.setIcon(QIcon(self.not_connected_profile))
|
||||
button.setIconSize(QtCore.QSize(175, 60))
|
||||
|
||||
button.show()
|
||||
button.setCheckable(True)
|
||||
self.button_group.addButton(button)
|
||||
self.buttons.append(button)
|
||||
|
||||
button.clicked.connect(
|
||||
lambda checked, pid=profile_id: self.print_profile_details(f"Profile_{pid}"))
|
||||
|
||||
return button
|
||||
|
||||
def create_profile_name_label(self, parent_label, name):
|
||||
child_label = QLabel(parent_label)
|
||||
child_label.setGeometry(0, 65, 175, 30)
|
||||
child_label.setText(str(name or "Unnamed Profile"))
|
||||
child_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
child_label.setAttribute(
|
||||
Qt.WidgetAttribute.WA_TransparentForMouseEvents)
|
||||
child_label.show()
|
||||
|
||||
def create_profile_icons(self, parent_label, value):
|
||||
for index, icon in enumerate(menu_icons(self.btn_path, value)):
|
||||
child_label = QLabel(parent_label)
|
||||
child_label.setObjectName("label_profiles")
|
||||
child_label.setGeometry(3 + index * 60, 5, 50, 50)
|
||||
child_label.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
|
||||
child_label.setStyleSheet("background-color: rgba(128, 128, 128, 0.5); border-radius: 25px;")
|
||||
pixmap = self._load_profile_icon_pixmap(icon["path"], icon["kind"])
|
||||
child_label.setPixmap(self._add_browser_warning(pixmap, icon["warning"]))
|
||||
child_label.show()
|
||||
|
||||
|
||||
# def _load_profile_icon_pixmap(self, image_path, label_name):
|
||||
# pixmap = QPixmap(image_path)
|
||||
# fallback_names = {
|
||||
# 'location': 'default_location_mini.png',
|
||||
# 'browser': 'default_browser_mini.png',
|
||||
# }
|
||||
# fallback_name = fallback_names.get(label_name)
|
||||
|
||||
# if pixmap.isNull() and fallback_name:
|
||||
# pixmap = QPixmap(os.path.join(self.btn_path, fallback_name))
|
||||
|
||||
# if not pixmap.isNull():
|
||||
# # Scaled to optimize memory and ensure high quality
|
||||
# return pixmap.scaled(
|
||||
# 50, 50,
|
||||
# Qt.AspectRatioMode.KeepAspectRatio,
|
||||
# Qt.TransformationMode.SmoothTransformation
|
||||
# )
|
||||
|
||||
# # Placeholder is already 50x50, so no scaling needed
|
||||
# placeholder = QPixmap(50, 50)
|
||||
# placeholder.fill(QtGui.QColor(200, 200, 200))
|
||||
# return placeholder
|
||||
|
||||
|
||||
def _load_profile_icon_pixmap(self, image_path, label_name):
|
||||
pixmap = QPixmap(image_path)
|
||||
fallback_names = {
|
||||
'location': 'default_location_mini.png',
|
||||
'browser': 'default_browser_mini.png',
|
||||
}
|
||||
fallback_name = fallback_names.get(label_name)
|
||||
if not pixmap.isNull() or fallback_name is None:
|
||||
return pixmap
|
||||
|
||||
pixmap = QPixmap(os.path.join(self.btn_path, fallback_name))
|
||||
if not pixmap.isNull():
|
||||
return pixmap
|
||||
|
||||
placeholder = QPixmap(50, 50)
|
||||
placeholder.fill(QtGui.QColor(200, 200, 200))
|
||||
return placeholder
|
||||
|
||||
def _add_browser_warning(self, pixmap, needs_warning):
|
||||
if not needs_warning:
|
||||
return pixmap
|
||||
painter = QPainter(pixmap)
|
||||
painter.drawPixmap(pixmap.width() - 22, pixmap.height() - 21,
|
||||
QPixmap(os.path.join(self.btn_path, 'warning.png')))
|
||||
painter.end()
|
||||
return pixmap
|
||||
|
||||
def create_verification_widget(self, values):
|
||||
print("create_verification_widget triggered")
|
||||
|
|
@ -448,17 +434,7 @@ class MenuPage(Page):
|
|||
|
||||
value_widget.setText(value)
|
||||
value_widget.setCursorPosition(0)
|
||||
value_widget.setStyleSheet(f"""
|
||||
QLineEdit {{
|
||||
color: white;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.3);
|
||||
padding: 5px 0px;
|
||||
font-size: 12px;
|
||||
{self.font_style}
|
||||
}}
|
||||
""")
|
||||
value_widget.setStyleSheet(verification_style_one(self.font_style))
|
||||
container_layout.addWidget(value_widget, 1)
|
||||
|
||||
checkmark_widget = QLabel("✓")
|
||||
|
|
@ -474,16 +450,7 @@ class MenuPage(Page):
|
|||
QIcon(os.path.join(self.btn_path, "paste_button.png")))
|
||||
copy_button.setIconSize(QSize(24, 24))
|
||||
copy_button.setFixedSize(30, 30)
|
||||
copy_button.setStyleSheet(f"""
|
||||
QPushButton {{
|
||||
background: transparent;
|
||||
border: none;
|
||||
}}
|
||||
QPushButton:hover {{
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 4px;
|
||||
}}
|
||||
""")
|
||||
copy_button.setStyleSheet(VERIFICATION_STYLE_TWO)
|
||||
|
||||
copy_button.clicked.connect(
|
||||
lambda checked=False, val=full_value: self.copy_verification_value(val))
|
||||
|
|
@ -501,46 +468,43 @@ class MenuPage(Page):
|
|||
self.update_status.update_status(
|
||||
"Verification value copied to clipboard")
|
||||
|
||||
def print_profile_details(self, profile_name):
|
||||
summary = self.profile_info.get(profile_name)
|
||||
if summary is None:
|
||||
self.clear_profile_details()
|
||||
|
||||
def print_profile_details(self, profile_id: int):
|
||||
"""Fetch from core a single profile's DTO data to render it."""
|
||||
print(f"entering print_profile_details with profile_id {profile_id}")
|
||||
get_one_serialized_profile(profile_id=profile_id, ui_caller=self.render_on_left_side)
|
||||
|
||||
|
||||
@mainthread
|
||||
def render_on_left_side(self, result: Result):
|
||||
"""Recieve the results for the profile data, convert it, and render it."""
|
||||
if not result.valid:
|
||||
print(f"Data fetch was not valid. {result.message}")
|
||||
return
|
||||
details = get_profile_details(summary["id"], summary,
|
||||
self.connection_manager.is_profile_connected(summary["id"]),
|
||||
self.update_status.update_status)
|
||||
self._render_profile_details(details)
|
||||
|
||||
def _render_profile_details(self, details):
|
||||
raw_data = result.data
|
||||
|
||||
# print(f"details entering _render_profile_details is {details}")
|
||||
# convert raw dict to dto:
|
||||
dto = convert_one_render_dto(raw_data)
|
||||
|
||||
self.reverse_id = details["id"]
|
||||
# clear whatever is on the left side first:
|
||||
self.clear_profile_details()
|
||||
|
||||
# THIS IS UPDATING THE PROFILE ID
|
||||
# sets this for the entire page (very critical):
|
||||
self.reverse_id = dto.profile_id
|
||||
|
||||
# THIS IS ALSO UPDATING THE PROFILE ID
|
||||
self.update_status.current_profile_id = self.reverse_id
|
||||
|
||||
self.current_profile_location = details["location"]
|
||||
|
||||
# Update buttons:
|
||||
self.update_action_button_visuals(self.reverse_id)
|
||||
|
||||
# verification icons:
|
||||
self._set_verification_icons_enabled()
|
||||
|
||||
self.update_status_message(details)
|
||||
for label in self.additional_labels:
|
||||
label.deleteLater()
|
||||
self.additional_labels.clear()
|
||||
if details["background_asset"]:
|
||||
self._detail_image(details["background_asset"], (0, 60, 410, 354), lower=True)
|
||||
renderers = {"verification": self._render_verification,
|
||||
"operator_details": self._render_operator_details,
|
||||
"protocol_image": self._render_protocol_image}
|
||||
renderers[details["display_mode"]](details)
|
||||
if details["show_verified_icon"]:
|
||||
self._detail_image("verified_profile.png", (20, 50, 100, 80))
|
||||
self.add_selected_profile(details["selection"])
|
||||
self._render_protocol_image(dto)
|
||||
self._render_operator_details(dto)
|
||||
|
||||
|
||||
def _detail_image(self, asset, geometry, lower=False, pixmap=None):
|
||||
label = QLabel(self)
|
||||
|
|
@ -556,19 +520,29 @@ class MenuPage(Page):
|
|||
def _render_verification(self, details):
|
||||
self.additional_labels.append(self.create_verification_widget(details["verification_values"]))
|
||||
|
||||
def _render_protocol_image(self, details):
|
||||
pixmap = QPixmap(os.path.join(self.btn_path, details["primary_asset"]))
|
||||
if details["location_overlay"]:
|
||||
pixmap = self.build_encrypted_proxy_detail_pixmap(pixmap, details["location"], details["overlay_y"])
|
||||
self._detail_image(details["primary_asset"], (0, 80 if details["raised_primary"] else 90, 400, 300), pixmap=pixmap)
|
||||
|
||||
def _render_operator_details(self, details):
|
||||
color = "black" if details["is_session"] else "white"
|
||||
self._detail_image(f"icon_{details['location']}.png", (20, 125, 100, 100))
|
||||
def _render_protocol_image(self, dto):
|
||||
primary_asset = dto.primary_asset
|
||||
profile_type = dto.profile_type
|
||||
pixmap = QPixmap(str(primary_asset))
|
||||
self._detail_image(primary_asset, (0, 80 if profile_type == ProfileType.SESSION else 90, 400, 300), pixmap=pixmap)
|
||||
|
||||
|
||||
def _render_operator_details(self, dto: RenderProfileDto):
|
||||
"""Create the left display for the profile, based on the DTO data"""
|
||||
profile_type = dto.profile_type
|
||||
location_text = dto.location_text
|
||||
operator_name = dto.operator_name
|
||||
nostr_public_key = dto.nostr_public_key
|
||||
detail_image = dto.detail_image
|
||||
|
||||
color = "black" if profile_type == ProfileType.SESSION else "white"
|
||||
|
||||
self._detail_image(detail_image, (20, 125, 100, 100))
|
||||
text_blocks = [
|
||||
((130, 110, 260, 250), f"Location: {details['location_text']}\n\nOperator: {details['operator_name']}",
|
||||
((130, 110, 260, 250), f"Location: {location_text}\n\nOperator: {operator_name}",
|
||||
QtGui.QTextOption.WrapMode.WrapAtWordBoundaryOrAnywhere),
|
||||
((20, 260, 370, 80), f"Nostr: {details['nostr_public_key']}", QtGui.QTextOption.WrapMode.WrapAnywhere),
|
||||
((20, 260, 370, 80), f"Nostr: {nostr_public_key}", QtGui.QTextOption.WrapMode.WrapAnywhere),
|
||||
]
|
||||
for geometry, text, wrapping in text_blocks:
|
||||
widget = QTextEdit(self)
|
||||
|
|
@ -584,6 +558,7 @@ class MenuPage(Page):
|
|||
self.additional_labels.append(widget)
|
||||
|
||||
def build_encrypted_proxy_detail_pixmap(self, pixmap, location, overlay_y):
|
||||
"""This will be phased out. But it was for a different display for proxy profiles"""
|
||||
if pixmap.isNull():
|
||||
return pixmap
|
||||
|
||||
|
|
@ -630,32 +605,9 @@ class MenuPage(Page):
|
|||
|
||||
def _set_task_busy(self, busy):
|
||||
pass
|
||||
# self._task_busy = busy
|
||||
# for button in (self.boton_just, self.boton_just_session, self.disconnect_button,
|
||||
# self.disconnect_system_wide_button, self.boton_create, self.boton_edit):
|
||||
# button.setEnabled(not busy)
|
||||
# self.scroll_area.setEnabled(not busy)
|
||||
# self.boton_just.setEnabled(not busy and self.reverse_id is not None)
|
||||
# self.boton_just_session.setEnabled(not busy and self.reverse_id is not None)
|
||||
# self.boton_edit.setEnabled(not busy and self.reverse_id is not None
|
||||
# and not self.connection_manager.is_profile_connected(self.reverse_id))
|
||||
|
||||
|
||||
def post_disconnect(self, result: Result):
|
||||
"""Recieve the callback after disconnection"""
|
||||
# print(f"result is {result}")
|
||||
|
||||
# --------------------
|
||||
# BOTTOM LEFT STATUS BAR
|
||||
# --------------------
|
||||
# if result.message:
|
||||
# if len(result.message) > 20:
|
||||
# self.update_status.disable_marquee
|
||||
# self.update_status.update_status(result.message)
|
||||
|
||||
# --------------------
|
||||
# WORKED
|
||||
# --------------------
|
||||
if result.valid:
|
||||
self.boton_edit.setEnabled(True)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -20,3 +20,4 @@ forced_sync_popup_style = """
|
|||
background-color: #c0392b;
|
||||
}
|
||||
"""
|
||||
|
||||
|
|
|
|||
|
|
@ -12,3 +12,29 @@ CREATE_BUTTON_STYLE = """
|
|||
background-color: rgba(200, 200, 200, 80);
|
||||
}
|
||||
"""
|
||||
|
||||
def verification_style_one(font_style: str) -> str:
|
||||
return f"""
|
||||
QLineEdit {{
|
||||
color: white;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.3);
|
||||
padding: 5px 0px;
|
||||
font-size: 12px;
|
||||
{font_style}
|
||||
}}
|
||||
"""
|
||||
|
||||
|
||||
|
||||
VERIFICATION_STYLE_TWO = f"""
|
||||
QPushButton {{
|
||||
background: transparent;
|
||||
border: none;
|
||||
}}
|
||||
QPushButton:hover {{
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 4px;
|
||||
}}
|
||||
"""
|
||||
Loading…
Reference in a new issue