Merge pull request #1135 from Trial97/installed_mods

This commit is contained in:
DioEgizio 2023-06-16 09:56:51 +02:00 committed by GitHub
commit 412349ac58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 43 additions and 9 deletions

View File

@ -6,12 +6,14 @@
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
#include "minecraft/mod/ModFolderModel.h"
#include <QMessageBox> #include <QMessageBox>
#include <algorithm>
namespace ResourceDownload { namespace ResourceDownload {
ModModel::ModModel(BaseInstance const& base_inst, ResourceAPI* api) : ResourceModel(api), m_base_instance(base_inst) {} ModModel::ModModel(BaseInstance& base_inst, ResourceAPI* api) : ResourceModel(api), m_base_instance(base_inst) {}
/******** Make data requests ********/ /******** Make data requests ********/
@ -24,7 +26,7 @@ ResourceAPI::SearchArgs ModModel::createSearchArguments()
std::optional<std::list<Version>> versions{}; std::optional<std::list<Version>> versions{};
{ // Version filter { // Version filter
if (!m_filter->versions.empty()) if (!m_filter->versions.empty())
versions = m_filter->versions; versions = m_filter->versions;
} }
@ -67,4 +69,14 @@ void ModModel::searchWithTerm(const QString& term, unsigned int sort, bool filte
refresh(); refresh();
} }
bool ModModel::isPackInstalled(ModPlatform::IndexedPack::Ptr pack) const
{
auto allMods = static_cast<MinecraftInstance&>(m_base_instance).loaderModList()->allMods();
return std::any_of(allMods.cbegin(), allMods.cend(), [pack](Mod* mod) {
if (auto meta = mod->metadata(); meta)
return meta->provider == pack->provider && meta->project_id == pack->addonId;
return false;
});
}
} // namespace ResourceDownload } // namespace ResourceDownload

View File

@ -24,7 +24,7 @@ class ModModel : public ResourceModel {
Q_OBJECT Q_OBJECT
public: public:
ModModel(const BaseInstance&, ResourceAPI* api); ModModel(BaseInstance&, ResourceAPI* api);
/* Ask the API for more information */ /* Ask the API for more information */
void searchWithTerm(const QString& term, unsigned int sort, bool filter_changed); void searchWithTerm(const QString& term, unsigned int sort, bool filter_changed);
@ -42,9 +42,10 @@ class ModModel : public ResourceModel {
protected: protected:
auto documentToArray(QJsonDocument& obj) const -> QJsonArray override = 0; auto documentToArray(QJsonDocument& obj) const -> QJsonArray override = 0;
virtual bool isPackInstalled(ModPlatform::IndexedPack::Ptr) const override;
protected: protected:
const BaseInstance& m_base_instance; BaseInstance& m_base_instance;
std::shared_ptr<ModFilterWidget::Filter> m_filter = nullptr; std::shared_ptr<ModFilterWidget::Filter> m_filter = nullptr;
}; };

View File

@ -77,6 +77,8 @@ auto ResourceModel::data(const QModelIndex& index, int role) const -> QVariant
return pack->description; return pack->description;
case UserDataTypes::SELECTED: case UserDataTypes::SELECTED:
return pack->isAnyVersionSelected(); return pack->isAnyVersionSelected();
case UserDataTypes::INSTALLED:
return this->isPackInstalled(pack);
default: default:
break; break;
} }
@ -95,6 +97,7 @@ QHash<int, QByteArray> ResourceModel::roleNames() const
roles[UserDataTypes::TITLE] = "title"; roles[UserDataTypes::TITLE] = "title";
roles[UserDataTypes::DESCRIPTION] = "description"; roles[UserDataTypes::DESCRIPTION] = "description";
roles[UserDataTypes::SELECTED] = "selected"; roles[UserDataTypes::SELECTED] = "selected";
roles[UserDataTypes::INSTALLED] = "installed";
return roles; return roles;
} }

View File

