fxstyle
Styling, theming, and color management for fxgui.
This module provides comprehensive styling functionality including
- Multiple theme support with dynamic theme switching
- Theme persistence across application restarts (via fxconfig)
- QSS stylesheet loading with dynamic color replacement
- Custom QProxyStyle for standard icon overrides
- Theme toggling with icon cache invalidation
- Color loading from JSONC configuration files
Classes:
| Name | Description |
|---|---|
FXProxyStyle |
Custom style providing Material Design icons for Qt standard icons. |
Functions:
| Name | Description |
|---|---|
load_stylesheet |
Load and customize QSS stylesheets. |
get_colors |
Get the cached color configuration. |
set_color_file |
Set a custom color configuration file. |
apply_theme |
Apply a theme to a widget (stylesheet + icons). |
get_available_themes |
Get list of available theme names. |
get_theme |
Get the current theme name. |
get_theme_colors |
Get the color palette for the current theme. |
save_theme |
Save the current theme to persistent storage. |
load_saved_theme |
Load the previously saved theme. |
Constants
STYLE_FILE: Path to the default QSS stylesheet. DEFAULT_COLOR_FILE: Path to the default color configuration.
Examples:
Loading a stylesheet with a theme:
>>> from fxgui import fxstyle
>>> stylesheet = fxstyle.load_stylesheet(theme="dracula")
>>> widget.setStyleSheet(stylesheet)
Applying a theme to a window:
Theme persistence (automatic):
Themes are automatically saved when using apply_theme().
On next application startup, the saved theme is automatically loaded.
FXProxyStyle
Bases: QProxyStyle
A custom style class that extends QProxyStyle to provide custom icons.
Methods:
| Name | Description |
|---|---|
set_icon_color |
Set the color of the icons. |
standardIcon |
Return an icon for the given standardIcon. |
set_icon_color
set_icon_color(color: str)
Set the color of the icons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The color to set the icons to. |
required |
standardIcon
standardIcon(
standardIcon: StandardPixmap,
option: Optional[QStyleOption] = None,
widget: Optional[QWidget] = None,
) -> QIcon
Return an icon for the given standardIcon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
StandardPixmap
|
The standard pixmap for which an icon should be returned. |
required |
|
Optional[QStyleOption]
|
An option that can be used to fine-tune the look of the icon. Defaults to None. |
None
|
|
Optional[QWidget]
|
The widget for which the icon is being requested. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
QIcon
|
The icon for the standardIcon. If no custom icon is found, |
QIcon
|
the default icon is returned. |
apply_theme
Apply a theme to the widget with full style updates.
This function handles all necessary state updates including: - Switching the stylesheet - Updating icon colors - Invalidating icon caches
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
QWidget
|
The QWidget subclass to apply the theme to. |
required |
|
str
|
The theme name to apply (e.g., "dark", "light", or custom). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The theme that was applied. |
Examples:
Apply a specific theme:
get_accent_colors
Get the accent colors for the current theme.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing 'primary' and 'secondary' accent colors |
dict
|
from the current theme. |
Examples:
get_available_themes
get_colors
Get the cached color configuration dictionary.
This is the preferred way to access colors throughout the application. Colors are loaded once from the JSONC file and cached for subsequent calls.
Returns:
| Type | Description |
|---|---|
dict
|
The complete color configuration containing 'accent', 'feedback', |
dict
|
'dcc', and 'themes' sections. |
Examples:
get_contrast_text_color
get_contrast_text_color(background_hex: str) -> str
Determine whether to use white or black text on a given background.
Uses WCAG luminance calculation to ensure readable contrast.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The background color as a hex string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
"#FFFFFF" for dark backgrounds, "#000000" for light backgrounds. |
get_icon_color
get_luminance
get_luminance(hex_color: str) -> float
Calculate the relative luminance of a color.
Uses the WCAG 2.0 formula for relative luminance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
A hex color string (e.g., "#007ACC" or "007ACC"). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The relative luminance value between 0 (black) and 1 (white). |
get_theme
Get the current theme name.
On first access, the theme is loaded from persistent storage. If no theme was previously saved, defaults to "dark".
Returns:
| Type | Description |
|---|---|
str
|
The current theme name (e.g., "dark", "light"). |
get_theme_colors
invalidate_standard_icon_map
Invalidate the cached standard icon map.
This should be called when changing themes so icons are regenerated with the new color scheme on next access.
load_saved_theme
load_stylesheet
load_stylesheet(
style_file: str = STYLE_FILE, extra: Optional[str] = None, theme: str = None
) -> str
Load the stylesheet and replace placeholders with actual values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The path to the QSS file. Defaults to |
STYLE_FILE
|
|
Optional[str]
|
Extra stylesheet content to append. Defaults to None. |
None
|
|
str
|
The theme to use (e.g., "dark", "light", "dracula"). If None, uses the saved theme from persistent storage. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The stylesheet with all placeholders replaced. |
replace_colors
replace_colors(
stylesheet: str, colors_dict: dict = None, prefix: str = ""
) -> str
Replace color placeholders in a stylesheet with actual color values.
This function searches for placeholders in the format @{prefix}{key}
and replaces them with the corresponding color values from the dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The stylesheet string containing color placeholders. |
required |
|
dict
|
Dictionary containing color definitions. Only top-level non-dict values are used. Defaults to colors from get_colors(). |
None
|
|
str
|
Prefix for placeholder names. Defaults to empty string. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
The stylesheet with all matching placeholders replaced. |
Examples:
save_theme
save_theme(theme: str) -> None
set_color_file
set_color_file(jsonc_file: str) -> None
Set a custom color configuration file.
This clears the color cache and sets the new file as the active
color source. The next call to get_colors() will load from this file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Path to the custom JSONC color configuration file. |
required |
Examples:
set_style
set_style(widget: QWidget, style: str = None) -> FXProxyStyle
Set the style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
QWidget
|
The QWidget subclass to set the style to. |
required |
|
str
|
The style to set. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
FXProxyStyle
|
The custom style. |
Note
You can retrieve the styles available on your system with
QStyleFactory.keys(). Only those string values are accepted
in the style argument.