99 Zeilen
3.1 KiB
Python
99 Zeilen
3.1 KiB
Python
"""
|
|
Theme-Aware Base Widget - Base class for all custom widgets
|
|
Provides automatic theme updates and color access
|
|
"""
|
|
|
|
from PyQt5.QtWidgets import QWidget
|
|
from PyQt5.QtCore import pyqtSlot
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ThemeAwareWidget(QWidget):
|
|
"""
|
|
Base class for all custom widgets that need theme support.
|
|
Automatically connects to theme changes and provides helper methods.
|
|
"""
|
|
|
|
def __init__(self, parent=None):
|
|
"""
|
|
Initialize theme-aware widget.
|
|
|
|
Args:
|
|
parent: Parent widget
|
|
"""
|
|
super().__init__(parent)
|
|
self._theme_manager = None
|
|
self._setup_theme_connection()
|
|
|
|
def _setup_theme_connection(self):
|
|
"""Setup connection to theme manager for automatic updates."""
|
|
try:
|
|
# Try to find theme manager in main window
|
|
main_window = self.window()
|
|
if main_window and hasattr(main_window, 'theme_manager'):
|
|
self._theme_manager = main_window.theme_manager
|
|
# Connect to theme change signal
|
|
self._theme_manager.theme_changed.connect(self._on_theme_changed)
|
|
logger.debug(f"{self.__class__.__name__} connected to theme manager")
|
|
except Exception as e:
|
|
logger.warning(f"Could not connect to theme manager: {e}")
|
|
|
|
@pyqtSlot(str)
|
|
def _on_theme_changed(self, theme_name: str):
|
|
"""
|
|
Called when theme changes.
|
|
Override in subclasses to handle theme-specific updates.
|
|
|
|
Args:
|
|
theme_name: Name of the new theme ('light' or 'dark')
|
|
"""
|
|
# Subclasses should override this method
|
|
pass
|
|
|
|
def get_theme_color(self, color_key: str) -> str:
|
|
"""
|
|
Get a color from the current theme.
|
|
|
|
Args:
|
|
color_key: Key of the color in theme configuration
|
|
|
|
Returns:
|
|
Color value as hex string, or empty string if not found
|
|
"""
|
|
if self._theme_manager:
|
|
return self._theme_manager.get_color(color_key)
|
|
return ''
|
|
|
|
def get_current_theme(self) -> str:
|
|
"""
|
|
Get the current theme name.
|
|
|
|
Returns:
|
|
Current theme name ('light' or 'dark'), or 'light' as default
|
|
"""
|
|
if self._theme_manager:
|
|
return self._theme_manager.get_current_theme()
|
|
return 'light'
|
|
|
|
def is_dark_mode(self) -> bool:
|
|
"""
|
|
Check if dark mode is currently active.
|
|
|
|
Returns:
|
|
True if dark mode is active, False otherwise
|
|
"""
|
|
if self._theme_manager:
|
|
return self._theme_manager.is_dark_mode()
|
|
return False
|
|
|
|
def showEvent(self, event):
|
|
"""
|
|
Override showEvent to ensure theme connection is established.
|
|
Some widgets might not have access to main window during __init__.
|
|
"""
|
|
super().showEvent(event)
|
|
# Try to setup theme connection again if not established
|
|
if not self._theme_manager:
|
|
self._setup_theme_connection() |