rangefilter.cpp
1 #include "rangefilter.h" 2 3 #include "qvariantlessthan.h" 4 5 namespace qqsfpm { 6 7 /*! 8 \qmltype RangeFilter 9 \inherits RoleFilter 10 \inqmlmodule SortFilterProxyModel 11 \ingroup Filters 12 \brief Filters rows between boundary values. 13 14 A RangeFilter is a \l RoleFilter that accepts rows if their data is between the filter's minimum and maximum value. 15 16 In the following example, only rows with their \c price role set to a value between the tow boundary of the slider will be accepted : 17 \code 18 RangeSlider { 19 id: priceRangeSlider 20 } 21 22 SortFilterProxyModel { 23 sourceModel: priceModel 24 filters: RangeFilter { 25 roleName: "price" 26 minimumValue: priceRangeSlider.first.value 27 maximumValue: priceRangeSlider.second.value 28 } 29 } 30 \endcode 31 */ 32 33 /*! 34 \qmlproperty int RangeFilter::minimumValue 35 36 This property holds the minimumValue of the filter. 37 Rows with a value lower than \c minimumValue will be rejected. 38 39 By default, no value is set. 40 41 \sa minimumInclusive 42 */ 43 QVariant RangeFilter::minimumValue() const 44 { 45 return m_minimumValue; 46 } 47 48 void RangeFilter::setMinimumValue(const QVariant &minimumValue) 49 { 50 if (m_minimumValue == minimumValue) 51 return; 52 53 m_minimumValue = minimumValue; 54 Q_EMIT minimumValueChanged(); 55 invalidate(); 56 } 57 58 /*! 59 \qmlproperty int RangeFilter::minimumInclusive 60 61 This property holds whether the \l minimumValue is inclusive. 62 63 By default, the \l minimumValue is inclusive. 64 65 \sa minimumValue 66 */ 67 bool RangeFilter::minimumInclusive() const 68 { 69 return m_minimumInclusive; 70 } 71 72 void RangeFilter::setMinimumInclusive(bool minimumInclusive) 73 { 74 if (m_minimumInclusive == minimumInclusive) 75 return; 76 77 m_minimumInclusive = minimumInclusive; 78 Q_EMIT minimumInclusiveChanged(); 79 invalidate(); 80 } 81 82 /*! 83 \qmlproperty int RangeFilter::maximumValue 84 85 This property holds the maximumValue of the filter. 86 Rows with a value higher than \c maximumValue will be rejected. 87 88 By default, no value is set. 89 90 \sa maximumInclusive 91 */ 92 QVariant RangeFilter::maximumValue() const 93 { 94 return m_maximumValue; 95 } 96 97 void RangeFilter::setMaximumValue(const QVariant &maximumValue) 98 { 99 if (m_maximumValue == maximumValue) 100 return; 101 102 m_maximumValue = maximumValue; 103 Q_EMIT maximumValueChanged(); 104 invalidate(); 105 } 106 107 /*! 108 \qmlproperty int RangeFilter::maximumInclusive 109 110 This property holds whether the \l minimumValue is inclusive. 111 112 By default, the \l minimumValue is inclusive. 113 114 \sa minimumValue 115 */ 116 bool RangeFilter::maximumInclusive() const 117 { 118 return m_maximumInclusive; 119 } 120 121 void RangeFilter::setMaximumInclusive(bool maximumInclusive) 122 { 123 if (m_maximumInclusive == maximumInclusive) 124 return; 125 126 m_maximumInclusive = maximumInclusive; 127 Q_EMIT maximumInclusiveChanged(); 128 invalidate(); 129 } 130 131 bool RangeFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const 132 { 133 const QVariant value = sourceData(sourceIndex, proxyModel); 134 bool lessThanMin = m_minimumValue.isValid() && 135 (m_minimumInclusive ? qqsfpm::lessThan(value, m_minimumValue) 136 : !qqsfpm::lessThan(m_minimumValue, value)); 137 bool moreThanMax = m_maximumValue.isValid() && 138 (m_maximumInclusive ? qqsfpm::lessThan(m_maximumValue, value) 139 : !qqsfpm::lessThan(value, m_maximumValue)); 140 return !(lessThanMin || moreThanMax); 141 } 142 143 }