11import os
2+ from typing import Optional
23from PySide6 .QtCore import Qt , QTimer
34from PySide6 .QtWidgets import QApplication , QWidget
45from loguru import logger
56
67from app .tools .settings_access import readme_settings_async
78from app .tools .variable import FONT_APPLY_DELAY
9+ from app .core .utils import safe_execute
810
911
10- def configure_dpi_scale ():
12+ def configure_dpi_scale () -> None :
1113 """在创建QApplication之前配置DPI缩放模式"""
1214 try :
13- dpiScale = readme_settings_async ("basic_settings" , "dpiScale" )
14- if dpiScale == "Auto" :
15- QApplication .setHighDpiScaleFactorRoundingPolicy (
16- Qt .HighDpiScaleFactorRoundingPolicy .PassThrough
17- )
18- os .environ ["QT_ENABLE_HIGHDPI_SCALING" ] = "1"
19- logger .debug ("DPI缩放已设置为自动模式" )
15+ dpi_scale = readme_settings_async ("basic_settings" , "dpiScale" )
16+ if dpi_scale == "Auto" :
17+ _set_auto_dpi ()
2018 else :
21- os .environ ["QT_ENABLE_HIGHDPI_SCALING" ] = "0"
22- os .environ ["QT_SCALE_FACTOR" ] = str (dpiScale )
23- logger .debug (f"DPI缩放已设置为{ dpiScale } 倍" )
19+ _set_manual_dpi (dpi_scale )
2420 except Exception as e :
2521 logger .warning (f"读取DPI设置失败,使用默认设置: { e } " )
26- QApplication .setHighDpiScaleFactorRoundingPolicy (
27- Qt .HighDpiScaleFactorRoundingPolicy .PassThrough
28- )
29- os .environ ["QT_ENABLE_HIGHDPI_SCALING" ] = "1"
22+ _set_auto_dpi ()
3023
3124
32- def apply_font_settings ():
25+ def _set_auto_dpi () -> None :
26+ """设置自动DPI缩放"""
27+ QApplication .setHighDpiScaleFactorRoundingPolicy (
28+ Qt .HighDpiScaleFactorRoundingPolicy .PassThrough
29+ )
30+ os .environ ["QT_ENABLE_HIGHDPI_SCALING" ] = "1"
31+ logger .debug ("DPI缩放已设置为自动模式" )
32+
33+
34+ def _set_manual_dpi (scale : str ) -> None :
35+ """设置手动DPI缩放
36+
37+ Args:
38+ scale: 缩放倍数
39+ """
40+ os .environ ["QT_ENABLE_HIGHDPI_SCALING" ] = "0"
41+ os .environ ["QT_SCALE_FACTOR" ] = str (scale )
42+ logger .debug (f"DPI缩放已设置为{ scale } 倍" )
43+
44+
45+ def apply_font_settings () -> None :
3346 """应用字体设置 - 优化版本,使用字体管理器异步加载"""
3447 font_family = readme_settings_async ("basic_settings" , "font" )
3548
3649 from qfluentwidgets import setFontFamilies
3750
3851 setFontFamilies ([font_family ])
39- QTimer .singleShot (FONT_APPLY_DELAY , lambda : apply_font_to_application (font_family ))
52+ QTimer .singleShot (
53+ FONT_APPLY_DELAY ,
54+ lambda : safe_execute (
55+ apply_font_to_application , font_family , error_message = "应用字体失败"
56+ ),
57+ )
4058
4159
42- def apply_font_to_application (font_family : str ):
60+ def apply_font_to_application (font_family : str ) -> None :
4361 """应用字体设置到整个应用程序,优化版本使用字体管理器
4462
4563 Args:
46- font_family (str) : 字体家族名称
64+ font_family: 字体家族名称
4765 """
48- try :
49- current_font = QApplication .font ()
50- app_font = current_font
51- app_font .setFamily (font_family )
52- widgets_updated = 0
53- widgets_skipped = 0
54- for widget in QApplication .allWidgets ():
55- if isinstance (widget , QWidget ):
56- if update_widget_fonts (widget , app_font , font_family ):
57- widgets_updated += 1
58- else :
59- widgets_skipped += 1
60- logger .debug (
61- f"已应用字体: { font_family } , 更新了{ widgets_updated } 个控件字体, 跳过了{ widgets_skipped } 个已有相同字体的控件"
62- )
63- except Exception as e :
64- logger .error (f"应用字体失败: { e } " )
66+ current_font = QApplication .font ()
67+ app_font = current_font
68+ app_font .setFamily (font_family )
6569
70+ widgets_updated = 0
71+ widgets_skipped = 0
72+
73+ for widget in QApplication .allWidgets ():
74+ if isinstance (widget , QWidget ):
75+ if update_widget_fonts (widget , app_font , font_family ):
76+ widgets_updated += 1
77+ else :
78+ widgets_skipped += 1
6679
67- def update_widget_fonts (widget : QWidget , font , font_family : str ) -> bool :
80+ logger .debug (
81+ f"已应用字体: { font_family } , 更新了{ widgets_updated } 个控件字体, "
82+ f"跳过了{ widgets_skipped } 个已有相同字体的控件"
83+ )
84+
85+
86+ def update_widget_fonts (widget : Optional [QWidget ], font , font_family : str ) -> bool :
6887 """更新控件及其子控件的字体,优化版本减少内存占用,特别处理ComboBox等控件
6988
7089 Args:
@@ -78,27 +97,22 @@ def update_widget_fonts(widget: QWidget, font, font_family: str) -> bool:
7897 if widget is None :
7998 return False
8099
81- try :
82- if not hasattr (widget , "font" ) or not hasattr (widget , "setFont" ):
83- return False
84- current_widget_font = widget .font ()
85- if current_widget_font .family () == font_family :
86- updated = False
87- else :
88- new_font = font
89- new_font .setBold (current_widget_font .bold ())
90- new_font .setItalic (current_widget_font .italic ())
91- widget .setFont (new_font )
92- updated = True
100+ if not hasattr (widget , "font" ) or not hasattr (widget , "setFont" ):
101+ return False
93102
94- if isinstance (widget , QWidget ):
95- children = widget .children ()
96- for child in children :
97- if isinstance (child , QWidget ):
98- child_updated = update_widget_fonts (child , font , font_family )
99- if child_updated :
100- updated = True
101- return updated
102- except Exception as e :
103- logger .exception ("更新控件字体时发生异常: {}" , e )
103+ current_widget_font = widget .font ()
104+ if current_widget_font .family () == font_family :
104105 return False
106+
107+ new_font = font
108+ new_font .setBold (current_widget_font .bold ())
109+ new_font .setItalic (current_widget_font .italic ())
110+ widget .setFont (new_font )
111+
112+ if isinstance (widget , QWidget ):
113+ children = widget .children ()
114+ for child in children :
115+ if isinstance (child , QWidget ):
116+ update_widget_fonts (child , font , font_family )
117+
118+ return True
0 commit comments