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  /* getters */
42  int totalCount() const;
43  bool invertMatch() const;
44  QHash<int, QByteArray> roleNames() const override;
45 
46  /* setters */
47  void setModel(QAbstractItemModel *model);
48  void setInvertMatch(bool invertMatch);
49 
50  Q_INVOKABLE int mapFromSource(int row);
51  Q_INVOKABLE int mapToSource(int row);
52 
53 Q_SIGNALS:
54  void totalCountChanged();
55  void countChanged();
56  void invertMatchChanged(bool);
57  void modelChanged();
58 
59 private:
60  bool m_invertMatch;
61 };
62 
63 #endif // UNITYSORTFILTERPROXYMODELQML_H