diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..1c7cb5d --- /dev/null +++ b/app/utils.py @@ -0,0 +1,10 @@ +import app.env as env +from qfluentwidgets import setTheme, Theme +def switchTheme(theme): + if theme == "dark": + setTheme(Theme.DARK) + env.theme = "dark" + elif theme == "light": + setTheme(Theme.LIGHT) + else: + setTheme(Theme.AUTO) \ No newline at end of file diff --git a/config.json b/config.json deleted file mode 100644 index c21f73d..0000000 --- a/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "1":"13" -} \ No newline at end of file diff --git a/interface/json_edit.py b/interface/json_edit.py index 261647f..0a60943 100644 --- a/interface/json_edit.py +++ b/interface/json_edit.py @@ -1,17 +1,25 @@ from PyQt5.QtWidgets import QApplication, QTextEdit -from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QFontMetrics +from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QFontMetrics, QColor from PyQt5.QtCore import QRegularExpression, Qt - +import app.env as env class CodeEditor(QTextEdit): def __init__(self, text:str=""): super().__init__() self.setFont(QFont("Courier New", 12)) fontMetrics = QFontMetrics(self.font()) - tabWidth = 4 * fontMetrics.width(' ') # Calculate the width of a tab + tabWidth = 4 * fontMetrics.width(' ') # 计算tab的宽度 self.setTabStopWidth(tabWidth) self.setPlainText(text) self.highlighter = CodeHighlighter(self.document()) self.highlighter.rehighlight() + if env.config.theme== "dark": + # 设置深色模式的样式 + self.setStyleSheet(""" + background-color: #2b2b2b; /* 背景颜色 */ + color: #ffffff; /* 文本颜色 */ + selection-background-color: #4e9a06; /* 选中背景颜色 */ + selection-color: #ffffff; /* 选中文本颜色 */ + """) def paste(self): super().paste() @@ -24,21 +32,21 @@ class CodeHighlighter(QSyntaxHighlighter): # 定义 JSON 高亮规则 self.keyFormat = QTextCharFormat() - self.keyFormat.setForeground(Qt.GlobalColor.darkBlue) + self.keyFormat.setForeground(QColor("#0000ff")) # 蓝色关键字 self.keyFormat.setFontWeight(QFont.Bold) self.valueFormat = QTextCharFormat() - self.valueFormat.setForeground(Qt.GlobalColor.darkGreen) + self.valueFormat.setForeground(QColor("#008000")) # 绿色字符串值 self.boolFormat = QTextCharFormat() - self.boolFormat.setForeground(Qt.GlobalColor.red) + self.boolFormat.setForeground(QColor("#ff0000")) # 红色布尔值 self.numberFormat = QTextCharFormat() - self.numberFormat.setForeground(Qt.GlobalColor.darkCyan) + self.numberFormat.setForeground(QColor("#008080")) # 青色数字 # 创建蓝色格式 blueFormat = QTextCharFormat() - blueFormat.setForeground(Qt.GlobalColor.blue) + blueFormat.setForeground(QColor("#0000ff")) self.rules = [ (QRegularExpression(r'\b(true|false|null)\b'), self.boolFormat), diff --git a/interface/setting.py b/interface/setting.py index 22fe498..d269b41 100644 --- a/interface/setting.py +++ b/interface/setting.py @@ -3,6 +3,8 @@ from PyQt5.QtCore import Qt from qfluentwidgets import FluentIcon as FIF from qfluentwidgets import CommandBar, Action, InfoBar, InfoBarPosition, SubtitleLabel from .json_edit import CodeEditor +import app.env as env +import os class SettingWidget(QWidget): def __init__(self): @@ -23,7 +25,7 @@ class SettingWidget(QWidget): self.base.addLayout(bar_layout) # 加载 JSON 配置 - with open("config.json", "r") as config_file: + with open(os.path.join(env.dirs.user_config_path, "config.json"), "r") as config_file: config_data = config_file.read() self.c1 = CodeEditor(config_data) # 将代码编辑器添加到垂直布局中 diff --git a/main.py b/main.py index 947cb44..d331c2b 100644 --- a/main.py +++ b/main.py @@ -18,10 +18,8 @@ class MainWindow(FluentWindow): if env.config.theme == "dark": setTheme(Theme.DARK) - elif env.config.theme == "light": - setTheme(Theme.LIGHT) else: - setTheme(Theme.AUTO) + setTheme(Theme.LIGHT) # 定义id和对象映射 self.id2obj = {