-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathnextmodel.cpp
More file actions
239 lines (197 loc) · 7.09 KB
/
nextmodel.cpp
File metadata and controls
239 lines (197 loc) · 7.09 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
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
#include "nextmodel.h"
#include <QFont>
#include <QPalette>
#include <QApplication>
#include <QRegularExpression>
#include "property.h"
#include <editor/propertyedit.h>
QString fromCamelCase(const TString &s) {
static QRegularExpression regExp1 {"(.)([A-Z][a-z]+)"};
static QRegularExpression regExp2 {"([a-z0-9])([A-Z])"};
QString result = s.data();
result.replace(regExp1, "\\1 \\2");
result.replace(regExp2, "\\1 \\2");
result[0] = result[0].toUpper();
return result;
}
NextModel::NextModel(QObject *parent):
BaseObjectModel(parent) {
}
NextModel::~NextModel() {
delete m_rootItem;
}
void NextModel::addItem(Object *propertyObject) {
const MetaObject *metaObject = propertyObject->metaObject();
Property *propertyItem = static_cast<Property *>(m_rootItem);
int count = metaObject->propertyCount();
if(count) {
TString name = propertyObject->typeName();
if(name.isEmpty()) {
name = metaObject->name();
}
propertyItem = new Property(name, static_cast<Property *>(m_rootItem), true);
propertyItem->setPropertyObject(propertyObject);
connect(propertyItem, &Property::propertyChanged, this, &NextModel::propertyChanged);
for(int i = 0; i < count; i++) {
MetaProperty property = metaObject->property(i);
if(!TString(property.name()).toLower().contains("enable")) {
uint32_t type = property.read(propertyObject).type();
if(type < MetaType::QUATERNION || type >= MetaType::OBJECT) {
Property *p = new Property(property.name(), (propertyItem) ? propertyItem : static_cast<Property *>(m_rootItem), false);
p->setPropertyObject(propertyObject);
const char *annotation = property.table()->annotation;
if(annotation) {
p->setEditorHints(annotation);
}
connect(p, &Property::propertyChanged, this, &NextModel::propertyChanged);
}
}
}
}
if(propertyItem) {
updateDynamicProperties(propertyItem, propertyObject);
}
emit layoutAboutToBeChanged();
emit layoutChanged();
}
void NextModel::updateDynamicProperties(Property *parent, Object *propertyObject) {
// Get dynamic property names
auto dynamicProperties = propertyObject->dynamicPropertyNames();
StringList dynamicPropertiesFiltered;
// Remove invalid properites and those we don't want to add
for(auto &it : dynamicProperties) {
// Skip user defined hidden properties starting with _
if(it.front() != '_') {
dynamicPropertiesFiltered.push_back(it);
}
}
if(dynamicPropertiesFiltered.empty()) {
return;
}
Property *it = parent;
// Add properties left in the list
for(const TString &dynProp : dynamicPropertiesFiltered) {
QStringList list = QString(dynProp.data()).split('/');
Property *s = it;
for(int i = 0; i < list.size(); i++) {
Property *p = nullptr;
if(it && i < list.size() - 1) {
QString path = list.mid(0, i + 1).join('/');
Property *child = it->findChild<Property *>(path);
if(child) {
it = child;
} else {
p = new Property(path.toStdString(), it, parent == m_rootItem);
p->setPropertyObject(propertyObject);
it = p;
}
} else if(!list[i].isEmpty()) {
p = new Property(dynProp, it, false);
p->setPropertyObject(propertyObject);
p->setEditorHints(propertyObject->dynamicPropertyInfo(dynProp.data()));
connect(p, &Property::propertyChanged, this, &NextModel::propertyChanged);
p->setProperty("__Dynamic", true);
}
}
it = s;
}
}
int NextModel::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return 2;
}
QVariant NextModel::data(const QModelIndex &index, int role) const {
if(!index.isValid()) {
return QVariant();
}
Property *item = static_cast<Property *>(index.internalPointer());
switch(role) {
case Qt::ToolTipRole:
case Qt::DisplayRole:
case Qt::EditRole: {
if(index.column() == 0) {
return fromCamelCase(item->name().replace('_', ' '));
}
} break;
case Qt::BackgroundRole: {
if(item->isRoot()) {
return QApplication::palette("QTreeView").brush(QPalette::Normal, QPalette::Button).color();
}
if(index.column() == 0) {
return QColor(0, 0, 255);
}
} break;
case Qt::FontRole: {
if(item->isRoot()) {
QFont font = QApplication::font("QTreeView");
font.setBold(true);
font.setPointSize(font.pointSizeF() + 2);
return font;
}
} break;
case Qt::SizeHintRole: {
return QSize(1, 26);
}
case Qt::CheckStateRole: {
if(index.column() == 0 && item->isCheckable()) {
return item->isChecked() ? Qt::Checked : Qt::Unchecked;
}
} break;
default: break;
}
return QVariant();
}
// edit methods
bool NextModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if(!index.isValid()) {
return false;
}
Property *item = static_cast<Property *>(index.internalPointer());
if(role == Qt::EditRole) {
emit dataChanged(index, index);
return true;
} else if(role == Qt::CheckStateRole) {
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
item->setChecked(state == Qt::Checked);
emit dataChanged(index, index);
return true;
}
return false;
}
Qt::ItemFlags NextModel::flags(const QModelIndex &index) const {
if(!index.isValid()) {
return Qt::ItemIsEnabled;
}
Property *item = static_cast<Property *>(index.internalPointer());
// only allow change of value attribute
Qt::ItemFlags result;
if(item->isRoot()) {
result |= Qt::ItemIsEnabled;
} else if(item->isReadOnly()) {
result |= Qt::ItemIsDragEnabled | Qt::ItemIsSelectable;
} else {
result |= Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if(index.column() == 1) {
result |= Qt::ItemIsEditable;
}
}
if(index.column() == 0 && item->isCheckable()) {
result |= Qt::ItemIsUserCheckable;
}
return result;
}
QVariant NextModel::headerData(int section, Qt::Orientation orientation, int role) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case 0: return tr("Property");
case 1: return tr("Value");
}
}
return QVariant();
}
void NextModel::clear() {
delete m_rootItem;
m_rootItem = new Property("Root", nullptr, true);
beginResetModel();
endResetModel();
}