fix size column sorting
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
34e1a5e660
commit
242ddbb7e1
@ -65,29 +65,24 @@ std::pair<Version, Version> DataPack::compatibleVersions() const
|
||||
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);
|
||||
|
||||
switch (type) {
|
||||
default: {
|
||||
auto res = Resource::compare(other, type);
|
||||
if (res.first != 0)
|
||||
return res;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return Resource::compare(other, type, order);
|
||||
case SortType::PACK_FORMAT: {
|
||||
auto this_ver = packFormat();
|
||||
auto other_ver = cast_other.packFormat();
|
||||
|
||||
if (this_ver > other_ver)
|
||||
return { 1, type == SortType::PACK_FORMAT };
|
||||
return 1;
|
||||
if (this_ver < other_ver)
|
||||
return { -1, type == SortType::PACK_FORMAT };
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return { 0, false };
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool DataPack::applyFilter(QRegularExpression filter) const
|
||||
|
@ -56,7 +56,7 @@ class DataPack : public Resource {
|
||||
|
||||
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;
|
||||
|
||||
protected:
|
||||
|
@ -78,41 +78,33 @@ void Mod::setDetails(const ModDetails& 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);
|
||||
if (!cast_other)
|
||||
return Resource::compare(other, type);
|
||||
return Resource::compare(other, type, order);
|
||||
|
||||
switch (type) {
|
||||
default:
|
||||
case SortType::ENABLED:
|
||||
case SortType::NAME:
|
||||
case SortType::DATE:
|
||||
case SortType::SIZE: {
|
||||
auto res = Resource::compare(other, type);
|
||||
if (res.first != 0)
|
||||
return res;
|
||||
break;
|
||||
}
|
||||
case SortType::SIZE:
|
||||
return Resource::compare(other, type, order);
|
||||
case SortType::VERSION: {
|
||||
auto this_ver = Version(version());
|
||||
auto other_ver = Version(cast_other->version());
|
||||
if (this_ver > other_ver)
|
||||
return { 1, type == SortType::VERSION };
|
||||
return 1;
|
||||
if (this_ver < other_ver)
|
||||
return { -1, type == SortType::VERSION };
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
case SortType::PROVIDER: {
|
||||
auto compare_result =
|
||||
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 QString::compare(provider().value_or("Unknown"), cast_other->provider().value_or("Unknown"), Qt::CaseInsensitive);
|
||||
}
|
||||
}
|
||||
return { 0, false };
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Mod::applyFilter(QRegularExpression filter) const
|
||||
|
@ -88,7 +88,7 @@ class Mod : public Resource {
|
||||
|
||||
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;
|
||||
|
||||
// Delete all the files of this mod
|
||||
|
@ -30,7 +30,7 @@ std::tuple<QString, qint64> calculateFileSize(const QFileInfo& file)
|
||||
auto str = QObject::tr("item");
|
||||
if (count != 1)
|
||||
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() };
|
||||
}
|
||||
@ -79,15 +79,15 @@ static void removeThePrefix(QString& string)
|
||||
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) {
|
||||
default:
|
||||
case SortType::ENABLED:
|
||||
if (enabled() && !other.enabled())
|
||||
return { 1, type == SortType::ENABLED };
|
||||
return 1;
|
||||
if (!enabled() && other.enabled())
|
||||
return { -1, type == SortType::ENABLED };
|
||||
return -1;
|
||||
break;
|
||||
case SortType::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(other_name);
|
||||
|
||||
auto compare_result = QString::compare(this_name, other_name, Qt::CaseInsensitive);
|
||||
if (compare_result != 0)
|
||||
return { compare_result, type == SortType::NAME };
|
||||
break;
|
||||
return QString::compare(this_name, other_name, Qt::CaseInsensitive);
|
||||
}
|
||||
case SortType::DATE:
|
||||
if (dateTimeChanged() > other.dateTimeChanged())
|
||||
return { 1, type == SortType::DATE };
|
||||
return 1;
|
||||
if (dateTimeChanged() < other.dateTimeChanged())
|
||||
return { -1, type == SortType::DATE };
|
||||
return -1;
|
||||
break;
|
||||
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())
|
||||
return { 1, type == SortType::SIZE };
|
||||
return 1;
|
||||
if (sizeInfo() < other.sizeInfo())
|
||||
return { -1, type == SortType::SIZE };
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { 0, false };
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Resource::applyFilter(QRegularExpression filter) const
|
||||
|
@ -55,10 +55,11 @@ class Resource : public QObject {
|
||||
* > 0: 'this' comes after 'other'
|
||||
* = 0: 'this' is equal to 'other'
|
||||
* < 0: 'this' comes before 'other'
|
||||
*
|
||||
* The second argument in the pair is true if the sorting type that decided which one is greater was 'type'.
|
||||
* the order is used to force items to be allways at top and not used for sorting
|
||||
*/
|
||||
[[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),
|
||||
* or if such filter includes the Resource (true).
|
||||
|
@ -615,13 +615,11 @@ SortType ResourceFolderModel::columnToSortKey(size_t column) const
|
||||
auto const& resource_left = model->at(source_left.row());
|
||||
auto const& resource_right = model->at(source_right.row());
|
||||
|
||||
auto compare_result = resource_left.compare(resource_right, column_sort_key);
|
||||
if (compare_result.first == 0)
|
||||
auto compare_result = resource_left.compare(resource_right, column_sort_key, sortOrder());
|
||||
if (compare_result == 0)
|
||||
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
||||
|
||||
if (compare_result.second || sortOrder() != Qt::DescendingOrder)
|
||||
return (compare_result.first < 0);
|
||||
return (compare_result.first > 0);
|
||||
return compare_result < 0;
|
||||
}
|
||||
|
||||
QString ResourceFolderModel::instDirPath() const
|
||||
|
@ -94,29 +94,24 @@ std::pair<Version, Version> ResourcePack::compatibleVersions() const
|
||||
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);
|
||||
|
||||
switch (type) {
|
||||
default: {
|
||||
auto res = Resource::compare(other, type);
|
||||
if (res.first != 0)
|
||||
return res;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return Resource::compare(other, type, order);
|
||||
case SortType::PACK_FORMAT: {
|
||||
auto this_ver = packFormat();
|
||||
auto other_ver = cast_other.packFormat();
|
||||
|
||||
if (this_ver > other_ver)
|
||||
return { 1, type == SortType::PACK_FORMAT };
|
||||
return 1;
|
||||
if (this_ver < other_ver)
|
||||
return { -1, type == SortType::PACK_FORMAT };
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return { 0, false };
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ResourcePack::applyFilter(QRegularExpression filter) const
|
||||
|
@ -44,7 +44,7 @@ class ResourcePack : public Resource {
|
||||
|
||||
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;
|
||||
|
||||
protected:
|
||||
|
Loading…
Reference in New Issue
Block a user