From 10e03e32b67bb10fcffdba8bdec22b0cc853c217 Mon Sep 17 00:00:00 2001 From: PYS Date: Fri, 2 Dec 2016 15:35:53 +0100 Subject: [PATCH] Add the possibility to specify a negative index in IndexFilter to start from the end --- filter.cpp | 42 +++++++++++++++++++-------------------- filter.h | 23 +++++++++------------ tests/tst_indexfilter.qml | 33 +++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/filter.cpp b/filter.cpp index 308600a..6dec486 100644 --- a/filter.cpp +++ b/filter.cpp @@ -99,54 +99,52 @@ bool ValueFilter::filterRow(const QModelIndex& sourceIndex) const return !m_value.isValid() || m_value == sourceData(sourceIndex); } -int IndexFilter::minimumIndex() const +const QVariant& IndexFilter::minimumIndex() const { return m_minimumIndex; } -void IndexFilter::setMinimumIndex(int minimumIndex) +void IndexFilter::setMinimumIndex(const QVariant& minimumIndex) { - if ((m_minimumIndex == minimumIndex) && m_minimumIndexIsSet) + if (m_minimumIndex == minimumIndex) return; m_minimumIndex = minimumIndex; - m_minimumIndexIsSet = true; Q_EMIT minimumIndexChanged(); Q_EMIT filterChanged(); } -void IndexFilter::resetMinimumIndex() -{ - m_minimumIndexIsSet = false; - Q_EMIT filterChanged(); -} - -int IndexFilter::maximumIndex() const +const QVariant& IndexFilter::maximumIndex() const { return m_maximumIndex; } -void IndexFilter::setMaximumIndex(int maximumIndex) +void IndexFilter::setMaximumIndex(const QVariant& maximumIndex) { - if ((m_maximumIndex == maximumIndex) && m_maximumIndexIsSet) + if (m_maximumIndex == maximumIndex) return; m_maximumIndex = maximumIndex; - m_maximumIndexIsSet = true; Q_EMIT maximumIndexChanged(); Q_EMIT filterChanged(); } -void IndexFilter::resetMaximumIndex() -{ - m_maximumIndexIsSet = false; - Q_EMIT filterChanged(); -} - -bool IndexFilter::filterRow(const QModelIndex &sourceIndex) const +bool IndexFilter::filterRow(const QModelIndex& sourceIndex) const { + int sourceRowCount = proxyModel()->sourceModel()->rowCount(); int sourceRow = sourceIndex.row(); - return (!m_minimumIndexIsSet || sourceRow >= m_minimumIndex) && (!m_maximumIndexIsSet || sourceRow <= m_maximumIndex); + + bool minimumIsValid; + int minimum = m_minimumIndex.toInt(&minimumIsValid); + int actualMinimum = (sourceRowCount + minimum) % sourceRowCount; + bool lowerThanMinimumIndex = minimumIsValid && sourceRow < actualMinimum; + + bool maximumIsValid; + int maximum = m_maximumIndex.toInt(&maximumIsValid); + int actualMaximum = (sourceRowCount + maximum) % sourceRowCount; + bool greaterThanMaximumIndex = maximumIsValid && sourceRow >actualMaximum; + + return !lowerThanMinimumIndex && !greaterThanMaximumIndex; } QString RegexpFilter::pattern() const diff --git a/filter.h b/filter.h index 39c8148..c8e8f09 100644 --- a/filter.h +++ b/filter.h @@ -90,33 +90,28 @@ private: class IndexFilter: public Filter { Q_OBJECT - Q_PROPERTY(int minimumIndex READ minimumIndex WRITE setMinimumIndex NOTIFY minimumIndexChanged RESET resetMinimumIndex) - Q_PROPERTY(int maximumIndex READ maximumIndex WRITE setMaximumIndex NOTIFY maximumIndexChanged RESET resetMaximumIndex) + Q_PROPERTY(QVariant minimumIndex READ minimumIndex WRITE setMinimumIndex NOTIFY minimumIndexChanged) + Q_PROPERTY(QVariant maximumIndex READ maximumIndex WRITE setMaximumIndex NOTIFY maximumIndexChanged) public: using Filter::Filter; - int minimumIndex() const; - void setMinimumIndex(int minimumIndex); - void resetMinimumIndex(); + const QVariant& minimumIndex() const; + void setMinimumIndex(const QVariant& minimumIndex); - int maximumIndex() const; - void setMaximumIndex(int maximumIndex); - void resetMaximumIndex(); + const QVariant& maximumIndex() const; + void setMaximumIndex(const QVariant& maximumIndex); protected: - bool filterRow(const QModelIndex &sourceIndex) const override; + bool filterRow(const QModelIndex& sourceIndex) const override; Q_SIGNALS: void minimumIndexChanged(); void maximumIndexChanged(); private: - int m_minimumIndex = 0; - bool m_minimumIndexIsSet = false; - - int m_maximumIndex = 0; - bool m_maximumIndexIsSet = false; + QVariant m_minimumIndex; + QVariant m_maximumIndex; }; class RegexpFilter : public RoleFilter { diff --git a/tests/tst_indexfilter.qml b/tests/tst_indexfilter.qml index 09132d5..1c22f3f 100644 --- a/tests/tst_indexfilter.qml +++ b/tests/tst_indexfilter.qml @@ -33,7 +33,38 @@ Item { property int expectedModelCount: 2 property var expectedValues: [5,4] minimumIndex: 1; maximumIndex: 3; inverted: true - } + }, + IndexFilter { + property string tag: "last" + property int expectedModelCount: 1 + property var expectedValues: [4] + minimumIndex: -1 + }, + IndexFilter { + property string tag: "fromEnd" + property int expectedModelCount: 2 + property var expectedValues: [2, 4] + minimumIndex: -2 + }, + IndexFilter { + property string tag: "fromEndRange" + property int expectedModelCount: 2 + property var expectedValues: [1, 2] + minimumIndex: -3 + maximumIndex: -2 + }, + IndexFilter { + property string tag: "mixedSignRange" + property int expectedModelCount: 3 + property var expectedValues: [3, 1, 2] + minimumIndex: 1 + maximumIndex: -2 + }, + IndexFilter { + property string tag: "noFilter" + property int expectedModelCount: 5 + property var expectedValues: [5, 3, 1, 2, 4] + } ] ListModel {