universallistmodel.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #pragma once
  2. #include <universallistmodelbase.h>
  3. #include <QObject>
  4. #include <QList>
  5. #include <QHash>
  6. #include <QPointer>
  7. #include <QMetaProperty>
  8. #include <QMetaObject>
  9. #include <QDebug>
  10. /*!
  11. * \brief Universal list model is QObject-base list model abstraction.
  12. * It exposes all objects properties as data-roles.
  13. */
  14. template <typename T>
  15. class UniversalListModel : public UniversalListModelBase
  16. {
  17. public:
  18. UniversalListModel(QObject* parent = 0) : UniversalListModelBase(parent) {}
  19. ~UniversalListModel() {
  20. clear();
  21. }
  22. int rowCount(const QModelIndex &parent) const override {
  23. Q_UNUSED(parent)
  24. return count();
  25. }
  26. int count() const override {
  27. return m_container.count();
  28. }
  29. QHash<int, QByteArray> roleNames() const override {
  30. if(s_roleNames.isEmpty()) {
  31. int propertyCount = T::staticMetaObject.propertyCount();
  32. for(int i = 1; i < propertyCount; i++) {
  33. s_roleNames.insert(Qt::UserRole + i, T::staticMetaObject.property(i).name());
  34. }
  35. s_roleNames[propertyCount] = "modelData";
  36. }
  37. return s_roleNames;
  38. }
  39. QVariant data(const QModelIndex &index, int role) const override
  40. {
  41. int row = index.row();
  42. if(row < 0 || row >= m_container.count() || m_container.at(row).isNull()) {
  43. return QVariant();
  44. }
  45. T* dataPtr = m_container.at(row).data();
  46. if(s_roleNames.value(role) == "modelData") {
  47. return QVariant::fromValue(dataPtr);
  48. }
  49. return dataPtr->property(s_roleNames.value(role));
  50. }
  51. /*!
  52. * \brief append
  53. * \param value
  54. * \return
  55. */
  56. int append(T* value) {
  57. Q_ASSERT_X(value != nullptr, fullTemplateName(), "Trying to add member of NULL");
  58. if(m_container.indexOf(value) >= 0) {
  59. #ifdef DEBUG
  60. qDebug() << fullTemplateName() << "Member already exists";
  61. #endif
  62. return -1;
  63. }
  64. beginInsertRows(QModelIndex(), m_container.count(), m_container.count());
  65. m_container.append(QPointer<T>(value));
  66. emit countChanged();
  67. endInsertRows();
  68. return m_container.count() - 1;
  69. }
  70. /*!
  71. * \brief prepend
  72. * \param value
  73. * \return
  74. */
  75. int prepend(T* value) {
  76. Q_ASSERT_X(value != nullptr, fullTemplateName(), "Trying to add member of NULL");
  77. if(m_container.indexOf(value) >= 0) {
  78. #ifdef DEBUG
  79. qDebug() << fullTemplateName() << "Member already exists";
  80. #endif
  81. return -1;
  82. }
  83. beginInsertRows(QModelIndex(), 0, 0);
  84. m_container.prepend(QPointer<T>(value));
  85. emit countChanged();
  86. endInsertRows();
  87. return 0;
  88. }
  89. /*!
  90. * \brief remove
  91. * \param value
  92. */
  93. void remove(T* value) {
  94. Q_ASSERT_X(value != nullptr, fullTemplateName(), ": Trying to remove member of NULL");
  95. int valueIndex = m_container.indexOf(value);
  96. if(valueIndex >= 0) {
  97. beginRemoveRows(QModelIndex(), valueIndex, valueIndex);
  98. m_container.removeAt(valueIndex);
  99. emit countChanged();
  100. endRemoveRows();
  101. }
  102. }
  103. /*!
  104. * \brief Resets container with new container passed as parameter
  105. * \param container a data for model. Should contain QPointer's to objects.
  106. * Passing empty container makes model empty. This method should be used to cleanup model.
  107. */
  108. void reset(const QList<QPointer<T> >& container) {
  109. beginResetModel();
  110. clear();
  111. m_container = container;
  112. emit countChanged();
  113. endResetModel();
  114. }
  115. /*!
  116. * \brief Returns the item at index position i in the list. i must be a valid index position in the list (i.e., 0 <= i < rowCount()).
  117. * This function is very fast (constant time).
  118. * \param i index of looking object
  119. * \return Object at provided index
  120. */
  121. T* at(int i) const {
  122. return m_container.at(i);
  123. }
  124. /*!
  125. * \brief Looking for index of objec
  126. * \param value
  127. * \return
  128. */
  129. int indexOf(T* value) const {
  130. return m_container.indexOf(value);
  131. }
  132. /*!
  133. * \brief findByProperty method finds item in internal container property of that is provided value
  134. * \param propertyName Latin1 name of looking property
  135. * \param value property of corresponded type inside QVariant container
  136. * \return
  137. */
  138. QPointer<T> findByProperty(const char* propertyName, const QVariant& value) const {
  139. auto iter = std::find_if(m_container.begin(), m_container.end(), [=](const QPointer<T> &item) -> bool {
  140. return item->property(propertyName) == value;
  141. });
  142. if(iter != m_container.end()) {
  143. return *iter;
  144. }
  145. return QPointer<T>();
  146. }
  147. /*!
  148. * \brief container returns internal container
  149. * \return
  150. */
  151. QList<QPointer<T> > container() const {
  152. return m_container;
  153. }
  154. protected:
  155. void clear() {
  156. m_container.clear();
  157. emit countChanged();
  158. }
  159. QList<QPointer<T> > m_container;
  160. static QHash<int, QByteArray> s_roleNames;
  161. private:
  162. static QByteArray fullTemplateName() { //Debug helper
  163. return QString("UniversalListModel<%1>").arg(T::staticMetaObject.className()).toLatin1();
  164. }
  165. };
  166. template<typename T>
  167. QHash<int, QByteArray> UniversalListModel<T>::s_roleNames;