From edec823c00dfaedd4277a4a7d98a42a79df6deb5 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 26 Sep 2012 18:30:38 +0200 Subject: [PATCH] Avoid looping over the indexes multiple times to create a persistent list. Change-Id: I089d272254eb531cd27c7b23fbab4d7183ba01d4 Reviewed-by: Marc Mutz Reviewed-by: Stephen Kelly --- .../itemmodels/qitemselectionmodel.cpp | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index 254e952b8c..6932df8491 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -291,7 +291,8 @@ QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange & it avoid concatenating list and works on one */ -static void indexesFromRange(const QItemSelectionRange &range, QModelIndexList &result) +template +static void indexesFromRange(const QItemSelectionRange &range, ModelIndexContainer &result) { if (range.isValid() && range.model()) { const QModelIndex topLeft = range.topLeft(); @@ -303,7 +304,7 @@ static void indexesFromRange(const QItemSelectionRange &range, QModelIndexList & QModelIndex index = columnLeader.sibling(row, column); Qt::ItemFlags flags = range.model()->flags(index); if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) - result.append(index); + result.push_back(index); } } } @@ -458,6 +459,15 @@ QModelIndexList QItemSelection::indexes() const return result; } +static QList qSelectionPersistentindexes(const QItemSelection &sel) +{ + QList result; + QList::const_iterator it = sel.constBegin(); + for (; it != sel.constEnd(); ++it) + indexesFromRange(*it, result); + return result; +} + /*! Merges the \a other selection with this QItemSelection using the \a command given. This method guarantees that no ranges are overlapping. @@ -826,13 +836,8 @@ void QItemSelectionModelPrivate::_q_layoutAboutToBeChanged() } tableSelected = false; - QModelIndexList indexes = ranges.indexes(); - QModelIndexList::const_iterator it; - for (it = indexes.constBegin(); it != indexes.constEnd(); ++it) - savedPersistentIndexes.append(QPersistentModelIndex(*it)); - indexes = currentSelection.indexes(); - for (it = indexes.constBegin(); it != indexes.constEnd(); ++it) - savedPersistentCurrentIndexes.append(QPersistentModelIndex(*it)); + savedPersistentIndexes = qSelectionPersistentindexes(ranges); + savedPersistentCurrentIndexes = qSelectionPersistentindexes(currentSelection); } /*!