Updated the size sort code
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
b47ec7f2dc
commit
13d29ac6f4
@ -3,6 +3,7 @@
|
||||
#include <QDirIterator>
|
||||
#include <QFileInfo>
|
||||
#include <QRegularExpression>
|
||||
#include <tuple>
|
||||
|
||||
#include "FileSystem.h"
|
||||
#include "StringUtils.h"
|
||||
@ -20,7 +21,7 @@ void Resource::setFile(QFileInfo file_info)
|
||||
parseFile();
|
||||
}
|
||||
|
||||
QString calculateFileSize(const QFileInfo& file)
|
||||
std::tuple<QString, qint64> calculateFileSize(const QFileInfo& file)
|
||||
{
|
||||
if (file.isDir()) {
|
||||
auto dir = QDir(file.absoluteFilePath());
|
||||
@ -29,9 +30,9 @@ QString 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);
|
||||
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()
|
||||
@ -42,7 +43,7 @@ void Resource::parseFile()
|
||||
|
||||
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()) {
|
||||
m_type = ResourceType::FOLDER;
|
||||
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 };
|
||||
break;
|
||||
case SortType::SIZE: {
|
||||
if (fileinfo().size() > other.fileinfo().size())
|
||||
if (sizeInfo() > other.sizeInfo())
|
||||
return { 1, type == SortType::SIZE };
|
||||
if (fileinfo().size() < other.fileinfo().size())
|
||||
if (sizeInfo() < other.sizeInfo())
|
||||
return { -1, type == SortType::SIZE };
|
||||
break;
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ class Resource : public QObject {
|
||||
[[nodiscard]] auto type() const -> ResourceType { return m_type; }
|
||||
[[nodiscard]] bool enabled() const { return m_enabled; }
|
||||
[[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 bool valid() const { return m_type != ResourceType::UNKNOWN; }
|
||||
@ -119,4 +120,5 @@ class Resource : public QObject {
|
||||
bool m_is_resolved = false;
|
||||
int m_resolution_ticket = 0;
|
||||
QString m_size_str;
|
||||
qint64 m_size_info;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user