-
Notifications
You must be signed in to change notification settings - Fork 41
/
lineeditdelegate.cpp
256 lines (208 loc) · 7.44 KB
/
lineeditdelegate.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "lineeditdelegate.h"
#include <QCompleter>
#include <QTreeView>
#include "domitem.h"
#include "dommodel.h"
#include "editortab.h"
#include "editortabswidget.h"
extern EditorTabsWidget* tabWidget;
QLineEdit* lineEdit;
void setTextCompleter(QLineEdit* editor);
LineEditDelegate::LineEditDelegate(QObject* parent) { Q_UNUSED(parent); }
QWidget* LineEditDelegate::createEditor(QWidget* parent,
const QStyleOptionViewItem& /*option*/,
const QModelIndex& /*index*/) const {
EditorTab* tab = tabWidget->getCurentTab();
DomModel* model = tab->getModel();
QModelIndex index = tab->currentIndex();
DomItem* item = model->itemForIndex(index);
if (index.column() == 1) {
return 0;
}
//父节点的数组和字典值,不允许编辑
if (index.column() == 2 &&
(item->getType() == "array" || item->getType() == "dict"))
return 0;
if (index.column() == 2 && item->getType() == "bool") return 0;
if (index.data().toString() == "plist") return 0;
QLineEdit* editor = new QLineEdit(parent);
return editor;
}
void LineEditDelegate::setEditorData(QWidget* editor,
const QModelIndex& index) const {
lineEdit = static_cast<QLineEdit*>(editor);
connect(lineEdit, &QLineEdit::editingFinished, this,
&LineEditDelegate::on_setText);
connect(lineEdit, &QLineEdit::textChanged, this,
&LineEditDelegate::on_textChanged);
QString value = index.data().toString();
EditorTab* tab = tabWidget->getCurentTab();
DomModel* model = tab->getModel();
DomItem* item = model->itemForIndex(index);
if (item->getType() == "string") setTextCompleter(lineEdit);
if (item->getType() == "data" && index.column() == 2) {
QRegExp regx("[A-Fa-f0-9- ]{2,1024}");
QValidator* validator = new QRegExpValidator(regx, lineEdit);
lineEdit->setValidator(validator);
lineEdit->setPlaceholderText(tr("Hexadecimal"));
}
if (item->getType() == "date" && index.column() == 2) {
if (value == "")
value = QDate::currentDate().toString("yyyy-MM-ddT") +
QTime::currentTime().toString("hh:mm:ssZ");
}
lineEdit->setText(value);
tab->treeView->resizeColumnToContents(0);
}
void LineEditDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const {
Q_UNUSED(editor);
// get old val form the model
QString oldVal = model->data(index).toString();
// get new val from editor
QString newVal = lineEdit->text();
// get index of type item
QModelIndex typeIndex = model->index(index.row(), 1, index.parent());
// get type val
QString type = model->data(typeIndex).toString();
// check new value
QString val = (checkInput(type, newVal, index.column())) ? newVal : oldVal;
// set data
// model->setData(index, val, Qt::EditRole);
if (newVal != oldVal) emit dataChanged(index, val);
}
void LineEditDelegate::updateEditorGeometry(QWidget* editor,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
bool LineEditDelegate::checkInput(const QString& type, const QString& val,
int col) {
bool ok;
if (col != 2)
ok = true;
else {
if (type == "integer") {
val.toLongLong(&ok);
} else {
if (type == "real") {
val.toFloat(&ok);
} else {
// we shouldn`t edit values of a dict or array
ok = ((type == "array" || type == "dict") && col == 2) ? false : true;
}
}
}
// array
EditorTab* tab = tabWidget->getCurentTab();
DomModel* model = tab->getModel();
const QModelIndex index = tab->currentIndex();
DomItem* item = model->itemForIndex(index.parent());
if (item->getType() == "array" && col == 0) {
QString str = val.mid(4, val.length() - 4);
if (val.mid(0, 5) != "Item " || !str.toInt() || str.toInt() == 0) return 0;
}
if (type == "date") {
QDateTime date = QDateTime::fromString(val);
if (!date.isValid()) {
}
}
if (type == "data" && col == 2) {
if (val.trimmed().count() % 2 != 0) return 0;
}
if (val.trimmed() == "plist") return 0;
if (val.trimmed().mid(0, 4) == "Item" && item->getType() != "array") return 0;
return ok;
}
//行高(以此为准)
QSize LineEditDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const {
QSize size = QStyledItemDelegate::sizeHint(option, index);
#ifdef Q_OS_WIN32
size.setHeight(size.height() + 4);
#endif
#ifdef Q_OS_LINUX
size.setHeight(size.height() + 4);
#endif
#ifdef Q_OS_MAC
size.setHeight(size.height() + 6);
#endif
return size;
}
void setTextCompleter(QLineEdit* editor) {
QStringList textList;
textList.append("Any");
textList.append("i386");
textList.append("x86_64");
textList.append("layout-id");
textList.append("Auto");
textList.append("i386-user32");
textList.append("Cacheless");
textList.append("Mkext");
textList.append("Prelinked");
textList.append("None");
textList.append("RTC");
textList.append("NVRAM");
textList.append("Disabled");
textList.append("Full");
textList.append("Short");
textList.append("Default");
textList.append("Builtin");
textList.append("External");
textList.append("Apple");
textList.append("Old");
textList.append("Modern");
textList.append("Signed");
textList.append("Secure");
textList.append("Optional");
textList.append("Basic");
textList.append("Create");
textList.append("TryOverwrite");
textList.append("Overwrite");
textList.append("Custom");
textList.append("Upgradable");
textList.append("Soldered");
textList.append("Enabled");
textList.append("V1");
textList.append("V2");
textList.append("AMI");
textList.append("ASUS");
textList.append("Max");
textList.append("BuiltinGraphics");
textList.append("SystemGraphics");
textList.append("SystemText");
textList.append("SystemGeneric");
textList.append("System");
textList.append("OEM");
textList.append("Reserved");
textList.append("LoaderCode");
textList.append("LoaderData");
textList.append("BootServiceCode");
textList.append("BootServiceData");
textList.append("RuntimeCode");
textList.append("RuntimeData");
textList.append("Available");
textList.append("Persistent");
textList.append("UnusableMemory");
textList.append("ACPIReclaimMemory");
textList.append("ACPIMemoryNVS");
textList.append("MemoryMappedIO");
textList.append("MemoryMappedIOPortSpace");
textList.append("PalCode");
QCompleter* editCompleter = new QCompleter(textList);
editCompleter->setCaseSensitivity(Qt::CaseSensitive);
editCompleter->setCompletionMode(QCompleter::PopupCompletion);
editor->setCompleter(editCompleter);
}
void LineEditDelegate::on_setText() {
EditorTab* tab = tabWidget->getCurentTab();
DomModel* model = tab->getModel();
QModelIndex index = tab->currentIndex();
DomItem* item = model->itemForIndex(index);
if (item->getType() == "data" && index.column() == 2) {
QString str = lineEdit->text();
lineEdit->setText(str.toUpper());
}
}
void LineEditDelegate::on_textChanged() {}