From 48794f5057f49373a7b8803db8bab7131e04575c Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 2 Apr 2020 12:57:56 +0200 Subject: [PATCH] Port example over to QRegularExpression Change-Id: I1e15bfa0a2973aabcad78f3eba4bb4903f8f3f6f Reviewed-by: Alex Blasche --- .../doc/src/customsortfiltermodel.qdoc | 8 +++---- .../customsortfiltermodel/filterwidget.cpp | 22 +++++++++---------- .../customsortfiltermodel/filterwidget.h | 16 +++++++++----- .../mysortfilterproxymodel.cpp | 4 ++-- .../customsortfiltermodel/window.cpp | 22 +++++++++++++++---- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/examples/widgets/doc/src/customsortfiltermodel.qdoc b/examples/widgets/doc/src/customsortfiltermodel.qdoc index 97725ead049..ebbd29a9213 100644 --- a/examples/widgets/doc/src/customsortfiltermodel.qdoc +++ b/examples/widgets/doc/src/customsortfiltermodel.qdoc @@ -250,15 +250,15 @@ The \c textFilterChanged() function is called whenever the user changes the filter pattern or the case sensitivity. - We first retrieve the preferred syntax (the QRegExp::PatternSyntax + We first retrieve the preferred syntax (the FilterWidget::PatternSyntax enum is used to interpret the meaning of the given pattern), then we determine the preferred case sensitivity. Based on these preferences and the current filter pattern, we set the proxy - model's \l {QSortFilterProxyModel::}{filterRegExp} property. The - \l {QSortFilterProxyModel::}{filterRegExp} property holds the + model's \l {QSortFilterProxyModel::}{filterRegularExpression} property. The + \l {QSortFilterProxyModel::}{filterRegularExpression} property holds the regular expression used to filter the contents of the source model. Note that calling QSortFilterProxyModel's \l - {QSortFilterProxyModel::}{setFilterRegExp()} function also updates + {QSortFilterProxyModel::}{setFilterRegularExpression()} function also updates the model. \snippet itemviews/customsortfiltermodel/window.cpp 9 diff --git a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp index 302042a6b36..bfcc8f84fbb 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp +++ b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp @@ -72,18 +72,18 @@ FilterWidget::FilterWidget(QWidget *parent) menu->addSeparator(); m_patternGroup->setExclusive(true); - QAction *patternAction = menu->addAction("Fixed String"); - patternAction->setData(QVariant(int(QRegExp::FixedString))); + QAction *patternAction = menu->addAction("Regular Expression"); patternAction->setCheckable(true); patternAction->setChecked(true); - m_patternGroup->addAction(patternAction); - patternAction = menu->addAction("Regular Expression"); - patternAction->setCheckable(true); - patternAction->setData(QVariant(int(QRegExp::RegExp2))); + patternAction->setData(QVariant(int(RegularExpression))); m_patternGroup->addAction(patternAction); patternAction = menu->addAction("Wildcard"); patternAction->setCheckable(true); - patternAction->setData(QVariant(int(QRegExp::Wildcard))); + patternAction->setData(QVariant(int(Wildcard))); + m_patternGroup->addAction(patternAction); + patternAction = menu->addAction("Fixed String"); + patternAction->setData(QVariant(int(FixedString))); + patternAction->setCheckable(true); m_patternGroup->addAction(patternAction); connect(m_patternGroup, &QActionGroup::triggered, this, &FilterWidget::filterChanged); @@ -113,17 +113,17 @@ void FilterWidget::setCaseSensitivity(Qt::CaseSensitivity cs) m_caseSensitivityAction->setChecked(cs == Qt::CaseSensitive); } -static inline QRegExp::PatternSyntax patternSyntaxFromAction(const QAction *a) +static inline FilterWidget::PatternSyntax patternSyntaxFromAction(const QAction *a) { - return static_cast(a->data().toInt()); + return static_cast(a->data().toInt()); } -QRegExp::PatternSyntax FilterWidget::patternSyntax() const +FilterWidget::PatternSyntax FilterWidget::patternSyntax() const { return patternSyntaxFromAction(m_patternGroup->checkedAction()); } -void FilterWidget::setPatternSyntax(QRegExp::PatternSyntax s) +void FilterWidget::setPatternSyntax(PatternSyntax s) { const QList actions = m_patternGroup->actions(); for (QAction *a : actions) { diff --git a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h index 70214b862ec..0404d670b22 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h +++ b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h @@ -52,28 +52,32 @@ #define FILTERWIDGET_H #include -#include QT_BEGIN_NAMESPACE class QAction; class QActionGroup; QT_END_NAMESPACE -Q_DECLARE_METATYPE(QRegExp::PatternSyntax) - class FilterWidget : public QLineEdit { Q_OBJECT Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity) - Q_PROPERTY(QRegExp::PatternSyntax patternSyntax READ patternSyntax WRITE setPatternSyntax) + Q_PROPERTY(PatternSyntax patternSyntax READ patternSyntax WRITE setPatternSyntax) public: explicit FilterWidget(QWidget *parent = nullptr); Qt::CaseSensitivity caseSensitivity() const; void setCaseSensitivity(Qt::CaseSensitivity); - QRegExp::PatternSyntax patternSyntax() const; - void setPatternSyntax(QRegExp::PatternSyntax); + enum PatternSyntax { + RegularExpression, + Wildcard, + FixedString + }; + Q_ENUM(PatternSyntax) + + PatternSyntax patternSyntax() const; + void setPatternSyntax(PatternSyntax); signals: void filterChanged(); diff --git a/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp b/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp index fd17876f2b3..da31f8e361a 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp +++ b/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp @@ -83,8 +83,8 @@ bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent); QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent); - return (sourceModel()->data(index0).toString().contains(filterRegExp()) - || sourceModel()->data(index1).toString().contains(filterRegExp())) + return (sourceModel()->data(index0).toString().contains(filterRegularExpression()) + || sourceModel()->data(index1).toString().contains(filterRegularExpression())) && dateInRange(sourceModel()->data(index2).toDate()); } //! [3] diff --git a/examples/widgets/itemviews/customsortfiltermodel/window.cpp b/examples/widgets/itemviews/customsortfiltermodel/window.cpp index 3356c971ad5..3423ac4ee9c 100644 --- a/examples/widgets/itemviews/customsortfiltermodel/window.cpp +++ b/examples/widgets/itemviews/customsortfiltermodel/window.cpp @@ -148,10 +148,24 @@ void Window::setSourceModel(QAbstractItemModel *model) //! [8] void Window::textFilterChanged() { - QRegExp regExp(filterWidget->text(), - filterWidget->caseSensitivity(), - filterWidget->patternSyntax()); - proxyModel->setFilterRegExp(regExp); + FilterWidget::PatternSyntax s = filterWidget->patternSyntax(); + QString pattern = filterWidget->text(); + switch (s) { + case FilterWidget::Wildcard: + pattern = QRegularExpression::wildcardToRegularExpression(pattern); + break; + case FilterWidget::FixedString: + pattern = QRegularExpression::escape(pattern); + break; + default: + break; + } + + QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption; + if (filterWidget->caseSensitivity() == Qt::CaseInsensitive) + options |= QRegularExpression::CaseInsensitiveOption; + QRegularExpression regularExpression(pattern, options); + proxyModel->setFilterRegularExpression(regularExpression); } //! [8]