mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2025-06-12 05:07:46 +02:00
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>
This commit is contained in:
@ -39,8 +39,10 @@
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QString>
|
||||
#include <QRegularExpression>
|
||||
|
||||
#include "MetadataHandler.h"
|
||||
#include "Version.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@ -111,6 +113,51 @@ void Mod::setMetadata(const Metadata::ModStruct& metadata)
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<int, bool> Mod::compare(const Resource& other, SortType type) const
|
||||
{
|
||||
auto cast_other = dynamic_cast<Mod const*>(&other);
|
||||
if (!cast_other)
|
||||
return Resource::compare(other, type);
|
||||
|
||||
switch (type) {
|
||||
default:
|
||||
case SortType::ENABLED:
|
||||
if (enabled() && !cast_other->enabled())
|
||||
return { 1, type == SortType::ENABLED };
|
||||
if (!enabled() && cast_other->enabled())
|
||||
return { -1, type == SortType::ENABLED };
|
||||
case SortType::NAME:
|
||||
case SortType::DATE: {
|
||||
auto res = Resource::compare(other, type);
|
||||
if (res.first != 0)
|
||||
return res;
|
||||
}
|
||||
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 };
|
||||
if (this_ver < other_ver)
|
||||
return { -1, type == SortType::VERSION };
|
||||
}
|
||||
}
|
||||
return { 0, false };
|
||||
}
|
||||
|
||||
bool Mod::applyFilter(QRegularExpression filter) const
|
||||
{
|
||||
if (filter.match(description()).hasMatch())
|
||||
return true;
|
||||
|
||||
for (auto& author : authors()) {
|
||||
if (filter.match(author).hasMatch()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return Resource::applyFilter(filter);
|
||||
}
|
||||
|
||||
auto Mod::destroy(QDir& index_dir, bool preserve_metadata) -> bool
|
||||
{
|
||||
if (!preserve_metadata) {
|
||||
|
Reference in New Issue
Block a user