/ filters / valuefilter.cpp
valuefilter.cpp
 1  #include "valuefilter.h"
 2  
 3  namespace qqsfpm {
 4  
 5  /*!
 6      \qmltype ValueFilter
 7      \inherits RoleFilter
 8      \inqmlmodule SortFilterProxyModel
 9      \ingroup Filters
10      \brief Filters rows matching exactly a value.
11  
12      A ValueFilter is a simple \l RoleFilter that accepts rows matching exactly the filter's value
13  
14      In the following example, only rows with their \c favorite role set to \c true will be accepted when the checkbox is checked :
15      \code
16      CheckBox {
17         id: showOnlyFavoriteCheckBox
18      }
19  
20      SortFilterProxyModel {
21         sourceModel: contactModel
22         filters: ValueFilter {
23             roleName: "favorite"
24             value: true
25             enabled: showOnlyFavoriteCheckBox.checked
26         }
27      }
28      \endcode
29  
30  */
31  
32  /*!
33      \qmlproperty variant ValueFilter::value
34  
35      This property holds the value used to filter the contents of the source model.
36  */
37  const QVariant &ValueFilter::value() const
38  {
39      return m_value;
40  }
41  
42  void ValueFilter::setValue(const QVariant& value)
43  {
44      if (m_value == value)
45          return;
46  
47      m_value = value;
48      Q_EMIT valueChanged();
49      invalidate();
50  }
51  
52  bool ValueFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
53  {
54      QVariant srcData = sourceData(sourceIndex, proxyModel);
55  #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
56      // Implicitly convert the types. This was the behavior in Qt5 and makes QML
57      // interop much easier, e.g. when comparing QByteArray against QString
58      if (srcData.metaType() != m_value.metaType()) {
59          QVariant converted = srcData;
60          if (converted.convert(m_value.metaType())) {
61              srcData = converted;
62          }
63      }
64  #endif
65      return !m_value.isValid() || m_value == srcData;
66  }
67  
68  }