-
Notifications
You must be signed in to change notification settings - Fork 6
/
QQmlGadgetListModel.h
408 lines (389 loc) · 14.3 KB
/
QQmlGadgetListModel.h
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#ifndef QQMLGADGETLISTMODEL_H
#define QQMLGADGETLISTMODEL_H
#include <QAbstractListModel>
#include <QByteArray>
#include <QChar>
#include <QDebug>
#include <QHash>
#include <QList>
#include <QMetaMethod>
#include <QMetaObject>
#include <QMetaProperty>
#include <QObject>
#include <QString>
#include <QStringBuilder>
#include <QVariant>
#include <QVector>
#include "QQmlQListVariantConverter.h"
// custom foreach for QList, which uses no copy and check pointer non-null
/*
#define FOREACH_PTR_IN_QLIST(_type_, _var_, _list_) \
for (typename QList<_type_ *>::const_iterator it = _list_.begin (); it != _list_.end (); it++) \
for (_type_ * _var_ = (_type_ *) (* it); _var_ != Q_NULLPTR; _var_ = Q_NULLPTR)
*/
class QQmlGadgetListModelBase : public QAbstractListModel { // abstract Qt base class
Q_OBJECT
Q_PROPERTY (int count READ count NOTIFY countChanged)
public:
explicit QQmlGadgetListModelBase (QObject * parent = Q_NULLPTR) : QAbstractListModel (parent) { }
virtual bool setData(const QModelIndex& index, const QVariant& value, int role) {
return QAbstractListModel::setData(index, value, role);
}
virtual QVariant data(const QModelIndex& index, int role) const = 0;
public slots: // virtual methods API for QML
virtual int size (void) const = 0;
virtual int count (void) const = 0;
virtual bool isEmpty (void) const = 0;
virtual bool contains (QVariant item) const = 0;
virtual int indexOf (QVariant item) const = 0;
virtual int roleForName (const QByteArray & name) const = 0;
virtual void clear (void) = 0;
virtual void append (QVariant item) = 0;
virtual void prepend (QVariant item) = 0;
virtual void insert (int idx, QVariant item) = 0;
virtual void move (int idx, int pos) = 0;
virtual void remove (QVariant item) = 0;
virtual void remove (int idx) = 0;
virtual void replace(int idx, QVariant item) = 0;
virtual QVariant get (int idx) const = 0;
virtual QVariant get (const QString & uid) const = 0;
virtual QVariant getFirst (void) const = 0;
virtual QVariant getLast (void) const = 0;
virtual QVariantList toVarArray (void) const = 0;
virtual bool setData(int idx, const QVariant& value, const QByteArray& roleName) = 0;
virtual QVariant data(int idx, const QByteArray& roleName) const = 0;
//protected slots: // internal callback
// virtual void onItemPropertyChanged (void) = 0;
signals: // notifier
void countChanged (void);
};
template<class ItemType> class QQmlGadgetListModel : public QQmlGadgetListModelBase {
public:
explicit QQmlGadgetListModel (QObject * parent = Q_NULLPTR,
const QByteArray & displayRole = QByteArray (),
const QByteArray & uidRole = QByteArray ())
: QQmlGadgetListModelBase (parent)
, m_count (0)
, m_uidRoleName (uidRole)
, m_dispRoleName (displayRole)
, m_metaObj (ItemType::staticMetaObject)
{
static QSet<QByteArray> roleNamesBlacklist;
if (roleNamesBlacklist.isEmpty ()) {
roleNamesBlacklist << QByteArrayLiteral ("id")
<< QByteArrayLiteral ("index")
<< QByteArrayLiteral ("class")
<< QByteArrayLiteral ("model")
<< QByteArrayLiteral ("modelData");
}
static const char * HANDLER = "onItemPropertyChanged()";
m_handler = metaObject ()->method (metaObject ()->indexOfMethod (HANDLER));
if (!displayRole.isEmpty ()) {
m_roles.insert (Qt::DisplayRole, QByteArrayLiteral ("display"));
}
m_roles.insert (baseRole (), QByteArrayLiteral ("qtObject"));
const int len = m_metaObj.propertyCount ();
for (int propertyIdx = 0, role = (baseRole () +1); propertyIdx < len; propertyIdx++, role++) {
QMetaProperty metaProp = m_metaObj.property (propertyIdx);
const QByteArray propName = QByteArray (metaProp.name ());
if (!roleNamesBlacklist.contains (propName)) {
m_roles.insert (role, propName);
if (metaProp.hasNotifySignal ()) {
m_signalIdxToRole.insert (metaProp.notifySignalIndex (), role);
}
}
else {
static const QByteArray CLASS_NAME = (QByteArrayLiteral ("QQmlGadgetListModel<") % m_metaObj.className () % '>');
qWarning () << "Can't have" << propName << "as a role name in" << qPrintable (CLASS_NAME);
}
}
}
bool setData (const QModelIndex & index, const QVariant & value, int role) {
bool ret = false;
int row = index.row();
if(row >= 0 && row < m_items.size()) {
ItemType& item = m_items[row];
const QByteArray rolename = (role != Qt::DisplayRole ? m_roles.value (role, emptyBA ()) : m_dispRoleName);
if (role != baseRole () && !rolename.isEmpty ()) {
int propertyIndex = m_metaObj.indexOfProperty(rolename);
ret = m_metaObj.property(propertyIndex).writeOnGadget(&item, value);
if (ret) {
QVector<int> roles;
roles.resize(1);
roles.first() = role;
emit dataChanged(index, index, roles);
}
}
}
return ret;
}
QVariant data (const QModelIndex & index, int role) const {
QVariant ret;
int row = index.row();
if(row >= 0 && row < m_items.size()) {
const ItemType& item = at (row);
const QByteArray rolename = (role != Qt::DisplayRole ? m_roles.value (role, emptyBA ()) : m_dispRoleName);
if (!rolename.isEmpty ()) {
int propertyIndex = m_metaObj.indexOfProperty(rolename);
ret.setValue (role != baseRole () ? m_metaObj.property(propertyIndex).readOnGadget(&item) : QVariant::fromValue (item));
}
}
return ret;
}
QHash<int, QByteArray> roleNames (void) const {
return m_roles;
}
typedef typename QList<ItemType>::iterator iterator;
iterator begin (void) {
return m_items.begin ();
}
iterator end (void) {
return m_items.end ();
}
typedef typename QList<ItemType>::const_iterator const_iterator;
const_iterator constBegin (void) const {
return m_items.constBegin ();
}
const_iterator constEnd (void) const {
return m_items.constEnd ();
}
public: // C++ API
ItemType at (int idx) const {
ItemType ret;
if (idx >= 0 && idx < m_items.size ()) {
ret = m_items.value (idx);
}
return ret;
}
ItemType getByUid (const QString & uid) const {
ItemType ret;
if(!m_indexByUid.contains(uid)) {
return *(m_indexByUid.value (uid, Q_NULLPTR));
}
return ret;
}
Q_INVOKABLE int roleForName (const QByteArray & name) const {
return m_roles.key (name, -1);
}
int count (void) const {
return m_count;
}
int size (void) const {
return m_count;
}
bool isEmpty (void) const {
return m_items.isEmpty ();
}
bool contains (ItemType item) const {
return m_items.contains (item);
}
int indexOf (ItemType item) const {
return m_items.indexOf (item);
}
void clear (void) {
if (!m_items.isEmpty ()) {
beginRemoveRows (noParent (), 0, m_items.count () -1);
for(int i = 0; i < m_items.size(); i++) {
dereferenceItem(&m_items[i]);
}
m_items.clear ();
endRemoveRows ();
updateCounter ();
}
}
void append (ItemType item) {
const int pos = m_items.count ();
beginInsertRows (noParent (), pos, pos);
m_items.append (item);
referenceItem (&m_items.last());
endInsertRows ();
updateCounter ();
}
void prepend (ItemType item) {
beginInsertRows (noParent (), 0, 0);
m_items.prepend (item);
referenceItem (&m_items.first());
endInsertRows ();
updateCounter ();
}
void insert (int idx, ItemType item) {
beginInsertRows (noParent (), idx, idx);
m_items.insert (idx, item);
referenceItem (&m_items[idx]);
endInsertRows ();
updateCounter ();
}
void append (const QList<ItemType> & itemList) {
insert(m_items.count(), itemList);
}
void prepend (const QList<ItemType> & itemList) {
insert(0, itemList);
}
void insert (int idx, const QList<ItemType> & itemList) {
if (!itemList.isEmpty ()) {
beginInsertRows (noParent (), idx, idx + itemList.count () -1);
m_items.reserve (m_items.count () + itemList.count ());
for(int i = 0; i < itemList.count(); i++) {
m_items.insert(idx + i, itemList.at(i));
referenceItem(&m_items[i]);
}
endInsertRows ();
updateCounter ();
}
}
void move (int idx, int pos) {
if (idx != pos) {
const int lowest = qMin (idx, pos);
const int highest = qMax (idx, pos);
beginMoveRows (noParent (), highest, highest, noParent (), lowest);
m_items.move (highest, lowest);
endMoveRows ();
}
}
void remove (ItemType item) {
const int idx = m_items.indexOf (item);
remove (idx);
}
void remove (int idx) {
if (idx >= 0 && idx < m_items.size ()) {
beginRemoveRows (noParent (), idx, idx);
ItemType& item = m_items[idx]; //.takeAt (idx);
dereferenceItem (&item);
m_items.takeAt(idx);
endRemoveRows ();
updateCounter ();
}
}
void replace (int idx, ItemType item) {
if(idx >= 0 && idx < m_items.size()) {
if(item != m_items.at(idx)) {
m_items.replace(idx, item);
QModelIndex modelIndex = index(idx);
dataChanged(modelIndex, modelIndex, roleNames().keys().toVector());
}
}
}
ItemType first (void) const {
return m_items.first ();
}
ItemType last (void) const {
return m_items.last ();
}
const QList<ItemType> & toList (void) const {
return m_items;
}
public: // QML slots implementation
void append (QVariant item) {
append (item.value<ItemType>());
}
void prepend (QVariant item) {
prepend (item.value<ItemType>());
}
void insert (int idx, QVariant item) {
insert (idx, item.value<ItemType>());
}
void remove (QVariant item) {
remove (item.value<ItemType>());
}
void replace (int idx, QVariant item) {
replace (idx, item.value<ItemType>());
}
bool contains (QVariant item) const {
return contains (item.value<ItemType>());
}
int indexOf (QVariant item) const {
return indexOf (item.value<ItemType>());
}
int indexOf (const QString & uid) const {
return indexOf (get (uid));
}
QVariant get (int idx) const {
return QVariant::fromValue(at(idx));
}
QVariant get (const QString & uid) const {
return QVariant::fromValue(getByUid(uid));
}
QVariant getFirst (void) const {
return QVariant::fromValue(first());
}
QVariant getLast (void) const {
return QVariant::fromValue(last());
}
QVariantList toVarArray (void) const {
return qListToVariant<ItemType> (m_items);
}
bool setData(int idx, const QVariant& value, const QByteArray& roleName) {
return setData(index(idx), value, roleForName(roleName));
}
QVariant data(int idx, const QByteArray& rolename) const {
return data(index(idx), roleForName(rolename));
}
protected: // internal stuff
static const QString & emptyStr (void) {
static const QString ret = QStringLiteral ("");
return ret;
}
static const QByteArray & emptyBA (void) {
static const QByteArray ret = QByteArrayLiteral ("");
return ret;
}
static const QModelIndex & noParent (void) {
static const QModelIndex ret = QModelIndex ();
return ret;
}
static const int & baseRole (void) {
static const int ret = Qt::UserRole;
return ret;
}
int rowCount (const QModelIndex & parent = QModelIndex ()) const {
Q_UNUSED (parent);
return m_items.count ();
}
void referenceItem (ItemType * item) {
if (item != Q_NULLPTR) {
if (!m_uidRoleName.isEmpty ()) {
const QString key = m_indexByUid.key (item, emptyStr ());
if (!key.isEmpty ()) {
m_indexByUid.remove (key);
}
int propertyIndex = m_metaObj.indexOfProperty(m_uidRoleName);
const QString value = m_metaObj.property(propertyIndex).readOnGadget(item).toString ();
if (!value.isEmpty ()) {
m_indexByUid.insert (value, item);
}
}
}
}
void dereferenceItem (ItemType* item) {
if (item != Q_NULLPTR) {
if (!m_uidRoleName.isEmpty ()) {
const QString key = m_indexByUid.key (item, emptyStr ());
if (!key.isEmpty ()) {
m_indexByUid.remove (key);
}
}
}
}
inline void updateCounter (void) {
if (m_count != m_items.count ()) {
m_count = m_items.count ();
emit countChanged ();
}
}
private: // data members
int m_count;
QByteArray m_uidRoleName;
QByteArray m_dispRoleName;
QMetaObject m_metaObj;
QMetaMethod m_handler;
QHash<int, QByteArray> m_roles;
QHash<int, int> m_signalIdxToRole;
QList<ItemType> m_items;
QHash<QString, ItemType*> m_indexByUid;
};
#define QML_GADGETMODEL_PROPERTY(type, name) \
protected: Q_PROPERTY (QQmlGadgetListModelBase * name READ get_##name CONSTANT) \
private: QQmlGadgetListModel<type> * m_##name; \
public: QQmlGadgetListModel<type> * get_##name (void) const { return m_##name; } \
private:
#endif // QQMLOBJECTLISTMODEL_H