Itemmodels: s/QPair/std::pair/

Also port from qMakePair() to just braced initialization and CTAD.

As a drive-by, use emplacement instead of appending pairs explicitly,
rewrite typedef into `using`, and remove the qpair.h include from the
.cpp file (cannot affect other TUs and <utility> is considered
included in qcompilerdetection.h).

Task-number: QTBUG-115841
Change-Id: Ia985520dfce6b77b1c0fe7669fc92d2df97e1b06
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit 91d341e560e9a54d201279d53701ff071256ad6c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-12-12 09:58:57 +01:00 committed by Qt Cherry-pick Bot
parent b63f92bb0f
commit 41c9754b4c
4 changed files with 28 additions and 29 deletions

View File

@ -238,7 +238,7 @@ QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange &
*/ */
static void rowLengthsFromRange(const QItemSelectionRange &range, QList<QPair<QPersistentModelIndex, uint>> &result) static void rowLengthsFromRange(const QItemSelectionRange &range, QList<std::pair<QPersistentModelIndex, uint>> &result)
{ {
if (range.isValid() && range.model()) { if (range.isValid() && range.model()) {
const QModelIndex topLeft = range.topLeft(); const QModelIndex topLeft = range.topLeft();
@ -249,7 +249,7 @@ static void rowLengthsFromRange(const QItemSelectionRange &range, QList<QPair<QP
// We don't need to keep track of ItemIsSelectable and ItemIsEnabled here. That is // We don't need to keep track of ItemIsSelectable and ItemIsEnabled here. That is
// required in indexesFromRange() because that method is called from public API // required in indexesFromRange() because that method is called from public API
// which requires the limitation. // which requires the limitation.
result.push_back(qMakePair(QPersistentModelIndex(topLeft.sibling(row, column)), width)); result.emplace_back(topLeft.sibling(row, column), width);
} }
} }
} }
@ -432,9 +432,9 @@ QModelIndexList QItemSelection::indexes() const
return qSelectionIndexes<QModelIndexList>(*this); return qSelectionIndexes<QModelIndexList>(*this);
} }
static QList<QPair<QPersistentModelIndex, uint>> qSelectionPersistentRowLengths(const QItemSelection &sel) static QList<std::pair<QPersistentModelIndex, uint>> qSelectionPersistentRowLengths(const QItemSelection &sel)
{ {
QList<QPair<QPersistentModelIndex, uint>> result; QList<std::pair<QPersistentModelIndex, uint>> result;
for (const QItemSelectionRange &range : sel) for (const QItemSelectionRange &range : sel)
rowLengthsFromRange(range, result); rowLengthsFromRange(range, result);
return result; return result;
@ -875,7 +875,7 @@ void QItemSelectionModelPrivate::layoutAboutToBeChanged(const QList<QPersistentM
/*! /*!
\internal \internal
*/ */
static QItemSelection mergeRowLengths(const QList<QPair<QPersistentModelIndex, uint>> &rowLengths) static QItemSelection mergeRowLengths(const QList<std::pair<QPersistentModelIndex, uint>> &rowLengths)
{ {
if (rowLengths.isEmpty()) if (rowLengths.isEmpty())
return QItemSelection(); return QItemSelection();

View File

@ -82,8 +82,8 @@ public:
QItemSelectionModel::SelectionFlags currentCommand; QItemSelectionModel::SelectionFlags currentCommand;
QList<QPersistentModelIndex> savedPersistentIndexes; QList<QPersistentModelIndex> savedPersistentIndexes;
QList<QPersistentModelIndex> savedPersistentCurrentIndexes; QList<QPersistentModelIndex> savedPersistentCurrentIndexes;
QList<QPair<QPersistentModelIndex, uint>> savedPersistentRowLengths; QList<std::pair<QPersistentModelIndex, uint>> savedPersistentRowLengths;
QList<QPair<QPersistentModelIndex, uint>> savedPersistentCurrentRowLengths; QList<std::pair<QPersistentModelIndex, uint>> savedPersistentCurrentRowLengths;
// optimization when all indexes are selected // optimization when all indexes are selected
bool tableSelected; bool tableSelected;
QPersistentModelIndex tableParent; QPersistentModelIndex tableParent;

View File

@ -6,7 +6,6 @@
#include <qsize.h> #include <qsize.h>
#include <qdebug.h> #include <qdebug.h>
#include <qdatetime.h> #include <qdatetime.h>
#include <qpair.h>
#include <qstringlist.h> #include <qstringlist.h>
#include <private/qabstractitemmodel_p.h> #include <private/qabstractitemmodel_p.h>
#include <private/qabstractproxymodel_p.h> #include <private/qabstractproxymodel_p.h>
@ -16,7 +15,7 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
typedef QList<QPair<QModelIndex, QPersistentModelIndex>> QModelIndexPairList; using QModelIndexPairList = QList<std::pair<QModelIndex, QPersistentModelIndex>>;
struct QSortFilterProxyModelDataChanged struct QSortFilterProxyModelDataChanged
{ {
@ -333,10 +332,10 @@ public:
int find_source_sort_column() const; int find_source_sort_column() const;
void sort_source_rows(QList<int> &source_rows, void sort_source_rows(QList<int> &source_rows,
const QModelIndex &source_parent) const; 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 QList<int> &proxy_to_source, const QList<int> &source_items,
const QModelIndex &source_parent, Qt::Orientation orient) const; 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; const QList<int> &source_to_proxy, const QList<int> &source_items) const;
void insert_source_items( void insert_source_items(
QList<int> &source_to_proxy, QList<int> &proxy_to_source, QList<int> &source_to_proxy, QList<int> &proxy_to_source,
@ -705,10 +704,10 @@ void QSortFilterProxyModelPrivate::sort_source_rows(
The result is a vector of pairs, where each pair represents a The result is a vector of pairs, where each pair represents a
(start, end) tuple, sorted in ascending order. (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 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()) if (source_items.isEmpty())
return proxy_intervals; return proxy_intervals;
@ -725,19 +724,19 @@ QList<QPair<int, int>> QSortFilterProxyModelPrivate::proxy_intervals_for_source_
++source_items_index; ++source_items_index;
} }
// Add interval to result // 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()); std::stable_sort(proxy_intervals.begin(), proxy_intervals.end());
// Consolidate adjacent intervals // Consolidate adjacent intervals
for (int i = proxy_intervals.size()-1; i > 0; --i) { for (int i = proxy_intervals.size()-1; i > 0; --i) {
QPair<int, int> &interval = proxy_intervals[i]; std::pair<int, int> &interval = proxy_intervals[i];
QPair<int, int> &preceeding_interval = proxy_intervals[i - 1]; std::pair<int, int> &preceeding_interval = proxy_intervals[i - 1];
if (interval.first == preceeding_interval.second + 1) { if (interval.first == preceeding_interval.second + 1) {
preceeding_interval.second = interval.second; preceeding_interval.second = interval.second;
interval.first = interval.second = -1; 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; return proxy_intervals;
} }
@ -766,7 +765,7 @@ void QSortFilterProxyModelPrivate::remove_source_items(
const auto end = proxy_intervals.rend(); const auto end = proxy_intervals.rend();
for (auto it = proxy_intervals.rbegin(); it != end; ++it) { 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_start = interval.first;
const int proxy_end = interval.second; const int proxy_end = interval.second;
remove_proxy_interval(source_to_proxy, proxy_to_source, proxy_start, proxy_end, remove_proxy_interval(source_to_proxy, proxy_to_source, proxy_start, proxy_end,
@ -820,12 +819,12 @@ void QSortFilterProxyModelPrivate::remove_proxy_interval(
items), where items is a vector containing the (sorted) source items that items), where items is a vector containing the (sorted) source items that
should be inserted at that proxy model location. 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 QList<int> &proxy_to_source, const QList<int> &source_items,
const QModelIndex &source_parent, Qt::Orientation orient) const const QModelIndex &source_parent, Qt::Orientation orient) const
{ {
Q_Q(const QSortFilterProxyModel); Q_Q(const QSortFilterProxyModel);
QList<QPair<int, QList<int>>> proxy_intervals; QList<std::pair<int, QList<int>>> proxy_intervals;
if (source_items.isEmpty()) if (source_items.isEmpty())
return proxy_intervals; return proxy_intervals;
@ -880,7 +879,7 @@ QList<QPair<int, QList<int>>> QSortFilterProxyModelPrivate::proxy_intervals_for_
} }
// Add interval to result // 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; return proxy_intervals;
} }
@ -908,7 +907,7 @@ void QSortFilterProxyModelPrivate::insert_source_items(
const auto end = proxy_intervals.rend(); const auto end = proxy_intervals.rend();
for (auto it = proxy_intervals.rbegin(); it != end; ++it) { 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 int proxy_start = interval.first;
const QList<int> &source_items = interval.second; const QList<int> &source_items = interval.second;
const int proxy_end = proxy_start + source_items.size() - 1; const int proxy_end = proxy_start + source_items.size() - 1;
@ -1137,7 +1136,7 @@ void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &sour
Qt::Orientation orient, int start, int end, int delta_item_count, bool remove) Qt::Orientation orient, int start, int end, int delta_item_count, bool remove)
{ {
// see if any mapped children should be (re)moved // 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(); auto it2 = parent_mapping->mapped_children.begin();
for ( ; it2 != parent_mapping->mapped_children.end();) { for ( ; it2 != parent_mapping->mapped_children.end();) {
const QModelIndex source_child_index = *it2; const QModelIndex source_child_index = *it2;
@ -1171,7 +1170,7 @@ void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &sour
Mapping *cm = source_index_mapping.take(source_child_index); Mapping *cm = source_index_mapping.take(source_child_index);
Q_ASSERT(cm); Q_ASSERT(cm);
// we do not reinsert right away, because the new index might be identical with another, old index // 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);
} }
} }
@ -1228,7 +1227,7 @@ QModelIndexPairList QSortFilterProxyModelPrivate::store_persistent_indexes() con
for (const QPersistentModelIndexData *data : std::as_const(persistent.indexes)) { for (const QPersistentModelIndexData *data : std::as_const(persistent.indexes)) {
const QModelIndex &proxy_index = data->index; const QModelIndex &proxy_index = data->index;
QModelIndex source_index = q->mapToSource(proxy_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; return source_indexes;
} }

View File

@ -292,12 +292,12 @@ bool QStringListModel::moveRows(const QModelIndex &sourceParent, int sourceRow,
return true; return true;
} }
static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) static bool ascendingLessThan(const std::pair<QString, int> &s1, const std::pair<QString, int> &s2)
{ {
return s1.first < s2.first; return s1.first < s2.first;
} }
static bool decendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) static bool decendingLessThan(const std::pair<QString, int> &s1, const std::pair<QString, int> &s2)
{ {
return s1.first > s2.first; return s1.first > s2.first;
} }
@ -309,11 +309,11 @@ void QStringListModel::sort(int, Qt::SortOrder order)
{ {
emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint); emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
QList<QPair<QString, int>> list; QList<std::pair<QString, int>> list;
const int lstCount = lst.size(); const int lstCount = lst.size();
list.reserve(lstCount); list.reserve(lstCount);
for (int i = 0; i < lstCount; ++i) for (int i = 0; i < lstCount; ++i)
list.append(QPair<QString, int>(lst.at(i), i)); list.emplace_back(lst.at(i), i);
if (order == Qt::AscendingOrder) if (order == Qt::AscendingOrder)
std::sort(list.begin(), list.end(), ascendingLessThan); std::sort(list.begin(), list.end(), ascendingLessThan);