fix size column sorting

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2024-06-09 23:53:40 +03:00
parent 34e1a5e660
commit 242ddbb7e1
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
9 changed files with 48 additions and 62 deletions

View File

@ -65,29 +65,24 @@ std::pair<Version, Version> DataPack::compatibleVersions() const
return s_pack_format_versions.constFind(m_pack_format).value(); return s_pack_format_versions.constFind(m_pack_format).value();
} }
std::pair<int, bool> DataPack::compare(const Resource& other, SortType type) const int DataPack::compare(const Resource& other, SortType type, Qt::SortOrder order) const
{ {
auto const& cast_other = static_cast<DataPack const&>(other); auto const& cast_other = static_cast<DataPack const&>(other);
switch (type) { switch (type) {
default: { default:
auto res = Resource::compare(other, type); return Resource::compare(other, type, order);
if (res.first != 0)
return res;
break;
}
case SortType::PACK_FORMAT: { case SortType::PACK_FORMAT: {
auto this_ver = packFormat(); auto this_ver = packFormat();
auto other_ver = cast_other.packFormat(); auto other_ver = cast_other.packFormat();
if (this_ver > other_ver) if (this_ver > other_ver)
return { 1, type == SortType::PACK_FORMAT }; return 1;
if (this_ver < other_ver) if (this_ver < other_ver)
return { -1, type == SortType::PACK_FORMAT }; return -1;
break; break;
} }
} }
return { 0, false }; return 0;
} }
bool DataPack::applyFilter(QRegularExpression filter) const bool DataPack::applyFilter(QRegularExpression filter) const

View File

@ -56,7 +56,7 @@ class DataPack : public Resource {
bool valid() const override; bool valid() const override;
[[nodiscard]] auto compare(Resource const& other, SortType type) const -> std::pair<int, bool> override; [[nodiscard]] int compare(Resource const& other, SortType type, Qt::SortOrder order = Qt::SortOrder::AscendingOrder) const override;
[[nodiscard]] bool applyFilter(QRegularExpression filter) const override; [[nodiscard]] bool applyFilter(QRegularExpression filter) const override;
protected: protected:

View File

@ -78,41 +78,33 @@ void Mod::setDetails(const ModDetails& details)
m_local_details = details; m_local_details = details;
} }
std::pair<int, bool> Mod::compare(const Resource& other, SortType type) const int Mod::compare(const Resource& other, SortType type, Qt::SortOrder order) const
{ {
auto cast_other = dynamic_cast<Mod const*>(&other); auto cast_other = dynamic_cast<Mod const*>(&other);
if (!cast_other) if (!cast_other)
return Resource::compare(other, type); return Resource::compare(other, type, order);
switch (type) { switch (type) {
default: default:
case SortType::ENABLED: case SortType::ENABLED:
case SortType::NAME: case SortType::NAME:
case SortType::DATE: case SortType::DATE:
case SortType::SIZE: { case SortType::SIZE:
auto res = Resource::compare(other, type); return Resource::compare(other, type, order);
if (res.first != 0)
return res;
break;
}
case SortType::VERSION: { case SortType::VERSION: {
auto this_ver = Version(version()); auto this_ver = Version(version());
auto other_ver = Version(cast_other->version()); auto other_ver = Version(cast_other->version());
if (this_ver > other_ver) if (this_ver > other_ver)
return { 1, type == SortType::VERSION }; return 1;
if (this_ver < other_ver) if (this_ver < other_ver)
return { -1, type == SortType::VERSION }; return -1;
break; break;
} }
case SortType::PROVIDER: { case SortType::PROVIDER: {
auto compare_result = return QString::compare(provider().value_or("Unknown"), cast_other->provider().value_or("Unknown"), Qt::CaseInsensitive);
QString::compare(provider().value_or("Unknown"), cast_other->provider().value_or("Unknown"), Qt::CaseInsensitive);
if (compare_result != 0)
return { compare_result, type == SortType::PROVIDER };
break;
} }
} }
return { 0, false }; return 0;
} }
bool Mod::applyFilter(QRegularExpression filter) const bool Mod::applyFilter(QRegularExpression filter) const

