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:
flow
2022-08-10 14:42:24 -03:00
parent af2cf2734d
commit 1e2f0ab308
8 changed files with 265 additions and 16 deletions

View File

@ -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) {