KItemModels

ksortfilterproxymodel.h
1/*
2 * SPDX-FileCopyrightText: 2010 Marco Martin <mart@kde.org>
3 * SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#ifndef KSORTFILTERPROXYMODEL_H
9#define KSORTFILTERPROXYMODEL_H
10
11#include <QAbstractItemModel>
12#include <QJSValue>
13#include <QList>
14#include <QQmlParserStatus>
15#include <QSortFilterProxyModel>
16#include <qqmlregistration.h>
17
18#include <array>
19
20/**
21 * @class KSortFilterProxyModel
22 * @short Filter and sort an existing QAbstractItemModel
23 *
24 * @since 5.67
25 */
26class KSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus
27{
29 QML_ELEMENT
31
32 /**
33 * The string for the filter, only rows with their filterRole matching filterString will be displayed
34 */
35 Q_PROPERTY(QString filterString READ filterString WRITE setFilterString NOTIFY filterStringChanged)
36 /**
37 * A JavaScript callable that can be used to perform advanced filters on a given row.
38 * The callback is passed the source row, and source parent for a given row as arguments
39 *
40 * The callable's return value is evaluated as boolean to determine
41 * whether the row is accepted (true) or filtered out (false). It overrides the default implementation
42 * that uses filterRegExp or filterString; while filterCallback is set those two properties are
43 * ignored. Attempts to write a non-callable to this property are silently ignored, but you can set
44 * it to null.
45 *
46 * @code
47 * filterRowCallback: function(source_row, source_parent) {
48 * return sourceModel.data(sourceModel.index(source_row, 0, source_parent), Qt.DisplayRole) == "...";
49 * };
50 * @endcode
51 */
52 Q_PROPERTY(QJSValue filterRowCallback READ filterRowCallback WRITE setFilterRowCallback NOTIFY filterRowCallbackChanged)
53
54 /**
55 * A JavaScript callable that can be used to perform advanced filters on a given column.
56 * The callback is passed the source column, and source parent for a given column as arguments.
57 *
58 * @see filterRowCallback
59 */
60 Q_PROPERTY(QJSValue filterColumnCallback READ filterColumnCallback WRITE setFilterColumnCallback NOTIFY filterColumnCallbackChanged)
61
62 /**
63 * The role of the sourceModel on which the filter will be applied.
64 * This can either be the numerical role value or the role name as a string.
65 */
66 Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged)
67
68 /**
69 * The role of the sourceModel that will be used for sorting. if empty the order will be left unaltered
70 * This can either be the numerical role value or the role name as a string.
71 */
72 Q_PROPERTY(QString sortRoleName READ sortRoleName WRITE setSortRoleName NOTIFY sortRoleNameChanged)
73
74 /**
75 * One of Qt.AscendingOrder or Qt.DescendingOrder
76 */
77 Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
78
79 /**
80 * Specify which column should be used for sorting
81 * The default value is -1.
82 * If \a sortRole is set, the default value is 0.
83 */
84 Q_PROPERTY(int sortColumn READ sortColumn WRITE setSortColumn NOTIFY sortColumnChanged)
85
86 /**
87 * The number of top level rows.
88 */
89 Q_PROPERTY(int count READ rowCount NOTIFY rowCountChanged)
90
91public:
92 explicit KSortFilterProxyModel(QObject *parent = nullptr);
93 ~KSortFilterProxyModel() override;
94
95 void setSourceModel(QAbstractItemModel *sourceModel) override;
96
97 void setFilterRowCallback(const QJSValue &callback);
99
100 void setFilterString(const QString &filterString);
101 QString filterString() const;
102
103 void setFilterColumnCallback(const QJSValue &callback);
105
106 void setFilterRoleName(const QString &roleName);
107 QString filterRoleName() const;
108
109 void setSortRoleName(const QString &roleName);
110 QString sortRoleName() const;
111
112 void setSortOrder(const Qt::SortOrder order);
113 void setSortColumn(int column);
114
115 void classBegin() override;
116 void componentComplete() override;
117
118public Q_SLOTS:
119 /**
120 * Invalidates the current filtering.
121 *
122 * This function should be called if you are implementing custom filtering through
123 * filterRowCallback or filterColumnCallback, and your filter parameters have changed.
124 *
125 * @since 5.70
126 */
127 void invalidateFilter();
128
130 void filterStringChanged();
131 void filterRoleNameChanged();
132 void sortRoleNameChanged();
133 void sortOrderChanged();
134 void sortColumnChanged();
135 void filterRowCallbackChanged(const QJSValue &);
136 void filterColumnCallbackChanged(const QJSValue &);
137 void rowCountChanged();
138
139protected:
140 int roleNameToId(const QString &name) const;
141 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
142 bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const override;
143
144protected Q_SLOTS:
145 // This method is called whenever we suspect that role names mapping might have gone stale.
146 // It must not alter the source of truth for sort/filter properties.
147 void syncRoleNames();
148 // These methods are dealing with individual pairs of properties. They are
149 // called on various occasions, and need to check whether the invocation
150 // has been caused by a standalone base type's property change
151 // (switching source of truth to role ID) or as a side-effect of a sync.
152 void syncSortRoleProperties();
153 void syncFilterRoleProperties();
154
155private:
156 // conveniently, role ID is the default source of truth, turning it into
157 // zero-initialization.
158 enum SourceOfTruthForRoleProperty : bool {
159 SourceOfTruthIsRoleID = false,
160 SourceOfTruthIsRoleName = true,
161 };
162
163 bool m_componentCompleted : 1;
164 SourceOfTruthForRoleProperty m_sortRoleSourceOfTruth : 1;
165 SourceOfTruthForRoleProperty m_filterRoleSourceOfTruth : 1;
166 bool m_sortRoleGuard : 1;
167 bool m_filterRoleGuard : 1;
168 // default role name corresponds to the standard mapping of the default Qt::DisplayRole in QAbstractItemModel::roleNames
169 QString m_sortRoleName{QStringLiteral("display")};
170 QString m_filterRoleName{QStringLiteral("display")};
171 QString m_filterString;
172 QJSValue m_filterRowCallback;
173 QJSValue m_filterColumnCallback;
174 QHash<QString, int> m_roleIds;
175 std::array<QMetaObject::Connection, 3> m_sourceModelConnections;
176};
177
178#endif
QJSValue filterRowCallback
A JavaScript callable that can be used to perform advanced filters on a given row.
QString sortRoleName
The role of the sourceModel that will be used for sorting.
Qt::SortOrder sortOrder
One of Qt.AscendingOrder or Qt.DescendingOrder.
QString filterRoleName
The role of the sourceModel on which the filter will be applied.
int count
The number of top level rows.
int sortColumn
Specify which column should be used for sorting The default value is -1.
QJSValue filterColumnCallback
A JavaScript callable that can be used to perform advanced filters on a given column.
QML_ELEMENTQString filterString
The string for the filter, only rows with their filterRole matching filterString will be displayed.
void invalidateFilter()
Invalidates the current filtering.
QAbstractItemModel(QObject *parent)
Q_INTERFACES(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QSortFilterProxyModel(QObject *parent)
virtual QModelIndex parent(const QModelIndex &child) const const override
virtual int rowCount(const QModelIndex &parent) const const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:04:43 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.