universallistmodel.h 5.5 KB

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