universallistmodel.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef UNIVERSALLISTMODEL_H
  2. #define UNIVERSALLISTMODEL_H
  3. #include <QAbstractListModel>
  4. #include <QObject>
  5. #include <QList>
  6. #include <QHash>
  7. #include <QPointer>
  8. #include <QMetaProperty>
  9. #include <QMetaObject>
  10. #include <QDebug>
  11. template <typename T>
  12. class UniversalListModel : public QAbstractListModel
  13. {
  14. public:
  15. UniversalListModel(QObject* parent = 0) : QAbstractListModel(parent) {}
  16. ~UniversalListModel() {
  17. clear();
  18. }
  19. int rowCount(const QModelIndex &parent) const {
  20. Q_UNUSED(parent)
  21. return m_container.count();
  22. }
  23. QHash<int, QByteArray> roleNames() const {
  24. if(s_roleNames.isEmpty()) {
  25. int propertyCount = T::staticMetaObject.propertyCount();
  26. for(int i = 0; i < propertyCount; i++) {
  27. s_roleNames.insert(Qt::UserRole + i, T::staticMetaObject.property(i).name());
  28. }
  29. }
  30. return s_roleNames;
  31. }
  32. QVariant data(const QModelIndex &index, int role) const
  33. {
  34. int row = index.row();
  35. if(row < 0 || row >= m_container.count() || m_container.at(row).isNull()) {
  36. return QVariant();
  37. }
  38. T* dataPtr = m_container.at(row).data();
  39. return dataPtr->property(s_roleNames.value(role));
  40. }
  41. bool add(T* value) {
  42. Q_ASSERT_X(value != nullptr, fullTemplateName(), "Trying to add member of NULL");
  43. if(m_container.indexOf(value) >= 0) {
  44. #ifdef DEBUG
  45. qDebug() << fullTemplateName() << "Member already exists";
  46. #endif
  47. return false;
  48. }
  49. beginInsertRows(QModelIndex(), m_container.count(), m_container.count());
  50. m_container.append(QPointer<T>(value));
  51. endInsertRows();
  52. return true;
  53. }
  54. void remove(T* value) {
  55. Q_ASSERT_X(value != nullptr, fullTemplateName(), ": Trying to remove member of NULL");
  56. int valueIndex = m_container.indexOf(value);
  57. if(valueIndex >= 0) {
  58. beginRemoveRows(QModelIndex(), valueIndex, valueIndex);
  59. m_container.removeAt(valueIndex);
  60. endRemoveRows();
  61. }
  62. }
  63. void reset(const QList<QPointer<T> >& container) {
  64. beginResetModel();
  65. clear();
  66. m_container = container;
  67. endResetModel();
  68. }
  69. protected:
  70. void clear() {
  71. foreach (const QPointer<T>& value, m_container) {
  72. delete value.data();
  73. }
  74. m_container.clear();
  75. }
  76. QList<QPointer<T> > m_container;
  77. static QHash<int, QByteArray> s_roleNames;
  78. private:
  79. static QByteArray fullTemplateName() { //Debug helper
  80. return QString("UniversalListModel<%1>").arg(T::staticMetaObject.className()).toLatin1();
  81. }
  82. };
  83. template<typename T>
  84. QHash<int, QByteArray> UniversalListModel<T>::s_roleNames;
  85. #endif // UNIVERSALLISTMODEL_H