-
Notifications
You must be signed in to change notification settings - Fork 5
/
qqmlobjectlistmodel.h
431 lines (418 loc) · 14.2 KB
/
qqmlobjectlistmodel.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#ifndef QQMLOBJECTLISTMODEL_H
#define QQMLOBJECTLISTMODEL_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>
class QQmlObjectListModelBase : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
explicit QQmlObjectListModelBase(QObject* parent= Q_NULLPTR) : QAbstractListModel(parent) {}
public slots: // API for QML
virtual int size(void) const= 0;
virtual int count(void) const= 0;
virtual bool isEmpty(void) const= 0;
virtual bool contains(QObject* item) const= 0;
virtual int indexOf(QObject* item) const= 0;
virtual int roleForName(const QByteArray& name) const= 0;
virtual void clear(void)= 0;
virtual void append(QObject* item)= 0;
virtual void prepend(QObject* item)= 0;
virtual void insert(int idx, QObject* item)= 0;
virtual void move(int idx, int pos)= 0;
virtual void remove(QObject* item)= 0;
virtual void remove(int idx)= 0;
virtual QObject* get(int idx) const= 0;
virtual QObject* get(const QString& uid) const= 0;
virtual QObject* getFirst(void) const= 0;
virtual QObject* getLast(void) const= 0;
virtual QVariantList toVarArray(void) const= 0;
protected slots: // internal callback
virtual void onItemPropertyChanged(void)= 0;
signals: // notifier
void countChanged(void);
};
template <class ItemType>
class QQmlObjectListModel : public QQmlObjectListModelBase
{
public:
explicit QQmlObjectListModel(QObject* parent= Q_NULLPTR, const QByteArray& displayRole= QByteArray(),
const QByteArray& uidRole= QByteArray())
: QQmlObjectListModelBase(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");
}
m_handler= metaObject()->method(metaObject()->indexOfMethod("onItemPropertyChanged()"));
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(metaProp.name());
if(!roleNamesBlacklist.contains(propName))
{
m_roles.insert(role, propName);
if(metaProp.hasNotifySignal())
{
m_signalIdxToRole.insert(metaProp.notifySignalIndex(), role);
}
}
else
{
qCritical() << "Can't have" << propName << "as a role name in"
<< qPrintable(QByteArrayLiteral("QQmlObjectListModel<") % m_metaObj.className() % '>');
}
}
}
typedef typename QList<ItemType*>::const_iterator const_iterator;
const_iterator begin(void) const { return m_items.begin(); }
const_iterator end(void) const { return m_items.end(); }
public: // C++ API
ItemType* at(int idx) const
{
ItemType* ret= Q_NULLPTR;
if(idx >= 0 && idx < m_items.size())
{
ret= m_items.value(idx);
}
return ret;
}
ItemType* getByUid(const QString& uid) const { return m_indexByUid.value(uid, Q_NULLPTR); }
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, count() - 1);
foreach(ItemType* item, m_items)
{
dereferenceItem(item);
}
m_items.clear();
updateCounter();
endRemoveRows();
}
}
void append(ItemType* item)
{
if(item != Q_NULLPTR)
{
int pos= m_items.count();
beginInsertRows(noParent(), pos, pos);
m_items.append(item);
referenceItem(item);
endInsertRows();
}
}
void prepend(ItemType* item)
{
if(item != Q_NULLPTR)
{
beginInsertRows(noParent(), 0, 0);
m_items.prepend(item);
referenceItem(item);
endInsertRows();
}
}
void insert(int idx, ItemType* item)
{
if(item != Q_NULLPTR)
{
beginInsertRows(noParent(), idx, idx);
m_items.insert(idx, item);
referenceItem(item);
endInsertRows();
}
}
void append(const QList<ItemType*>& itemList)
{
itemList.removeAll(Q_NULLPTR);
if(!itemList.isEmpty())
{
int pos= m_items.count();
beginInsertRows(noParent(), pos, pos + itemList.count() - 1);
m_items.append(itemList);
foreach(ItemType* item, itemList)
{
referenceItem(item);
}
endInsertRows();
}
}
void prepend(const QList<ItemType*>& itemList)
{
itemList.removeAll(Q_NULLPTR);
if(!itemList.isEmpty())
{
beginInsertRows(noParent(), 0, itemList.count() - 1);
int offset= 0;
foreach(ItemType* item, itemList)
{
m_items.insert(offset, item);
referenceItem(item);
}
endInsertRows();
}
}
void insert(int idx, const QList<ItemType*>& itemList)
{
itemList.removeAll(Q_NULLPTR);
if(!itemList.isEmpty())
{
beginInsertRows(noParent(), idx, idx + itemList.count() - 1);
int offset= 0;
foreach(ItemType* item, itemList)
{
m_items.insert(idx + offset, item);
referenceItem(item);
offset++;
}
endInsertRows();
}
}
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)
{
if(item != Q_NULLPTR)
{
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.takeAt(idx);
dereferenceItem(item);
endRemoveRows();
}
}
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(QObject* item) { append(qobject_cast<ItemType*>(item)); }
void prepend(QObject* item) { prepend(qobject_cast<ItemType*>(item)); }
void insert(int idx, QObject* item) { insert(idx, qobject_cast<ItemType*>(item)); }
void remove(QObject* item) { remove(qobject_cast<ItemType*>(item)); }
bool contains(QObject* item) const { return contains(qobject_cast<ItemType*>(item)); }
int indexOf(QObject* item) const { return indexOf(qobject_cast<ItemType*>(item)); }
QObject* get(int idx) const { return static_cast<QObject*>(at(idx)); }
QObject* get(const QString& uid) const { return static_cast<QObject*>(getByUid(uid)); }
QObject* getFirst(void) const { return static_cast<QObject*>(first()); }
QObject* getLast(void) const { return static_cast<QObject*>(last()); }
QVariantList toVarArray(void) const
{
QVariantList ret;
ret.reserve(m_items.size());
foreach(ItemType* item, m_items)
{
ret.append(QVariant::fromValue(static_cast<QObject*>(item)));
}
return ret;
}
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();
}
bool setData(const QModelIndex& index, const QVariant& value, int role)
{
bool ret= false;
ItemType* item= at(index.row());
QByteArray rolename= (role != Qt::DisplayRole ? m_roles.value(role, emptyBA()) : m_dispRoleName);
if(item != Q_NULLPTR && role != baseRole() && !rolename.isEmpty())
{
ret= item->setProperty(rolename, value);
}
return ret;
}
QVariant data(const QModelIndex& index, int role) const
{
QVariant ret;
ItemType* item= at(index.row());
QByteArray rolename= (role != Qt::DisplayRole ? m_roles.value(role, emptyBA()) : m_dispRoleName);
if(item != Q_NULLPTR && !rolename.isEmpty())
{
ret.setValue(role != baseRole() ? item->property(rolename) : QVariant::fromValue(item));
}
return ret;
}
QHash<int, QByteArray> roleNames(void) const { return m_roles; }
void referenceItem(ItemType* item)
{
if(item != Q_NULLPTR)
{
if(item->parent() == Q_NULLPTR)
{
item->setParent(this);
}
foreach(int signalIdx, m_signalIdxToRole.keys())
{
QMetaMethod notifier= item->metaObject()->method(signalIdx);
connect(item, notifier, this, m_handler, Qt::UniqueConnection);
}
if(!m_uidRoleName.isEmpty())
{
QString key= m_indexByUid.key(item, emptyStr());
if(!key.isEmpty())
{
m_indexByUid.remove(key);
}
QString value= item->property(m_uidRoleName).toString();
if(!value.isEmpty())
{
m_indexByUid.insert(value, item);
}
}
updateCounter();
}
}
void dereferenceItem(ItemType* item)
{
if(item != Q_NULLPTR)
{
disconnect(this, Q_NULLPTR, item, Q_NULLPTR);
disconnect(item, Q_NULLPTR, this, Q_NULLPTR);
if(item->parent() == this)
{ // FIXME : maybe that's not the best way to test ownership ?
item->deleteLater();
}
if(!m_uidRoleName.isEmpty())
{
QString key= m_indexByUid.key(item, emptyStr());
if(!key.isEmpty())
{
m_indexByUid.remove(key);
}
}
updateCounter();
}
}
void onItemPropertyChanged(void)
{
ItemType* item= qobject_cast<ItemType*>(sender());
const int row= m_items.indexOf(item);
const int sig= senderSignalIndex();
const int role= m_signalIdxToRole.value(sig, -1);
if(row >= 0 && role >= 0)
{
QModelIndex index= QAbstractListModel::index(row, 0, noParent());
QVector<int> rolesList;
rolesList.append(role);
if(m_roles.value(role) == m_dispRoleName)
{
rolesList.append(Qt::DisplayRole);
}
emit dataChanged(index, index, rolesList);
}
if(!m_uidRoleName.isEmpty())
{
QByteArray roleName= m_roles.value(role, emptyBA());
if(!roleName.isEmpty() && roleName == m_uidRoleName)
{
QString key= m_indexByUid.key(item, emptyStr());
if(!key.isEmpty())
{
m_indexByUid.remove(key);
}
QString value= item->property(m_uidRoleName).toString();
if(!value.isEmpty())
{
m_indexByUid.insert(value, item);
}
}
}
}
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_OBJMODEL_PROPERTY(type, name) \
protected: \
Q_PROPERTY(QQmlObjectListModelBase* name READ get_##name CONSTANT) \
private: \
QQmlObjectListModel<type>* m_##name; \
\
public: \
QQmlObjectListModel<type>* get_##name(void) const { return m_##name; } \
\
private:
#endif // QQMLOBJECTLISTMODEL_H