From 1aaec41c051d107811924098580e08ae33890885 Mon Sep 17 00:00:00 2001 From: Alex Jbanca Date: Tue, 6 Feb 2024 09:21:30 +0200 Subject: [PATCH] fix: Revert commit - Improve sorting performance The proper fix will be handled in another task --- qqmlsortfilterproxymodel.cpp | 11 ++++++----- sorters/expressionsorter.cpp | 1 - sorters/filtersorter.cpp | 2 -- sorters/sorter.cpp | 15 ++++++++------- sorters/sorter.h | 4 +--- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/qqmlsortfilterproxymodel.cpp b/qqmlsortfilterproxymodel.cpp index 4e87ff1..3ebca88 100644 --- a/qqmlsortfilterproxymodel.cpp +++ b/qqmlsortfilterproxymodel.cpp @@ -350,10 +350,10 @@ bool QQmlSortFilterProxyModel::lessThan(const QModelIndex& source_left, const QM { if (m_completed) { if (!m_sortRoleName.isEmpty()) { - if (m_ascendingSortOrder) - return QSortFilterProxyModel::lessThan(source_left, source_right); - else - return QSortFilterProxyModel::lessThan(source_right, source_left); + if (QSortFilterProxyModel::lessThan(source_left, source_right)) + return m_ascendingSortOrder; + if (QSortFilterProxyModel::lessThan(source_right, source_left)) + return !m_ascendingSortOrder; } auto sortedSorters = m_sorters; std::stable_sort(sortedSorters.begin(), @@ -364,7 +364,8 @@ bool QQmlSortFilterProxyModel::lessThan(const QModelIndex& source_left, const QM for(auto sorter : sortedSorters) { if (sorter->enabled()) { int comparison = sorter->compareRows(source_left, source_right, *this); - return comparison == -1; + if (comparison != 0) + return comparison < 0; } } } diff --git a/sorters/expressionsorter.cpp b/sorters/expressionsorter.cpp index 2e4c230..0d2904d 100644 --- a/sorters/expressionsorter.cpp +++ b/sorters/expressionsorter.cpp @@ -59,7 +59,6 @@ void ExpressionSorter::setExpression(const QQmlScriptString& scriptString) void ExpressionSorter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) { - Sorter::proxyModelCompleted(proxyModel); updateContext(proxyModel); } diff --git a/sorters/filtersorter.cpp b/sorters/filtersorter.cpp index d902dd2..29025f1 100644 --- a/sorters/filtersorter.cpp +++ b/sorters/filtersorter.cpp @@ -48,8 +48,6 @@ int FilterSorter::compare(const QModelIndex& sourceLeft, const QModelIndex& sour void FilterSorter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) { - Sorter::proxyModelCompleted(proxyModel); - for (Filter* filter : qAsConst(m_filters)) filter->proxyModelCompleted(proxyModel); } diff --git a/sorters/sorter.cpp b/sorters/sorter.cpp index 6e7e36e..e9d0547 100644 --- a/sorters/sorter.cpp +++ b/sorters/sorter.cpp @@ -107,20 +107,21 @@ void Sorter::setPriority(int priority) int Sorter::compareRows(const QModelIndex &source_left, const QModelIndex &source_right, const QQmlSortFilterProxyModel& proxyModel) const { - if (m_sortOrder == Qt::AscendingOrder) - return compare(source_left, source_right, proxyModel); - else - return compare(source_right, source_left, proxyModel); + int comparison = compare(source_left, source_right, proxyModel); + return (m_sortOrder == Qt::AscendingOrder) ? comparison : -comparison; } int Sorter::compare(const QModelIndex &sourceLeft, const QModelIndex &sourceRight, const QQmlSortFilterProxyModel& proxyModel) const { - return lessThan(sourceLeft, sourceRight, proxyModel) ? -1 : 1; + if (lessThan(sourceLeft, sourceRight, proxyModel)) + return -1; + if (lessThan(sourceRight, sourceLeft, proxyModel)) + return 1; + return 0; } void Sorter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) { - m_proxyCompleted = true; Q_UNUSED(proxyModel) } @@ -134,7 +135,7 @@ bool Sorter::lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRi void Sorter::invalidate() { - if (m_enabled && m_proxyCompleted) + if (m_enabled) Q_EMIT invalidated(); } diff --git a/sorters/sorter.h b/sorters/sorter.h index db9ddac..f7ecd88 100644 --- a/sorters/sorter.h +++ b/sorters/sorter.h @@ -45,14 +45,12 @@ Q_SIGNALS: protected: virtual int compare(const QModelIndex& sourceLeft, const QModelIndex& sourceRight, const QQmlSortFilterProxyModel& proxyModel) const; virtual bool lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight, const QQmlSortFilterProxyModel& proxyModel) const; - virtual void invalidate(); + void invalidate(); private: bool m_enabled = true; Qt::SortOrder m_sortOrder = Qt::AscendingOrder; int m_priority = 0; - - bool m_proxyCompleted = false; }; }