Skip to content

Commit

Permalink
Fix incorrect sorting by "private" column
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Jul 13, 2024
1 parent eba5cbb commit 673b9fb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/gui/transferlistsortmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include "transferlistsortmodel.h"

#include <concepts>
#include <type_traits>

#include <QDateTime>
Expand All @@ -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<Qt::CaseInsensitive> &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)
Expand All @@ -61,27 +70,12 @@ namespace
}

template <typename T>
concept Validateable = requires (T t) { {t.isValid()} -> std::same_as<bool>; };

template <Validateable T>
bool isValid(const T &value)
int customCompare(const T left, const T right)
{
return value.isValid();
}
static_assert(std::is_arithmetic_v<T>);

// consider negative values as invalid
template <typename T>
requires std::is_arithmetic_v<T>
bool isValid(const T value)
{
return (value >= 0);
}

template <typename T>
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);
Expand All @@ -90,6 +84,18 @@ 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 left
Valid ? -1 : 1;
}

int adjustSubSortColumn(const int column)
{
return ((column >= 0) && (column < TransferListModel::NB_COLUMNS))
Expand Down Expand Up @@ -215,12 +221,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:
{
Expand Down
1 change: 1 addition & 0 deletions src/webui/api/synccontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 673b9fb

Please sign in to comment.