1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSORTFILTERPROXYMODEL_H
41#define QSORTFILTERPROXYMODEL_H
42
43#include <QtCore/qabstractproxymodel.h>
44#include <QtCore/qregexp.h>
45
46#if QT_CONFIG(regularexpression)
47# include <QtCore/qregularexpression.h>
48#endif
49
50QT_REQUIRE_CONFIG(sortfilterproxymodel);
51
52QT_BEGIN_NAMESPACE
53
54
55class QSortFilterProxyModelPrivate;
56class QSortFilterProxyModelLessThan;
57class QSortFilterProxyModelGreaterThan;
58
59class Q_CORE_EXPORT QSortFilterProxyModel : public QAbstractProxyModel
60{
61 friend class QSortFilterProxyModelLessThan;
62 friend class QSortFilterProxyModelGreaterThan;
63
64 Q_OBJECT
65 Q_PROPERTY(QRegExp filterRegExp READ filterRegExp WRITE setFilterRegExp)
66#if QT_CONFIG(regularexpression)
67 Q_PROPERTY(QRegularExpression filterRegularExpression READ filterRegularExpression WRITE setFilterRegularExpression)
68#endif
69 Q_PROPERTY(int filterKeyColumn READ filterKeyColumn WRITE setFilterKeyColumn)
70 Q_PROPERTY(bool dynamicSortFilter READ dynamicSortFilter WRITE setDynamicSortFilter)
71 Q_PROPERTY(Qt::CaseSensitivity filterCaseSensitivity READ filterCaseSensitivity WRITE setFilterCaseSensitivity NOTIFY filterCaseSensitivityChanged)
72 Q_PROPERTY(Qt::CaseSensitivity sortCaseSensitivity READ sortCaseSensitivity WRITE setSortCaseSensitivity NOTIFY sortCaseSensitivityChanged)
73 Q_PROPERTY(bool isSortLocaleAware READ isSortLocaleAware WRITE setSortLocaleAware NOTIFY sortLocaleAwareChanged)
74 Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole NOTIFY sortRoleChanged)
75 Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole NOTIFY filterRoleChanged)
76 Q_PROPERTY(bool recursiveFilteringEnabled READ isRecursiveFilteringEnabled WRITE setRecursiveFilteringEnabled NOTIFY recursiveFilteringEnabledChanged)
77
78public:
79 explicit QSortFilterProxyModel(QObject *parent = nullptr);
80 ~QSortFilterProxyModel();
81
82 void setSourceModel(QAbstractItemModel *sourceModel) override;
83
84 QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
85 QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
86
87 QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const override;
88 QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const override;
89
90 QRegExp filterRegExp() const;
91
92#if QT_CONFIG(regularexpression)
93 QRegularExpression filterRegularExpression() const;
94#endif
95
96 int filterKeyColumn() const;
97 void setFilterKeyColumn(int column);
98
99 Qt::CaseSensitivity filterCaseSensitivity() const;
100 void setFilterCaseSensitivity(Qt::CaseSensitivity cs);
101
102 Qt::CaseSensitivity sortCaseSensitivity() const;
103 void setSortCaseSensitivity(Qt::CaseSensitivity cs);
104
105 bool isSortLocaleAware() const;
106 void setSortLocaleAware(bool on);
107
108 int sortColumn() const;
109 Qt::SortOrder sortOrder() const;
110
111 bool dynamicSortFilter() const;
112 void setDynamicSortFilter(bool enable);
113
114 int sortRole() const;
115 void setSortRole(int role);
116
117 int filterRole() const;
118 void setFilterRole(int role);
119
120 bool isRecursiveFilteringEnabled() const;
121 void setRecursiveFilteringEnabled(bool recursive);
122
123public Q_SLOTS:
124 void setFilterRegExp(const QString &pattern);
125 void setFilterRegExp(const QRegExp &regExp);
126#if QT_CONFIG(regularexpression)
127 void setFilterRegularExpression(const QString &pattern);
128 void setFilterRegularExpression(const QRegularExpression &regularExpression);
129#endif
130 void setFilterWildcard(const QString &pattern);
131 void setFilterFixedString(const QString &pattern);
132#if QT_DEPRECATED_SINCE(5, 11)
133 QT_DEPRECATED_X("Use QSortFilterProxyModel::invalidate") void clear();
134#endif
135 void invalidate();
136
137protected:
138 virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
139 virtual bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const;
140 virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const;
141
142#if QT_DEPRECATED_SINCE(5, 11)
143 QT_DEPRECATED_X("Use QSortFilterProxyModel::invalidateFilter") void filterChanged();
144#endif
145 void invalidateFilter();
146
147public:
148 using QObject::parent;
149
150 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
151 QModelIndex parent(const QModelIndex &child) const override;
152 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
153
154 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
155 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
156 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
157
158 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
159 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
160
161 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
162 bool setHeaderData(int section, Qt::Orientation orientation,
163 const QVariant &value, int role = Qt::EditRole) override;
164
165 QMimeData *mimeData(const QModelIndexList &indexes) const override;
166 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
167 int row, int column, const QModelIndex &parent) override;
168
169 bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
170 bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
171 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
172 bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
173
174 void fetchMore(const QModelIndex &parent) override;
175 bool canFetchMore(const QModelIndex &parent) const override;
176 Qt::ItemFlags flags(const QModelIndex &index) const override;
177
178 QModelIndex buddy(const QModelIndex &index) const override;
179 QModelIndexList match(const QModelIndex &start, int role,
180 const QVariant &value, int hits = 1,
181 Qt::MatchFlags flags =
182 Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override;
183 QSize span(const QModelIndex &index) const override;
184 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
185
186 QStringList mimeTypes() const override;
187 Qt::DropActions supportedDropActions() const override;
188
189Q_SIGNALS:
190 void dynamicSortFilterChanged(bool dynamicSortFilter);
191 void filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity);
192 void sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity);
193 void sortLocaleAwareChanged(bool sortLocaleAware);
194 void sortRoleChanged(int sortRole);
195 void filterRoleChanged(int filterRole);
196 void recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled);
197
198private:
199 Q_DECLARE_PRIVATE(QSortFilterProxyModel)
200 Q_DISABLE_COPY(QSortFilterProxyModel)
201
202 Q_PRIVATE_SLOT(d_func(), void _q_sourceDataChanged(const QModelIndex &source_top_left, const QModelIndex &source_bottom_right, const QVector<int> &roles))
203 Q_PRIVATE_SLOT(d_func(), void _q_sourceHeaderDataChanged(Qt::Orientation orientation, int start, int end))
204 Q_PRIVATE_SLOT(d_func(), void _q_sourceAboutToBeReset())
205 Q_PRIVATE_SLOT(d_func(), void _q_sourceReset())
206 Q_PRIVATE_SLOT(d_func(), void _q_sourceLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint))
207 Q_PRIVATE_SLOT(d_func(), void _q_sourceLayoutChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint))
208 Q_PRIVATE_SLOT(d_func(), void _q_sourceRowsAboutToBeInserted(const QModelIndex &source_parent, int start, int end))
209 Q_PRIVATE_SLOT(d_func(), void _q_sourceRowsInserted(const QModelIndex &source_parent, int start, int end))
210 Q_PRIVATE_SLOT(d_func(), void _q_sourceRowsAboutToBeRemoved(const QModelIndex &source_parent, int start, int end))
211 Q_PRIVATE_SLOT(d_func(), void _q_sourceRowsRemoved(const QModelIndex &source_parent, int start, int end))
212 Q_PRIVATE_SLOT(d_func(), void _q_sourceRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))
213 Q_PRIVATE_SLOT(d_func(), void _q_sourceRowsMoved(QModelIndex,int,int,QModelIndex,int))
214 Q_PRIVATE_SLOT(d_func(), void _q_sourceColumnsAboutToBeInserted(const QModelIndex &source_parent, int start, int end))
215 Q_PRIVATE_SLOT(d_func(), void _q_sourceColumnsInserted(const QModelIndex &source_parent, int start, int end))
216 Q_PRIVATE_SLOT(d_func(), void _q_sourceColumnsAboutToBeRemoved(const QModelIndex &source_parent, int start, int end))
217 Q_PRIVATE_SLOT(d_func(), void _q_sourceColumnsRemoved(const QModelIndex &source_parent, int start, int end))
218 Q_PRIVATE_SLOT(d_func(), void _q_sourceColumnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))
219 Q_PRIVATE_SLOT(d_func(), void _q_sourceColumnsMoved(QModelIndex,int,int,QModelIndex,int))
220 Q_PRIVATE_SLOT(d_func(), void _q_clearMapping())
221};
222
223QT_END_NAMESPACE
224
225#endif // QSORTFILTERPROXYMODEL_H
226

source code of qtbase/src/corelib/itemmodels/qsortfilterproxymodel.h