Advertisement
Guest User

Untitled

a guest
Aug 6th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #ifndef QOBJECTLISTMODEL_H
  2. #define QOBJECTLISTMODEL_H
  3.  
  4. #include <QAbstractListModel>
  5. #include <QMetaProperty>
  6.  
  7. class QObjectListModelMagic : public QAbstractListModel
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. QObjectListModelMagic(QObject *parent) : QAbstractListModel(parent) { }
  13. Q_INVOKABLE virtual QObject *getItem(int index) = 0;
  14.  
  15. };
  16.  
  17. template<typename X>
  18. class QObjectListModel : public QObjectListModelMagic
  19. {
  20. QHash<int, QByteArray> _roles;
  21. QList<X*> _list;
  22.  
  23. public:
  24. explicit QObjectListModel(QObject *parent = 0, const QList<X*> &list = QList<X*>());
  25. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  26. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  27. QVariant data(const QModelIndex &index, int role) const;
  28. bool setData(const QModelIndex &index, const QVariant &value, int role);
  29.  
  30. void addItem(X *item);
  31. void removeItem(const X *item);
  32. void removeItem(int index);
  33. QObject* getItem(int index);
  34. };
  35.  
  36. template<typename T>
  37. QObjectListModel<T>::QObjectListModel(QObject *parent, const QList<T*> &list)
  38. : QObjectListModelMagic(parent),
  39. _list(list)
  40. {
  41. QMetaObject meta = T::staticMetaObject;
  42. for (int i = 0; i < meta.propertyCount(); i++)
  43. {
  44. QMetaProperty prop = meta.property(i);
  45. _roles[Qt::UserRole + i + 1] = QByteArray(prop.name());
  46. }
  47. setRoleNames(_roles);
  48. }
  49.  
  50. template<typename T>
  51. int QObjectListModel<T>::rowCount(const QModelIndex &parent) const
  52. {
  53. Q_UNUSED(parent);
  54. return _list.count();
  55. }
  56.  
  57. template<typename T>
  58. int QObjectListModel<T>::columnCount(const QModelIndex &parent) const
  59. {
  60. Q_UNUSED(parent);
  61. return _roles.count();
  62. }
  63.  
  64. template<typename T>
  65. QVariant QObjectListModel<T>::data(const QModelIndex &index, int role) const
  66. {
  67. if (index.row() < 0 || index.row() >= _list.count())
  68. return QVariant();
  69.  
  70. const QObject *obj = _list[index.row()];
  71. return obj->property(_roles[role].data());
  72. }
  73.  
  74. template<typename T>
  75. bool QObjectListModel<T>::setData(const QModelIndex &index, const QVariant &value, int role)
  76. {
  77. if (index.row() < 0 || index.row() >= _list.count())
  78. return false;
  79.  
  80. QObject *obj = _list[index.row()];
  81. return obj->setProperty(_roles[role].data(), value);
  82. }
  83.  
  84. template<typename T>
  85. void QObjectListModel<T>::addItem(T *item)
  86. {
  87. int z = _list.count();
  88. beginInsertRows(QModelIndex(), z, z);
  89. _list.append(item);
  90. endInsertRows();
  91. }
  92.  
  93. template<typename T>
  94. void QObjectListModel<T>::removeItem(const T *item)
  95. {
  96. int z = _list.indexOf(item);
  97. beginRemoveRows(QModelIndex(), z, z);
  98. _list.removeAt(z);
  99. endRemoveRows();
  100. }
  101.  
  102. template<typename T>
  103. void QObjectListModel<T>::removeItem(int index)
  104. {
  105. beginRemoveRows(QModelIndex(), index, index);
  106. _list.removeAt(index);
  107. endRemoveRows();
  108. }
  109.  
  110. template<typename T>
  111. QObject* QObjectListModel<T>::getItem(int index)
  112. {
  113. return _list[index];
  114. }
  115.  
  116. #endif // QOBJECTLISTMODEL_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement