From b0f9c06a9b199a7474eb94a6292b5824364344b6 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 26 Jun 2020 08:21:15 +0200 Subject: Use QList instead of QVector in corelib implementation Omitting state machine and docs for now. Task-number: QTBUG-84469 Change-Id: Ibfa5e7035515773461f6cdbff35299315ef65737 Reviewed-by: Sona Kurazyan --- src/corelib/itemmodels/qsortfilterproxymodel.cpp | 167 +++++++++++------------ 1 file changed, 83 insertions(+), 84 deletions(-) (limited to 'src/corelib/itemmodels/qsortfilterproxymodel.cpp') diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 03db159618..728be151c6 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -51,7 +51,7 @@ QT_BEGIN_NAMESPACE -typedef QVector > QModelIndexPairList; +typedef QList> QModelIndexPairList; struct QSortFilterProxyModelDataChanged { @@ -62,7 +62,7 @@ struct QSortFilterProxyModelDataChanged QModelIndex bottomRight; }; -static inline QSet qVectorToSet(const QVector &vector) +static inline QSet qListToSet(const QList &vector) { return {vector.begin(), vector.end()}; } @@ -154,11 +154,11 @@ public: }; struct Mapping { - QVector source_rows; - QVector source_columns; - QVector proxy_rows; - QVector proxy_columns; - QVector mapped_children; + QList source_rows; + QList source_columns; + QList proxy_rows; + QList proxy_columns; + QList mapped_children; QModelIndex source_parent; }; @@ -215,7 +215,7 @@ public: void _q_sourceDataChanged(const QModelIndex &source_top_left, const QModelIndex &source_bottom_right, - const QVector &roles); + const QList &roles); void _q_sourceHeaderDataChanged(Qt::Orientation orientation, int start, int end); void _q_sourceAboutToBeReset(); @@ -258,27 +258,27 @@ public: void sort(); bool update_source_sort_column(); int find_source_sort_column() const; - void sort_source_rows(QVector &source_rows, + void sort_source_rows(QList &source_rows, const QModelIndex &source_parent) const; - QVector > > proxy_intervals_for_source_items_to_add( - const QVector &proxy_to_source, const QVector &source_items, + QList>> proxy_intervals_for_source_items_to_add( + const QList &proxy_to_source, const QList &source_items, const QModelIndex &source_parent, Qt::Orientation orient) const; - QVector > proxy_intervals_for_source_items( - const QVector &source_to_proxy, const QVector &source_items) const; + QList> proxy_intervals_for_source_items( + const QList &source_to_proxy, const QList &source_items) const; void insert_source_items( - QVector &source_to_proxy, QVector &proxy_to_source, - const QVector &source_items, const QModelIndex &source_parent, + QList &source_to_proxy, QList &proxy_to_source, + const QList &source_items, const QModelIndex &source_parent, Qt::Orientation orient, bool emit_signal = true); void remove_source_items( - QVector &source_to_proxy, QVector &proxy_to_source, - const QVector &source_items, const QModelIndex &source_parent, + QList &source_to_proxy, QList &proxy_to_source, + const QList &source_items, const QModelIndex &source_parent, Qt::Orientation orient, bool emit_signal = true); void remove_proxy_interval( - QVector &source_to_proxy, QVector &proxy_to_source, + QList &source_to_proxy, QList &proxy_to_source, int proxy_start, int proxy_end, const QModelIndex &proxy_parent, Qt::Orientation orient, bool emit_signal = true); static inline void build_source_to_proxy_mapping( - const QVector &proxy_to_source, QVector &source_to_proxy, int start = 0); + const QList &proxy_to_source, QList &source_to_proxy, int start = 0); void source_items_inserted(const QModelIndex &source_parent, int start, int end, Qt::Orientation orient); void source_items_about_to_be_removed(const QModelIndex &source_parent, @@ -286,7 +286,7 @@ public: void source_items_removed(const QModelIndex &source_parent, int start, int end, Qt::Orientation orient); void proxy_item_range( - const QVector &source_to_proxy, const QVector &source_items, + const QList &source_to_proxy, const QList &source_items, int &proxy_low, int &proxy_high) const; QModelIndexPairList store_persistent_indexes() const; @@ -295,7 +295,7 @@ public: void filter_about_to_be_changed(const QModelIndex &source_parent = QModelIndex()); void filter_changed(Direction dir, const QModelIndex &source_parent = QModelIndex()); QSet handle_filter_changed( - QVector &source_to_proxy, QVector &proxy_to_source, + QList &source_to_proxy, QList &proxy_to_source, const QModelIndex &source_parent, Qt::Orientation orient); void updateChildrenMapping(const QModelIndex &source_parent, Mapping *parent_mapping, @@ -303,7 +303,7 @@ public: void _q_sourceModelDestroyed() override; - bool needsReorder(const QVector &source_rows, const QModelIndex &source_parent) const; + bool needsReorder(const QList &source_rows, const QModelIndex &source_parent) const; bool filterAcceptsRowInternal(int source_row, const QModelIndex &source_parent) const; bool recursiveChildAcceptsRow(int source_row, const QModelIndex &source_parent) const; @@ -582,7 +582,7 @@ int QSortFilterProxyModelPrivate::find_source_sort_column() const Sorts the given \a source_rows according to current sort column and order. */ void QSortFilterProxyModelPrivate::sort_source_rows( - QVector &source_rows, const QModelIndex &source_parent) const + QList &source_rows, const QModelIndex &source_parent) const { Q_Q(const QSortFilterProxyModel); if (source_sort_column >= 0) { @@ -609,10 +609,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. */ -QVector > QSortFilterProxyModelPrivate::proxy_intervals_for_source_items( - const QVector &source_to_proxy, const QVector &source_items) const +QList> QSortFilterProxyModelPrivate::proxy_intervals_for_source_items( + const QList &source_to_proxy, const QList &source_items) const { - QVector > proxy_intervals; + QList> proxy_intervals; if (source_items.isEmpty()) return proxy_intervals; @@ -657,8 +657,8 @@ QVector > QSortFilterProxyModelPrivate::proxy_intervals_for_sou rows/columnsRemoved(start, end) signals will be generated. */ void QSortFilterProxyModelPrivate::remove_source_items( - QVector &source_to_proxy, QVector &proxy_to_source, - const QVector &source_items, const QModelIndex &source_parent, + QList &source_to_proxy, QList &proxy_to_source, + const QList &source_items, const QModelIndex &source_parent, Qt::Orientation orient, bool emit_signal) { Q_Q(QSortFilterProxyModel); @@ -687,7 +687,7 @@ void QSortFilterProxyModelPrivate::remove_source_items( (inclusive) from this proxy model. */ void QSortFilterProxyModelPrivate::remove_proxy_interval( - QVector &source_to_proxy, QVector &proxy_to_source, int proxy_start, int proxy_end, + QList &source_to_proxy, QList &proxy_to_source, int proxy_start, int proxy_end, const QModelIndex &proxy_parent, Qt::Orientation orient, bool emit_signal) { Q_Q(QSortFilterProxyModel); @@ -725,19 +725,19 @@ 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. */ -QVector > > QSortFilterProxyModelPrivate::proxy_intervals_for_source_items_to_add( - const QVector &proxy_to_source, const QVector &source_items, +QList>> QSortFilterProxyModelPrivate::proxy_intervals_for_source_items_to_add( + const QList &proxy_to_source, const QList &source_items, const QModelIndex &source_parent, Qt::Orientation orient) const { Q_Q(const QSortFilterProxyModel); - QVector > > proxy_intervals; + QList>> proxy_intervals; if (source_items.isEmpty()) return proxy_intervals; int proxy_low = 0; int proxy_item = 0; int source_items_index = 0; - QVector source_items_in_interval; + QList 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(); @@ -786,7 +786,7 @@ QVector > > QSortFilterProxyModelPrivate::proxy_interva } // Add interval to result - proxy_intervals.append(QPair >(proxy_item, source_items_in_interval)); + proxy_intervals.append(QPair>(proxy_item, source_items_in_interval)); } return proxy_intervals; } @@ -800,8 +800,8 @@ QVector > > QSortFilterProxyModelPrivate::proxy_interva that the proper rows/columnsInserted(start, end) signals will be generated. */ void QSortFilterProxyModelPrivate::insert_source_items( - QVector &source_to_proxy, QVector &proxy_to_source, - const QVector &source_items, const QModelIndex &source_parent, + QList &source_to_proxy, QList &proxy_to_source, + const QList &source_items, const QModelIndex &source_parent, Qt::Orientation orient, bool emit_signal) { Q_Q(QSortFilterProxyModel); @@ -814,9 +814,9 @@ void QSortFilterProxyModelPrivate::insert_source_items( const auto end = proxy_intervals.rend(); for (auto it = proxy_intervals.rbegin(); it != end; ++it) { - const QPair > &interval = *it; + const QPair> &interval = *it; const int proxy_start = interval.first; - const QVector &source_items = interval.second; + const QList &source_items = interval.second; const int proxy_end = proxy_start + source_items.size() - 1; if (emit_signal) { @@ -877,8 +877,8 @@ void QSortFilterProxyModelPrivate::source_items_inserted( } Mapping *m = it.value(); - QVector &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; - QVector &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; + QList &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; + QList &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; int delta_item_count = end - start + 1; int old_item_count = source_to_proxy.size(); @@ -905,7 +905,7 @@ void QSortFilterProxyModelPrivate::source_items_inserted( } // Figure out which items to add to mapping based on filter - QVector source_items; + QList source_items; for (int i = start; i <= end; ++i) { if ((orient == Qt::Vertical) ? filterAcceptsRowInternal(i, source_parent) @@ -919,8 +919,8 @@ void QSortFilterProxyModelPrivate::source_items_inserted( // If it was new rows make sure to create mappings for columns so that a // valid mapping can be retrieved later and vice-versa. - QVector &orthogonal_proxy_to_source = (orient == Qt::Horizontal) ? m->source_rows : m->source_columns; - QVector &orthogonal_source_to_proxy = (orient == Qt::Horizontal) ? m->proxy_rows : m->proxy_columns; + QList &orthogonal_proxy_to_source = (orient == Qt::Horizontal) ? m->source_rows : m->source_columns; + QList &orthogonal_source_to_proxy = (orient == Qt::Horizontal) ? m->proxy_rows : m->proxy_columns; if (orthogonal_source_to_proxy.isEmpty()) { const int ortho_end = (orient == Qt::Horizontal) ? model->rowCount(source_parent) : model->columnCount(source_parent); @@ -965,11 +965,11 @@ void QSortFilterProxyModelPrivate::source_items_about_to_be_removed( } Mapping *m = it.value(); - QVector &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; - QVector &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; + QList &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; + QList &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; // figure out which items to remove - QVector source_items_to_remove; + QList source_items_to_remove; int proxy_count = proxy_to_source.size(); for (int proxy_item = 0; proxy_item < proxy_count; ++proxy_item) { int source_item = proxy_to_source.at(proxy_item); @@ -998,8 +998,8 @@ void QSortFilterProxyModelPrivate::source_items_removed( } Mapping *m = it.value(); - QVector &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; - QVector &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; + QList &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; + QList &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; if (end >= source_to_proxy.size()) end = source_to_proxy.size() - 1; @@ -1042,8 +1042,8 @@ 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 - QVector > moved_source_index_mappings; - QVector::iterator it2 = parent_mapping->mapped_children.begin(); + QList> moved_source_index_mappings; + auto it2 = parent_mapping->mapped_children.begin(); for ( ; it2 != parent_mapping->mapped_children.end();) { const QModelIndex source_child_index = *it2; const int pos = (orient == Qt::Vertical) @@ -1081,10 +1081,9 @@ void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &sour } // reinsert moved, mapped indexes - QVector >::iterator it = moved_source_index_mappings.begin(); - for (; it != moved_source_index_mappings.end(); ++it) { - it->second->source_parent = it->first; - source_index_mapping.insert(it->first, it->second); + for (auto &pair : qAsConst(moved_source_index_mappings)) { + pair.second->source_parent = pair.first; + source_index_mapping.insert(pair.first, pair.second); } } @@ -1092,7 +1091,7 @@ void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &sour \internal */ void QSortFilterProxyModelPrivate::proxy_item_range( - const QVector &source_to_proxy, const QVector &source_items, + const QList &source_to_proxy, const QList &source_items, int &proxy_low, int &proxy_high) const { proxy_low = INT_MAX; @@ -1111,7 +1110,7 @@ void QSortFilterProxyModelPrivate::proxy_item_range( \internal */ void QSortFilterProxyModelPrivate::build_source_to_proxy_mapping( - const QVector &proxy_to_source, QVector &source_to_proxy, int start) + const QList &proxy_to_source, QList &source_to_proxy, int start) { if (start == 0) source_to_proxy.fill(-1); @@ -1197,8 +1196,8 @@ void QSortFilterProxyModelPrivate::filter_changed(Direction dir, const QModelInd // the iterator it2. // The m->mapped_children vector can be appended to with indexes which are no longer filtered // out (in create_mapping) when this function recurses for child indexes. - const QVector mappedChildren = m->mapped_children; - QVector indexesToRemove; + const QList mappedChildren = m->mapped_children; + QList indexesToRemove; for (int i = 0; i < mappedChildren.size(); ++i) { const QModelIndex &source_child_index = mappedChildren.at(i); if (rows_removed.contains(source_child_index.row()) || columns_removed.contains(source_child_index.column())) { @@ -1208,8 +1207,8 @@ void QSortFilterProxyModelPrivate::filter_changed(Direction dir, const QModelInd filter_changed(dir, source_child_index); } } - QVector::const_iterator removeIt = indexesToRemove.constEnd(); - const QVector::const_iterator removeBegin = indexesToRemove.constBegin(); + QList::const_iterator removeIt = indexesToRemove.constEnd(); + const QList::const_iterator removeBegin = indexesToRemove.constBegin(); // We can't just remove these items from mappedChildren while iterating above and then // do something like m->mapped_children = mappedChildren, because mapped_children might @@ -1227,12 +1226,12 @@ void QSortFilterProxyModelPrivate::filter_changed(Direction dir, const QModelInd returns the removed items indexes */ QSet QSortFilterProxyModelPrivate::handle_filter_changed( - QVector &source_to_proxy, QVector &proxy_to_source, + QList &source_to_proxy, QList &proxy_to_source, const QModelIndex &source_parent, Qt::Orientation orient) { Q_Q(QSortFilterProxyModel); // Figure out which mapped items to remove - QVector source_items_remove; + QList source_items_remove; for (int i = 0; i < proxy_to_source.count(); ++i) { const int source_item = proxy_to_source.at(i); if ((orient == Qt::Vertical) @@ -1243,7 +1242,7 @@ QSet QSortFilterProxyModelPrivate::handle_filter_changed( } } // Figure out which non-mapped items to insert - QVector source_items_insert; + QList source_items_insert; int source_count = source_to_proxy.size(); for (int source_item = 0; source_item < source_count; ++source_item) { if (source_to_proxy.at(source_item) == -1) { @@ -1264,10 +1263,10 @@ QSet QSortFilterProxyModelPrivate::handle_filter_changed( insert_source_items(source_to_proxy, proxy_to_source, source_items_insert, source_parent, orient); } - return qVectorToSet(source_items_remove); + return qListToSet(source_items_remove); } -bool QSortFilterProxyModelPrivate::needsReorder(const QVector &source_rows, const QModelIndex &source_parent) const +bool QSortFilterProxyModelPrivate::needsReorder(const QList &source_rows, const QModelIndex &source_parent) const { Q_Q(const QSortFilterProxyModel); Q_ASSERT(source_sort_column != -1); @@ -1296,7 +1295,7 @@ bool QSortFilterProxyModelPrivate::needsReorder(const QVector &source_rows, void QSortFilterProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &source_top_left, const QModelIndex &source_bottom_right, - const QVector &roles) + const QList &roles) { Q_Q(QSortFilterProxyModel); if (!source_top_left.isValid() || !source_bottom_right.isValid()) @@ -1328,10 +1327,10 @@ void QSortFilterProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &sourc Mapping *m = it.value(); // Figure out how the source changes affect us - QVector source_rows_remove; - QVector source_rows_insert; - QVector source_rows_change; - QVector source_rows_resort; + QList source_rows_remove; + QList source_rows_insert; + QList source_rows_change; + QList source_rows_resort; int end = qMin(source_bottom_right.row(), m->proxy_rows.count() - 1); for (int source_row = source_top_left.row(); source_row <= end; ++source_row) { if (dynamic_sortfilter) { @@ -1361,8 +1360,8 @@ void QSortFilterProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &sourc if (!source_rows_remove.isEmpty()) { remove_source_items(m->proxy_rows, m->source_rows, source_rows_remove, source_parent, Qt::Vertical); - QSet source_rows_remove_set = qVectorToSet(source_rows_remove); - QVector::iterator childIt = m->mapped_children.end(); + QSet source_rows_remove_set = qListToSet(source_rows_remove); + QList::iterator childIt = m->mapped_children.end(); while (childIt != m->mapped_children.begin()) { --childIt; const QModelIndex source_child_index = *childIt; @@ -1433,14 +1432,14 @@ void QSortFilterProxyModelPrivate::_q_sourceHeaderDataChanged(Qt::Orientation or Q_Q(QSortFilterProxyModel); Mapping *m = create_mapping(QModelIndex()).value(); - const QVector &source_to_proxy = (orientation == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; + const QList &source_to_proxy = (orientation == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; - QVector proxy_positions; + QList proxy_positions; proxy_positions.reserve(end - start + 1); { Q_ASSERT(source_to_proxy.size() > end); - QVector::const_iterator it = source_to_proxy.constBegin() + start; - const QVector::const_iterator endIt = source_to_proxy.constBegin() + end + 1; + QList::const_iterator it = source_to_proxy.constBegin() + start; + const QList::const_iterator endIt = source_to_proxy.constBegin() + end + 1; for ( ; it != endIt; ++it) { if (*it != -1) proxy_positions.push_back(*it); @@ -1594,7 +1593,7 @@ void QSortFilterProxyModelPrivate::_q_sourceRowsInserted( return; // last_top_source should now become visible - _q_sourceDataChanged(last_top_source, last_top_source, QVector()); + _q_sourceDataChanged(last_top_source, last_top_source, QList()); } } @@ -1630,7 +1629,7 @@ void QSortFilterProxyModelPrivate::_q_sourceRowsRemoved( } if (to_hide.isValid()) - _q_sourceDataChanged(to_hide, to_hide, QVector()); + _q_sourceDataChanged(to_hide, to_hide, QList()); } } @@ -1934,8 +1933,8 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) beginResetModel(); - disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), - this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QVector))); + disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QList)), + this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QList))); disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); @@ -1991,8 +1990,8 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) QAbstractProxyModel::setSourceModel(sourceModel); - connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), - this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QVector))); + connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QList)), + this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QList))); connect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); @@ -2311,7 +2310,7 @@ bool QSortFilterProxyModel::removeRows(int row, int count, const QModelIndex &pa } // remove corresponding source intervals // ### if this proves to be slow, we can switch to single-row removal - QVector rows; + QList rows; rows.reserve(count); for (int i = row; i < row + count; ++i) rows.append(m->source_rows.at(i)); @@ -2351,7 +2350,7 @@ bool QSortFilterProxyModel::removeColumns(int column, int count, const QModelInd return d->model->removeColumns(source_column, count, source_parent); } // remove corresponding source intervals - QVector columns; + QList columns; columns.reserve(count); for (int i = column; i < column + count; ++i) columns.append(m->source_columns.at(i)); -- cgit v1.2.3