diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2012-09-26 18:30:38 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-09-27 18:35:55 +0200 |
commit | edec823c00dfaedd4277a4a7d98a42a79df6deb5 (patch) | |
tree | b6095f48c749f8080b0714386795aa05a55d032c /src/corelib/itemmodels | |
parent | 2d97c6a4748a6a94ce28f86b69c03bf1a56536aa (diff) |
Avoid looping over the indexes multiple times to create a persistent list.
Change-Id: I089d272254eb531cd27c7b23fbab4d7183ba01d4
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Diffstat (limited to 'src/corelib/itemmodels')
-rw-r--r-- | src/corelib/itemmodels/qitemselectionmodel.cpp | 23 |
1 files 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<typename ModelIndexContainer> +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<QPersistentModelIndex> qSelectionPersistentindexes(const QItemSelection &sel) +{ + QList<QPersistentModelIndex> result; + QList<QItemSelectionRange>::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); } /*! |