-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTestSerializeModel.py
More file actions
131 lines (108 loc) · 4.1 KB
/
TestSerializeModel.py
File metadata and controls
131 lines (108 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2025/08/08
@file: TestSerializeModel.py
@description:
"""
import json
import sys
from datetime import datetime
from Lib.qmodelmapper import QModelMapper
from Lib.serializewidget import Ui_SerializeWidget
try:
from PyQt5.QtCore import QRegExp, Qt
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat
from PyQt5.QtWidgets import QApplication, QWidget
except ImportError:
from PySide2.QtCore import QRegExp, Qt
from PySide2.QtGui import QSyntaxHighlighter, QTextCharFormat
from PySide2.QtWidgets import QApplication, QWidget
class HighlightingRule:
def __init__(self, pattern, color):
self.pattern = QRegExp(pattern)
self.format = QTextCharFormat()
self.format.setForeground(color)
class JsonHighlighter(QSyntaxHighlighter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._rules = [
# numbers
HighlightingRule(QRegExp('([-0-9.]+)(?!([^"]*"[\\s]*\\:))'), Qt.darkRed),
# key
HighlightingRule(QRegExp('("[^"]*")\\s*\\:'), Qt.darkBlue),
# value
HighlightingRule(QRegExp(':+(?:[: []*)("[^"]*")'), Qt.darkGreen),
]
def highlightBlock(self, text: str) -> None:
for rule in self._rules:
index = rule.pattern.indexIn(text)
while index >= 0:
length = rule.pattern.matchedLength()
self.setFormat(index, length, rule.format)
index = rule.pattern.indexIn(text, index + length)
class TestWindow(QWidget, Ui_SerializeWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setupUi(self)
self.resize(1200, 600)
# json view
self.highlighter = JsonHighlighter(self.editJsonView.document())
self.editJsonView.textChanged.connect(self.onJsonChanged)
# serialize model
QModelMapper.Debug = True
self.mapper = QModelMapper(self)
self.mapper.setData(
{
"input": {
"radioButton": True,
"checkBox": False,
},
"name": "Irony",
}
)
self.mapper.valueChanged.connect(self.onModelChanged)
self.treeJsonView.setModel(self.mapper.getModel())
self.treeJsonView.expandAll()
# comboBox
self.comboBox.addItems([f"Item {i}" for i in range(10)])
self.mapper.bind(self.radioButton, "input/radioButton")
self.mapper.bind(self.checkBox, "input/checkBox", True)
self.mapper.bind(self.comboBox, "input/comboBox")
self.mapper.bind(self.spinBox, "age")
self.mapper.bind(self.doubleSpinBox, "money")
self.mapper.bind(self.lineEdit, "name")
# date and time
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.mapper.bind(self.timeEdit, "input/time", now)
self.mapper.bind(self.dateEdit, "input/date", now)
self.mapper.bind(self.dateTimeEdit, "input/dateTime", now)
# edit
self.mapper.bind(self.plainTextEdit, "desc/desc")
self.mapper.bind(self.textEdit, "desc/text")
# Correlation
self.mapper.bind(self.spinBox_2, "correlation/slider")
self.mapper.bind(self.horizontalSlider, "correlation/slider")
self.mapper.bind(self.progressBar, "correlation/progress")
self.mapper.bind(self.verticalSlider, "correlation/progress")
self.treeJsonView.expandAll()
def onModelChanged(self):
data = self.mapper.getJson(indent=2)
self.editJsonView.blockSignals(True)
self.editJsonView.setPlainText(data)
self.editJsonView.blockSignals(False)
def onJsonChanged(self):
text = self.editJsonView.toPlainText().strip()
try:
data = json.loads(text)
self.mapper.setData(data)
except Exception:
pass
if __name__ == "__main__":
import cgitb
import sys
cgitb.enable(format="text")
app = QApplication(sys.argv)
w = TestWindow()
w.show()
sys.exit(app.exec_())