@ -116,6 +116,8 @@ class ResourceModel : public QAbstractListModel {
virtual void loadExtraPackInfo(ModPlatform::IndexedPack&, QJsonObject&); virtual void loadExtraPackInfo(ModPlatform::IndexedPack&, QJsonObject&);
virtual void loadIndexedPackVersions(ModPlatform::IndexedPack&, QJsonArray&); virtual void loadIndexedPackVersions(ModPlatform::IndexedPack&, QJsonArray&);
virtual bool isPackInstalled(ModPlatform::IndexedPack::Ptr) const { return false; }
protected: protected:
/* Basic search parameters */ /* Basic search parameters */
enum class SearchState { None, CanFetchMore, ResetRequested, Finished } m_search_state = SearchState::None; enum class SearchState { None, CanFetchMore, ResetRequested, Finished } m_search_state = SearchState::None;

View File

@ -60,6 +60,8 @@ QVariant ListModel::data(const QModelIndex& index, int role) const
return pack.description; return pack.description;
case UserDataTypes::SELECTED: case UserDataTypes::SELECTED:
return false; return false;
case UserDataTypes::INSTALLED:
return false;
default: default:
break; break;
} }

View File

@ -11,7 +11,7 @@
namespace ResourceDownload { namespace ResourceDownload {
FlameModModel::FlameModModel(BaseInstance const& base) : ModModel(base, new FlameAPI) {} FlameModModel::FlameModModel(BaseInstance& base) : ModModel(base, new FlameAPI) {}
void FlameModModel::loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) void FlameModModel::loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj)
{ {

View File

@ -14,7 +14,7 @@ class FlameModModel : public ModModel {
Q_OBJECT Q_OBJECT
public: public:
FlameModModel(const BaseInstance&); FlameModModel(BaseInstance&);
~FlameModModel() override = default; ~FlameModModel() override = default;
private: private:

View File

@ -106,6 +106,8 @@ auto ModpackListModel::data(const QModelIndex& index, int role) const -> QVarian
return pack.description; return pack.description;
case UserDataTypes::SELECTED: case UserDataTypes::SELECTED:
return false; return false;
case UserDataTypes::INSTALLED:
return false;
default: default:
break; break;
} }

View File

@ -25,7 +25,7 @@
namespace ResourceDownload { namespace ResourceDownload {
ModrinthModModel::ModrinthModModel(BaseInstance const& base) : ModModel(base, new ModrinthAPI) {} ModrinthModModel::ModrinthModModel(BaseInstance& base) : ModModel(base, new ModrinthAPI) {}
void ModrinthModModel::loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) void ModrinthModModel::loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj)
{ {

View File

@ -30,7 +30,7 @@ class ModrinthModModel : public ModModel {
Q_OBJECT Q_OBJECT
public: public:
ModrinthModModel(const BaseInstance&); ModrinthModModel(BaseInstance&);
~ModrinthModModel() override = default; ~ModrinthModModel() override = default;
private: private:

View File

@ -64,6 +64,17 @@ void ProjectItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& o
font.setBold(true); font.setBold(true);
font.setUnderline(true); font.setUnderline(true);
} }
if (index.data(UserDataTypes::INSTALLED).toBool()) {
auto hRect = opt.rect;
hRect.setX(hRect.x() + 1);
hRect.setY(hRect.y() + 1);
hRect.setHeight(hRect.height() - 2);
hRect.setWidth(hRect.width() - 2);
// Set nice font
font.setItalic(true);
font.setOverline(true);
painter->drawRect(hRect);
}
font.setPointSize(font.pointSize() + 2); font.setPointSize(font.pointSize() + 2);
painter->setFont(font); painter->setFont(font);

View File

@ -6,7 +6,8 @@
enum UserDataTypes { enum UserDataTypes {
TITLE = 257, // QString TITLE = 257, // QString
DESCRIPTION = 258, // QString DESCRIPTION = 258, // QString
SELECTED = 259 // bool SELECTED = 259, // bool
INSTALLED = 260 // bool
}; };
/** This is an item delegate composed of: /** This is an item delegate composed of: