diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2019-10-11 13:39:46 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2019-10-18 08:36:36 +0200 |
commit | a7df98a9a72ee460c6bd23388d52b1200e7ed3ba (patch) | |
tree | 8a596b740dd79883588c0b5127a1a39af46afcec /src/corelib/itemmodels | |
parent | 19f29802bf7daafacd0fd824c2a1349e80eac536 (diff) |
QSortFilterProxyModel: Add a cheaper way to find source_sort_column
There are two places where we are only interested in the mapping of
proxy to source sort column, rather than the full mapping. Creating the
full mapping is rather expensive as it iterates all rows and columns and
allocates a large number of objects. Just figuring out the n-th accepted
column can be much cheaper.
Fixes: QTBUG-41659
Change-Id: I7ea914cb695518b4d47cdc3ad67c7786380d8709
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
Diffstat (limited to 'src/corelib/itemmodels')
-rw-r--r-- | src/corelib/itemmodels/qsortfilterproxymodel.cpp | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 675bf4b8c3..978102035e 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -377,6 +377,7 @@ public: void sort(); bool update_source_sort_column(); + int find_source_sort_column() const; void sort_source_rows(QVector<int> &source_rows, const QModelIndex &source_parent) const; QVector<QPair<int, QVector<int > > > proxy_intervals_for_source_items_to_add( @@ -479,11 +480,8 @@ void QSortFilterProxyModelPrivate::_q_clearMapping() qDeleteAll(source_index_mapping); source_index_mapping.clear(); - if (dynamic_sortfilter && update_source_sort_column()) { - //update_source_sort_column might have created wrong mapping so we have to clear it again - qDeleteAll(source_index_mapping); - source_index_mapping.clear(); - } + if (dynamic_sortfilter) + source_sort_column = find_source_sort_column(); // update the persistent indexes update_persistent_indexes(source_indexes); @@ -640,6 +638,31 @@ bool QSortFilterProxyModelPrivate::update_source_sort_column() return old_source_sort_column != source_sort_column; } +/*! + \internal + + Find the source_sort_column without creating a full mapping and + without updating anything. +*/ +int QSortFilterProxyModelPrivate::find_source_sort_column() const +{ + if (proxy_sort_column == -1) + return -1; + + const QModelIndex rootIndex; + const int source_cols = model->columnCount(); + int accepted_columns = -1; + + Q_Q(const QSortFilterProxyModel); + for (int i = 0; i < source_cols; ++i) { + if (q->filterAcceptsColumn(i, rootIndex)) { + if (++accepted_columns == proxy_sort_column) + return i; + } + } + + return -1; +} /*! \internal @@ -1591,11 +1614,8 @@ void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged(const QList<QPersisten update_persistent_indexes(saved_persistent_indexes); saved_persistent_indexes.clear(); - if (dynamic_sortfilter && update_source_sort_column()) { - //update_source_sort_column might have created wrong mapping so we have to clear it again - qDeleteAll(source_index_mapping); - source_index_mapping.clear(); - } + if (dynamic_sortfilter) + source_sort_column = find_source_sort_column(); emit q->layoutChanged(saved_layoutChange_parents); saved_layoutChange_parents.clear(); |