/ qqmlsortfilterproxymodel.h
qqmlsortfilterproxymodel.h
  1  #ifndef QQMLSORTFILTERPROXYMODEL_H
  2  #define QQMLSORTFILTERPROXYMODEL_H
  3  
  4  #include <QSortFilterProxyModel>
  5  #include <QQmlParserStatus>
  6  #include "filters/filtercontainer.h"
  7  #include "sorters/sortercontainer.h"
  8  #include "proxyroles/proxyrolecontainer.h"
  9  
 10  namespace qqsfpm {
 11  
 12  class QQmlSortFilterProxyModel : public QSortFilterProxyModel,
 13                                   public QQmlParserStatus,
 14                                   public FilterContainer,
 15                                   public SorterContainer,
 16                                   public ProxyRoleContainer
 17  {
 18      Q_OBJECT
 19      Q_INTERFACES(QQmlParserStatus)
 20      Q_INTERFACES(qqsfpm::FilterContainer)
 21      Q_INTERFACES(qqsfpm::SorterContainer)
 22      Q_INTERFACES(qqsfpm::ProxyRoleContainer)
 23  
 24      Q_PROPERTY(int count READ count NOTIFY countChanged)
 25      Q_PROPERTY(bool delayed READ delayed WRITE setDelayed NOTIFY delayedChanged)
 26  
 27      Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged)
 28      Q_PROPERTY(QString filterPattern READ filterPattern WRITE setFilterPattern NOTIFY filterPatternChanged)
 29      Q_PROPERTY(QVariant filterValue READ filterValue WRITE setFilterValue NOTIFY filterValueChanged)
 30  
 31      Q_PROPERTY(QString sortRoleName READ sortRoleName WRITE setSortRoleName NOTIFY sortRoleNameChanged)
 32      Q_PROPERTY(bool ascendingSortOrder READ ascendingSortOrder WRITE setAscendingSortOrder NOTIFY ascendingSortOrderChanged)
 33  
 34      Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filtersListProperty)
 35      Q_PROPERTY(QQmlListProperty<qqsfpm::Sorter> sorters READ sortersListProperty)
 36      Q_PROPERTY(QQmlListProperty<qqsfpm::ProxyRole> proxyRoles READ proxyRolesListProperty)
 37  
 38  public:
 39      QQmlSortFilterProxyModel(QObject* parent = 0);
 40  
 41      int count() const;
 42  
 43      bool delayed() const;
 44      void setDelayed(bool delayed);
 45  
 46      const QString& filterRoleName() const;
 47      void setFilterRoleName(const QString& filterRoleName);
 48  
 49      QString filterPattern() const;
 50      void setFilterPattern(const QString& filterPattern);
 51  
 52      const QVariant& filterValue() const;
 53      void setFilterValue(const QVariant& filterValue);
 54  
 55      const QString& sortRoleName() const;
 56      void setSortRoleName(const QString& sortRoleName);
 57  
 58      bool ascendingSortOrder() const;
 59      void setAscendingSortOrder(bool ascendingSortOrder);
 60  
 61      void classBegin() override;
 62      void componentComplete() override;
 63  
 64      QVariant sourceData(const QModelIndex& sourceIndex, const QString& roleName) const;
 65      QVariant sourceData(const QModelIndex& sourceIndex, int role) const;
 66  
 67      QVariant data(const QModelIndex& index, int role) const override;
 68      QHash<int, QByteArray> roleNames() const override;
 69  
 70      Q_INVOKABLE int roleForName(const QString& roleName) const;
 71  
 72      Q_INVOKABLE QVariantMap get(int row) const;
 73      Q_INVOKABLE QVariant get(int row, const QString& roleName) const;
 74  
 75      Q_INVOKABLE QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;
 76      Q_INVOKABLE int mapToSource(int proxyRow) const;
 77      Q_INVOKABLE QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
 78      Q_INVOKABLE int mapFromSource(int sourceRow) const;
 79  
 80      void setSourceModel(QAbstractItemModel *sourceModel) override;
 81  
 82  Q_SIGNALS:
 83      void countChanged();
 84      void delayedChanged();
 85  
 86      void filterRoleNameChanged();
 87      void filterPatternChanged();
 88      void filterValueChanged();
 89  
 90      void sortRoleNameChanged();
 91      void ascendingSortOrderChanged();
 92  
 93  protected:
 94      bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
 95      bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override;
 96  
 97  protected Q_SLOTS:
 98      void resetInternalData();
 99  
100  private Q_SLOTS:
101      void queueInvalidateFilter();
102      void invalidateFilter();
103      void queueInvalidate();
104      void invalidate();
105      void updateRoleNames();
106      void updateFilterRole();
107      void updateSortRole();
108      void updateRoles();
109      void initRoles();
110      void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles);
111      void queueInvalidateProxyRoles();
112      void invalidateProxyRoles();
113  
114  private:
115      QVariantMap modelDataMap(const QModelIndex& modelIndex) const;
116  
117      void onFilterAppended(Filter* filter) override;
118      void onFilterRemoved(Filter* filter) override;
119      void onFiltersCleared() override;
120  
121      void onSorterAppended(Sorter* sorter) override;
122      void onSorterRemoved(Sorter* sorter) override;
123      void onSortersCleared() override;
124  
125      void onProxyRoleAppended(ProxyRole *proxyRole) override;
126      void onProxyRoleRemoved(ProxyRole *proxyRole) override;
127      void onProxyRolesCleared() override;
128  
129      bool m_delayed;
130      QString m_filterRoleName;
131      QVariant m_filterValue;
132      QString m_sortRoleName;
133      bool m_ascendingSortOrder = true;
134      bool m_completed = false;
135      QHash<int, QByteArray> m_roleNames;
136      QHash<int, QPair<ProxyRole*, QString>> m_proxyRoleMap;
137      QVector<int> m_proxyRoleNumbers;
138  
139      bool m_invalidateFilterQueued = false;
140      bool m_invalidateQueued = false;
141      bool m_invalidateProxyRolesQueued = false;
142  
143      QObject* m_qtbug_57971_signalsContext = nullptr;
144  };
145  
146  }
147  
148  #endif // QQMLSORTFILTERPROXYMODEL_H