refactor: move filters in separate files

This commit is contained in:
Grecko 2018-04-27 00:40:01 +02:00
parent febeade02b
commit df41e94e45
29 changed files with 1283 additions and 1088 deletions

View File

@ -3,10 +3,32 @@
INCLUDEPATH += $$PWD
HEADERS += $$PWD/qqmlsortfilterproxymodel.h \
$$PWD/filter.h \
$$PWD/sorter.h \
$$PWD/proxyrole.h
$$PWD/proxyrole.h \
$$PWD/filters/filter.h \
$$PWD/filters/filtercontainer.h \
$$PWD/filters/rolefilter.h \
$$PWD/filters/valuefilter.h \
$$PWD/filters/indexfilter.h \
$$PWD/filters/regexpfilter.h \
$$PWD/filters/rangefilter.h \
$$PWD/filters/expressionfilter.h \
$$PWD/filters/filtercontainerfilter.h \
$$PWD/filters/anyoffilter.h \
$$PWD/filters/alloffilter.h
SOURCES += $$PWD/qqmlsortfilterproxymodel.cpp \
$$PWD/filter.cpp \
$$PWD/sorter.cpp \
$$PWD/proxyrole.cpp
$$PWD/proxyrole.cpp \
$$PWD/filters/filter.cpp \
$$PWD/filters/filtercontainer.cpp \
$$PWD/filters/rolefilter.cpp \
$$PWD/filters/valuefilter.cpp \
$$PWD/filters/indexfilter.cpp \
$$PWD/filters/regexpfilter.cpp \
$$PWD/filters/rangefilter.cpp \
$$PWD/filters/expressionfilter.cpp \
$$PWD/filters/filtercontainerfilter.cpp \
$$PWD/filters/anyoffilter.cpp \
$$PWD/filters/alloffilter.cpp \
$$PWD/filters/filtersqmltypes.cpp

View File

