Debian Codegraph @

Dekko/utils/QmlObjectListModel.h

raw symbols SCIP
1 #ifndef QQMLOBJECTLISTMODEL_H
2 #define QQMLOBJECTLISTMODEL_H
3
4 #include <QAbstractListModel>
5 #include <QByteArray>
6 #include <QChar>
7 #include <QDebug>
8 #include <QHash>
9 #include <QList>
10 #include <QMetaMethod>
11 #include <QMetaObject>
12 #include <QMetaProperty>
13 #include <QObject>
14 #include <QString>
15 #include <QStringBuilder>
16 #include <QVariant>
17 #include <QVector>
18
19 template<typename T> QList<T> qListFromVariant (const QVariantList & list) {
20 QList<T> ret;
21 ret.reserve (list.size ());
22 for (QVariantList::const_iterator it = list.constBegin (); it != list.constEnd (); it++) {
23 const QVariant & var = (QVariant) (* it);
24 ret.append (var.value<T> ());
25 }
26 return ret;
27 }
28
29 template<typename T> QVariantList qListToVariant (const QList<T> & list) {
30 QVariantList ret;
31 ret.reserve (list.size ());
32 for (typename QList<T>::const_iterator it = list.constBegin (); it != list.constEnd (); it++) {
33 const T & val = (T) (* it);
34 ret.append (QVariant::fromValue (val));
35 }
36 return ret;
37 }
38
39 // custom foreach for QList, which uses no copy and check pointer non-null
40 #define FOREACH_PTR_IN_QLIST(_type_, _var_, _list_) \
41 for (typename QList<_type_ *>::const_iterator it = _list_.constBegin (); it != _list_.constEnd (); it++) \
42 for (_type_ * _var_ = (_type_ *) (* it); _var_ != Q_NULLPTR; _var_ = Q_NULLPTR)
43
44 class QQmlObjectListModelBase : public QAbstractListModel { // abstract Qt base class
45 Q_OBJECT
46 Q_PROPERTY (int count READ count NOTIFY countChanged)
47
48 public:
49 explicit QQmlObjectListModelBase (QObject * parent = Q_NULLPTR) : QAbstractListModel (parent) { }
50
51 public slots: // virtual methods API for QML
52 virtual int size (void) const = 0;
53 virtual int count (void) const = 0;
54 virtual bool isEmpty (void) const = 0;
55 virtual bool contains (QObject * item) const = 0;
56 virtual int indexOf (QObject * item) const = 0;
57 virtual int roleForName (const QByteArray & name) const = 0;
58 virtual void clear (void) = 0;
59 virtual void append (QObject * item) = 0;
60 virtual void prepend (QObject * item) = 0;
61 virtual void insert (int idx, QObject * item) = 0;
62 virtual void move (int idx, int pos) = 0;
63 virtual void remove (QObject * item) = 0;
64 virtual void remove (int idx) = 0;
65 virtual QObject * get (int idx) const = 0;
66 virtual QObject * get (const QString & uid) const = 0;
67 virtual QObject * getFirst (void) const = 0;
68 virtual QObject * getLast (void) const = 0;
69 virtual QVariantList toVarArray (void) const = 0;
70 virtual void enqueue(QObject *item) = 0;
71 virtual void dequeue() = 0;
72
73 protected slots: // internal callback
74 virtual void onItemPropertyChanged (void) = 0;
75
76 signals: // notifier
77 void countChanged (void);
78 };
79
80 template<class ItemType> class QQmlObjectListModel : public QQmlObjectListModelBase {
81 public:
82 explicit QQmlObjectListModel (QObject * parent = Q_NULLPTR,
83 const QByteArray & displayRole = QByteArray (),
84 const QByteArray & uidRole = QByteArray ())
85 : QQmlObjectListModelBase (parent)
86 , m_count (0)
87 , m_uidRoleName (uidRole)
88 , m_dispRoleName (displayRole)
89 , m_metaObj (ItemType::staticMetaObject)
90 {
91 static QSet<QByteArray> roleNamesBlacklist;
92 if (roleNamesBlacklist.isEmpty ()) {
93 roleNamesBlacklist << QByteArrayLiteral ("id")
94 << QByteArrayLiteral ("index")
95 << QByteArrayLiteral ("class")
96 << QByteArrayLiteral ("model")
97 << QByteArrayLiteral ("modelData");
98 }
99 static const char * HANDLER = "onItemPropertyChanged()";
100 m_handler = metaObject ()->method (metaObject ()->indexOfMethod (HANDLER));
101 if (!displayRole.isEmpty ()) {
102 m_roles.insert (Qt::DisplayRole, QByteArrayLiteral ("display"));
103 }
104 m_roles.insert (baseRole (), QByteArrayLiteral ("qtObject"));
105 const int len = m_metaObj.propertyCount ();
106 for (int propertyIdx = 0, role = (baseRole () +1); propertyIdx < len; propertyIdx++, role++) {
107 QMetaProperty metaProp = m_metaObj.property (propertyIdx);
108 const QByteArray propName = QByteArray::fromRawData (metaProp.name (), sizeof(metaProp.name()));
109 if (!roleNamesBlacklist.contains (propName)) {
110 m_roles.insert (role, propName);
111 if (metaProp.hasNotifySignal ()) {
112 m_signalIdxToRole.insert (metaProp.notifySignalIndex (), role);
113 }
114 }
115 else {
116 static const QByteArray CLASS_NAME = (QByteArrayLiteral ("QQmlObjectListModel<") % m_metaObj.className () % '>');
117 qWarning () << "Can't have" << propName << "as a role name in" << qPrintable (CLASS_NAME);
118 }
119 }
120 }
121 bool setData (const QModelIndex & index, const QVariant & value, int role) {
122 bool ret = false;
123 ItemType * item = at (index.row ());
124 const QByteArray rolename = (role != Qt::DisplayRole ? m_roles.value (role, emptyBA ()) : m_dispRoleName);
125 if (item != Q_NULLPTR && role != baseRole () && !rolename.isEmpty ()) {
126 ret = item->setProperty (rolename, value);
127 }
128 return ret;
129 }
130 QVariant data (const QModelIndex & index, int role) const {
131 QVariant ret;
132 ItemType * item = at (index.row ());
133 const QByteArray rolename = (role != Qt::DisplayRole ? m_roles.value (role, emptyBA ()) : m_dispRoleName);
134 // qDebug() << "ROLENAME CALLED: " << rolename;
135 if (item != Q_NULLPTR && !rolename.isEmpty ()) {
136 ret.setValue (role != baseRole () ? item->property (rolename) : QVariant::fromValue (static_cast<QObject *> (item)));
137 }
138 return ret;
139 }
140 QHash<int, QByteArray> roleNames (void) const {
141 return m_roles;
142 }
143 typedef typename QList<ItemType *>::iterator iterator;
144 iterator begin (void) const {
145 return m_items.begin ();
146 }
147 iterator end (void) const {
148 return m_items.end ();
149 }
150 typedef typename QList<ItemType *>::const_iterator const_iterator;
151 const_iterator constBegin (void) const {
152 return m_items.constBegin ();
153 }
154 const_iterator constEnd (void) const {
155 return m_items.constEnd ();
156 }
157
158 public: // C++ API
159 ItemType * at (int idx) const {
160 ItemType * ret = Q_NULLPTR;
161 if (idx >= 0 && idx < m_items.size ()) {
162 ret = m_items.value (idx);
163 }
164 return ret;
165 }
166 ItemType * getByUid (const QString & uid) const {
167 return m_indexByUid.value (uid, Q_NULLPTR);
168 }
169 Q_INVOKABLE int roleForName (const QByteArray & name) const {
170 return m_roles.key (name, -1);
171 }
172 int count (void) const {
173 return m_count;
174 }
175 int size (void) const {
176 return m_count;
177 }
178 bool isEmpty (void) const {
179 return m_items.isEmpty ();
180 }
181 bool contains (ItemType * item) const {
182 return m_items.contains (item);
183 }
184 int indexOf (ItemType * item) const {
185 return m_items.indexOf (item);
186 }
187 void clear (void) {
188 if (!m_items.isEmpty ()) {
189 beginRemoveRows (noParent (), 0, m_items.count () -1);
190 FOREACH_PTR_IN_QLIST (ItemType, item, m_items) {
191 dereferenceItem (item);
192 }
193 qDeleteAll(m_items);
194 if (!m_items.isEmpty()) {
195 m_items.clear ();
196 }
197 endRemoveRows ();
198 updateCounter ();
199 }
200 }
201 void append (ItemType * item) {
202 if (item != Q_NULLPTR) {
203 const int pos = m_items.count ();
204 beginInsertRows (noParent (), pos, pos);
205 m_items.append (item);
206 referenceItem (item);
207 endInsertRows ();
208 updateCounter ();
209 }
210 }
211 void prepend (ItemType * item) {
212 if (item != Q_NULLPTR) {
213 beginInsertRows (noParent (), 0, 0);
214 m_items.prepend (item);
215 referenceItem (item);
216 endInsertRows ();
217 updateCounter ();
218 }
219 }
220 void insert (int idx, ItemType * item) {
221 if (item != Q_NULLPTR) {
222 beginInsertRows (noParent (), idx, idx);
223 m_items.insert (idx, item);
224 referenceItem (item);
225 endInsertRows ();
226 updateCounter ();
227 }
228 }
229 void append (const QList<ItemType *> & itemList) {
230 if (!itemList.isEmpty ()) {
231 const int pos = m_items.count ();
232 beginInsertRows (noParent (), pos, pos + itemList.count () -1);
233 m_items.reserve (m_items.count () + itemList.count ());
234 m_items.append (itemList);
235 FOREACH_PTR_IN_QLIST (ItemType, item, itemList) {
236 referenceItem (item);
237 }
238 endInsertRows ();
239 updateCounter ();
240 }
241 }
242 void prepend (const QList<ItemType *> & itemList) {
243 if (!itemList.isEmpty ()) {
244 beginInsertRows (noParent (), 0, itemList.count () -1);
245 m_items.reserve (m_items.count () + itemList.count ());
246 int offset = 0;
247 FOREACH_PTR_IN_QLIST (ItemType, item, itemList) {
248 m_items.insert (offset, item);
249 referenceItem (item);
250 offset++;
251 }
252 endInsertRows ();
253 updateCounter ();
254 }
255 }
256 void insert (int idx, const QList<ItemType *> & itemList) {
257 if (!itemList.isEmpty ()) {
258 beginInsertRows (noParent (), idx, idx + itemList.count () -1);
259 m_items.reserve (m_items.count () + itemList.count ());
260 int offset = 0;
261 FOREACH_PTR_IN_QLIST (ItemType, item, itemList) {
262 m_items.insert (idx + offset, item);
263 referenceItem (item);
264 offset++;
265 }
266 endInsertRows ();
267 updateCounter ();
268 }
269 }
270 void move (int idx, int pos) {
271 if (idx != pos) {
272 const int lowest = qMin (idx, pos);
273 const int highest = qMax (idx, pos);
274 beginMoveRows (noParent (), highest, highest, noParent (), lowest);
275 m_items.move (highest, lowest);
276 endMoveRows ();
277 }
278 }
279 void remove (ItemType * item) {
280 if (item != Q_NULLPTR) {
281 const int idx = m_items.indexOf (item);
282 remove (idx);
283 }
284 }
285 void remove (int idx) {
286 if (idx >= 0 && idx < m_items.size ()) {
287 beginRemoveRows (noParent (), idx, idx);
288 ItemType * item = m_items.takeAt (idx);
289 dereferenceItem (item);
290 endRemoveRows ();
291 updateCounter ();
292 }
293 }
294
295 void enqueue(ItemType *item) {
296 // we don't allow insertion into 0 if
297 // the models not empty
298 if (isEmpty()) {
299 insert(0, item);
300 return;
301 }
302 int idx = 0;
303 foreach(ItemType *other, toList()) {
304 if (idx == 0 && !isEmpty()) {
305 continue;
306 }
307 if (item < other) {
308 insert(indexOf(other), item);
309 return;
310 }
311 ++idx;
312 }
313 // failsafe
314 append(item); // :-(
315 }
316
317 ItemType * first (void) const {
318 return m_items.first ();
319 }
320 ItemType * last (void) const {
321 return m_items.last ();
322 }
323 const QList<ItemType *> & toList (void) const {
324 return m_items;
325 }
326
327 public: // QML slots implementation
328 void append (QObject * item) {
329 append (qobject_cast<ItemType *> (item));
330 }
331 void prepend (QObject * item) {
332 prepend (qobject_cast<ItemType *> (item));
333 }
334 void insert (int idx, QObject * item) {
335 insert (idx, qobject_cast<ItemType *> (item));
336 }
337 void remove (QObject * item) {
338 remove (qobject_cast<ItemType *> (item));
339 }
340 bool contains (QObject * item) const {
341 return contains (qobject_cast<ItemType *> (item));
342 }
343 int indexOf (QObject * item) const {
344 return indexOf (qobject_cast<ItemType *> (item));
345 }
346 int indexOf (const QString & uid) const {
347 return indexOf (get (uid));
348 }
349 QObject * get (int idx) const {
350 return static_cast<QObject *> (at (idx));
351 }
352 QObject * get (const QString & uid) const {
353 return static_cast<QObject *> (getByUid (uid));
354 }
355 QObject * getFirst (void) const {
356 return static_cast<QObject *> (first ());
357 }
358 QObject * getLast (void) const {
359 return static_cast<QObject *> (last ());
360 }
361 QVariantList toVarArray (void) const {
362 return qListToVariant<ItemType *> (m_items);
363 }
364 void enqueue(QObject *item) {
365 return enqueue(qobject_cast<ItemType *>(item));
366 }
367 void dequeue() {
368 remove(0);
369 }
370
371 protected: // internal stuff
372 static const QString & emptyStr (void) {
373 static const QString ret = QStringLiteral ("");
374 return ret;
375 }
376 static const QByteArray & emptyBA (void) {
377 static const QByteArray ret = QByteArrayLiteral ("");
378 return ret;
379 }
380 static const QModelIndex & noParent (void) {
381 static const QModelIndex ret = QModelIndex ();
382 return ret;
383 }
384 static const int & baseRole (void) {
385 static const int ret = Qt::UserRole;
386 return ret;
387 }
388 int rowCount (const QModelIndex & parent = QModelIndex ()) const {
389 Q_UNUSED (parent);
390 return m_items.count ();
391 }
392 void referenceItem (ItemType * item) {
393 if (item != Q_NULLPTR) {
394 if (item->parent () == Q_NULLPTR) {
395 item->setParent (this);
396 }
397 const QList<int> signalsIdxList = m_signalIdxToRole.keys ();
398 for (QList<int>::const_iterator it = signalsIdxList.constBegin (); it != signalsIdxList.constEnd (); it++) {
399 const int signalIdx = (int) (* it);
400 QMetaMethod notifier = item->metaObject ()->method (signalIdx);
401 connect (item, notifier, this, m_handler, Qt::UniqueConnection);
402 }
403 if (!m_uidRoleName.isEmpty ()) {
404 const QString key = m_indexByUid.key (item, emptyStr ());
405 if (!key.isEmpty ()) {
406 m_indexByUid.remove (key);
407 }
408 const QString value = item->property (m_uidRoleName).toString ();
409 if (!value.isEmpty ()) {
410 m_indexByUid.insert (value, item);
411 }
412 }
413 }
414 }
415 void dereferenceItem (ItemType * item) {
416 if (item != Q_NULLPTR) {
417 disconnect (this, Q_NULLPTR, item, Q_NULLPTR);
418 disconnect (item, Q_NULLPTR, this, Q_NULLPTR);
419 if (!m_uidRoleName.isEmpty ()) {
420 const QString key = m_indexByUid.key (item, emptyStr ());
421 if (!key.isEmpty ()) {
422 m_indexByUid.remove (key);
423 }
424 }
425 item->deleteLater();
426 // if (item->parent () == this) { // FIXME : maybe that's not the best way to test ownership ?
427 // item->deleteLater ();
428 // }
429 }
430 }
431 void onItemPropertyChanged (void) {
432 ItemType * item = qobject_cast<ItemType *> (sender ());
433 const int row = m_items.indexOf (item);
434 const int sig = senderSignalIndex ();
435 const int role = m_signalIdxToRole.value (sig, -1);
436 if (row >= 0 && role >= 0) {
437 QModelIndex index = QAbstractListModel::index (row, 0, noParent ());
438 QVector<int> rolesList;
439 rolesList.append (role);
440 if (m_roles.value (role) == m_dispRoleName) {
441 rolesList.append (Qt::DisplayRole);
442 }
443 emit dataChanged (index, index, rolesList);
444 }
445 if (!m_uidRoleName.isEmpty ()) {
446 const QByteArray roleName = m_roles.value (role, emptyBA ());
447 if (!roleName.isEmpty () && roleName == m_uidRoleName) {
448 const QString key = m_indexByUid.key (item, emptyStr ());
449 if (!key.isEmpty ()) {
450 m_indexByUid.remove (key);
451 }
452 const QString value = item->property (m_uidRoleName).toString ();
453 if (!value.isEmpty ()) {
454 m_indexByUid.insert (value, item);
455 }
456 }
457 }
458 }
459 inline void updateCounter (void) {
460 if (m_count != m_items.count ()) {
461 m_count = m_items.count ();
462 emit countChanged ();
463 }
464 }
465
466 private: // data members
467 int m_count;
468 QByteArray m_uidRoleName;
469 QByteArray m_dispRoleName;
470 QMetaObject m_metaObj;
471 QMetaMethod m_handler;
472 QHash<int, QByteArray> m_roles;
473 QHash<int, int> m_signalIdxToRole;
474 QList<ItemType *> m_items;
475 QHash<QString, ItemType *> m_indexByUid;
476 };
477
478 #define QML_OBJMODEL_PROPERTY(type, name) \
479 protected: Q_PROPERTY (QQmlObjectListModelBase * name READ get_##name NOTIFY name##Changed ) \
480 Q_SIGNALS: \
481 void name##Changed(); \
482 private: QQmlObjectListModel<type> * m_##name; \
483 public: QQmlObjectListModel<type> * get_##name (void) const { return m_##name; } \
484 private:
485
486 #endif // QQMLOBJECTLISTMODEL_H