提交 1734a8ae 编写于 作者: 之一Yo's avatar 之一Yo

添加修改主题时是否保存配置的选项

上级 6af6c121
...@@ -253,7 +253,7 @@ class QConfig(QObject): ...@@ -253,7 +253,7 @@ class QConfig(QObject):
themeChanged = pyqtSignal(Theme) themeChanged = pyqtSignal(Theme)
themeMode = OptionsConfigItem( themeMode = OptionsConfigItem(
"MainWindow", "ThemeMode", Theme.AUTO, OptionsValidator(Theme), EnumSerializer(Theme)) "QFluentWidgets", "ThemeMode", Theme.AUTO, OptionsValidator(Theme), EnumSerializer(Theme))
def __init__(self): def __init__(self):
super().__init__() super().__init__()
...@@ -265,18 +265,21 @@ class QConfig(QObject): ...@@ -265,18 +265,21 @@ class QConfig(QObject):
""" get the value of config item """ """ get the value of config item """
return item.value return item.value
def set(self, item, value): def set(self, item, value, save=True):
""" set the value of config item """ """ set the value of config item """
if item.value == value: if item.value == value:
return return
item.value = value item.value = value
self.save()
if save:
self.save()
if item.restart: if item.restart:
self._cfg.appRestartSig.emit() self._cfg.appRestartSig.emit()
if item is self._cfg.themeMode: if item is self._cfg.themeMode:
self.theme = value
self._cfg.themeChanged.emit(value) self._cfg.themeChanged.emit(value)
def toDict(self, serialize=True): def toDict(self, serialize=True):
...@@ -300,6 +303,7 @@ class QConfig(QObject): ...@@ -300,6 +303,7 @@ class QConfig(QObject):
return items return items
def save(self): def save(self):
""" save config """
self._cfg.file.parent.mkdir(parents=True, exist_ok=True) self._cfg.file.parent.mkdir(parents=True, exist_ok=True)
with open(self._cfg.file, "w", encoding="utf-8") as f: with open(self._cfg.file, "w", encoding="utf-8") as f:
json.dump(self._cfg.toDict(), f, ensure_ascii=False, indent=4) json.dump(self._cfg.toDict(), f, ensure_ascii=False, indent=4)
...@@ -345,14 +349,7 @@ class QConfig(QObject): ...@@ -345,14 +349,7 @@ class QConfig(QObject):
if items.get(key) is not None: if items.get(key) is not None:
items[key].deserializeFrom(value) items[key].deserializeFrom(value)
if self.get(self.themeMode) == Theme.AUTO: self.theme = self.get(self.themeMode)
theme = darkdetect.theme()
if theme:
self._cfg._theme = Theme(theme)
else:
self._cfg._theme = Theme.LIGHT
else:
self._cfg._theme = self.get(self.themeMode)
@property @property
def theme(self): def theme(self):
......
...@@ -87,14 +87,8 @@ def setStyleSheet(widget, file, theme=Theme.AUTO, register=True): ...@@ -87,14 +87,8 @@ def setStyleSheet(widget, file, theme=Theme.AUTO, register=True):
widget.setStyleSheet(getStyleSheet(file, theme)) widget.setStyleSheet(getStyleSheet(file, theme))
def setTheme(theme: Theme): def updateStyleSheet():
""" set the theme of application """ """ update the style sheet of all fluent widgets """
if theme == Theme.AUTO:
theme = darkdetect.theme()
qconfig.theme = Theme(theme) if theme else Theme.LIGHT
else:
qconfig.theme = theme
removes = [] removes = []
for widget, file in styleSheetManager.items(): for widget, file in styleSheetManager.items():
try: try:
...@@ -103,4 +97,23 @@ def setTheme(theme: Theme): ...@@ -103,4 +97,23 @@ def setTheme(theme: Theme):
removes.append(widget) removes.append(widget)
for widget in removes: for widget in removes:
styleSheetManager.deregister(widget) styleSheetManager.deregister(widget)
\ No newline at end of file
def setTheme(theme: Theme, save=False):
""" set the theme of application
Parameters
----------
theme: Theme
theme mode
save: bool
whether to save the change to config file
"""
if theme == Theme.AUTO:
theme = darkdetect.theme()
theme = Theme(theme) if theme else Theme.LIGHT
qconfig.set(qconfig.themeMode, theme, save)
updateStyleSheet()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册