From 47ee71d7e232ae70276148221f0ac8bcc987bdde Mon Sep 17 00:00:00 2001 From: moyanj <1561515308@qq.com> Date: Sun, 8 Sep 2024 20:57:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0json=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env.py | 0 interface/json_edit.py | 63 ++++++++++++++++++++++++++++++++++++++++++ interface/main.py | 7 +++-- interface/setting.py | 12 ++------ 4 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 env.py create mode 100644 interface/json_edit.py diff --git a/env.py b/env.py new file mode 100644 index 0000000..e69de29 diff --git a/interface/json_edit.py b/interface/json_edit.py new file mode 100644 index 0000000..50729ab --- /dev/null +++ b/interface/json_edit.py @@ -0,0 +1,63 @@ +from PyQt5.QtWidgets import QApplication, QTextEdit +from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QColor +from PyQt5.QtCore import QRegularExpression, Qt + +class CodeEditor(QTextEdit): + def __init__(self): + super().__init__() + self.setFont(QFont("Courier New", 12)) + self.highlighter = CodeHighlighter(self.document()) + self.highlighter.rehighlight() + + def paste(self): + super().paste() + self.highlighter.rehighlight() + +class CodeHighlighter(QSyntaxHighlighter): + def __init__(self, document): + super().__init__(document) + self.highlightingRules = [] + + # 定义 JSON 高亮规则 + self.keyFormat = QTextCharFormat() + self.keyFormat.setForeground(Qt.GlobalColor.darkBlue) + self.keyFormat.setFontWeight(QFont.Bold) + + self.valueFormat = QTextCharFormat() + self.valueFormat.setForeground(Qt.GlobalColor.darkGreen) + + self.boolFormat = QTextCharFormat() + self.boolFormat.setForeground(Qt.GlobalColor.red) + + self.numberFormat = QTextCharFormat() + self.numberFormat.setForeground(Qt.GlobalColor.darkCyan) + + # 创建蓝色格式 + blueFormat = QTextCharFormat() + blueFormat.setForeground(Qt.GlobalColor.blue) + + self.rules = [ + (QRegularExpression(r'\b(true|false|null)\b'), self.boolFormat), + (QRegularExpression(r'\b(-?\d+\.?\d*)\b'), self.numberFormat), + (QRegularExpression(r'\{'), blueFormat), + (QRegularExpression(r'\}'), blueFormat), + (QRegularExpression(r'\['), blueFormat), + (QRegularExpression(r'\]'), blueFormat), + (QRegularExpression(r'\"(\\.|[^"\\])*\"'), self.valueFormat), + (QRegularExpression(r'\b(\w+)\s*:'), self.keyFormat) + ] + + def highlightBlock(self, text): + for pattern, format in self.rules: + expression = QRegularExpression(pattern) + matchIterator = expression.globalMatch(text) + while matchIterator.hasNext(): + match = matchIterator.next() + self.setFormat(match.capturedStart(), match.capturedLength(), format) + +if __name__ == "__main__": + import sys + app = QApplication(sys.argv) + editor = CodeEditor() + editor.show() + sys.exit(app.exec_()) \ No newline at end of file diff --git a/interface/main.py b/interface/main.py index a271cf0..419547f 100644 --- a/interface/main.py +++ b/interface/main.py @@ -1,9 +1,10 @@ from PyQt5.QtWidgets import QWidget -from qfluentwidgets import PushButton, VBoxLayout +from qfluentwidgets import VBoxLayout, LargeTitleLabel class MainWidget(QWidget): def __init__(self, title="主页"): super().__init__() + self.base = VBoxLayout(self) - PushButton(title, self) # 创建按钮并添加到布局 - self.setLayout(VBoxLayout(self)) # 设置布局 \ No newline at end of file + self.base.addWidget(LargeTitleLabel(title)) # 创建按钮并添加到布局 + self.setLayout(self.base) # 设置布局 \ No newline at end of file diff --git a/interface/setting.py b/interface/setting.py index 0d4d8cd..a4aacef 100644 --- a/interface/setting.py +++ b/interface/setting.py @@ -1,21 +1,15 @@ from PyQt5.QtWidgets import QWidget, QHBoxLayout from qfluentwidgets import SwitchSettingCard from qfluentwidgets import FluentIcon as FIF +from .json_edit import CodeEditor class SettingWidget(QWidget): def __init__(self, title="设置"): super().__init__() self.base = QHBoxLayout(self) - c1 = SwitchSettingCard(FIF.DEVELOPER_TOOLS, "调试模式") - c1.checkedChanged.connect(self.on_switch_changed) + c1 = CodeEditor() self.base.addWidget(c1) self.setLayout(self.base) # 设置布局 - def on_switch_changed(self, isChecked): - if isChecked: - print("调试模式已开启") - # 在这里添加开启调试模式的代码 - else: - print("调试模式已关闭") - # 在这里添加关闭调试模式的代码 +