diff options
Diffstat (limited to 'src/corelib/itemmodels/qabstractitemmodel.cpp')
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.cpp | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 6e97c2fd39..46ac703615 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> @@ -60,7 +61,7 @@ Q_LOGGING_CATEGORY(lcCheckIndex, "qt.core.qabstractitemmodel.checkindex") QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index) { Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list - QPersistentModelIndexData *d = 0; + QPersistentModelIndexData *d = nullptr; QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model()); QHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes; const auto it = indexes.constFind(index); @@ -135,7 +136,7 @@ void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data) */ QPersistentModelIndex::QPersistentModelIndex() - : d(0) + : d(nullptr) { } @@ -157,7 +158,7 @@ QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other) */ QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index) - : d(0) + : d(nullptr) { if (index.isValid()) { d = QPersistentModelIndexData::create(index); @@ -175,7 +176,7 @@ QPersistentModelIndex::~QPersistentModelIndex() { if (d && !d->ref.deref()) { QPersistentModelIndexData::destroy(d); - d = 0; + d = nullptr; } } @@ -256,7 +257,7 @@ QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other d = QPersistentModelIndexData::create(other); if (d) d->ref.ref(); } else { - d = 0; + d = nullptr; } return *this; } @@ -343,7 +344,7 @@ void *QPersistentModelIndex::internalPointer() const { if (d) return d->index.internalPointer(); - return 0; + return nullptr; } /*! @@ -431,7 +432,7 @@ Qt::ItemFlags QPersistentModelIndex::flags() const { if (d) return d->index.flags(); - return 0; + return { }; } /*! @@ -441,7 +442,7 @@ const QAbstractItemModel *QPersistentModelIndex::model() const { if (d) return d->index.model(); - return 0; + return nullptr; } /*! @@ -483,7 +484,7 @@ QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx) class QEmptyItemModel : public QAbstractItemModel { public: - explicit QEmptyItemModel(QObject *parent = 0) : QAbstractItemModel(parent) {} + explicit QEmptyItemModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {} QModelIndex index(int, int, const QModelIndex &) const override { return QModelIndex(); } QModelIndex parent(const QModelIndex &) const override { return QModelIndex(); } int rowCount(const QModelIndex &) const override { return 0; } @@ -1949,10 +1950,10 @@ QStringList QAbstractItemModel::mimeTypes() const QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const { if (indexes.count() <= 0) - return 0; + return nullptr; QStringList types = mimeTypes(); if (types.isEmpty()) - return 0; + return nullptr; QMimeData *data = new QMimeData(); QString format = types.at(0); QByteArray encoded; @@ -2295,7 +2296,7 @@ Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const { Q_D(const QAbstractItemModel); if (!d->indexValid(index)) - return 0; + return { }; return Qt::ItemIsSelectable|Qt::ItemIsEnabled; } @@ -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: |