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>
bb10
Ulf Hermann 2019-10-11 13:39:46 +02:00
parent 19f29802bf
commit a7df98a9a7
1 changed files with 30 additions and 10 deletions

View File

@ -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();