From 7f4cb43a334f0ddfc1fef80f390954ca9d609826 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Mon, 15 Jul 2024 08:42:02 +0300 Subject: [PATCH] Fix incorrect sorting by "private" column PR #21041. --- src/gui/transferlistsortmodel.cpp | 53 ++++++++++++++++++++++----------------- src/webui/api/synccontroller.cpp | 1 + 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/gui/transferlistsortmodel.cpp b/src/gui/transferlistsortmodel.cpp index 90b73044f..782aead74 100644 --- a/src/gui/transferlistsortmodel.cpp +++ b/src/gui/transferlistsortmodel.cpp @@ -28,7 +28,6 @@ #include "transferlistsortmodel.h" -#include #include #include @@ -47,11 +46,21 @@ namespace return (left < right) ? -1 : 1; } + int customCompare(const QDateTime &left, const QDateTime &right) + { + const bool isLeftValid = left.isValid(); + const bool isRightValid = right.isValid(); + + if (isLeftValid == isRightValid) + return threeWayCompare(left, right); + return isLeftValid ? -1 : 1; + } + int customCompare(const TagSet &left, const TagSet &right, const Utils::Compare::NaturalCompare &compare) { for (auto leftIter = left.cbegin(), rightIter = right.cbegin(); - (leftIter != left.cend()) && (rightIter != right.cend()); - ++leftIter, ++rightIter) + (leftIter != left.cend()) && (rightIter != right.cend()); + ++leftIter, ++rightIter) { const int result = compare(leftIter->toString(), rightIter->toString()); if (result != 0) @@ -61,27 +70,12 @@ namespace } template - concept Validateable = requires (T t) { {t.isValid()} -> std::same_as; }; - - template - bool isValid(const T &value) + int customCompare(const T left, const T right) { - return value.isValid(); - } + static_assert(std::is_arithmetic_v); - // consider negative values as invalid - template - requires std::is_arithmetic_v - bool isValid(const T value) - { - return (value >= 0); - } - - template - int customCompare(const T &left, const T &right) - { - const bool isLeftValid = isValid(left); - const bool isRightValid = isValid(right); + const bool isLeftValid = (left >= 0); + const bool isRightValid = (right >= 0); if (isLeftValid && isRightValid) return threeWayCompare(left, right); @@ -90,6 +84,17 @@ namespace return isLeftValid ? -1 : 1; } + int compareAsBool(const QVariant &left, const QVariant &right) + { + const bool leftValid = left.isValid(); + const bool rightValid = right.isValid(); + if (leftValid && rightValid) + return threeWayCompare(left.toBool(), right.toBool()); + if (!leftValid && !rightValid) + return 0; + return leftValid ? -1 : 1; + } + int adjustSubSortColumn(const int column) { return ((column >= 0) && (column < TransferListModel::NB_COLUMNS)) @@ -215,12 +220,14 @@ int TransferListSortModel::compare(const QModelIndex &left, const QModelIndex &r case TransferListModel::TR_DLLIMIT: case TransferListModel::TR_DLSPEED: - case TransferListModel::TR_PRIVATE: case TransferListModel::TR_QUEUE_POSITION: case TransferListModel::TR_UPLIMIT: case TransferListModel::TR_UPSPEED: return customCompare(leftValue.toInt(), rightValue.toInt()); + case TransferListModel::TR_PRIVATE: + return compareAsBool(leftValue, rightValue); + case TransferListModel::TR_PEERS: case TransferListModel::TR_SEEDS: { diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index fd81cd3fe..1ad55dbae 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -222,6 +222,7 @@ namespace case QMetaType::UInt: case QMetaType::QDateTime: case QMetaType::Nullptr: + case QMetaType::UnknownType: if (prevData[key] != value) syncData[key] = value; break; -- 2.11.4.GIT