AbstractGroupProxyModel.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef ABSTRACTGROUPPROXYMODEL_H
  9. #define ABSTRACTGROUPPROXYMODEL_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <QAbstractProxyModel>
  12. #endif
  13. #if !defined(Q_MOC_RUN)
  14. #include <QPixmap>
  15. #endif
  16. class AbstractGroupProxyModel
  17. : public QAbstractProxyModel
  18. {
  19. Q_OBJECT
  20. public:
  21. AbstractGroupProxyModel(QObject* parent = 0);
  22. ~AbstractGroupProxyModel();
  23. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
  24. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
  25. int rowCount(const QModelIndex& parent = QModelIndex()) const override;
  26. int columnCount(const QModelIndex& parent = QModelIndex()) const override;
  27. QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
  28. QModelIndex parent(const QModelIndex& index) const override;
  29. bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
  30. Qt::ItemFlags flags(const QModelIndex& index) const override;
  31. QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;
  32. QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
  33. void setSourceModel(QAbstractItemModel* sourceModel) override;
  34. signals:
  35. void GroupUpdated();
  36. protected:
  37. virtual QStringList GroupForSourceIndex(const QModelIndex& sourceIndex) const = 0;
  38. virtual bool IsGroupIndex([[maybe_unused]] const QModelIndex& sourceIndex) const { return false; }
  39. void slotSourceAboutToBeReset();
  40. void slotSourceReset();
  41. void RebuildTree();
  42. int subGroupCount() const;
  43. private:
  44. struct GroupItem
  45. {
  46. QPersistentModelIndex groupSourceIndex;
  47. QString groupTitle;
  48. QVector<GroupItem*> subGroups;
  49. QVector<QPersistentModelIndex> sourceIndexes;
  50. ~GroupItem()
  51. {
  52. qDeleteAll(subGroups);
  53. }
  54. };
  55. GroupItem* FindIndex(const QModelIndex& index, GroupItem* group = 0) const;
  56. GroupItem* FindGroup(GroupItem* group, GroupItem* parent = 0) const;
  57. void SourceRowsInserted(const QModelIndex& parent, int from, int to);
  58. void SourceRowsAboutToBeRemoved(const QModelIndex& parent, int from, int to);
  59. void SourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
  60. GroupItem* CreateGroupIfNotExists(QStringList group);
  61. void RemoveEmptyGroup(GroupItem* group);
  62. GroupItem m_rootItem;
  63. };
  64. #endif // ABSTRACTGROUPPROXYMODEL_H