@ -1,787 +0,0 @@
#include "filter.h"
#include "qqmlsortfilterproxymodel.h"
#include <QtQml>
namespace qqsfpm {
FilterContainer::~FilterContainer()
{
}
QList<Filter*> FilterContainer::filters() const
{
return m_filters;
}
void FilterContainer::appendFilter(Filter* filter)
{
m_filters.append(filter);
onFilterAppended(filter);
}
void FilterContainer::removeFilter(Filter* filter)
{
m_filters.removeOne(filter);
onFilterRemoved(filter);
}
void FilterContainer::clearFilters()
{
m_filters.clear();
onFiltersCleared();
}
QQmlListProperty<Filter> FilterContainer::filtersListProperty()
{
return QQmlListProperty<Filter>(reinterpret_cast<QObject*>(this), &m_filters,
&FilterContainer::append_filter,
&FilterContainer::count_filter,
&FilterContainer::at_filter,
&FilterContainer::clear_filters);
}
void FilterContainer::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
{
if (!filter)
return;
FilterContainer* that = reinterpret_cast<FilterContainer*>(list->object);
that->appendFilter(filter);
}
int FilterContainer::count_filter(QQmlListProperty<Filter>* list)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
}
Filter* FilterContainer::at_filter(QQmlListProperty<Filter>* list, int index)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
}
void FilterContainer::clear_filters(QQmlListProperty<Filter> *list)
{
FilterContainer* that = reinterpret_cast<FilterContainer*>(list->object);
that->clearFilters();
}
/*!
\qmltype Filter
\inqmlmodule SortFilterProxyModel
\brief Base type for the \l SortFilterProxyModel filters
The Filter type cannot be used directly in a QML file.
It exists to provide a set of common properties and methods,
available across all the other filter types that inherit from it.
Attempting to use the Filter type directly will result in an error.
*/
Filter::Filter(QObject *parent) : QObject(parent)
{
}
/*!
\qmlproperty bool Filter::enabled
This property holds whether the filter is enabled.
A disabled filter will accept every rows unconditionally (even if it's inverted).
By default, filters are enabled.
*/
bool Filter::enabled() const
{
return m_enabled;
}
void Filter::setEnabled(bool enabled)
{
if (m_enabled == enabled)
return;
m_enabled = enabled;
Q_EMIT enabledChanged();
Q_EMIT invalidated();
}
/*!
\qmlproperty bool Filter::inverted
This property holds whether the filter is inverted.
When a filter is inverted, a row normally accepted would be rejected, and vice-versa.
By default, filters are not inverted.
*/
bool Filter::inverted() const
{
return m_inverted;
}
void Filter::setInverted(bool inverted)
{
if (m_inverted == inverted)
return;
m_inverted = inverted;
Q_EMIT invertedChanged();
invalidate();
}
bool Filter::filterAcceptsRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return !m_enabled || filterRow(sourceIndex, proxyModel) ^ m_inverted;
}
void Filter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
Q_UNUSED(proxyModel)
}
void Filter::invalidate()
{
if (m_enabled)
Q_EMIT invalidated();
}
/*!
\qmltype RoleFilter
\qmlabstract
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Base type for filters based on a source model role
The RoleFilter type cannot be used directly in a QML file.
It exists to provide a set of common properties and methods,
available across all the other filter types that inherit from it.
Attempting to use the RoleFilter type directly will result in an error.
*/
/*!
\qmlproperty string RoleFilter::roleName
This property holds the role name that the filter is using to query the source model's data when filtering items.
*/
const QString& RoleFilter::roleName() const
{
return m_roleName;
}
void RoleFilter::setRoleName(const QString& roleName)
{
if (m_roleName == roleName)
return;
m_roleName = roleName;
Q_EMIT roleNameChanged();
invalidate();
}
QVariant RoleFilter::sourceData(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return proxyModel.sourceData(sourceIndex, m_roleName);
}
/*!
\qmltype ValueFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows matching exactly a value
A ValueFilter is a simple \l RoleFilter that accepts rows matching exactly the filter's value
In the following example, only rows with their \c favorite role set to \c true will be accepted when the checkbox is checked :
\code
CheckBox {
id: showOnlyFavoriteCheckBox
}
SortFilterProxyModel {
sourceModel: contactModel
filters: ValueFilter {
roleName: "favorite"
value: true
enabled: showOnlyFavoriteCheckBox.checked
}
}
\endcode
*/
/*!
\qmlproperty variant ValueFilter::value
This property holds the value used to filter the contents of the source model.
*/
const QVariant &ValueFilter::value() const
{
return m_value;
}
void ValueFilter::setValue(const QVariant& value)
{
if (m_value == value)
return;
m_value = value;
Q_EMIT valueChanged();
invalidate();
}
bool ValueFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return !m_value.isValid() || m_value == sourceData(sourceIndex, proxyModel);
}
/*!
\qmltype IndexFilter
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filters rows based on their source index
An IndexFilter is a filter allowing contents to be filtered based on their source model index.
In the following example, only the first row of the source model will be accepted:
\code
SortFilterProxyModel {
sourceModel: contactModel
filters: IndexFilter {
maximumIndex: 0
}
}
\endcode
*/
/*!
\qmlproperty int IndexFilter::minimumIndex
This property holds the minimumIndex of the filter.
Rows with a source index lower than \c minimumIndex will be rejected.
If \c minimumIndex is negative, it is counted from the end of the source model, meaning that :
\code minimumIndex: -1\endcode
is equivalent to :
\code minimumIndex: sourceModel.count - 1\endcode
By default, no value is set.
*/
const QVariant& IndexFilter::minimumIndex() const
{
return m_minimumIndex;
}
void IndexFilter::setMinimumIndex(const QVariant& minimumIndex)
{
if (m_minimumIndex == minimumIndex)
return;
m_minimumIndex = minimumIndex;
Q_EMIT minimumIndexChanged();
invalidate();
}
/*!
\qmlproperty int IndexFilter::maximumIndex
This property holds the maximumIndex of the filter.
Rows with a source index higher than \c maximumIndex will be rejected.
If \c maximumIndex is negative, it is counted from the end of the source model, meaning that:
\code maximumIndex: -1\endcode
is equivalent to :
\code maximumIndex: sourceModel.count - 1\endcode
By default, no value is set.
*/
const QVariant& IndexFilter::maximumIndex() const
{
return m_maximumIndex;
}
void IndexFilter::setMaximumIndex(const QVariant& maximumIndex)
{
if (m_maximumIndex == maximumIndex)
return;
m_maximumIndex = maximumIndex;
Q_EMIT maximumIndexChanged();
invalidate();
}
bool IndexFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
int sourceRowCount = proxyModel.sourceModel()->rowCount();
int sourceRow = sourceIndex.row();
bool minimumIsValid;
int minimum = m_minimumIndex.toInt(&minimumIsValid);
if (minimumIsValid) {
int actualMinimum = minimum < 0 ? sourceRowCount + minimum : minimum;
if (sourceRow < actualMinimum)
return false;
}
bool maximumIsValid;
int maximum = m_maximumIndex.toInt(&maximumIsValid);
if (maximumIsValid) {
int actualMaximum = maximum < 0 ? sourceRowCount + maximum : maximum;
if (sourceRow > actualMaximum)
return false;
}
return true;
}
/*!
\qmltype RegExpFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows matching a regular expression
A RegExpFilter is a \l RoleFilter that accepts rows matching a regular rexpression.
In the following example, only rows with their \c lastName role beggining with the content of textfield the will be accepted:
\code
TextField {
id: nameTextField
}
SortFilterProxyModel {
sourceModel: contactModel
filters: RegExpFilter {
roleName: "lastName"
pattern: "^" + nameTextField.displayText
}
}
\endcode
*/
/*!
\qmlproperty bool RegExpFilter::pattern
The pattern used to filter the contents of the source model.
\sa syntax
*/
QString RegExpFilter::pattern() const
{
return m_pattern;
}
void RegExpFilter::setPattern(const QString& pattern)
{
if (m_pattern == pattern)
return;
m_pattern = pattern;
m_regExp.setPattern(pattern);
Q_EMIT patternChanged();
invalidate();
}
/*!
\qmlproperty enum RegExpFilter::syntax
The pattern used to filter the contents of the source model.
Only the source model's value having their \l roleName data matching this \l pattern with the specified \l syntax will be kept.
\value RegExpFilter.RegExp A rich Perl-like pattern matching syntax. This is the default.
\value RegExpFilter.Wildcard This provides a simple pattern matching syntax similar to that used by shells (command interpreters) for "file globbing".
\value RegExpFilter.FixedString The pattern is a fixed string. This is equivalent to using the RegExp pattern on a string in which all metacharacters are escaped.
\value RegExpFilter.RegExp2 Like RegExp, but with greedy quantifiers.
\value RegExpFilter.WildcardUnix This is similar to Wildcard but with the behavior of a Unix shell. The wildcard characters can be escaped with the character "\".
\value RegExpFilter.W3CXmlSchema11 The pattern is a regular expression as defined by the W3C XML Schema 1.1 specification.
\sa pattern
*/
RegExpFilter::PatternSyntax RegExpFilter::syntax() const
{
return m_syntax;
}
void RegExpFilter::setSyntax(RegExpFilter::PatternSyntax syntax)
{
if (m_syntax == syntax)
return;
m_syntax = syntax;
m_regExp.setPatternSyntax(static_cast<QRegExp::PatternSyntax>(syntax));
Q_EMIT syntaxChanged();
invalidate();
}
/*!
\qmlproperty Qt::CaseSensitivity RegExpFilter::caseSensitivity
This property holds the caseSensitivity of the filter.
*/
Qt::CaseSensitivity RegExpFilter::caseSensitivity() const
{
return m_caseSensitivity;
}
void RegExpFilter::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
{
if (m_caseSensitivity == caseSensitivity)
return;
m_caseSensitivity = caseSensitivity;
m_regExp.setCaseSensitivity(caseSensitivity);
Q_EMIT caseSensitivityChanged();
invalidate();
}
bool RegExpFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
QString string = sourceData(sourceIndex, proxyModel).toString();
return m_regExp.indexIn(string) != -1;
}
/*!
\qmltype RangeFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows between boundary values
A RangeFilter is a \l RoleFilter that accepts rows if their data is between the filter's minimum and maximum value.
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 :
\code
RangeSlider {
id: priceRangeSlider
}
SortFilterProxyModel {
sourceModel: priceModel
filters: RangeFilter {
roleName: "price"
minimumValue: priceRangeSlider.first.value
maximumValue: priceRangeSlider.second.value
}
}
\endcode
*/
/*!
\qmlproperty int RangeFilter::minimumValue
This property holds the minimumValue of the filter.
Rows with a value lower than \c minimumValue will be rejected.
By default, no value is set.
\sa minimumInclusive
*/
QVariant RangeFilter::minimumValue() const
{
return m_minimumValue;
}
void RangeFilter::setMinimumValue(QVariant minimumValue)
{
if (m_minimumValue == minimumValue)
return;
m_minimumValue = minimumValue;
Q_EMIT minimumValueChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::minimumInclusive
This property holds whether the \l minimumValue is inclusive.
By default, the \l minimumValue is inclusive.
\sa minimumValue
*/
bool RangeFilter::minimumInclusive() const
{
return m_minimumInclusive;
}
void RangeFilter::setMinimumInclusive(bool minimumInclusive)
{
if (m_minimumInclusive == minimumInclusive)
return;
m_minimumInclusive = minimumInclusive;
Q_EMIT minimumInclusiveChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::maximumValue
This property holds the maximumValue of the filter.
Rows with a value higher than \c maximumValue will be rejected.
By default, no value is set.
\sa maximumInclusive
*/
QVariant RangeFilter::maximumValue() const
{
return m_maximumValue;
}
void RangeFilter::setMaximumValue(QVariant maximumValue)
{
if (m_maximumValue == maximumValue)
return;
m_maximumValue = maximumValue;
Q_EMIT maximumValueChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::maximumInclusive
This property holds whether the \l minimumValue is inclusive.
By default, the \l minimumValue is inclusive.
\sa minimumValue
*/
bool RangeFilter::maximumInclusive() const
{
return m_maximumInclusive;
}
void RangeFilter::setMaximumInclusive(bool maximumInclusive)
{
if (m_maximumInclusive == maximumInclusive)
return;
m_maximumInclusive = maximumInclusive;
Q_EMIT maximumInclusiveChanged();
invalidate();
}
bool RangeFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
QVariant value = sourceData(sourceIndex, proxyModel);
bool lessThanMin = m_minimumValue.isValid() &&
m_minimumInclusive ? value < m_minimumValue : value <= m_minimumValue;
bool moreThanMax = m_maximumValue.isValid() &&
m_maximumInclusive ? value > m_maximumValue : value >= m_maximumValue;
return !(lessThanMin || moreThanMax);
}
/*!
\qmltype ExpressionFilter
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filters row with a custom filtering
An ExpressionFilter is a \l Filter allowing to implement custom filtering based on a javascript expression.
*/
/*!
\qmlproperty expression ExpressionFilter::expression
An expression to implement custom filtering, it must evaluate to a boolean.
It has the same syntax has a \l {http://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html} {Property Binding} except it will be evaluated for each of the source model's rows.
Rows that have their expression evaluating to \c true will be accepted by the model.
Data for each row is exposed like for a delegate of a QML View.
This expression is reevaluated for a row every time its model data changes.
When an external property (not \c index or in \c model) the expression depends on changes, the expression is reevaluated for every row of the source model.
To capture the properties the expression depends on, the expression is first executed with invalid data and each property access is detected by the QML engine.
This means that if a property is not accessed because of a conditional, it won't be captured and the expression won't be reevaluted when this property changes.
A workaround to this problem is to access all the properties the expressions depends unconditionally at the beggining of the expression.
*/
const QQmlScriptString& ExpressionFilter::expression() const
{
return m_scriptString;
}
void ExpressionFilter::setExpression(const QQmlScriptString& scriptString)
{
if (m_scriptString == scriptString)
return;
m_scriptString = scriptString;
updateExpression();
Q_EMIT expressionChanged();
invalidate();
}
void ExpressionFilter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
updateContext(proxyModel);
}
bool ExpressionFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
if (!m_scriptString.isEmpty()) {
QVariantMap modelMap;
QHash<int, QByteArray> roles = proxyModel.roleNames();
QQmlContext context(qmlContext(this));
auto addToContext = [&] (const QString &name, const QVariant& value) {
context.setContextProperty(name, value);
modelMap.insert(name, value);
};
for (auto it = roles.cbegin(); it != roles.cend(); ++it)
addToContext(it.value(), proxyModel.sourceData(sourceIndex, it.key()));
addToContext("index", sourceIndex.row());
context.setContextProperty("model", modelMap);
QQmlExpression expression(m_scriptString, &context);
QVariant variantResult = expression.evaluate();
if (expression.hasError()) {
qWarning() << expression.error();
return true;
}
if (variantResult.canConvert<bool>()) {
return variantResult.toBool();
} else {
qWarning("%s:%i:%i : Can't convert result to bool",
expression.sourceFile().toUtf8().data(),
expression.lineNumber(),
expression.columnNumber());
return true;
}
}
return true;
}
void ExpressionFilter::updateContext(const QQmlSortFilterProxyModel& proxyModel)
{
delete m_context;
m_context = new QQmlContext(qmlContext(this), this);
// what about roles changes ?
QVariantMap modelMap;
auto addToContext = [&] (const QString &name, const QVariant& value) {
m_context->setContextProperty(name, value);
modelMap.insert(name, value);
};
for (const QByteArray& roleName : proxyModel.roleNames().values())
addToContext(roleName, QVariant());
addToContext("index", -1);
m_context->setContextProperty("model", modelMap);
updateExpression();
}
void ExpressionFilter::updateExpression()
{
if (!m_context)
return;
delete m_expression;
m_expression = new QQmlExpression(m_scriptString, m_context, 0, this);
connect(m_expression, &QQmlExpression::valueChanged, this, &ExpressionFilter::invalidate);
m_expression->setNotifyOnValueChanged(true);
m_expression->evaluate();
}
void FilterContainerFilter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
for (Filter* filter : m_filters)
filter->proxyModelCompleted(proxyModel);
}
void FilterContainerFilter::onFilterAppended(Filter* filter)
{
connect(filter, &Filter::invalidated, this, &FilterContainerFilter::invalidate);
invalidate();
}
void FilterContainerFilter::onFilterRemoved(Filter* filter)
{
Q_UNUSED(filter)
invalidate();
}
void qqsfpm::FilterContainerFilter::onFiltersCleared()
{
invalidate();
}
/*!
\qmltype AnyOf
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filter container accepting rows accepted by at least one of its child filters
The AnyOf type is a \l Filter container that accepts rows if any of its contained (and enabled) filters accept them.
In the following example, only the rows where the \c firstName role or the \c lastName role match the text entered in the \c nameTextField will be accepted :
\code
TextField {
id: nameTextField
}
SortFilterProxyModel {
sourceModel: contactModel
filters: AnyOf {
RegExpFilter {
roleName: "lastName"
pattern: nameTextField.text
caseSensitivity: Qt.CaseInsensitive
}
RegExpFilter {
roleName: "firstName"
pattern: nameTextField.text
caseSensitivity: Qt.CaseInsensitive
}
}
}
\endcode
*/
bool AnyOfFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
//return true if any of the enabled filters return true
return std::any_of(m_filters.begin(), m_filters.end(),
[&sourceIndex, &proxyModel] (Filter* filter) {
return filter->enabled() && filter->filterAcceptsRow(sourceIndex, proxyModel);
}
);
}
/*!
\qmltype AllOf
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filter container accepting rows accepted by all its child filters
The AllOf type is a \l Filter container that accepts rows if all of its contained (and enabled) filters accept them, or if it has no filter.
Using it as a top level filter has the same effect as putting all its child filters as top level filters. It can however be usefull to use an AllOf filter when nested in an AnyOf filter.
*/
bool AllOfFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
//return true if all filters return false, or if there is no filter.
return std::all_of(m_filters.begin(), m_filters.end(),
[&sourceIndex, &proxyModel] (Filter* filter) {
return filter->filterAcceptsRow(sourceIndex, proxyModel);
}
);
}
void registerFilterTypes() {
qmlRegisterUncreatableType<Filter>("SortFilterProxyModel", 0, 2, "Filter", "Filter is an abstract class");
qmlRegisterType<ValueFilter>("SortFilterProxyModel", 0, 2, "ValueFilter");
qmlRegisterType<IndexFilter>("SortFilterProxyModel", 0, 2, "IndexFilter");
qmlRegisterType<RegExpFilter>("SortFilterProxyModel", 0, 2, "RegExpFilter");
qmlRegisterType<RangeFilter>("SortFilterProxyModel", 0, 2, "RangeFilter");
qmlRegisterType<ExpressionFilter>("SortFilterProxyModel", 0, 2, "ExpressionFilter");
qmlRegisterType<AnyOfFilter>("SortFilterProxyModel", 0, 2, "AnyOf");
qmlRegisterType<AllOfFilter>("SortFilterProxyModel", 0, 2, "AllOf");
}
Q_COREAPP_STARTUP_FUNCTION(registerFilterTypes)
}

