Skip to content

Commit

Permalink
Port to Qt 6
Browse files Browse the repository at this point in the history
The PatternSyntax enum had to be removed as QRegularExpression only
supports Perl-compatible regular expressions.

As QVariant's comparison operators were removed, a simplified
comparison is now done as a starting point, but should probably
be extended to support more types.

Fixes #84
Fixes #86
  • Loading branch information
mitchcurtis committed May 14, 2022
1 parent 5a93088 commit fe50203
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 120 deletions.
6 changes: 4 additions & 2 deletions SortFilterProxyModel.pri
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ HEADERS += $$PWD/qqmlsortfilterproxymodel.h \
$$PWD/proxyroles/singlerole.h \
$$PWD/proxyroles/regexprole.h \
$$PWD/sorters/filtersorter.h \
$$PWD/proxyroles/filterrole.h
$$PWD/proxyroles/filterrole.h \
$$PWD/utils/utils.h

SOURCES += $$PWD/qqmlsortfilterproxymodel.cpp \
$$PWD/filters/filter.cpp \
Expand Down Expand Up @@ -57,4 +58,5 @@ SOURCES += $$PWD/qqmlsortfilterproxymodel.cpp \
$$PWD/proxyroles/singlerole.cpp \
$$PWD/proxyroles/regexprole.cpp \
$$PWD/sorters/filtersorter.cpp \
$$PWD/proxyroles/filterrole.cpp
$$PWD/proxyroles/filterrole.cpp \
$$PWD/utils/utils.cpp
2 changes: 2 additions & 0 deletions SortFilterProxyModel.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Group {
"sorters/sortersqmltypes.cpp",
"sorters/stringsorter.cpp",
"sorters/stringsorter.h",
"utils/utils.cpp",
"utils/utils.h",
"qqmlsortfilterproxymodel.cpp",
"qqmlsortfilterproxymodel.h"
]
Expand Down
4 changes: 2 additions & 2 deletions filters/filtercontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ void FilterContainer::append_filter(QQmlListProperty<Filter>* list, Filter* filt
that->appendFilter(filter);
}

int FilterContainer::count_filter(QQmlListProperty<Filter>* list)
qsizetype 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)
Filter* FilterContainer::at_filter(QQmlListProperty<Filter>* list, qsizetype index)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
Expand Down
4 changes: 2 additions & 2 deletions filters/filtercontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class FilterContainer {
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 qsizetype count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, qsizetype index);
static void clear_filters(QQmlListProperty<Filter>* list);
};

Expand Down
3 changes: 2 additions & 1 deletion filters/rangefilter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "rangefilter.h"
#include "../utils/utils.h"

namespace qqsfpm {

Expand Down Expand Up @@ -128,7 +129,7 @@ void RangeFilter::setMaximumInclusive(bool maximumInclusive)

bool RangeFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
QVariant value = sourceData(sourceIndex, 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() &&
Expand Down
47 changes: 12 additions & 35 deletions filters/regexpfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ namespace qqsfpm {
\sa syntax
*/
RegExpFilter::RegExpFilter() :
m_caseSensitivity(m_regExp.patternOptions().testFlag(
QRegularExpression::CaseInsensitiveOption) ? Qt::CaseInsensitive : Qt::CaseSensitive)
{
}

QString RegExpFilter::pattern() const
{
return m_pattern;
Expand All @@ -51,38 +57,6 @@ void RegExpFilter::setPattern(const QString& pattern)
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 RoleFilter::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
Expand All @@ -99,15 +73,18 @@ void RegExpFilter::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
return;

m_caseSensitivity = caseSensitivity;
m_regExp.setCaseSensitivity(caseSensitivity);
QRegularExpression::PatternOptions patternOptions = m_regExp.patternOptions();
if (caseSensitivity == Qt::CaseInsensitive)
patternOptions.setFlag(QRegularExpression::CaseInsensitiveOption);
m_regExp.setPatternOptions(patternOptions);
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;
const QString string = sourceData(sourceIndex, proxyModel).toString();
return m_regExp.match(string).hasMatch();
}

}
23 changes: 6 additions & 17 deletions filters/regexpfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@

#include "rolefilter.h"

#include <QRegularExpression>

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;

RegExpFilter();

QString pattern() const;
void setPattern(const QString& pattern);

PatternSyntax syntax() const;
void setSyntax(PatternSyntax syntax);

Qt::CaseSensitivity caseSensitivity() const;
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity);

Expand All @@ -37,13 +28,11 @@ class RegExpFilter : public RoleFilter {

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());
QRegularExpression m_regExp;
Qt::CaseSensitivity m_caseSensitivity;
QString m_pattern = m_regExp.pattern();
};

