1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QSORTFILTERPROXYMODEL_H
6#define QSORTFILTERPROXYMODEL_H
7
8#include <QtCore/qabstractproxymodel.h>
9
10#include <QtCore/qregularexpression.h>
11
12QT_REQUIRE_CONFIG(sortfilterproxymodel);
13
14QT_BEGIN_NAMESPACE
15
16
17class QSortFilterProxyModelPrivate;
18class QSortFilterProxyModelLessThan;
19class QSortFilterProxyModelGreaterThan;
20
21class Q_CORE_EXPORT QSortFilterProxyModel : public QAbstractProxyModel
22{
23 friend class QSortFilterProxyModelLessThan;
24 friend class QSortFilterProxyModelGreaterThan;
25
26 Q_OBJECT
27 Q_PROPERTY(QRegularExpression filterRegularExpression READ filterRegularExpression
28 WRITE setFilterRegularExpression BINDABLE bindableFilterRegularExpression)
29 Q_PROPERTY(int filterKeyColumn READ filterKeyColumn WRITE setFilterKeyColumn
30 BINDABLE bindableFilterKeyColumn)
31 Q_PROPERTY(bool dynamicSortFilter READ dynamicSortFilter WRITE setDynamicSortFilter
32 BINDABLE bindableDynamicSortFilter)
33 Q_PROPERTY(Qt::CaseSensitivity filterCaseSensitivity READ filterCaseSensitivity
34 WRITE setFilterCaseSensitivity NOTIFY filterCaseSensitivityChanged
35 BINDABLE bindableFilterCaseSensitivity)
36 Q_PROPERTY(Qt::CaseSensitivity sortCaseSensitivity READ sortCaseSensitivity
37 WRITE setSortCaseSensitivity NOTIFY sortCaseSensitivityChanged
38 BINDABLE bindableSortCaseSensitivity)
39 Q_PROPERTY(bool isSortLocaleAware READ isSortLocaleAware WRITE setSortLocaleAware
40 NOTIFY sortLocaleAwareChanged BINDABLE bindableIsSortLocaleAware)
41 Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole NOTIFY sortRoleChanged
42 BINDABLE bindableSortRole)
43 Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole NOTIFY filterRoleChanged
44 BINDABLE bindableFilterRole)
45 Q_PROPERTY(bool recursiveFilteringEnabled READ isRecursiveFilteringEnabled
46 WRITE setRecursiveFilteringEnabled NOTIFY recursiveFilteringEnabledChanged
47 BINDABLE bindableRecursiveFilteringEnabled)
48 Q_PROPERTY(bool autoAcceptChildRows READ autoAcceptChildRows WRITE setAutoAcceptChildRows
49 NOTIFY autoAcceptChildRowsChanged BINDABLE bindableAutoAcceptChildRows)
50
51public:
52 explicit QSortFilterProxyModel(QObject *parent = nullptr);
53 ~QSortFilterProxyModel();
54
55 void setSourceModel(QAbstractItemModel *sourceModel) override;
56
57 QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
58 QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
59
60 QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const override;
61 QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const override;
62
63 QRegularExpression filterRegularExpression() const;
64 QBindable<QRegularExpression> bindableFilterRegularExpression();
65
66 int filterKeyColumn() const;
67 void setFilterKeyColumn(int column);
68 QBindable<int> bindableFilterKeyColumn();
69
70 Qt::CaseSensitivity filterCaseSensitivity() const;
71 void setFilterCaseSensitivity(Qt::CaseSensitivity cs);
72 QBindable<Qt::CaseSensitivity> bindableFilterCaseSensitivity();
73
74 Qt::CaseSensitivity sortCaseSensitivity() const;
75 void setSortCaseSensitivity(Qt::CaseSensitivity cs);
76 QBindable<Qt::CaseSensitivity> bindableSortCaseSensitivity();
77
78 bool isSortLocaleAware() const;
79 void setSortLocaleAware(bool on);
80 QBindable<bool> bindableIsSortLocaleAware();
81
82 int sortColumn() const;
83 Qt::SortOrder sortOrder() const;
84
85 bool dynamicSortFilter() const;
86 void setDynamicSortFilter(bool enable);
87 QBindable<bool> bindableDynamicSortFilter();
88
89 int sortRole() const;
90 void setSortRole(int role);
91 QBindable<int> bindableSortRole();
92
93 int filterRole() const;
94 void setFilterRole(int role);
95 QBindable<int> bindableFilterRole();
96
97 bool isRecursiveFilteringEnabled() const;
98 void setRecursiveFilteringEnabled(bool recursive);
99 QBindable<bool> bindableRecursiveFilteringEnabled();
100
101 bool autoAcceptChildRows() const;
102 void setAutoAcceptChildRows(bool accept);
103 QBindable<bool> bindableAutoAcceptChildRows();
104
105 enum class Direction {
106 Rows = 0x01,
107 Columns = 0x02,
108 Both = Rows | Columns,
109 };
110 Q_DECLARE_FLAGS(Directions, Direction)
111
112public Q_SLOTS:
113 void setFilterRegularExpression(const QString &pattern);
114 void setFilterRegularExpression(const QRegularExpression &regularExpression);
115 void setFilterWildcard(const QString &pattern);
116 void setFilterFixedString(const QString &pattern);
117 void invalidate();
118
119protected:
120 virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
121 virtual bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const;
122 virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const;
123
124 void beginFilterChange();
125 void endFilterChange(Directions directions = Direction::Both);
126#if QT_DEPRECATED_SINCE(6, 13)
127 QT_DEPRECATED_VERSION_X_6_13("Use begin/endFilterChange() instead")
128 void invalidateFilter();
129 QT_DEPRECATED_VERSION_X_6_13("Use begin/endFilterChange(QSortFilterProxyModel::Direction::Rows) instead")
130 void invalidateRowsFilter();
131 QT_DEPRECATED_VERSION_X_6_13("Use begin/endFilterChange(QSortFilterProxyModel::Direction::Columns) instead")
132 void invalidateColumnsFilter();
133#endif
134
135public:
136 using QObject::parent;
137
138 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
139 QModelIndex parent(const QModelIndex &child) const override;
140 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
141
142 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
143 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
144 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
145
146 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
147 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
148
149 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
150 bool setHeaderData(int section, Qt::Orientation orientation,
151 const QVariant &value, int role = Qt::EditRole) override;
152
153 QMimeData *mimeData(const QModelIndexList &indexes) const override;
154 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
155 int row, int column, const QModelIndex &parent) override;
156
157 bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
158 bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
159 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
160 bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
161
162 void fetchMore(const QModelIndex &parent) override;
163 bool canFetchMore(const QModelIndex &parent) const override;
164 Qt::ItemFlags flags(const QModelIndex &index) const override;
165
166 QModelIndex buddy(const QModelIndex &index) const override;
167 QModelIndexList match(const QModelIndex &start, int role,
168 const QVariant &value, int hits = 1,
169 Qt::MatchFlags flags =
170 Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override;
171 QSize span(const QModelIndex &index) const override;
172 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
173
174 QStringList mimeTypes() const override;
175 Qt::DropActions supportedDropActions() const override;
176
177Q_SIGNALS:
178 void dynamicSortFilterChanged(bool dynamicSortFilter);
179 void filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity);
180 void sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity);
181 void sortLocaleAwareChanged(bool sortLocaleAware);
182 void sortRoleChanged(int sortRole);
183 void filterRoleChanged(int filterRole);
184 void recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled);
185 void autoAcceptChildRowsChanged(bool autoAcceptChildRows);
186
187private:
188 Q_DECLARE_PRIVATE(QSortFilterProxyModel)
189 Q_DISABLE_COPY(QSortFilterProxyModel)
190};
191
192Q_DECLARE_OPERATORS_FOR_FLAGS(QSortFilterProxyModel::Directions)
193
194QT_END_NAMESPACE
195
196#endif // QSORTFILTERPROXYMODEL_H
197

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