294
filter.h
View File

@ -1,294 +0,0 @@
#ifndef FILTER_H
#define FILTER_H
#include <QObject>
#include <QQmlExpression>
#include <QQmlListProperty>
#include <qqml.h>
namespace qqsfpm {
class Filter;
class QQmlSortFilterProxyModel;
class FilterContainer {
public:
virtual ~FilterContainer();
QList<Filter*> filters() const;
void appendFilter(Filter* filter);
void removeFilter(Filter* filter);
void clearFilters();
QQmlListProperty<Filter> filtersListProperty();
protected:
QList<Filter*> m_filters;
private:
virtual void onFilterAppended(Filter* filter) = 0;
virtual void onFilterRemoved(Filter* filter) = 0;
virtual void onFiltersCleared() = 0;
static void append_filter(QQmlListProperty<Filter>* list, Filter* filter);
static int count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, int index);
static void clear_filters(QQmlListProperty<Filter>* list);
};
}
#define FilterContainer_iid "fr.grecko.SortFilterProxyModel.FilterContainer"
Q_DECLARE_INTERFACE(qqsfpm::FilterContainer, FilterContainer_iid)
namespace qqsfpm {
class Filter : public QObject
{
Q_OBJECT
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
Q_PROPERTY(bool inverted READ inverted WRITE setInverted NOTIFY invertedChanged)
public:
explicit Filter(QObject *parent = nullptr);
virtual ~Filter() = default;
bool enabled() const;
void setEnabled(bool enabled);
bool inverted() const;
void setInverted(bool inverted);
bool filterAcceptsRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const;
virtual void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel);
Q_SIGNALS:
void enabledChanged();
void invertedChanged();
void invalidated();
protected:
virtual bool filterRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const = 0;
void invalidate();
private:
bool m_enabled = true;
bool m_inverted = false;
};
class RoleFilter : public Filter
{
Q_OBJECT
Q_PROPERTY(QString roleName READ roleName WRITE setRoleName NOTIFY roleNameChanged)
public:
using Filter::Filter;
const QString& roleName() const;
void setRoleName(const QString& roleName);
Q_SIGNALS:
void roleNameChanged();
protected:
QVariant sourceData(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const;
private:
QString m_roleName;
};
class ValueFilter : public RoleFilter {
Q_OBJECT
Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
public:
using RoleFilter::RoleFilter;
const QVariant& value() const;
void setValue(const QVariant& value);
protected:
bool filterRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void valueChanged();
private:
QVariant m_value;
};
class IndexFilter: public Filter {
Q_OBJECT
Q_PROPERTY(QVariant minimumIndex READ minimumIndex WRITE setMinimumIndex NOTIFY minimumIndexChanged)
Q_PROPERTY(QVariant maximumIndex READ maximumIndex WRITE setMaximumIndex NOTIFY maximumIndexChanged)
public:
using Filter::Filter;
const QVariant& minimumIndex() const;
void setMinimumIndex(const QVariant& minimumIndex);
const QVariant& maximumIndex() const;
void setMaximumIndex(const QVariant& maximumIndex);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void minimumIndexChanged();
void maximumIndexChanged();
private:
QVariant m_minimumIndex;
QVariant m_maximumIndex;
};
class RegExpFilter : public RoleFilter {
Q_OBJECT
Q_PROPERTY(QString pattern READ pattern WRITE setPattern NOTIFY patternChanged)
Q_PROPERTY(PatternSyntax syntax READ syntax WRITE setSyntax NOTIFY syntaxChanged)
Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity NOTIFY caseSensitivityChanged)
public:
enum PatternSyntax {
RegExp = QRegExp::RegExp,
Wildcard = QRegExp::Wildcard,
FixedString = QRegExp::FixedString,
RegExp2 = QRegExp::RegExp2,
WildcardUnix = QRegExp::WildcardUnix,
W3CXmlSchema11 = QRegExp::W3CXmlSchema11 };
Q_ENUMS(PatternSyntax)
using RoleFilter::RoleFilter;
QString pattern() const;
void setPattern(const QString& pattern);
PatternSyntax syntax() const;
void setSyntax(PatternSyntax syntax);
Qt::CaseSensitivity caseSensitivity() const;
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void patternChanged();
void syntaxChanged();
void caseSensitivityChanged();
private:
QRegExp m_regExp;
Qt::CaseSensitivity m_caseSensitivity = m_regExp.caseSensitivity();
PatternSyntax m_syntax = static_cast<PatternSyntax>(m_regExp.patternSyntax());
QString m_pattern = m_regExp.pattern();
};
class RangeFilter : public RoleFilter
{
Q_OBJECT
Q_PROPERTY(QVariant minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged)
Q_PROPERTY(bool minimumInclusive READ minimumInclusive WRITE setMinimumInclusive NOTIFY minimumInclusiveChanged)
Q_PROPERTY(QVariant maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged)
Q_PROPERTY(bool maximumInclusive READ maximumInclusive WRITE setMaximumInclusive NOTIFY maximumInclusiveChanged)
public:
using RoleFilter::RoleFilter;
QVariant minimumValue() const;
void setMinimumValue(QVariant minimumValue);
bool minimumInclusive() const;
void setMinimumInclusive(bool minimumInclusive);
QVariant maximumValue() const;
void setMaximumValue(QVariant maximumValue);
bool maximumInclusive() const;
void setMaximumInclusive(bool maximumInclusive);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void minimumValueChanged();
void minimumInclusiveChanged();
void maximumValueChanged();
void maximumInclusiveChanged();
private:
QVariant m_minimumValue;
bool m_minimumInclusive = true;
QVariant m_maximumValue;
bool m_maximumInclusive = true;
};
class ExpressionFilter : public Filter
{
Q_OBJECT
Q_PROPERTY(QQmlScriptString expression READ expression WRITE setExpression NOTIFY expressionChanged)
public:
using Filter::Filter;
const QQmlScriptString& expression() const;
void setExpression(const QQmlScriptString& scriptString);
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void expressionChanged();
private:
void updateContext(const QQmlSortFilterProxyModel& proxyModel);
void updateExpression();
QQmlScriptString m_scriptString;
QQmlExpression* m_expression = nullptr;
QQmlContext* m_context = nullptr;
};
class FilterContainerFilter : public Filter, public FilterContainer {
Q_OBJECT
Q_INTERFACES(qqsfpm::FilterContainer)
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filtersListProperty NOTIFY filtersChanged)
Q_CLASSINFO("DefaultProperty", "filters")
public:
using Filter::Filter;
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
Q_SIGNALS:
void filtersChanged();
private:
void onFilterAppended(Filter* filter) override;
void onFilterRemoved(Filter* filter) override;
void onFiltersCleared() override;
};
class AnyOfFilter : public FilterContainerFilter {
Q_OBJECT
public:
using FilterContainerFilter::FilterContainerFilter;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
};
class AllOfFilter : public FilterContainerFilter {
Q_OBJECT
public:
using FilterContainerFilter::FilterContainerFilter;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
};
}
#endif // FILTER_H

