添加json编辑器
This commit is contained in:
parent
6d93c035b8
commit
47ee71d7e2
63
interface/json_edit.py
Normal file
63
interface/json_edit.py
Normal file
|
@ -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_())
|
|
@ -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)) # 设置布局
|
||||
self.base.addWidget(LargeTitleLabel(title)) # 创建按钮并添加到布局
|
||||
self.setLayout(self.base) # 设置布局
|
|
@ -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("调试模式已关闭")
|
||||
# 在这里添加关闭调试模式的代码
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user