diff options
Diffstat (limited to 'src/corelib/itemmodels/qabstractitemmodel.cpp')
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.cpp | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index d88960ba0c..57a6c2f239 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -46,7 +46,9 @@ #include <qdebug.h> #include <qvector.h> #include <qregexp.h> -#include <qregularexpression.h> +#if QT_CONFIG(regularexpression) +# include <qregularexpression.h> +#endif #include <qstack.h> #include <qbitarray.h> #include <qdatetime.h> @@ -63,7 +65,7 @@ QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex & Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list QPersistentModelIndexData *d = nullptr; QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model()); - QHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes; + QMultiHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes; const auto it = indexes.constFind(index); if (it != indexes.cend()) { d = (*it); @@ -665,8 +667,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent, Q_UNUSED(last); QVector<QPersistentModelIndexData *> persistent_moved; if (first < q->rowCount(parent)) { - for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); - it != persistent.indexes.constEnd(); ++it) { + for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) { QPersistentModelIndexData *data = *it; const QModelIndex &index = data->index; if (index.row() >= first && index.isValid() && index.parent() == parent) { @@ -702,14 +703,13 @@ void QAbstractItemModelPrivate::itemsAboutToBeMoved(const QModelIndex &srcParent QVector<QPersistentModelIndexData *> persistent_moved_in_source; QVector<QPersistentModelIndexData *> persistent_moved_in_destination; - QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it; - const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator begin = persistent.indexes.constBegin(); - const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator end = persistent.indexes.constEnd(); + const auto begin = persistent.indexes.constBegin(); + const auto end = persistent.indexes.constEnd(); const bool sameParent = (srcParent == destinationParent); const bool movingUp = (srcFirst > destinationChild); - for ( it = begin; it != end; ++it) { + for (auto it = begin; it != end; ++it) { QPersistentModelIndexData *data = *it; const QModelIndex &index = data->index; const QModelIndex &parent = index.parent(); @@ -814,8 +814,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent, QVector<QPersistentModelIndexData *> persistent_invalidated; // find the persistent indexes that are affected by the change, either by being in the removed subtree // or by being on the same level and below the removed rows - for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); - it != persistent.indexes.constEnd(); ++it) { + for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) { QPersistentModelIndexData *data = *it; bool level_changed = false; QModelIndex current = data->index; @@ -858,7 +857,9 @@ void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent, for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_invalidated.constBegin(); it != persistent_invalidated.constEnd(); ++it) { QPersistentModelIndexData *data = *it; - persistent.indexes.erase(persistent.indexes.constFind(data->index)); + auto pit = persistent.indexes.constFind(data->index); + if (pit != persistent.indexes.cend()) + persistent.indexes.erase(pit); data->index = QModelIndex(); } } @@ -870,8 +871,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &pare Q_UNUSED(last); QVector<QPersistentModelIndexData *> persistent_moved; if (first < q->columnCount(parent)) { - for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); - it != persistent.indexes.constEnd(); ++it) { + for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) { QPersistentModelIndexData *data = *it; const QModelIndex &index = data->index; if (index.column() >= first && index.isValid() && index.parent() == parent) @@ -907,8 +907,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &paren QVector<QPersistentModelIndexData *> persistent_invalidated; // find the persistent indexes that are affected by the change, either by being in the removed subtree // or by being on the same level and to the right of the removed columns - for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); - it != persistent.indexes.constEnd(); ++it) { + for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) { QPersistentModelIndexData *data = *it; bool level_changed = false; QModelIndex current = data->index; @@ -952,7 +951,9 @@ void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent, for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_invalidated.constBegin(); it != persistent_invalidated.constEnd(); ++it) { QPersistentModelIndexData *data = *it; - persistent.indexes.erase(persistent.indexes.constFind(data->index)); + auto index = persistent.indexes.constFind(data->index); + if (index != persistent.indexes.constEnd()) + persistent.indexes.erase(index); data->index = QModelIndex(); } } @@ -2362,7 +2363,9 @@ 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 +#if QT_CONFIG(regularexpression) QRegularExpression rx; // only create it if needed +#endif const int column = start.column(); QModelIndex p = parent(start); int from = start.row(); @@ -2380,6 +2383,7 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, if (value == v) result.append(idx); } else { // QString or regular expression based matching +#if QT_CONFIG(regularexpression) if (matchType == Qt::MatchRegularExpression) { if (rx.pattern().isEmpty()) { if (value.userType() == QMetaType::QRegularExpression) { @@ -2395,25 +2399,23 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, rx.setPattern(QRegularExpression::wildcardToRegularExpression(value.toString())); if (cs == Qt::CaseInsensitive) rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption); - } else { + } else +#endif + { 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 +#if QT_CONFIG(regularexpression) case Qt::MatchRegularExpression: Q_FALLTHROUGH(); case Qt::MatchWildcard: if (t.contains(rx)) result.append(idx); break; +#endif case Qt::MatchStartsWith: if (t.startsWith(text, cs)) result.append(idx); @@ -3297,7 +3299,11 @@ void QAbstractItemModel::endResetModel() { Q_D(QAbstractItemModel); d->invalidatePersistentIndexes(); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + resetInternalData(); +#else QMetaObject::invokeMethod(this, "resetInternalData"); +#endif emit modelReset(QPrivateSignal()); } @@ -3375,8 +3381,7 @@ QModelIndexList QAbstractItemModel::persistentIndexList() const Q_D(const QAbstractItemModel); QModelIndexList result; result.reserve(d->persistent.indexes.count()); - for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = d->persistent.indexes.constBegin(); - it != d->persistent.indexes.constEnd(); ++it) { + for (auto it = d->persistent.indexes.constBegin(); it != d->persistent.indexes.constEnd(); ++it) { QPersistentModelIndexData *data = *it; result.append(data->index); } @@ -3994,8 +3999,8 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti */ void QAbstractItemModelPrivate::Persistent::insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data) { - QHash<QModelIndex,QPersistentModelIndexData *>::iterator newIt = indexes.insert(key, data); - QHash<QModelIndex,QPersistentModelIndexData *>::iterator it = newIt; + auto newIt = indexes.insert(key, data); + auto it = newIt; ++it; while (it != indexes.end() && it.key() == key) { qSwap(*newIt,*it); |