25
filters/alloffilter.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "alloffilter.h"
namespace qqsfpm {
/*!
\qmltype AllOf
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filter container accepting rows accepted by all its child filters
The AllOf type is a \l Filter container that accepts rows if all of its contained (and enabled) filters accept them, or if it has no filter.
Using it as a top level filter has the same effect as putting all its child filters as top level filters. It can however be usefull to use an AllOf filter when nested in an AnyOf filter.
*/
bool AllOfFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
//return true if all filters return false, or if there is no filter.
return std::all_of(m_filters.begin(), m_filters.end(),
[&sourceIndex, &proxyModel] (Filter* filter) {
return filter->filterAcceptsRow(sourceIndex, proxyModel);
}
);
}
}

20
filters/alloffilter.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef ALLOFFILTER_H
#define ALLOFFILTER_H
#include "filtercontainerfilter.h"
namespace qqsfpm {
class AllOfFilter : public FilterContainerFilter {
Q_OBJECT
public:
using FilterContainerFilter::FilterContainerFilter;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
};
}
#endif // ALLOFFILTER_H

46
filters/anyoffilter.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "anyoffilter.h"
namespace qqsfpm {
/*!
\qmltype AnyOf
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filter container accepting rows accepted by at least one of its child filters
The AnyOf type is a \l Filter container that accepts rows if any of its contained (and enabled) filters accept them.
In the following example, only the rows where the \c firstName role or the \c lastName role match the text entered in the \c nameTextField will be accepted :
\code
TextField {
id: nameTextField
}
SortFilterProxyModel {
sourceModel: contactModel
filters: AnyOf {
RegExpFilter {
roleName: "lastName"
pattern: nameTextField.text
caseSensitivity: Qt.CaseInsensitive
}
RegExpFilter {
roleName: "firstName"
pattern: nameTextField.text
caseSensitivity: Qt.CaseInsensitive
}
}
}
\endcode
*/
bool AnyOfFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
//return true if any of the enabled filters return true
return std::any_of(m_filters.begin(), m_filters.end(),
[&sourceIndex, &proxyModel] (Filter* filter) {
return filter->enabled() && filter->filterAcceptsRow(sourceIndex, proxyModel);
}
);
}
}