Expand Down
4 changes: 2 additions & 2 deletions proxyroles/proxyrolecontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ void ProxyRoleContainer::append_proxyRole(QQmlListProperty<ProxyRole>* list, Pro
that->appendProxyRole(proxyRole);
}

int ProxyRoleContainer::count_proxyRole(QQmlListProperty<ProxyRole>* list)
qsizetype ProxyRoleContainer::count_proxyRole(QQmlListProperty<ProxyRole>* list)
{
QList<ProxyRole*>* ProxyRoles = static_cast<QList<ProxyRole*>*>(list->data);
return ProxyRoles->count();
}

ProxyRole* ProxyRoleContainer::at_proxyRole(QQmlListProperty<ProxyRole>* list, int index)
ProxyRole* ProxyRoleContainer::at_proxyRole(QQmlListProperty<ProxyRole>* list, qsizetype index)
{
QList<ProxyRole*>* ProxyRoles = static_cast<QList<ProxyRole*>*>(list->data);
return ProxyRoles->at(index);
Expand Down
4 changes: 2 additions & 2 deletions proxyroles/proxyrolecontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class ProxyRoleContainer {
virtual void onProxyRolesCleared() = 0;

static void append_proxyRole(QQmlListProperty<ProxyRole>* list, ProxyRole* proxyRole);
static int count_proxyRole(QQmlListProperty<ProxyRole>* list);
static ProxyRole* at_proxyRole(QQmlListProperty<ProxyRole>* list, int index);
static qsizetype count_proxyRole(QQmlListProperty<ProxyRole>* list);
static ProxyRole* at_proxyRole(QQmlListProperty<ProxyRole>* list, qsizetype index);
static void clear_proxyRoles(QQmlListProperty<ProxyRole>* list);
};

Expand Down
23 changes: 3 additions & 20 deletions qqmlsortfilterproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,20 @@ void QQmlSortFilterProxyModel::setFilterRoleName(const QString& filterRoleName)

QString QQmlSortFilterProxyModel::filterPattern() const
{
return filterRegExp().pattern();
return filterRegularExpression().pattern();
}

void QQmlSortFilterProxyModel::setFilterPattern(const QString& filterPattern)
{
QRegExp regExp = filterRegExp();
QRegularExpression regExp = filterRegularExpression();
if (regExp.pattern() == filterPattern)
return;

regExp.setPattern(filterPattern);
QSortFilterProxyModel::setFilterRegExp(regExp);
QSortFilterProxyModel::setFilterRegularExpression(regExp);
Q_EMIT filterPatternChanged();
}

QQmlSortFilterProxyModel::PatternSyntax QQmlSortFilterProxyModel::filterPatternSyntax() const
{
return static_cast<PatternSyntax>(filterRegExp().patternSyntax());
}

void QQmlSortFilterProxyModel::setFilterPatternSyntax(QQmlSortFilterProxyModel::PatternSyntax patternSyntax)
{
QRegExp regExp = filterRegExp();
QRegExp::PatternSyntax patternSyntaxTmp = static_cast<QRegExp::PatternSyntax>(patternSyntax);
if (regExp.patternSyntax() == patternSyntaxTmp)
return;

regExp.setPatternSyntax(patternSyntaxTmp);
QSortFilterProxyModel::setFilterRegExp(regExp);
Q_EMIT filterPatternSyntaxChanged();
}

const QVariant& QQmlSortFilterProxyModel::filterValue() const
{
return m_filterValue;
Expand Down
16 changes: 1 addition & 15 deletions qqmlsortfilterproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel,

Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged)
Q_PROPERTY(QString filterPattern READ filterPattern WRITE setFilterPattern NOTIFY filterPatternChanged)
Q_PROPERTY(PatternSyntax filterPatternSyntax READ filterPatternSyntax WRITE setFilterPatternSyntax NOTIFY filterPatternSyntaxChanged)
Q_PROPERTY(QVariant filterValue READ filterValue WRITE setFilterValue NOTIFY filterValueChanged)

Q_PROPERTY(QString sortRoleName READ sortRoleName WRITE setSortRoleName NOTIFY sortRoleNameChanged)
Expand All @@ -37,15 +36,6 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel,
Q_PROPERTY(QQmlListProperty<qqsfpm::ProxyRole> proxyRoles READ proxyRolesListProperty)

public:
enum PatternSyntax {
RegExp = QRegExp::RegExp,
Wildcard = QRegExp::Wildcard,
FixedString = QRegExp::FixedString,
RegExp2 = QRegExp::RegExp2,
WildcardUnix = QRegExp::WildcardUnix,
W3CXmlSchema11 = QRegExp::W3CXmlSchema11 };
Q_ENUMS(PatternSyntax)

QQmlSortFilterProxyModel(QObject* parent = 0);

int count() const;
Expand All @@ -59,9 +49,6 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel,
QString filterPattern() const;
void setFilterPattern(const QString& filterPattern);

PatternSyntax filterPatternSyntax() const;
void setFilterPatternSyntax(PatternSyntax patternSyntax);

const QVariant& filterValue() const;
void setFilterValue(const QVariant& filterValue);

Expand Down Expand Up @@ -97,7 +84,6 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel,
void delayedChanged();

void filterRoleNameChanged();
void filterPatternSyntaxChanged();
void filterPatternChanged();
void filterValueChanged();

Expand All @@ -109,7 +95,7 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel,
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override;

protected Q_SLOTS:
void resetInternalData();
void resetInternalData() override;

private Q_SLOTS:
void queueInvalidateFilter();
Expand Down
11 changes: 3 additions & 8 deletions sorters/rolesorter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "rolesorter.h"
#include "qqmlsortfilterproxymodel.h"
#include "../utils/utils.h"

namespace qqsfpm {

Expand Down Expand Up @@ -56,14 +57,8 @@ QPair<QVariant, QVariant> RoleSorter::sourceData(const QModelIndex &sourceLeft,

int RoleSorter::compare(const QModelIndex &sourceLeft, const QModelIndex& sourceRight, const QQmlSortFilterProxyModel& proxyModel) const
{
QPair<QVariant, QVariant> pair = sourceData(sourceLeft, sourceRight, proxyModel);
QVariant leftValue = pair.first;
QVariant rightValue = pair.second;
if (leftValue < rightValue)
return -1;
if (leftValue > rightValue)
return 1;
return 0;
const QPair<QVariant, QVariant> pair = sourceData(sourceLeft, sourceRight, proxyModel);
return compareVariants(pair.first, pair.second);
}

}
4 changes: 2 additions & 2 deletions sorters/sortercontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ void SorterContainer::append_sorter(QQmlListProperty<Sorter>* list, Sorter* sort
that->appendSorter(sorter);
}

int SorterContainer::count_sorter(QQmlListProperty<Sorter>* list)
qsizetype SorterContainer::count_sorter(QQmlListProperty<Sorter>* list)
{
QList<Sorter*>* sorters = static_cast<QList<Sorter*>*>(list->data);
return sorters->count();
}

Sorter* SorterContainer::at_sorter(QQmlListProperty<Sorter>* list, int index)
Sorter* SorterContainer::at_sorter(QQmlListProperty<Sorter>* list, qsizetype index)
{
QList<Sorter*>* sorters = static_cast<QList<Sorter*>*>(list->data);
return sorters->at(index);
Expand Down
4 changes: 2 additions & 2 deletions sorters/sortercontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class SorterContainer {
virtual void onSortersCleared() = 0;

static void append_sorter(QQmlListProperty<Sorter>* list, Sorter* sorter);
static int count_sorter(QQmlListProperty<Sorter>* list);
static Sorter* at_sorter(QQmlListProperty<Sorter>* list, int index);
static qsizetype count_sorter(QQmlListProperty<Sorter>* list);
static Sorter* at_sorter(QQmlListProperty<Sorter>* list, qsizetype index);
static void clear_sorters(QQmlListProperty<Sorter>* list);
};

Expand Down
2 changes: 2 additions & 0 deletions tests/BLACKLIST
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[StringSorterTests::test_stringSorters:doNotIgnorePunctuation]
macos
9 changes: 4 additions & 5 deletions tests/tst_rolesorter.qml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ Item {

verify(testModel.count === sorter.expectedValues.length,
"Expected count " + sorter.expectedValues.length + ", actual count: " + testModel.count);
for (var i = 0; i < testModel.count; i++)
{
var modelValue = testModel.get(i, sorter.roleName);
verify(modelValue === sorter.expectedValues[i],
"Expected testModel value " + sorter.expectedValues[i] + ", actual: " + modelValue);
let actualValues = [];
for (var i = 0; i < testModel.count; i++) {
actualValues.push(testModel.get(i, sorter.roleName));
}
compare(actualValues, sorter.expectedValues);
}
}
}
Loading

0 comments on commit fe50203

Please sign in to comment.