View File

@ -88,7 +88,7 @@ class Mod : public Resource {
bool valid() const override; bool valid() const override;
[[nodiscard]] auto compare(Resource const& other, SortType type) const -> std::pair<int, bool> override; [[nodiscard]] int compare(Resource const& other, SortType type, Qt::SortOrder order = Qt::SortOrder::AscendingOrder) const override;
[[nodiscard]] bool applyFilter(QRegularExpression filter) const override; [[nodiscard]] bool applyFilter(QRegularExpression filter) const override;
// Delete all the files of this mod // Delete all the files of this mod

View File

@ -30,7 +30,7 @@ std::tuple<QString, qint64> calculateFileSize(const QFileInfo& file)
auto str = QObject::tr("item"); auto str = QObject::tr("item");
if (count != 1) if (count != 1)
str = QObject::tr("items"); str = QObject::tr("items");
return { QString("%1 %2").arg(QString::number(count), str), -count }; return { QString("%1 %2").arg(QString::number(count), str), count };
} }
return { StringUtils::humanReadableFileSize(file.size(), true), file.size() }; return { StringUtils::humanReadableFileSize(file.size(), true), file.size() };
} }
@ -79,15 +79,15 @@ static void removeThePrefix(QString& string)
string = string.trimmed(); string = string.trimmed();
} }
std::pair<int, bool> Resource::compare(const Resource& other, SortType type) const int Resource::compare(const Resource& other, SortType type, Qt::SortOrder order) const
{ {
switch (type) { switch (type) {
default: default:
case SortType::ENABLED: case SortType::ENABLED:
if (enabled() && !other.enabled()) if (enabled() && !other.enabled())
return { 1, type == SortType::ENABLED }; return 1;
if (!enabled() && other.enabled()) if (!enabled() && other.enabled())
return { -1, type == SortType::ENABLED }; return -1;
break; break;
case SortType::NAME: { case SortType::NAME: {
QString this_name{ name() }; QString this_name{ name() };
@ -96,27 +96,32 @@ std::pair<int, bool> Resource::compare(const Resource& other, SortType type) con
removeThePrefix(this_name); removeThePrefix(this_name);
removeThePrefix(other_name); removeThePrefix(other_name);
auto compare_result = QString::compare(this_name, other_name, Qt::CaseInsensitive); return QString::compare(this_name, other_name, Qt::CaseInsensitive);
if (compare_result != 0)
return { compare_result, type == SortType::NAME };
break;
} }
case SortType::DATE: case SortType::DATE:
if (dateTimeChanged() > other.dateTimeChanged()) if (dateTimeChanged() > other.dateTimeChanged())
return { 1, type == SortType::DATE }; return 1;
if (dateTimeChanged() < other.dateTimeChanged()) if (dateTimeChanged() < other.dateTimeChanged())
return { -1, type == SortType::DATE }; return -1;
break; break;
case SortType::SIZE: { case SortType::SIZE: {
auto order_op = order == Qt::SortOrder::AscendingOrder ? 1 : -1;
if (this->type() != other.type()) {
if (this->type() == ResourceType::FOLDER)
return -1 * order_op;
if (other.type() == ResourceType::FOLDER)
return 1 * order_op;
}
if (sizeInfo() > other.sizeInfo()) if (sizeInfo() > other.sizeInfo())
return { 1, type == SortType::SIZE }; return 1;
if (sizeInfo() < other.sizeInfo()) if (sizeInfo() < other.sizeInfo())
return { -1, type == SortType::SIZE }; return -1;
break; break;
} }
} }
return { 0, false }; return 0;
} }
bool Resource::applyFilter(QRegularExpression filter) const bool Resource::applyFilter(QRegularExpression filter) const

