1// Copyright (C) 2025 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
4#include <QtQmlModels/private/qqmlvaluefilter_p.h>
5#include <QtQmlModels/private/qqmlsortfilterproxymodel_p.h>
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \qmltype ValueFilter
11 \inherits RoleFilter
12 \inqmlmodule QtQml.Models
13 \since 6.10
14 \preliminary
15 \brief Filters data in a \l SortFilterProxyModel based on role name and
16 value.
17
18 ValueFilter allows the user to filter the data according to the role name
19 or specified value or both as configured in the source model. The role name
20 used to filter the data shall be based on model
21 \l{QAbstractItemModel::roleNames()}{role name}. The default value for
22 role name is \c "display".
23
24 The following snippet shows how ValueFilter can be used to only include
25 data from the source model where the value of the role name \c "favorite"
26 is \c "true":
27
28 \qml
29 SortFilterProxyModel {
30 model: sourceModel
31 filters: [
32 ValueFilter {
33 roleName: "favorite"
34 value: true
35 }
36 ]
37 }
38 \endqml
39*/
40
41QQmlValueFilter::QQmlValueFilter(QObject *parent) :
42 QQmlRoleFilter (new QQmlValueFilterPrivate, parent)
43{
44
45}
46
47/*!
48 \qmlproperty string ValueFilter::value
49
50 This property holds specific value that can be used to filter the data.
51
52 The default value is empty string.
53*/
54const QVariant& QQmlValueFilter::value() const
55{
56 Q_D(const QQmlValueFilter);
57 return d->m_value;
58}
59
60void QQmlValueFilter::setValue(const QVariant& value)
61{
62 Q_D(QQmlValueFilter);
63 if (d->m_value == value)
64 return;
65 d->m_value = value;
66 // Update the model for the change in the role name
67 emit valueChanged();
68 // Invalidate the model for the change in the role name
69 invalidate();
70}
71
72void QQmlValueFilter::resetValue()
73{
74 Q_D(QQmlValueFilter);
75 d->m_value = QVariant();
76}
77
78/*!
79 \internal
80*/
81bool QQmlValueFilter::filterAcceptsRowInternal(int row, const QModelIndex& sourceParent, const QQmlSortFilterProxyModel *proxyModel) const
82{
83 Q_D(const QQmlValueFilter);
84 if (d->m_roleName.isEmpty())
85 return true;
86 int role = itemRole(proxyModel);
87 const bool isValidVal = (!d->m_value.isValid() || !d->m_value.isNull());
88 if (role > -1) {
89 if (column() > -1) {
90 const QModelIndex &index = proxyModel->sourceModel()->index(row, column: column(), parent: sourceParent);
91 const QVariant &value = proxyModel->sourceModel()->data(index, role);
92 return (value.isValid() && (!isValidVal || d->m_value == value));
93 } else {
94 const int columnCount = proxyModel->sourceModel()->columnCount(parent: sourceParent);
95 for (int column = 0; column < columnCount; column++) {
96 const QModelIndex &index = proxyModel->sourceModel()->index(row, column, parent: sourceParent);
97 const QVariant &value = proxyModel->sourceModel()->data(index, role);
98 if (value.isValid() && (!isValidVal || d->m_value == value))
99 return true;
100 }
101 }
102 }
103 return false;
104}
105
106QT_END_NAMESPACE
107
108#include "moc_qqmlvaluefilter_p.cpp"
109

source code of qtdeclarative/src/qmlmodels/sfpm/filters/qqmlvaluefilter.cpp