20
filters/anyoffilter.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef ANYOFFILTER_H
#define ANYOFFILTER_H
#include "filtercontainerfilter.h"
namespace qqsfpm {
class AnyOfFilter : public FilterContainerFilter {
Q_OBJECT
public:
using FilterContainerFilter::FilterContainerFilter;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
};
}
#endif // ANYOFFILTER_H

View File

@ -0,0 +1,124 @@
#include "expressionfilter.h"
#include "qqmlsortfilterproxymodel.h"
#include <QtQml>
namespace qqsfpm {
/*!
\qmltype ExpressionFilter
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filters row with a custom filtering
An ExpressionFilter is a \l Filter allowing to implement custom filtering based on a javascript expression.
*/
/*!
\qmlproperty expression ExpressionFilter::expression
An expression to implement custom filtering, it must evaluate to a boolean.
It has the same syntax has a \l {http://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html} {Property Binding} except it will be evaluated for each of the source model's rows.
Rows that have their expression evaluating to \c true will be accepted by the model.
Data for each row is exposed like for a delegate of a QML View.
This expression is reevaluated for a row every time its model data changes.
When an external property (not \c index or in \c model) the expression depends on changes, the expression is reevaluated for every row of the source model.
To capture the properties the expression depends on, the expression is first executed with invalid data and each property access is detected by the QML engine.
This means that if a property is not accessed because of a conditional, it won't be captured and the expression won't be reevaluted when this property changes.
A workaround to this problem is to access all the properties the expressions depends unconditionally at the beggining of the expression.
*/
const QQmlScriptString& ExpressionFilter::expression() const
{
return m_scriptString;
}
void ExpressionFilter::setExpression(const QQmlScriptString& scriptString)
{
if (m_scriptString == scriptString)
return;
m_scriptString = scriptString;
updateExpression();
Q_EMIT expressionChanged();
invalidate();
}
void ExpressionFilter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
updateContext(proxyModel);
}
bool ExpressionFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
if (!m_scriptString.isEmpty()) {
QVariantMap modelMap;
QHash<int, QByteArray> roles = proxyModel.roleNames();
QQmlContext context(qmlContext(this));
auto addToContext = [&] (const QString &name, const QVariant& value) {
context.setContextProperty(name, value);
modelMap.insert(name, value);
};
for (auto it = roles.cbegin(); it != roles.cend(); ++it)
addToContext(it.value(), proxyModel.sourceData(sourceIndex, it.key()));
addToContext("index", sourceIndex.row());
context.setContextProperty("model", modelMap);
QQmlExpression expression(m_scriptString, &context);
QVariant variantResult = expression.evaluate();
if (expression.hasError()) {
qWarning() << expression.error();
return true;
}
if (variantResult.canConvert<bool>()) {
return variantResult.toBool();
} else {
qWarning("%s:%i:%i : Can't convert result to bool",
expression.sourceFile().toUtf8().data(),
expression.lineNumber(),
expression.columnNumber());
return true;
}
}
return true;
}
void ExpressionFilter::updateContext(const QQmlSortFilterProxyModel& proxyModel)
{
delete m_context;
m_context = new QQmlContext(qmlContext(this), this);
// what about roles changes ?
QVariantMap modelMap;
auto addToContext = [&] (const QString &name, const QVariant& value) {
m_context->setContextProperty(name, value);
modelMap.insert(name, value);
};
for (const QByteArray& roleName : proxyModel.roleNames().values())
addToContext(roleName, QVariant());
addToContext("index", -1);
m_context->setContextProperty("model", modelMap);
updateExpression();
}
void ExpressionFilter::updateExpression()
{
if (!m_context)
return;
delete m_expression;
m_expression = new QQmlExpression(m_scriptString, m_context, 0, this);
connect(m_expression, &QQmlExpression::valueChanged, this, &ExpressionFilter::invalidate);
m_expression->setNotifyOnValueChanged(true);
m_expression->evaluate();
}
}

View File

@ -0,0 +1,41 @@
#ifndef EXPRESSIONFILTER_H
#define EXPRESSIONFILTER_H
#include "filter.h"
#include <QQmlScriptString>
class QQmlExpression;
namespace qqsfpm {
class ExpressionFilter : public Filter
{
Q_OBJECT
Q_PROPERTY(QQmlScriptString expression READ expression WRITE setExpression NOTIFY expressionChanged)
public:
using Filter::Filter;
const QQmlScriptString& expression() const;
void setExpression(const QQmlScriptString& scriptString);
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void expressionChanged();
private:
void updateContext(const QQmlSortFilterProxyModel& proxyModel);
void updateExpression();
QQmlScriptString m_scriptString;
QQmlExpression* m_expression = nullptr;
QQmlContext* m_context = nullptr;
};
}
#endif // EXPRESSIONFILTER_H

83
filters/filter.cpp Normal file
View File

@ -0,0 +1,83 @@
#include "filter.h"
#include "qqmlsortfilterproxymodel.h"
namespace qqsfpm {
/*!
\qmltype Filter
\inqmlmodule SortFilterProxyModel
\brief Base type for the \l SortFilterProxyModel filters
The Filter type cannot be used directly in a QML file.
It exists to provide a set of common properties and methods,
available across all the other filter types that inherit from it.
Attempting to use the Filter type directly will result in an error.
*/
Filter::Filter(QObject *parent) : QObject(parent)
{
}
/*!
\qmlproperty bool Filter::enabled
This property holds whether the filter is enabled.
A disabled filter will accept every rows unconditionally (even if it's inverted).
By default, filters are enabled.
*/
bool Filter::enabled() const
{
return m_enabled;
}
void Filter::setEnabled(bool enabled)
{
if (m_enabled == enabled)
return;
m_enabled = enabled;
Q_EMIT enabledChanged();
Q_EMIT invalidated();
}
/*!
\qmlproperty bool Filter::inverted
This property holds whether the filter is inverted.
When a filter is inverted, a row normally accepted would be rejected, and vice-versa.
By default, filters are not inverted.
*/
bool Filter::inverted() const
{
return m_inverted;
}
void Filter::setInverted(bool inverted)
{
if (m_inverted == inverted)
return;
m_inverted = inverted;
Q_EMIT invertedChanged();
invalidate();
}
bool Filter::filterAcceptsRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return !m_enabled || filterRow(sourceIndex, proxyModel) ^ m_inverted;
}
void Filter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
Q_UNUSED(proxyModel)
}
void Filter::invalidate()
{
if (m_enabled)
Q_EMIT invalidated();
}
}