View File

@ -55,10 +55,11 @@ class Resource : public QObject {
* > 0: 'this' comes after 'other' * > 0: 'this' comes after 'other'
* = 0: 'this' is equal to 'other' * = 0: 'this' is equal to 'other'
* < 0: 'this' comes before 'other' * < 0: 'this' comes before 'other'
* * the order is used to force items to be allways at top and not used for sorting
* The second argument in the pair is true if the sorting type that decided which one is greater was 'type'.
*/ */
[[nodiscard]] virtual auto compare(Resource const& other, SortType type = SortType::NAME) const -> std::pair<int, bool>; [[nodiscard]] virtual int compare(Resource const& other,
SortType type = SortType::NAME,
Qt::SortOrder order = Qt::SortOrder::AscendingOrder) const;
/** Returns whether the given filter should filter out 'this' (false), /** Returns whether the given filter should filter out 'this' (false),
* or if such filter includes the Resource (true). * or if such filter includes the Resource (true).

View File

@ -615,13 +615,11 @@ SortType ResourceFolderModel::columnToSortKey(size_t column) const
auto const& resource_left = model->at(source_left.row()); auto const& resource_left = model->at(source_left.row());
auto const& resource_right = model->at(source_right.row()); auto const& resource_right = model->at(source_right.row());
auto compare_result = resource_left.compare(resource_right, column_sort_key); auto compare_result = resource_left.compare(resource_right, column_sort_key, sortOrder());
if (compare_result.first == 0) if (compare_result == 0)
return QSortFilterProxyModel::lessThan(source_left, source_right); return QSortFilterProxyModel::lessThan(source_left, source_right);
if (compare_result.second || sortOrder() != Qt::DescendingOrder) return compare_result < 0;
return (compare_result.first < 0);
return (compare_result.first > 0);
} }
QString ResourceFolderModel::instDirPath() const QString ResourceFolderModel::instDirPath() const

View File

@ -94,29 +94,24 @@ std::pair<Version, Version> ResourcePack::compatibleVersions() const
return s_pack_format_versions.constFind(m_pack_format).value(); return s_pack_format_versions.constFind(m_pack_format).value();
} }
std::pair<int, bool> ResourcePack::compare(const Resource& other, SortType type) const int ResourcePack::compare(const Resource& other, SortType type, Qt::SortOrder order) const
{ {
auto const& cast_other = static_cast<ResourcePack const&>(other); auto const& cast_other = static_cast<ResourcePack const&>(other);
switch (type) { switch (type) {
default: { default:
auto res = Resource::compare(other, type); return Resource::compare(other, type, order);
if (res.first != 0)
return res;
break;
}
case SortType::PACK_FORMAT: { case SortType::PACK_FORMAT: {
auto this_ver = packFormat(); auto this_ver = packFormat();
auto other_ver = cast_other.packFormat(); auto other_ver = cast_other.packFormat();
if (this_ver > other_ver) if (this_ver > other_ver)
return { 1, type == SortType::PACK_FORMAT }; return 1;
if (this_ver < other_ver) if (this_ver < other_ver)
return { -1, type == SortType::PACK_FORMAT }; return -1;
break; break;
} }
} }
return { 0, false }; return 0;
} }
bool ResourcePack::applyFilter(QRegularExpression filter) const bool ResourcePack::applyFilter(QRegularExpression filter) const

View File

@ -44,7 +44,7 @@ class ResourcePack : public Resource {
bool valid() const override; bool valid() const override;
[[nodiscard]] auto compare(Resource const& other, SortType type) const -> std::pair<int, bool> override; [[nodiscard]] int compare(Resource const& other, SortType type, Qt::SortOrder order = Qt::SortOrder::AscendingOrder) const override;
[[nodiscard]] bool applyFilter(QRegularExpression filter) const override; [[nodiscard]] bool applyFilter(QRegularExpression filter) const override;
protected: protected: