-
Notifications
You must be signed in to change notification settings - Fork 141
/
LVGLPropertyModel.cpp
235 lines (199 loc) · 7.89 KB
/
LVGLPropertyModel.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
#include "LVGLPropertyModel.h"
#include <QComboBox>
#include <QDebug>
#include <QLineEdit>
#include <QSpinBox>
#include "LVGLCore.h"
#include "widgets/LVGLWidget.h"
LVGLPropertyModel::LVGLPropertyModel(QObject *parent)
: QAbstractItemModel(parent), m_obj(nullptr) {}
QVariant LVGLPropertyModel::headerData(int section, Qt::Orientation orientation,
int role) const {
if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole))
return QVariant();
if (section == 0)
return "Property";
else if (section == 1)
return "Value";
return QVariant();
}
QModelIndex LVGLPropertyModel::index(int row, int column,
const QModelIndex &parent) const {
if (!hasIndex(row, column, parent) || (m_obj == nullptr))
return QModelIndex();
if (parent.isValid()) {
const LVGLProperty *p =
static_cast<const LVGLProperty *>(parent.internalPointer());
const LVGLProperty *c = p->child(row);
return createIndex(row, column, const_cast<LVGLProperty *>(c));
}
auto props = m_obj->widgetClass()->properties();
if (row < props.size())
return createIndex(row, column, const_cast<LVGLProperty *>(props.at(row)));
return QModelIndex();
}
QModelIndex LVGLPropertyModel::parent(const QModelIndex &index) const {
if (!index.isValid() || (m_obj == nullptr)) return QModelIndex();
const LVGLProperty *item =
static_cast<const LVGLProperty *>(index.internalPointer());
const LVGLProperty *p = item->parent();
if (p == nullptr) return QModelIndex();
return createIndex(p->row(), 0, const_cast<LVGLProperty *>(p));
}
int LVGLPropertyModel::rowCount(const QModelIndex &parent) const {
if (m_obj == nullptr) return 0;
if (parent.isValid()) {
const LVGLProperty *p =
static_cast<const LVGLProperty *>(parent.internalPointer());
return p->count();
}
if (m_obj->widgetClass()) return m_obj->widgetClass()->properties().size();
return 0;
}
int LVGLPropertyModel::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent)
return 2;
}
QVariant LVGLPropertyModel::data(const QModelIndex &index, int role) const {
if ((m_obj == nullptr) || !index.isValid()) return QVariant();
auto prop = reinterpret_cast<LVGLProperty *>(index.internalPointer());
if (role == Qt::DisplayRole) {
if (index.column() == 0) {
return prop->name();
} else if (index.column() == 1) {
QVariant val = prop->value(m_obj);
if (val.type() != QVariant::List) return val;
QStringList ret;
for (const QVariant &v : val.toList()) ret << v.toString();
return "[" + ret.join(",") + "]";
}
} else if (role == (Qt::UserRole + 1)) {
LVGLObjectCast cast;
cast.ptr = m_obj;
return cast.i;
}
return QVariant();
}
LVGLObject *LVGLPropertyModel::object() const { return m_obj; }
Qt::ItemFlags LVGLPropertyModel::flags(const QModelIndex &index) const {
Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
auto prop = reinterpret_cast<LVGLProperty *>(index.internalPointer());
if (index.isValid() && (index.column() == 1) && prop && prop->hasEditor())
return Qt::ItemIsEditable | defaultFlags;
else
return defaultFlags;
}
QModelIndex LVGLPropertyModel::propIndex(const LVGLProperty *prop,
const LVGLWidget *widget, int column) {
const LVGLProperty *p = prop->parent();
if (p == nullptr) {
int row = widget->properties().indexOf(const_cast<LVGLProperty *>(prop));
return createIndex(row, column, const_cast<LVGLProperty *>(prop));
}
int row = p->indexOf(prop);
return createIndex(row, column, const_cast<LVGLProperty *>(prop));
}
void LVGLPropertyModel::setObject(LVGLObject *obj) {
const int count1 = obj ? obj->widgetClass()->properties().size() : 0;
const int count2 = m_obj ? m_obj->widgetClass()->properties().size() : 0;
if (count1 > count2) {
beginInsertRows(QModelIndex(), count2, count1 - 1);
m_obj = obj;
endInsertRows();
} else if (count1 < count2) {
beginRemoveRows(QModelIndex(), count1, count2 - 1);
m_obj = obj;
endRemoveRows();
} else {
m_obj = obj;
}
}
LVGLPropertyDelegate::LVGLPropertyDelegate(QObject *parent)
: QStyledItemDelegate(parent) {}
QWidget *LVGLPropertyDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
Q_UNUSED(option)
if (!index.isValid() || (index.column() != 1)) return nullptr;
auto prop = reinterpret_cast<LVGLProperty *>(index.internalPointer());
if (prop) return prop->editor(parent);
return nullptr;
}
void LVGLPropertyDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const {
(void)editor;
if (!index.isValid() || (index.column() != 1)) return;
LVGLObjectCast cast;
auto prop = reinterpret_cast<LVGLProperty *>(index.internalPointer());
cast.i = index.data(Qt::UserRole + 1).toLongLong();
if ((cast.ptr == nullptr) || (prop == nullptr)) return;
prop->updateEditor(cast.ptr);
}
void LVGLPropertyDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const {
(void)editor;
if (!index.isValid() || (index.column() != 1)) return;
LVGLObjectCast cast;
auto prop = reinterpret_cast<LVGLProperty *>(index.internalPointer());
cast.i = index.data(Qt::UserRole + 1).toLongLong();
if ((cast.ptr == nullptr) || (prop == nullptr)) return;
prop->updateWidget(cast.ptr);
emit model->dataChanged(index, index);
};
#if 0
void LVGLPropertyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (!index.isValid() || (index.column() != 1))
return;
LVGLObjectCast cast;
cast.i = index.data(Qt::UserRole + 1).toLongLong();
if (cast.ptr == nullptr)
return;
auto prop = reinterpret_cast<LVGLProperty*>(index.internalPointer());
if (prop->name == "Source") {
QComboBox *widget = qobject_cast<QComboBox*>(editor);
widget->setCurrentText(prop->get(cast.ptr).toString());
} else if (prop->type == QVariant::String) {
QLineEdit *widget = qobject_cast<QLineEdit*>(editor);
widget->setText(prop->get(cast.ptr).toString());
} else if (prop->type == QVariant::Int) {
QSpinBox *widget = qobject_cast<QSpinBox*>(editor);
widget->setValue(prop->get(cast.ptr).toInt());
} else if (prop->type == QVariant::Bool) {
QComboBox *widget = qobject_cast<QComboBox*>(editor);
widget->setCurrentIndex(prop->get(cast.ptr).toBool() ? 0 : 1);
} else if (prop->type == QVariant::StringList) {
QComboBox *widget = qobject_cast<QComboBox*>(editor);
widget->setCurrentIndex(prop->defaultVal.toStringList().indexOf(prop->get(cast.ptr).toString()));
}
}
void LVGLPropertyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
Q_UNUSED(model)
if (!index.isValid() || (index.column() != 1))
return;
LVGLObjectCast cast;
cast.i = index.data(Qt::UserRole + 1).toLongLong();
if (cast.ptr == nullptr)
return;
auto prop = reinterpret_cast<LVGLProperty*>(index.internalPointer());
if (prop->name == "Source") {
QComboBox *widget = qobject_cast<QComboBox*>(editor);
prop->set(cast.ptr, widget->currentText());
} else if (prop->type == QVariant::String) {
QLineEdit *widget = qobject_cast<QLineEdit*>(editor);
prop->set(cast.ptr, widget->text());
} else if (prop->type == QVariant::Int) {
QSpinBox *widget = qobject_cast<QSpinBox*>(editor);
prop->set(cast.ptr, widget->value());
} else if (prop->type == QVariant::Bool) {
QComboBox *widget = qobject_cast<QComboBox*>(editor);
prop->set(cast.ptr, widget->currentIndex() == 0);
} else if (prop->type == QVariant::StringList) {
QComboBox *widget = qobject_cast<QComboBox*>(editor);
prop->set(cast.ptr, prop->defaultVal.toStringList().at(widget->currentIndex()));
}
emit model->dataChanged(index, index);
}
#endif