46
filters/filter.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef FILTER_H
#define FILTER_H
#include <QObject>
namespace qqsfpm {
class QQmlSortFilterProxyModel;
class Filter : public QObject
{
Q_OBJECT
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
Q_PROPERTY(bool inverted READ inverted WRITE setInverted NOTIFY invertedChanged)
public:
explicit Filter(QObject *parent = nullptr);
virtual ~Filter() = default;
bool enabled() const;
void setEnabled(bool enabled);
bool inverted() const;
void setInverted(bool inverted);
bool filterAcceptsRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const;
virtual void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel);
Q_SIGNALS:
void enabledChanged();
void invertedChanged();
void invalidated();
protected:
virtual bool filterRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const = 0;
void invalidate();
private:
bool m_enabled = true;
bool m_inverted = false;
};
}
#endif // FILTER_H

View File

@ -0,0 +1,69 @@
#include "filtercontainer.h"
namespace qqsfpm {
FilterContainer::~FilterContainer()
{
}
QList<Filter*> FilterContainer::filters() const
{
return m_filters;
}
void FilterContainer::appendFilter(Filter* filter)
{
m_filters.append(filter);
onFilterAppended(filter);
}
void FilterContainer::removeFilter(Filter* filter)
{
m_filters.removeOne(filter);
onFilterRemoved(filter);
}
void FilterContainer::clearFilters()
{
m_filters.clear();
onFiltersCleared();
}
QQmlListProperty<Filter> FilterContainer::filtersListProperty()
{
return QQmlListProperty<Filter>(reinterpret_cast<QObject*>(this), &m_filters,
&FilterContainer::append_filter,
&FilterContainer::count_filter,
&FilterContainer::at_filter,
&FilterContainer::clear_filters);
}
void FilterContainer::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
{
if (!filter)
return;
FilterContainer* that = reinterpret_cast<FilterContainer*>(list->object);
that->appendFilter(filter);
}
int FilterContainer::count_filter(QQmlListProperty<Filter>* list)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
}
Filter* FilterContainer::at_filter(QQmlListProperty<Filter>* list, int index)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
}
void FilterContainer::clear_filters(QQmlListProperty<Filter> *list)
{
FilterContainer* that = reinterpret_cast<FilterContainer*>(list->object);
that->clearFilters();
}
}

42
filters/filtercontainer.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef FILTERCONTAINER_H
#define FILTERCONTAINER_H
#include <QList>
#include <QQmlListProperty>
namespace qqsfpm {
class Filter;
class QQmlSortFilterProxyModel;
class FilterContainer {
public:
virtual ~FilterContainer();
QList<Filter*> filters() const;
void appendFilter(Filter* filter);
void removeFilter(Filter* filter);
void clearFilters();
QQmlListProperty<Filter> filtersListProperty();
protected:
QList<Filter*> m_filters;
private:
virtual void onFilterAppended(Filter* filter) = 0;
virtual void onFilterRemoved(Filter* filter) = 0;
virtual void onFiltersCleared() = 0;
static void append_filter(QQmlListProperty<Filter>* list, Filter* filter);
static int count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, int index);
static void clear_filters(QQmlListProperty<Filter>* list);
};
}
#define FilterContainer_iid "fr.grecko.SortFilterProxyModel.FilterContainer"
Q_DECLARE_INTERFACE(qqsfpm::FilterContainer, FilterContainer_iid)
#endif // FILTERCONTAINER_H

View File

@ -0,0 +1,28 @@
#include "filtercontainerfilter.h"
namespace qqsfpm {
void FilterContainerFilter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
for (Filter* filter : m_filters)
filter->proxyModelCompleted(proxyModel);
}
void FilterContainerFilter::onFilterAppended(Filter* filter)
{
connect(filter, &Filter::invalidated, this, &FilterContainerFilter::invalidate);
invalidate();
}
void FilterContainerFilter::onFilterRemoved(Filter* filter)
{
Q_UNUSED(filter)
invalidate();
}
void qqsfpm::FilterContainerFilter::onFiltersCleared()
{
invalidate();
}
}

View File

@ -0,0 +1,31 @@
#ifndef FILTERCONTAINERFILTER_H
#define FILTERCONTAINERFILTER_H
#include "filter.h"
#include "filtercontainer.h"
namespace qqsfpm {
class FilterContainerFilter : public Filter, public FilterContainer {
Q_OBJECT
Q_INTERFACES(qqsfpm::FilterContainer)
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filtersListProperty NOTIFY filtersChanged)
Q_CLASSINFO("DefaultProperty", "filters")
public:
using Filter::Filter;
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
Q_SIGNALS:
void filtersChanged();
private:
void onFilterAppended(Filter* filter) override;
void onFilterRemoved(Filter* filter) override;
void onFiltersCleared() override;
};
}
#endif // FILTERCONTAINERFILTER_H

View File

@ -0,0 +1,27 @@
#include "filter.h"
#include "valuefilter.h"
#include "indexfilter.h"
#include "regexpfilter.h"
#include "rangefilter.h"
#include "expressionfilter.h"
#include "anyoffilter.h"
#include "alloffilter.h"
#include <QQmlEngine>
#include <QCoreApplication>
namespace qqsfpm {
void registerFiltersTypes() {
qmlRegisterUncreatableType<Filter>("SortFilterProxyModel", 0, 2, "Filter", "Filter is an abstract class");
qmlRegisterType<ValueFilter>("SortFilterProxyModel", 0, 2, "ValueFilter");
qmlRegisterType<IndexFilter>("SortFilterProxyModel", 0, 2, "IndexFilter");
qmlRegisterType<RegExpFilter>("SortFilterProxyModel", 0, 2, "RegExpFilter");
qmlRegisterType<RangeFilter>("SortFilterProxyModel", 0, 2, "RangeFilter");
qmlRegisterType<ExpressionFilter>("SortFilterProxyModel", 0, 2, "ExpressionFilter");
qmlRegisterType<AnyOfFilter>("SortFilterProxyModel", 0, 2, "AnyOf");
qmlRegisterType<AllOfFilter>("SortFilterProxyModel", 0, 2, "AllOf");
}
Q_COREAPP_STARTUP_FUNCTION(registerFiltersTypes)
}

103
filters/indexfilter.cpp Normal file
View File

@ -0,0 +1,103 @@
#include "indexfilter.h"
#include "qqmlsortfilterproxymodel.h"
namespace qqsfpm {
/*!
\qmltype IndexFilter
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filters rows based on their source index
An IndexFilter is a filter allowing contents to be filtered based on their source model index.
In the following example, only the first row of the source model will be accepted:
\code
SortFilterProxyModel {
sourceModel: contactModel
filters: IndexFilter {
maximumIndex: 0
}
}
\endcode
*/
/*!
\qmlproperty int IndexFilter::minimumIndex
This property holds the minimumIndex of the filter.
Rows with a source index lower than \c minimumIndex will be rejected.
If \c minimumIndex is negative, it is counted from the end of the source model, meaning that :
\code minimumIndex: -1\endcode
is equivalent to :
\code minimumIndex: sourceModel.count - 1\endcode
By default, no value is set.
*/
const QVariant& IndexFilter::minimumIndex() const
{
return m_minimumIndex;
}
void IndexFilter::setMinimumIndex(const QVariant& minimumIndex)
{
if (m_minimumIndex == minimumIndex)
return;
m_minimumIndex = minimumIndex;
Q_EMIT minimumIndexChanged();
invalidate();
}
/*!
\qmlproperty int IndexFilter::maximumIndex
This property holds the maximumIndex of the filter.
Rows with a source index higher than \c maximumIndex will be rejected.
If \c maximumIndex is negative, it is counted from the end of the source model, meaning that:
\code maximumIndex: -1\endcode
is equivalent to :
\code maximumIndex: sourceModel.count - 1\endcode
By default, no value is set.
*/
const QVariant& IndexFilter::maximumIndex() const
{
return m_maximumIndex;
}
void IndexFilter::setMaximumIndex(const QVariant& maximumIndex)
{
if (m_maximumIndex == maximumIndex)
return;
m_maximumIndex = maximumIndex;
Q_EMIT maximumIndexChanged();
invalidate();
}
bool IndexFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
int sourceRowCount = proxyModel.sourceModel()->rowCount();
int sourceRow = sourceIndex.row();
bool minimumIsValid;
int minimum = m_minimumIndex.toInt(&minimumIsValid);
if (minimumIsValid) {
int actualMinimum = minimum < 0 ? sourceRowCount + minimum : minimum;
if (sourceRow < actualMinimum)
return false;
}
bool maximumIsValid;
int maximum = m_maximumIndex.toInt(&maximumIsValid);
if (maximumIsValid) {
int actualMaximum = maximum < 0 ? sourceRowCount + maximum : maximum;
if (sourceRow > actualMaximum)
return false;
}
return true;
}
}

