/
Flipper
/
Marginal
Обзор
Документация
Войти
/
Flipper
/
Marginal
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
master
src/AppearanceManager.py
84 строки
3 KB
Alex
Marginal v1.0.0 RELEASE
11 янв 2026, 18:02
11 янв 2026, 18:02
5e1eff8
Код
Авторство
О чём код?
import os from PySide6.QtWidgets import QApplication, QAbstractItemView import utils import compiled_resources class AppearanceManager(): """ Manages the application's visual appearance by loading and applying Qt Style Sheets (QSS). Handles theme loading, fallback to defaults, and widget refresh after theme changes. """ def __init__(self, app: QApplication, configManager, logger=None): """ Initialize AppearanceManager with Qt application and configuration. Args: app (QApplication): The main Qt application instance. configManager (ConfigManager): Application configuration manager. logger (Logger, optional): Application logger for error reporting. """ self.app = app self.logger = logger self.configManager = configManager # Theme directory paths self.themeDir = os.path.join(utils.getBasePath(), "resources", "themes") self.emergencyTheme = os.path.join(utils.getBasePath(), r"local", "") self.prevTheme = str() # Track previously applied theme # Apply initial theme from configuration theme = self.configManager.getConfig("appearance")["theme"] self.applyTheme(theme) def applyTheme(self, themeName: str): """ Apply a Qt Style Sheet theme to the application. Args: themeName (str): Name of the theme file (without .qss extension). Returns: None Note: Theme fallback order: 1. User-selected theme 2. Default theme from configuration 3. Emergency empty theme (no styling) """ # Skip if theme name is empty or same as previous theme if themeName is not str() and themeName == self.prevTheme: return pathToTheme = os.path.join(self.themeDir, f"{themeName}.qss") # Check if requested theme exists, fall back to default if not if not os.path.exists(pathToTheme): defaultTheme = self.configManager.getDefault("appearance")["theme"] defaultThemePath = os.path.join(self.themeDir, f"{defaultTheme}.qss") if os.path.exists(defaultThemePath): pathToTheme = defaultThemePath else: pathToTheme = self.emergencyTheme if self.logger: self.logger.log( "AppearanceManager", "Failed to load previous theme - reverting to default." ) # Load and apply the theme with open(pathToTheme, "r", encoding="utf-8") as file: self.app.setStyleSheet(file.read()) self.prevTheme = themeName # Refresh all widgets to apply new theme for widget in self.app.allWidgets(): if isinstance(widget, QAbstractItemView): widget.viewport().update() else: widget.update()