diff options
author | flow <flowlnlnln@gmail.com> | 2022-08-10 14:42:24 -0300 |
---|---|---|
committer | flow <flowlnlnln@gmail.com> | 2022-08-20 10:47:08 -0300 |
commit | 1e2f0ab3083f002071938275a97b13c0c4633e64 (patch) | |
tree | 3c4cc9c3c1768b7cec7cc876e218be22ea43e0c4 /launcher/minecraft/mod/Resource.cpp | |
parent | af2cf2734da211d443b3046fb0f9733f101d9d9d (diff) | |
download | PrismLauncher-1e2f0ab3083f002071938275a97b13c0c4633e64.tar.gz PrismLauncher-1e2f0ab3083f002071938275a97b13c0c4633e64.tar.bz2 PrismLauncher-1e2f0ab3083f002071938275a97b13c0c4633e64.zip |
refactor: move more tied logic to model and move logic to the resources
This moves the QSortFilterProxyModel to the resource model files,
acessible via a factory method, and moves the sorting and filtering to
the objects themselves, decoupling the code a bit.
This also adds a basic implementation of methods in the
ResourceFolderModel, simplifying the process of constructing a new model
from it.
Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/minecraft/mod/Resource.cpp')
-rw-r--r-- | launcher/minecraft/mod/Resource.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/launcher/minecraft/mod/Resource.cpp b/launcher/minecraft/mod/Resource.cpp index 8771a20f..c58df3d8 100644 --- a/launcher/minecraft/mod/Resource.cpp +++ b/launcher/minecraft/mod/Resource.cpp @@ -1,5 +1,7 @@ #include "Resource.h" +#include <QRegularExpression> + #include "FileSystem.h" Resource::Resource(QObject* parent) : QObject(parent) {} @@ -46,6 +48,43 @@ void Resource::parseFile() m_changed_date_time = m_file_info.lastModified(); } +static void removeThePrefix(QString& string) +{ + QRegularExpression regex(QStringLiteral("^(?:the|teh) +"), QRegularExpression::CaseInsensitiveOption); + string.remove(regex); + string = string.trimmed(); +} + +std::pair<int, bool> Resource::compare(const Resource& other, SortType type) const +{ + switch (type) { + default: + case SortType::NAME: { + QString this_name{ name() }; + QString other_name{ other.name() }; + + 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 }; + } + case SortType::DATE: + if (dateTimeChanged() > other.dateTimeChanged()) + return { 1, type == SortType::DATE }; + if (dateTimeChanged() < other.dateTimeChanged()) + return { -1, type == SortType::DATE }; + } + + return { 0, false }; +} + +bool Resource::applyFilter(QRegularExpression filter) const +{ + return filter.match(name()).hasMatch(); +} + bool Resource::destroy() { m_type = ResourceType::UNKNOWN; |