37
filters/indexfilter.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef INDEXFILTER_H
#define INDEXFILTER_H
#include "filter.h"
#include <QVariant>
namespace qqsfpm {
class IndexFilter: public Filter {
Q_OBJECT
Q_PROPERTY(QVariant minimumIndex READ minimumIndex WRITE setMinimumIndex NOTIFY minimumIndexChanged)
Q_PROPERTY(QVariant maximumIndex READ maximumIndex WRITE setMaximumIndex NOTIFY maximumIndexChanged)
public:
using Filter::Filter;
const QVariant& minimumIndex() const;
void setMinimumIndex(const QVariant& minimumIndex);
const QVariant& maximumIndex() const;
void setMaximumIndex(const QVariant& maximumIndex);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void minimumIndexChanged();
void maximumIndexChanged();
private:
QVariant m_minimumIndex;
QVariant m_maximumIndex;
};
}
#endif // INDEXFILTER_H

138
filters/rangefilter.cpp Normal file
View File

@ -0,0 +1,138 @@
#include "rangefilter.h"
namespace qqsfpm {
/*!
\qmltype RangeFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows between boundary values
A RangeFilter is a \l RoleFilter that accepts rows if their data is between the filter's minimum and maximum value.
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 :
\code
RangeSlider {
id: priceRangeSlider
}
SortFilterProxyModel {
sourceModel: priceModel
filters: RangeFilter {
roleName: "price"
minimumValue: priceRangeSlider.first.value
maximumValue: priceRangeSlider.second.value
}
}
\endcode
*/
/*!
\qmlproperty int RangeFilter::minimumValue
This property holds the minimumValue of the filter.
Rows with a value lower than \c minimumValue will be rejected.
By default, no value is set.
\sa minimumInclusive
*/
QVariant RangeFilter::minimumValue() const
{
return m_minimumValue;
}
void RangeFilter::setMinimumValue(QVariant minimumValue)
{
if (m_minimumValue == minimumValue)
return;
m_minimumValue = minimumValue;
Q_EMIT minimumValueChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::minimumInclusive
This property holds whether the \l minimumValue is inclusive.
By default, the \l minimumValue is inclusive.
\sa minimumValue
*/
bool RangeFilter::minimumInclusive() const
{
return m_minimumInclusive;
}
void RangeFilter::setMinimumInclusive(bool minimumInclusive)
{
if (m_minimumInclusive == minimumInclusive)
return;
m_minimumInclusive = minimumInclusive;
Q_EMIT minimumInclusiveChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::maximumValue
This property holds the maximumValue of the filter.
Rows with a value higher than \c maximumValue will be rejected.
By default, no value is set.
\sa maximumInclusive
*/
QVariant RangeFilter::maximumValue() const
{
return m_maximumValue;
}
void RangeFilter::setMaximumValue(QVariant maximumValue)
{
if (m_maximumValue == maximumValue)
return;
m_maximumValue = maximumValue;
Q_EMIT maximumValueChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::maximumInclusive
This property holds whether the \l minimumValue is inclusive.
By default, the \l minimumValue is inclusive.
\sa minimumValue
*/
bool RangeFilter::maximumInclusive() const
{
return m_maximumInclusive;
}
void RangeFilter::setMaximumInclusive(bool maximumInclusive)
{
if (m_maximumInclusive == maximumInclusive)
return;
m_maximumInclusive = maximumInclusive;
Q_EMIT maximumInclusiveChanged();
invalidate();
}
bool RangeFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
QVariant value = sourceData(sourceIndex, proxyModel);
bool lessThanMin = m_minimumValue.isValid() &&
m_minimumInclusive ? value < m_minimumValue : value <= m_minimumValue;
bool moreThanMax = m_maximumValue.isValid() &&
m_maximumInclusive ? value > m_maximumValue : value >= m_maximumValue;
return !(lessThanMin || moreThanMax);
}
}

48
filters/rangefilter.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef RANGEFILTER_H
#define RANGEFILTER_H
#include "rolefilter.h"
#include <QVariant>
namespace qqsfpm {
class RangeFilter : public RoleFilter
{
Q_OBJECT
Q_PROPERTY(QVariant minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged)
Q_PROPERTY(bool minimumInclusive READ minimumInclusive WRITE setMinimumInclusive NOTIFY minimumInclusiveChanged)
Q_PROPERTY(QVariant maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged)
Q_PROPERTY(bool maximumInclusive READ maximumInclusive WRITE setMaximumInclusive NOTIFY maximumInclusiveChanged)
public:
using RoleFilter::RoleFilter;
QVariant minimumValue() const;
void setMinimumValue(QVariant minimumValue);
bool minimumInclusive() const;
void setMinimumInclusive(bool minimumInclusive);
QVariant maximumValue() const;
void setMaximumValue(QVariant maximumValue);
bool maximumInclusive() const;
void setMaximumInclusive(bool maximumInclusive);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void minimumValueChanged();
void minimumInclusiveChanged();
void maximumValueChanged();
void maximumInclusiveChanged();
private:
QVariant m_minimumValue;
bool m_minimumInclusive = true;
QVariant m_maximumValue;
bool m_maximumInclusive = true;
};
}
#endif // RANGEFILTER_H

112
filters/regexpfilter.cpp Normal file
View File

@ -0,0 +1,112 @@
#include "regexpfilter.h"
#include <QVariant>
namespace qqsfpm {
/*!
\qmltype RegExpFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows matching a regular expression
A RegExpFilter is a \l RoleFilter that accepts rows matching a regular rexpression.
In the following example, only rows with their \c lastName role beggining with the content of textfield the will be accepted:
\code
TextField {
id: nameTextField
}
SortFilterProxyModel {
sourceModel: contactModel
filters: RegExpFilter {
roleName: "lastName"
pattern: "^" + nameTextField.displayText
}
}
\endcode
*/
/*!
\qmlproperty bool RegExpFilter::pattern
The pattern used to filter the contents of the source model.
\sa syntax
*/
QString RegExpFilter::pattern() const
{
return m_pattern;
}
void RegExpFilter::setPattern(const QString& pattern)
{
if (m_pattern == pattern)
return;
m_pattern = pattern;
m_regExp.setPattern(pattern);
Q_EMIT patternChanged();
invalidate();
}
/*!
\qmlproperty enum RegExpFilter::syntax
The pattern used to filter the contents of the source model.
Only the source model's value having their \l roleName data matching this \l pattern with the specified \l syntax will be kept.
\value RegExpFilter.RegExp A rich Perl-like pattern matching syntax. This is the default.
\value RegExpFilter.Wildcard This provides a simple pattern matching syntax similar to that used by shells (command interpreters) for "file globbing".
\value RegExpFilter.FixedString The pattern is a fixed string. This is equivalent to using the RegExp pattern on a string in which all metacharacters are escaped.
\value RegExpFilter.RegExp2 Like RegExp, but with greedy quantifiers.
\value RegExpFilter.WildcardUnix This is similar to Wildcard but with the behavior of a Unix shell. The wildcard characters can be escaped with the character "\".
\value RegExpFilter.W3CXmlSchema11 The pattern is a regular expression as defined by the W3C XML Schema 1.1 specification.
\sa pattern
*/
RegExpFilter::PatternSyntax RegExpFilter::syntax() const
{
return m_syntax;
}
void RegExpFilter::setSyntax(RegExpFilter::PatternSyntax syntax)
{
if (m_syntax == syntax)
return;
m_syntax = syntax;
m_regExp.setPatternSyntax(static_cast<QRegExp::PatternSyntax>(syntax));
Q_EMIT syntaxChanged();
invalidate();
}
/*!
\qmlproperty Qt::CaseSensitivity RegExpFilter::caseSensitivity
This property holds the caseSensitivity of the filter.
*/
Qt::CaseSensitivity RegExpFilter::caseSensitivity() const
{
return m_caseSensitivity;
}
void RegExpFilter::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
{
if (m_caseSensitivity == caseSensitivity)
return;
m_caseSensitivity = caseSensitivity;
m_regExp.setCaseSensitivity(caseSensitivity);
Q_EMIT caseSensitivityChanged();
invalidate();
}
bool RegExpFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
QString string = sourceData(sourceIndex, proxyModel).toString();
return m_regExp.indexIn(string) != -1;
}
}

52
filters/regexpfilter.h Normal file
View File

@ -0,0 +1,52 @@
#ifndef REGEXPFILTER_H
#define REGEXPFILTER_H
#include "rolefilter.h"
namespace qqsfpm {
class RegExpFilter : public RoleFilter {
Q_OBJECT
Q_PROPERTY(QString pattern READ pattern WRITE setPattern NOTIFY patternChanged)
Q_PROPERTY(PatternSyntax syntax READ syntax WRITE setSyntax NOTIFY syntaxChanged)
Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity NOTIFY caseSensitivityChanged)
public:
enum PatternSyntax {
RegExp = QRegExp::RegExp,
Wildcard = QRegExp::Wildcard,
FixedString = QRegExp::FixedString,
RegExp2 = QRegExp::RegExp2,
WildcardUnix = QRegExp::WildcardUnix,
W3CXmlSchema11 = QRegExp::W3CXmlSchema11 };
Q_ENUMS(PatternSyntax)
using RoleFilter::RoleFilter;
QString pattern() const;
void setPattern(const QString& pattern);
PatternSyntax syntax() const;
void setSyntax(PatternSyntax syntax);
Qt::CaseSensitivity caseSensitivity() const;
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void patternChanged();
void syntaxChanged();
void caseSensitivityChanged();
private:
QRegExp m_regExp;
Qt::CaseSensitivity m_caseSensitivity = m_regExp.caseSensitivity();
PatternSyntax m_syntax = static_cast<PatternSyntax>(m_regExp.patternSyntax());
QString m_pattern = m_regExp.pattern();
};
}
#endif // REGEXPFILTER_H

