Add the possibility to specify a negative index in IndexFilter to start from the end

This commit is contained in:
PYS 2016-12-02 15:35:53 +01:00
parent 65fb8595f8
commit 10e03e32b6
3 changed files with 61 additions and 37 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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 {