From 2bd5aca3a48631229b4fed85c3b4308ddfe7dfe3 Mon Sep 17 00:00:00 2001 From: Dmitry Khlestkov Date: Mon, 14 Jun 2021 06:57:50 +0300 Subject: [PATCH] Keep sub-sorting order (#15074) Fixes #15073 --- src/gui/transferlistsortmodel.cpp | 30 ++++++++++++++++++++++-------- src/gui/transferlistsortmodel.h | 8 ++++++-- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/gui/transferlistsortmodel.cpp b/src/gui/transferlistsortmodel.cpp index f7e0a1fab..a3826cabc 100644 --- a/src/gui/transferlistsortmodel.cpp +++ b/src/gui/transferlistsortmodel.cpp @@ -96,10 +96,24 @@ namespace TransferListSortModel::TransferListSortModel(QObject *parent) : QSortFilterProxyModel {parent} , m_subSortColumn {"TransferList/SubSortColumn", TransferListModel::TR_NAME, adjustSubSortColumn} + , m_subSortOrder {"TransferList/SubSortOrder", 0} { setSortRole(TransferListModel::UnderlyingDataRole); } +void TransferListSortModel::sort(const int column, const Qt::SortOrder order) +{ + if ((m_lastSortColumn != column) && (m_lastSortColumn != -1)) + { + m_subSortColumn = m_lastSortColumn; + m_subSortOrder = m_lastSortOrder; + } + m_lastSortColumn = column; + m_lastSortOrder = ((order == Qt::AscendingOrder) ? 0 : 1); + + QSortFilterProxyModel::sort(column, order); +} + void TransferListSortModel::setStatusFilter(TorrentFilter::Type filter) { if (m_filter.setType(filter)) @@ -219,16 +233,16 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex { Q_ASSERT(left.column() == right.column()); - if (m_lastSortColumn != left.column()) - { - if (m_lastSortColumn != -1) - m_subSortColumn = m_lastSortColumn; - m_lastSortColumn = left.column(); - } - const int result = compare(left, right); if (result == 0) - return compare(left.sibling(left.row(), m_subSortColumn), right.sibling(right.row(), m_subSortColumn)) < 0; + { + const int subResult = compare(left.sibling(left.row(), m_subSortColumn), right.sibling(right.row(), m_subSortColumn)); + // Qt inverses lessThan() result when ordered descending. + // For sub-sorting we have to do it manually. + // When both are ordered descending subResult must be double-inversed, which is the same as no inversion. + const bool inverseSubResult = (m_lastSortOrder != m_subSortOrder); // exactly one is descending + return (inverseSubResult ? (subResult > 0) : (subResult < 0)); + } return result < 0; } diff --git a/src/gui/transferlistsortmodel.h b/src/gui/transferlistsortmodel.h index b860098b2..16dea7d65 100644 --- a/src/gui/transferlistsortmodel.h +++ b/src/gui/transferlistsortmodel.h @@ -47,6 +47,8 @@ class TransferListSortModel final : public QSortFilterProxyModel public: explicit TransferListSortModel(QObject *parent = nullptr); + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + void setStatusFilter(TorrentFilter::Type filter); void setCategoryFilter(const QString &category); void disableCategoryFilter(); @@ -63,8 +65,10 @@ private: bool matchFilter(int sourceRow, const QModelIndex &sourceParent) const; TorrentFilter m_filter; - mutable CachedSettingValue m_subSortColumn; - mutable int m_lastSortColumn = -1; + CachedSettingValue m_subSortColumn; + CachedSettingValue m_subSortOrder; + int m_lastSortColumn = -1; + int m_lastSortOrder = 0; Utils::Compare::NaturalCompare m_naturalCompare; };