# src/certflow/config/ui_config.py
"""UI配置加载模块
提供用户界面的配置数据结构和加载功能,支持从YAML文件动态加载UI配置,
包括窗口设置、按钮样式、页面配置和主题样式等.
"""
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import yaml
[文档]
@dataclass
class PageConfig:
"""页面配置数据类
定义应用程序中单个页面的完整配置信息.
Attributes:
title: 页面标题配置,包含text、font_size、alignment等属性
buttons: 页面内按钮配置字典,键为按钮名称,值为ButtonConfig对象
custom_config: 页面特定的自定义配置,用于扩展功能
"""
title: dict[str, Any]
buttons: dict[str, ButtonConfig] = field(default_factory=dict)
custom_config: dict[str, Any] = field(default_factory=dict)
[文档]
@dataclass
class UIConfig:
"""UI完整配置数据类
聚合应用程序所有UI相关的配置,包括主窗口、标题栏、导航按钮、
各个页面、状态栏和样式定义.
Attributes:
main_window: 主窗口配置,包含标题、尺寸、最小尺寸等
title_bar: 标题栏配置,包含文本、字体、颜色、对齐方式等
nav_buttons: 导航按钮配置列表,每个元素为按钮配置字典
welcome_page: 欢迎页面配置
import_page: 导入页面配置
log_page: 日志页面配置
status_bar: 状态栏配置,包含默认消息、超时时间等
styles: 样式配置字典,包含全局样式和各组件样式
Examples:
>>> from pathlib import Path
>>> config = UIConfig.load(Path("config/ui.yaml"))
>>> buttons = config.get_nav_buttons()
>>> style = config.get_style("button.default")
"""
main_window: dict[str, Any]
title_bar: dict[str, Any]
nav_buttons: list
welcome_page: dict[str, Any]
import_page: dict[str, Any]
log_page: dict[str, Any]
status_bar: dict[str, Any]
styles: dict[str, str]
[文档]
@classmethod
def load(cls, config_path: Path) -> "UIConfig":
"""从YAML文件加载UI配置
读取指定的YAML配置文件,解析并构建UIConfig实例.
Args:
config_path: UI配置文件路径(YAML格式)
Returns:
UIConfig: 加载完成的UI配置实例
Raises:
FileNotFoundError: 配置文件不存在时抛出
yaml.YAMLError: YAML格式错误或解析失败时抛出
Examples:
>>> config = UIConfig.load(Path("config/ui.yaml"))
>>> print(config.main_window.get("title"))
"CertFlow - 证书管理系统"
"""
with open(config_path, encoding="utf-8") as f:
data = yaml.safe_load(f)
return cls(
main_window=data.get("main_window", {}),
title_bar=data.get("title_bar", {}),
nav_buttons=data.get("nav_buttons", []),
welcome_page=data.get("welcome_page", {}),
import_page=data.get("import_page", {}),
log_page=data.get("log_page", {}),
status_bar=data.get("status_bar", {}),
styles=data.get("styles", {}),
)
[文档]
def get_style(self, style_name: str, default: str = "") -> str:
"""获取指定名称的样式配置
支持使用点号分隔的路径访问嵌套样式配置.
Args:
style_name: 样式名称,支持嵌套路径如"button.default"
default: 样式不存在时返回的默认值,默认为空字符串
Returns:
str: 样式字符串,如果样式不存在则返回默认值
Examples:
>>> # 获取默认按钮样式
>>> btn_style = config.get_style("button.default")
>>> # 获取全局样式
>>> global_style = config.get_style("global", "")
"""
keys = style_name.split(".")
value = self.styles
for key in keys:
if isinstance(value, dict):
value = value.get(key, {})
else:
return default
return value if isinstance(value, str) else default
[文档]
class StyleBuilder:
"""样式构建器
提供静态方法,用于根据配置字典动态生成Qt CSS样式字符串.
支持按钮样式、标题样式等常见UI组件的样式生成.
Examples:
>>> style_config = {"padding": "15px", "font_size": "16px"}
>>> css = StyleBuilder.build_button_style(style_config)
"""
[文档]
@staticmethod
def build_title_style(config: dict[str, Any]) -> str:
"""构建标题样式CSS
根据配置生成标题栏或页面标题的样式字符串.
Args:
config: 标题样式配置字典,支持以下键:
- font_size: 字体大小(像素),默认为18
- font_weight: 字体粗细(normal/bold),默认为"normal"
- padding: 内边距,默认为"10px"
- background_color: 背景颜色,默认为"#2c7be5"
- text_color: 文字颜色,默认为"white"
Returns:
str: 标题CSS样式字符串,适用于QLabel或QWidget
Examples:
>>> config = {"font_size": 24, "font_weight": "bold", "text_color": "#333"}
>>> css = StyleBuilder.build_title_style(config)
>>> print(css)
"font-size: 24px; font-weight: bold; padding: 10px; background-color: #2c7be5; color: #333;"
"""
font_size = config.get("font_size", 18)
font_weight = config.get("font_weight", "normal")
padding = config.get("padding", "10px")
bg_color = config.get("background_color", "#2c7be5")
text_color = config.get("text_color", "white")
return f"""
font-size: {font_size}px;
font-weight: {font_weight};
padding: {padding};
background-color: {bg_color};
color: {text_color};
"""