Unity 8
unitysortfilterproxymodelqml.h
1 /*
2  * Copyright (C) 2012 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef UNITYSORTFILTERPROXYMODELQML_H
18 #define UNITYSORTFILTERPROXYMODELQML_H
19 
20 #include <QSortFilterProxyModel>
21 
22 class UnitySortFilterProxyModelQML : public QSortFilterProxyModel
23 {
24  Q_OBJECT
25 
26  Q_PROPERTY(QAbstractItemModel* model READ sourceModel WRITE setModel NOTIFY modelChanged)
27  Q_PROPERTY(int totalCount READ totalCount NOTIFY totalCountChanged)
28  Q_PROPERTY(int count READ count NOTIFY countChanged)
29  Q_PROPERTY(bool invertMatch READ invertMatch WRITE setInvertMatch NOTIFY invertMatchChanged)
30 
31 public:
32  explicit UnitySortFilterProxyModelQML(QObject *parent = 0);
33 
34  Q_INVOKABLE QVariantMap get(int row); // Use with caution, it can be slow to query all the roles
35  Q_INVOKABLE QVariant data(int row, int role);
36  Q_INVOKABLE int count();
37  Q_INVOKABLE int findFirst(int role, const QVariant& value) const;
38  Q_INVOKABLE int mapRowToSource(int row);
39  bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
40 
41  // Make the QSortFilterProxyModel variants also accessible
42  using QSortFilterProxyModel::data;
43  using QSortFilterProxyModel::mapFromSource;
44  using QSortFilterProxyModel::mapToSource;
45 
46  /* getters */
47  int totalCount() const;
48  bool invertMatch() const;
49  QHash<int, QByteArray> roleNames() const override;
50 
51  /* setters */
52  void setModel(QAbstractItemModel *model);
53  void setInvertMatch(bool invertMatch);
54 
55  Q_INVOKABLE int mapFromSource(int row);
56  Q_INVOKABLE int mapToSource(int row);
57 
58 Q_SIGNALS:
59  void totalCountChanged();
60  void countChanged();
61  void invertMatchChanged(bool);
62  void modelChanged();
63 
64 private:
65  bool m_invertMatch;
66 };
67 
68 #endif // UNITYSORTFILTERPROXYMODELQML_H