/ proxyroles / filterrole.cpp
filterrole.cpp
1 #include "filterrole.h" 2 #include "filters/filter.h" 3 4 namespace qqsfpm { 5 6 /*! 7 \qmltype FilterRole 8 \inherits SingleRole 9 \inqmlmodule SortFilterProxyModel 10 \ingroup ProxyRoles 11 \ingroup FilterContainer 12 \brief A role resolving to \c true for rows matching all its filters. 13 14 A FilterRole is a \l ProxyRole that returns \c true for rows matching all its filters. 15 16 In the following example, the \c isAdult role will be equal to \c true if the \c age role is superior or equal to 18. 17 \code 18 SortFilterProxyModel { 19 sourceModel: personModel 20 proxyRoles: FilterRole { 21 name: "isAdult" 22 RangeFilter { roleName: "age"; minimumValue: 18; minimumInclusive: true } 23 } 24 } 25 \endcode 26 \sa FilterContainer 27 */ 28 29 /*! 30 \qmlproperty list<Filter> FilterRole::filters 31 \default 32 33 This property holds the list of filters for this filter role. 34 The data of this role will be equal to the \c true if all its filters match the model row, \c false otherwise. 35 36 \sa Filter, FilterContainer 37 */ 38 39 void FilterRole::onFilterAppended(Filter* filter) 40 { 41 connect(filter, &Filter::invalidated, this, &FilterRole::invalidate); 42 invalidate(); 43 } 44 45 void FilterRole::onFilterRemoved(Filter* filter) 46 { 47 disconnect(filter, &Filter::invalidated, this, &FilterRole::invalidate); 48 invalidate(); 49 } 50 51 void FilterRole::onFiltersCleared() 52 { 53 invalidate(); 54 } 55 56 QVariant FilterRole::data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) 57 { 58 return std::all_of(m_filters.cbegin(), m_filters.cend(), 59 [&] (Filter* filter) { 60 return filter->filterAcceptsRow(sourceIndex, proxyModel); 61 } 62 ); 63 } 64 65 }