diff options
Diffstat (limited to 'src/corelib/itemmodels/qsortfilterproxymodel.cpp')
-rw-r--r-- | src/corelib/itemmodels/qsortfilterproxymodel.cpp | 124 |
1 files changed, 58 insertions, 66 deletions
diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index db1ed95182..f6c6c89c8e 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -6,7 +6,6 @@ #include <qsize.h> #include <qdebug.h> #include <qdatetime.h> -#include <qpair.h> #include <qstringlist.h> #include <private/qabstractitemmodel_p.h> #include <private/qabstractproxymodel_p.h> @@ -16,7 +15,7 @@ QT_BEGIN_NAMESPACE -typedef QList<QPair<QModelIndex, QPersistentModelIndex>> QModelIndexPairList; +using QModelIndexPairList = QList<std::pair<QModelIndex, QPersistentModelIndex>>; struct QSortFilterProxyModelDataChanged { @@ -256,7 +255,7 @@ public: */ void set_filter_pattern(const QString &pattern) { - QRegularExpression re = filter_regularexpression.value(); + QRegularExpression re = filter_regularexpression.valueBypassingBindings(); const auto cs = re.patternOptions() & QRegularExpression::CaseInsensitiveOption; re.setPattern(pattern); re.setPatternOptions(cs); @@ -333,10 +332,10 @@ public: int find_source_sort_column() const; void sort_source_rows(QList<int> &source_rows, const QModelIndex &source_parent) const; - QList<QPair<int, QList<int>>> proxy_intervals_for_source_items_to_add( + QList<std::pair<int, QList<int>>> proxy_intervals_for_source_items_to_add( const QList<int> &proxy_to_source, const QList<int> &source_items, const QModelIndex &source_parent, Qt::Orientation orient) const; - QList<QPair<int, int>> proxy_intervals_for_source_items( + QList<std::pair<int, int>> proxy_intervals_for_source_items( const QList<int> &source_to_proxy, const QList<int> &source_items) const; void insert_source_items( QList<int> &source_to_proxy, QList<int> &proxy_to_source, @@ -689,8 +688,10 @@ void QSortFilterProxyModelPrivate::sort_source_rows( QSortFilterProxyModelGreaterThan gt(source_sort_column, source_parent, model, q); std::stable_sort(source_rows.begin(), source_rows.end(), gt); } - } else { // restore the source model order - std::stable_sort(source_rows.begin(), source_rows.end()); + } else if (sort_order == Qt::AscendingOrder) { + std::stable_sort(source_rows.begin(), source_rows.end(), std::less{}); + } else { + std::stable_sort(source_rows.begin(), source_rows.end(), std::greater{}); } } @@ -705,10 +706,10 @@ void QSortFilterProxyModelPrivate::sort_source_rows( The result is a vector of pairs, where each pair represents a (start, end) tuple, sorted in ascending order. */ -QList<QPair<int, int>> QSortFilterProxyModelPrivate::proxy_intervals_for_source_items( +QList<std::pair<int, int>> QSortFilterProxyModelPrivate::proxy_intervals_for_source_items( const QList<int> &source_to_proxy, const QList<int> &source_items) const { - QList<QPair<int, int>> proxy_intervals; + QList<std::pair<int, int>> proxy_intervals; if (source_items.isEmpty()) return proxy_intervals; @@ -725,19 +726,19 @@ QList<QPair<int, int>> QSortFilterProxyModelPrivate::proxy_intervals_for_source_ ++source_items_index; } // Add interval to result - proxy_intervals.append(QPair<int, int>(first_proxy_item, last_proxy_item)); + proxy_intervals.emplace_back(first_proxy_item, last_proxy_item); } std::stable_sort(proxy_intervals.begin(), proxy_intervals.end()); // Consolidate adjacent intervals for (int i = proxy_intervals.size()-1; i > 0; --i) { - QPair<int, int> &interval = proxy_intervals[i]; - QPair<int, int> &preceeding_interval = proxy_intervals[i - 1]; + std::pair<int, int> &interval = proxy_intervals[i]; + std::pair<int, int> &preceeding_interval = proxy_intervals[i - 1]; if (interval.first == preceeding_interval.second + 1) { preceeding_interval.second = interval.second; interval.first = interval.second = -1; } } - proxy_intervals.removeIf([](QPair<int, int> interval) { return interval.first < 0; }); + proxy_intervals.removeIf([](std::pair<int, int> interval) { return interval.first < 0; }); return proxy_intervals; } @@ -766,7 +767,7 @@ void QSortFilterProxyModelPrivate::remove_source_items( const auto end = proxy_intervals.rend(); for (auto it = proxy_intervals.rbegin(); it != end; ++it) { - const QPair<int, int> &interval = *it; + const std::pair<int, int> &interval = *it; const int proxy_start = interval.first; const int proxy_end = interval.second; remove_proxy_interval(source_to_proxy, proxy_to_source, proxy_start, proxy_end, @@ -820,22 +821,21 @@ void QSortFilterProxyModelPrivate::remove_proxy_interval( items), where items is a vector containing the (sorted) source items that should be inserted at that proxy model location. */ -QList<QPair<int, QList<int>>> QSortFilterProxyModelPrivate::proxy_intervals_for_source_items_to_add( +QList<std::pair<int, QList<int>>> QSortFilterProxyModelPrivate::proxy_intervals_for_source_items_to_add( const QList<int> &proxy_to_source, const QList<int> &source_items, const QModelIndex &source_parent, Qt::Orientation orient) const { Q_Q(const QSortFilterProxyModel); - QList<QPair<int, QList<int>>> proxy_intervals; + QList<std::pair<int, QList<int>>> proxy_intervals; if (source_items.isEmpty()) return proxy_intervals; int proxy_low = 0; int proxy_item = 0; int source_items_index = 0; - QList<int> source_items_in_interval; bool compare = (orient == Qt::Vertical && source_sort_column >= 0 && dynamic_sortfilter); while (source_items_index < source_items.size()) { - source_items_in_interval.clear(); + QList<int> source_items_in_interval; int first_new_source_item = source_items.at(source_items_index); source_items_in_interval.append(first_new_source_item); ++source_items_index; @@ -881,7 +881,7 @@ QList<QPair<int, QList<int>>> QSortFilterProxyModelPrivate::proxy_intervals_for_ } // Add interval to result - proxy_intervals.append(QPair<int, QList<int>>(proxy_item, source_items_in_interval)); + proxy_intervals.emplace_back(proxy_item, std::move(source_items_in_interval)); } return proxy_intervals; } @@ -909,7 +909,7 @@ void QSortFilterProxyModelPrivate::insert_source_items( const auto end = proxy_intervals.rend(); for (auto it = proxy_intervals.rbegin(); it != end; ++it) { - const QPair<int, QList<int>> &interval = *it; + const std::pair<int, QList<int>> &interval = *it; const int proxy_start = interval.first; const QList<int> &source_items = interval.second; const int proxy_end = proxy_start + source_items.size() - 1; @@ -1138,7 +1138,7 @@ void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &sour Qt::Orientation orient, int start, int end, int delta_item_count, bool remove) { // see if any mapped children should be (re)moved - QList<QPair<QModelIndex, Mapping *>> moved_source_index_mappings; + QList<std::pair<QModelIndex, Mapping *>> moved_source_index_mappings; auto it2 = parent_mapping->mapped_children.begin(); for ( ; it2 != parent_mapping->mapped_children.end();) { const QModelIndex source_child_index = *it2; @@ -1172,7 +1172,7 @@ void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &sour Mapping *cm = source_index_mapping.take(source_child_index); Q_ASSERT(cm); // we do not reinsert right away, because the new index might be identical with another, old index - moved_source_index_mappings.append(QPair<QModelIndex, Mapping*>(new_index, cm)); + moved_source_index_mappings.emplace_back(new_index, cm); } } @@ -1229,7 +1229,7 @@ QModelIndexPairList QSortFilterProxyModelPrivate::store_persistent_indexes() con for (const QPersistentModelIndexData *data : std::as_const(persistent.indexes)) { const QModelIndex &proxy_index = data->index; QModelIndex source_index = q->mapToSource(proxy_index); - source_indexes.append(qMakePair(proxy_index, QPersistentModelIndex(source_index))); + source_indexes.emplace_back(proxy_index, source_index); } return source_indexes; } @@ -1267,7 +1267,7 @@ void QSortFilterProxyModelPrivate::update_persistent_indexes( */ void QSortFilterProxyModelPrivate::filter_about_to_be_changed(const QModelIndex &source_parent) { - if (!filter_regularexpression.value().pattern().isEmpty() + if (!filter_regularexpression.valueBypassingBindings().pattern().isEmpty() && source_index_mapping.constFind(source_parent) == source_index_mapping.constEnd()) { create_mapping(source_parent); } @@ -2009,7 +2009,7 @@ QSortFilterProxyModel::QSortFilterProxyModel(QObject *parent) : QAbstractProxyModel(*new QSortFilterProxyModelPrivate, parent) { Q_D(QSortFilterProxyModel); - QObjectPrivate::connect(this, &QSortFilterProxyModel::modelReset, d, + QObjectPrivate::connect(this, &QAbstractItemModel::modelReset, d, &QSortFilterProxyModelPrivate::_q_clearMapping); } @@ -2047,58 +2047,58 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) QAbstractProxyModel::setSourceModel(sourceModel); d->sourceConnections = std::array<QMetaObject::Connection, 18>{ - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::dataChanged, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::dataChanged, d, &QSortFilterProxyModelPrivate::_q_sourceDataChanged), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::headerDataChanged, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::headerDataChanged, d, &QSortFilterProxyModelPrivate::_q_sourceHeaderDataChanged), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::rowsAboutToBeInserted, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsAboutToBeInserted, d, &QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeInserted), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::rowsInserted, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsInserted, d, &QSortFilterProxyModelPrivate::_q_sourceRowsInserted), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::columnsAboutToBeInserted, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeInserted, d, &QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeInserted), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::columnsInserted, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsInserted, d, &QSortFilterProxyModelPrivate::_q_sourceColumnsInserted), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::rowsAboutToBeRemoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved, d, &QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeRemoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::rowsRemoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsRemoved, d, &QSortFilterProxyModelPrivate::_q_sourceRowsRemoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::columnsAboutToBeRemoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved, d, &QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeRemoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::columnsRemoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsRemoved, d, &QSortFilterProxyModelPrivate::_q_sourceColumnsRemoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::rowsAboutToBeMoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsAboutToBeMoved, d, &QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeMoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::rowsMoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsMoved, d, &QSortFilterProxyModelPrivate::_q_sourceRowsMoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::columnsAboutToBeMoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeMoved, d, &QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeMoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::columnsMoved, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsMoved, d, &QSortFilterProxyModelPrivate::_q_sourceColumnsMoved), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::layoutAboutToBeChanged, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutAboutToBeChanged, d, &QSortFilterProxyModelPrivate::_q_sourceLayoutAboutToBeChanged), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::layoutChanged, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutChanged, d, &QSortFilterProxyModelPrivate::_q_sourceLayoutChanged), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::modelAboutToBeReset, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::modelAboutToBeReset, d, &QSortFilterProxyModelPrivate::_q_sourceAboutToBeReset), - QObjectPrivate::connect(d->model, &QSortFilterProxyModel::modelReset, d, + QObjectPrivate::connect(d->model, &QAbstractItemModel::modelReset, d, &QSortFilterProxyModelPrivate::_q_sourceReset) }; endResetModel(); @@ -2435,11 +2435,7 @@ bool QSortFilterProxyModel::removeColumns(int column, int count, const QModelInd */ void QSortFilterProxyModel::fetchMore(const QModelIndex &parent) { - Q_D(QSortFilterProxyModel); - QModelIndex source_parent; - if (d->indexValid(parent)) - source_parent = mapToSource(parent); - d->model->fetchMore(source_parent); + QAbstractProxyModel::fetchMore(parent); } /*! @@ -2447,11 +2443,7 @@ void QSortFilterProxyModel::fetchMore(const QModelIndex &parent) */ bool QSortFilterProxyModel::canFetchMore(const QModelIndex &parent) const { - Q_D(const QSortFilterProxyModel); - QModelIndex source_parent; - if (d->indexValid(parent)) - source_parent = mapToSource(parent); - return d->model->canFetchMore(source_parent); + return QAbstractProxyModel::canFetchMore(parent); } /*! @@ -2459,11 +2451,7 @@ bool QSortFilterProxyModel::canFetchMore(const QModelIndex &parent) const */ Qt::ItemFlags QSortFilterProxyModel::flags(const QModelIndex &index) const { - Q_D(const QSortFilterProxyModel); - QModelIndex source_index; - if (d->indexValid(index)) - source_index = mapToSource(index); - return d->model->flags(source_index); + return QAbstractProxyModel::flags(index); } /*! @@ -2504,7 +2492,10 @@ QSize QSortFilterProxyModel::span(const QModelIndex &index) const } /*! - \reimp + \reimp + Sorts the model by \a column in the given \a order. + If the sort \a column is less than zero, the model will be sorted by source model row + in the given \a order. */ void QSortFilterProxyModel::sort(int column, Qt::SortOrder order) { @@ -2583,10 +2574,11 @@ void QSortFilterProxyModel::setFilterRegularExpression(const QRegularExpression { Q_D(QSortFilterProxyModel); const QScopedPropertyUpdateGroup guard; - const bool regExpChanged = regularExpression != d->filter_regularexpression.value(); + const bool regExpChanged = + regularExpression != d->filter_regularexpression.valueBypassingBindings(); d->filter_regularexpression.removeBindingUnlessInWrapper(); d->filter_casesensitive.removeBindingUnlessInWrapper(); - const Qt::CaseSensitivity cs = filterCaseSensitivity(); + const Qt::CaseSensitivity cs = d->filter_casesensitive.valueBypassingBindings(); d->filter_about_to_be_changed(); const Qt::CaseSensitivity updatedCs = regularExpression.patternOptions() & QRegularExpression::CaseInsensitiveOption @@ -2627,7 +2619,7 @@ void QSortFilterProxyModel::setFilterKeyColumn(int column) Q_D(QSortFilterProxyModel); d->filter_column.removeBindingUnlessInWrapper(); d->filter_about_to_be_changed(); - const auto oldColumn = d->filter_column.value(); + const auto oldColumn = d->filter_column.valueBypassingBindings(); d->filter_column.setValueBypassingBindings(column); d->filter_changed(QSortFilterProxyModelPrivate::Direction::Rows); if (oldColumn != column) @@ -2925,7 +2917,7 @@ void QSortFilterProxyModel::setSortRole(int role) { Q_D(QSortFilterProxyModel); d->sort_role.removeBindingUnlessInWrapper(); - if (d->sort_role == role) + if (d->sort_role.valueBypassingBindings() == role) return; d->sort_role.setValueBypassingBindings(role); d->sort(); @@ -2964,7 +2956,7 @@ void QSortFilterProxyModel::setFilterRole(int role) { Q_D(QSortFilterProxyModel); d->filter_role.removeBindingUnlessInWrapper(); - if (d->filter_role == role) + if (d->filter_role.valueBypassingBindings() == role) return; d->filter_about_to_be_changed(); d->filter_role.setValueBypassingBindings(role); @@ -3182,8 +3174,8 @@ void QSortFilterProxyModel::invalidateRowsFilter() bool QSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const { Q_D(const QSortFilterProxyModel); - QVariant l = (source_left.model() ? source_left.model()->data(source_left, d->sort_role) : QVariant()); - QVariant r = (source_right.model() ? source_right.model()->data(source_right, d->sort_role) : QVariant()); + const QVariant l = source_left.data(d->sort_role); + const QVariant r = source_right.data(d->sort_role); return QAbstractItemModelPrivate::isVariantLessThan(l, r, d->sort_casesensitivity, d->sort_localeaware); } |