diff options
author | Samuel Gaist <samuel.gaist@idiap.ch> | 2019-06-18 00:07:54 +0200 |
---|---|---|
committer | Samuel Gaist <samuel.gaist@idiap.ch> | 2019-10-16 21:53:30 +0200 |
commit | c222a9283d94ad1e29334bb2fba0beef7cc716c7 (patch) | |
tree | 20a72a144a7006cc737cdb1e74a5663c41c5d5cd /src/corelib/itemmodels | |
parent | 85ed676dff68b4d5cfa62112d758d80fb050594c (diff) |
QAbstractItemModel: implement QRegularExpression support for match
This is part of the migration of qtbase from QRexExp to
QRegularExpression.
[ChangeLog][QtCore][QAbstractItemModel] The match() method now
supports the new Qt::RegularExpression match flag value. This
will allow users to use either a string or a fully configured
QRegularExpression when doing searches. In the second case,
the case sensitivity flag will be ignored if passed.
Task-number: QTBUG-72587
Change-Id: I07c8d72a661c48b7f4fcf13ef8e95980bcdcb998
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Diffstat (limited to 'src/corelib/itemmodels')
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.cpp | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 6e97c2fd39..88555f9572 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -46,6 +46,7 @@ #include <qdebug.h> #include <qvector.h> #include <qregexp.h> +#include <qregularexpression.h> #include <qstack.h> #include <qbitarray.h> #include <qdatetime.h> @@ -2358,6 +2359,7 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, bool wrap = flags & Qt::MatchWrap; bool allHits = (hits == -1); QString text; // only convert to a string if it is needed + QRegularExpression rx; // only create it if needed const int column = start.column(); QModelIndex p = parent(start); int from = start.row(); @@ -2374,17 +2376,39 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, if (matchType == Qt::MatchExactly) { if (value == v) result.append(idx); - } else { // QString based matching - if (text.isEmpty()) // lazy conversion - text = value.toString(); + } else { // QString or regular expression based matching + if (matchType == Qt::MatchRegularExpression) { + if (rx.pattern().isEmpty()) { + if (value.type() == QVariant::RegularExpression) { + rx = value.toRegularExpression(); + } else { + rx.setPattern(value.toString()); + if (cs == Qt::CaseInsensitive) + rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption); + } + } + } else if (matchType == Qt::MatchWildcard) { + if (rx.pattern().isEmpty()) + rx.setPattern(QRegularExpression::wildcardToRegularExpression(value.toString())); + if (cs == Qt::CaseInsensitive) + rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption); + } else { + if (text.isEmpty()) // lazy conversion + text = value.toString(); + } + QString t = v.toString(); switch (matchType) { +#if QT_DEPRECATED_SINCE(5, 15) case Qt::MatchRegExp: if (QRegExp(text, cs).exactMatch(t)) result.append(idx); break; +#endif + case Qt::MatchRegularExpression: + Q_FALLTHROUGH(); case Qt::MatchWildcard: - if (QRegExp(text, cs, QRegExp::Wildcard).exactMatch(t)) + if (t.contains(rx)) result.append(idx); break; case Qt::MatchStartsWith: |