44
filters/rolefilter.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "rolefilter.h"
#include "qqmlsortfilterproxymodel.h"
namespace qqsfpm {
/*!
\qmltype RoleFilter
\qmlabstract
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Base type for filters based on a source model role
The RoleFilter type cannot be used directly in a QML file.
It exists to provide a set of common properties and methods,
available across all the other filter types that inherit from it.
Attempting to use the RoleFilter type directly will result in an error.
*/
/*!
\qmlproperty string RoleFilter::roleName
This property holds the role name that the filter is using to query the source model's data when filtering items.
*/
const QString& RoleFilter::roleName() const
{
return m_roleName;
}
void RoleFilter::setRoleName(const QString& roleName)
{
if (m_roleName == roleName)
return;
m_roleName = roleName;
Q_EMIT roleNameChanged();
invalidate();
}
QVariant RoleFilter::sourceData(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return proxyModel.sourceData(sourceIndex, m_roleName);
}
}

31
filters/rolefilter.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef ROLEFILTER_H
#define ROLEFILTER_H
#include "filter.h"
namespace qqsfpm {
class RoleFilter : public Filter
{
Q_OBJECT
Q_PROPERTY(QString roleName READ roleName WRITE setRoleName NOTIFY roleNameChanged)
public:
using Filter::Filter;
const QString& roleName() const;
void setRoleName(const QString& roleName);
Q_SIGNALS:
void roleNameChanged();
protected:
QVariant sourceData(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const;
private:
QString m_roleName;
};
}
#endif // ROLEFILTER_H

56
filters/valuefilter.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "valuefilter.h"
namespace qqsfpm {
/*!
\qmltype ValueFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows matching exactly a value
A ValueFilter is a simple \l RoleFilter that accepts rows matching exactly the filter's value
In the following example, only rows with their \c favorite role set to \c true will be accepted when the checkbox is checked :
\code
CheckBox {
id: showOnlyFavoriteCheckBox
}
SortFilterProxyModel {
sourceModel: contactModel
filters: ValueFilter {
roleName: "favorite"
value: true
enabled: showOnlyFavoriteCheckBox.checked
}
}
\endcode
*/
/*!
\qmlproperty variant ValueFilter::value
This property holds the value used to filter the contents of the source model.
*/
const QVariant &ValueFilter::value() const
{
return m_value;
}
void ValueFilter::setValue(const QVariant& value)
{
if (m_value == value)
return;
m_value = value;
Q_EMIT valueChanged();
invalidate();
}
bool ValueFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return !m_value.isValid() || m_value == sourceData(sourceIndex, proxyModel);
}
}

31
filters/valuefilter.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef VALUEFILTER_H
#define VALUEFILTER_H
#include "rolefilter.h"
#include <QVariant>
namespace qqsfpm {
class ValueFilter : public RoleFilter {
Q_OBJECT
Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
public:
using RoleFilter::RoleFilter;
const QVariant& value() const;
void setValue(const QVariant& value);
protected:
bool filterRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void valueChanged();
private:
QVariant m_value;
};
}
#endif // VALUEFILTER_H

View File

@ -5,7 +5,7 @@
#include <QCoreApplication>
#include <QDebug>
#include <QQmlInfo>
#include "filter.h"
#include "filters/filter.h"
#include "qqmlsortfilterproxymodel.h"
namespace qqsfpm {

View File

@ -6,7 +6,7 @@
#include <QQmlScriptString>
#include <QQmlExpression>
#include <qqml.h>
#include "filter.h"
#include "filters/filtercontainer.h"
namespace qqsfpm {

View File

@ -1,7 +1,7 @@
#include "qqmlsortfilterproxymodel.h"
#include <QtQml>
#include <algorithm>
#include "filter.h"
#include "filters/filter.h"
#include "sorter.h"
#include "proxyrole.h"