Updated the size sort code

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-12-22 23:35:48 +02:00
parent b47ec7f2dc
commit 13d29ac6f4
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
2 changed files with 9 additions and 6 deletions

View File

@ -3,6 +3,7 @@
#include <QDirIterator> #include <QDirIterator>
#include <QFileInfo> #include <QFileInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <tuple>
#include "FileSystem.h" #include "FileSystem.h"
#include "StringUtils.h" #include "StringUtils.h"
@ -20,7 +21,7 @@ void Resource::setFile(QFileInfo file_info)
parseFile(); parseFile();
} }
QString calculateFileSize(const QFileInfo& file) std::tuple<QString, qint64> calculateFileSize(const QFileInfo& file)
{ {
if (file.isDir()) { if (file.isDir()) {
auto dir = QDir(file.absoluteFilePath()); auto dir = QDir(file.absoluteFilePath());
@ -29,9 +30,9 @@ QString 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); return { QString("%1 %2").arg(QString::number(count), str), -count };
} }
return StringUtils::humanReadableFileSize(file.size(), true); return { StringUtils::humanReadableFileSize(file.size(), true), file.size() };
} }
void Resource::parseFile() void Resource::parseFile()
@ -42,7 +43,7 @@ void Resource::parseFile()
m_internal_id = file_name; m_internal_id = file_name;
m_size_str = calculateFileSize(m_file_info); std::tie(m_size_str, m_size_info) = calculateFileSize(m_file_info);
if (m_file_info.isDir()) { if (m_file_info.isDir()) {
m_type = ResourceType::FOLDER; m_type = ResourceType::FOLDER;
m_name = file_name; m_name = file_name;
@ -107,9 +108,9 @@ std::pair<int, bool> Resource::compare(const Resource& other, SortType type) con
return { -1, type == SortType::DATE }; return { -1, type == SortType::DATE };
break; break;
case SortType::SIZE: { case SortType::SIZE: {
if (fileinfo().size() > other.fileinfo().size()) if (sizeInfo() > other.sizeInfo())
return { 1, type == SortType::SIZE }; return { 1, type == SortType::SIZE };
if (fileinfo().size() < other.fileinfo().size()) if (sizeInfo() < other.sizeInfo())
return { -1, type == SortType::SIZE }; return { -1, type == SortType::SIZE };
break; break;
} }

View File

@ -46,6 +46,7 @@ class Resource : public QObject {
[[nodiscard]] auto type() const -> ResourceType { return m_type; } [[nodiscard]] auto type() const -> ResourceType { return m_type; }
[[nodiscard]] bool enabled() const { return m_enabled; } [[nodiscard]] bool enabled() const { return m_enabled; }
[[nodiscard]] QString sizeStr() const { return m_size_str; } [[nodiscard]] QString sizeStr() const { return m_size_str; }
[[nodiscard]] qint64 sizeInfo() const { return m_size_info; }
[[nodiscard]] virtual auto name() const -> QString { return m_name; } [[nodiscard]] virtual auto name() const -> QString { return m_name; }
[[nodiscard]] virtual bool valid() const { return m_type != ResourceType::UNKNOWN; } [[nodiscard]] virtual bool valid() const { return m_type != ResourceType::UNKNOWN; }
@ -119,4 +120,5 @@ class Resource : public QObject {
bool m_is_resolved = false; bool m_is_resolved = false;
int m_resolution_ticket = 0; int m_resolution_ticket = 0;
QString m_size_str; QString m_size_str;
qint64 m_size_info;
}; };