Merge pull request #392 from flowln/mod_filter

This commit is contained in:
Sefa Eyeoglu 2022-04-16 18:03:03 +02:00 committed by GitHub
commit a42d2afcee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 363 additions and 41 deletions

View File

@ -825,7 +825,6 @@ SET(LAUNCHER_SOURCES
ui/dialogs/ModDownloadDialog.cpp
ui/dialogs/ModDownloadDialog.h
# GUI - widgets
ui/widgets/Common.cpp
ui/widgets/Common.h
@ -849,6 +848,8 @@ SET(LAUNCHER_SOURCES
ui/widgets/LogView.h
ui/widgets/MCModInfoFrame.cpp
ui/widgets/MCModInfoFrame.h
ui/widgets/ModFilterWidget.cpp
ui/widgets/ModFilterWidget.h
ui/widgets/ModListView.cpp
ui/widgets/ModListView.h
ui/widgets/PageContainer.cpp
@ -907,6 +908,7 @@ qt5_wrap_ui(LAUNCHER_UI
ui/widgets/InstanceCardWidget.ui
ui/widgets/CustomCommands.ui
ui/widgets/MCModInfoFrame.ui
ui/widgets/ModFilterWidget.ui
ui/dialogs/CopyInstanceDialog.ui
ui/dialogs/ProfileSetupDialog.ui
ui/dialogs/ProgressDialog.ui

View File

@ -3,6 +3,8 @@
#include <QString>
#include <QList>
#include "Version.h"
namespace ModPlatform {
class ListModel;
}
@ -22,7 +24,7 @@ class ModAPI {
QString search;
QString sorting;
ModLoaderType mod_loader;
QString version;
std::list<Version> versions;
};
virtual void searchMods(CallerType* caller, SearchArgs&& args) const = 0;
@ -30,7 +32,7 @@ class ModAPI {
struct VersionSearchArgs {
QString addonId;
QList<QString> mcVersions;
std::list<Version> mcVersions;
ModLoaderType loader;
};
@ -53,4 +55,15 @@ class ModAPI {
}
return "";
}
protected:
inline auto getGameVersionsString(std::list<Version> mcVersions) const -> QString
{
QString s;
for(auto& ver : mcVersions){
s += QString("%1,").arg(ver.toString());
}
s.remove(s.length() - 1, 1); //remove last comma
return s;
}
};

View File

@ -6,6 +6,8 @@ class FlameAPI : public NetworkModAPI {
private:
inline auto getModSearchURL(SearchArgs& args) const -> QString override
{
auto gameVersionStr = args.versions.size() != 0 ? QString("gameVersion=%1").arg(args.versions.front().toString()) : QString();
return QString(
"https://addons-ecs.forgesvc.net/api/v2/addon/search?"
"gameId=432&"
@ -17,12 +19,12 @@ class FlameAPI : public NetworkModAPI {
"searchFilter=%2&"
"sort=%3&"
"modLoaderType=%4&"
"gameVersion=%5")
"%5")
.arg(args.offset)
.arg(args.search)
.arg(args.sorting)
.arg(args.mod_loader)
.arg(args.version);
.arg(gameVersionStr);
};
inline auto getVersionsURL(VersionSearchArgs& args) const -> QString override

View File

@ -22,12 +22,12 @@ class ModrinthAPI : public NetworkModAPI {
"limit=25&"
"query=%2&"
"index=%3&"
"facets=[[\"categories:%4\"],[\"versions:%5\"],[\"project_type:mod\"]]")
"facets=[[\"categories:%4\"],%5[\"project_type:mod\"]]")
.arg(args.offset)
.arg(args.search)
.arg(args.sorting)
.arg(getModLoaderString(args.mod_loader))
.arg(args.version);
.arg(getGameVersionsArray(args.versions));
};
inline auto getVersionsURL(VersionSearchArgs& args) const -> QString override
@ -40,15 +40,14 @@ class ModrinthAPI : public NetworkModAPI {
.arg(getModLoaderString(args.loader));
};
inline auto getGameVersionsString(QList<QString> mcVersions) const -> QString
auto getGameVersionsArray(std::list<Version> mcVersions) const -> QString
{
QString s;
for(int i = 0; i < mcVersions.count(); i++){
s += mcVersions.at(i);
if(i < mcVersions.count() - 1)
s += ",";
for(auto& ver : mcVersions){
s += QString("\"versions:%1\",").arg(ver.toString());
}
return s;
s.remove(s.length() - 1, 1); //remove last comma
return s.isEmpty() ? QString() : QString("[%1],").arg(s);
}
static auto getModLoaderString(ModLoaderType type) -> const QString

View File

@ -22,7 +22,9 @@ ModDownloadDialog::ModDownloadDialog(const std::shared_ptr<ModFolderModel> &mods
: QDialog(parent), mods(mods), m_instance(instance)
{
setObjectName(QStringLiteral("ModDownloadDialog"));
resize(400, 347);
resize(std::max(0.5*parent->width(), 400.0), std::max(0.75*parent->height(), 400.0));
m_verticalLayout = new QVBoxLayout(this);
m_verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
@ -80,7 +82,7 @@ void ModDownloadDialog::confirm()
tr("Confirm mods to download")
);
for(auto task : keys){
for(auto& task : keys){
confirm_dialog->appendMod(task, modTask.find(task).value()->getFilename());
}

View File

@ -72,14 +72,11 @@ void ListModel::performPaginatedSearch()
auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile();
m_parent->apiProvider()->searchMods(this,
{ nextSearchOffset, currentSearchTerm, getSorts()[currentSort], profile->getModLoader(), getMineVersions().at(0) });
{ nextSearchOffset, currentSearchTerm, getSorts()[currentSort], profile->getModLoader(), getMineVersions() });
}
void ListModel::searchWithTerm(const QString& term, const int sort)
void ListModel::refresh()
{
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort) { return; }
currentSearchTerm = term;
currentSort = sort;
if (jobPtr) {
jobPtr->abort();
searchState = ResetRequested;
@ -94,6 +91,20 @@ void ListModel::searchWithTerm(const QString& term, const int sort)
performPaginatedSearch();
}
void ListModel::searchWithTerm(const QString& term, const int sort, const bool filter_changed)
{
if (currentSearchTerm == term
&& currentSearchTerm.isNull() == term.isNull()
&& currentSort == sort
&& !filter_changed)
{ return; }
currentSearchTerm = term;
currentSort = sort;
refresh();
}
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
{
if (m_logoMap.contains(logo)) {
@ -223,9 +234,7 @@ void ListModel::versionRequestSucceeded(QJsonDocument doc, QString addonId)
/******** Helpers ********/
auto ModPlatform::ListModel::getMineVersions() const -> QList<QString>
auto ModPlatform::ListModel::getMineVersions() const -> std::list<Version>
{
return { (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))
->getPackProfile()
->getComponentVersion("net.minecraft") };
return m_parent->getFilter()->versions;
}

View File

@ -7,6 +7,7 @@
#include "net/NetJob.h"
class ModPage;
class Version;
namespace ModPlatform {
@ -33,7 +34,8 @@ class ListModel : public QAbstractListModel {
/* Ask the API for more information */
void fetchMore(const QModelIndex& parent) override;
void searchWithTerm(const QString& term, const int sort);
void refresh();
void searchWithTerm(const QString& term, const int sort, const bool filter_changed);
void requestModVersions(const ModPlatform::IndexedPack& current);
virtual void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) = 0;
@ -62,7 +64,7 @@ class ListModel : public QAbstractListModel {
void requestLogo(QString file, QString url);
inline auto getMineVersions() const -> QList<QString>;
inline auto getMineVersions() const -> std::list<Version>;
protected:
ModPage* m_parent;

View File

@ -2,21 +2,39 @@
#include "ui_ModPage.h"
#include <QKeyEvent>
#include <memory>
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "ui/dialogs/ModDownloadDialog.h"
ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api)
: QWidget(dialog), m_instance(instance), ui(new Ui::ModPage), dialog(dialog), api(api)
: QWidget(dialog)
, m_instance(instance)
, ui(new Ui::ModPage)
, dialog(dialog)
, filter_widget(static_cast<MinecraftInstance*>(instance)->getPackProfile()->getComponentVersion("net.minecraft"), this)
, api(api)
{
ui->setupUi(this);
connect(ui->searchButton, &QPushButton::clicked, this, &ModPage::triggerSearch);
connect(ui->modFilterButton, &QPushButton::clicked, this, &ModPage::filterMods);
ui->searchEdit->installEventFilter(this);
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
ui->gridLayout_3->addWidget(&filter_widget, 0, 0, 1, ui->gridLayout_3->columnCount());
filter_widget.setInstance(static_cast<MinecraftInstance*>(m_instance));
m_filter = filter_widget.getFilter();
connect(&filter_widget, &ModFilterWidget::filterChanged, this, [&]{
ui->searchButton->setStyleSheet("text-decoration: underline");
});
connect(&filter_widget, &ModFilterWidget::filterUnchanged, this, [&]{
ui->searchButton->setStyleSheet("text-decoration: none");
});
}
ModPage::~ModPage()
@ -49,9 +67,24 @@ auto ModPage::eventFilter(QObject* watched, QEvent* event) -> bool
/******** Callbacks to events in the UI (set up in the derived classes) ********/
void ModPage::filterMods()
{
filter_widget.setHidden(!filter_widget.isHidden());
}
void ModPage::triggerSearch()
{
listModel->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex());
auto changed = filter_widget.changed();
m_filter = filter_widget.getFilter();
if(changed){
ui->packView->clearSelection();
ui->packDescription->clear();
ui->versionSelectionBox->clear();
updateSelectionButton();
}
listModel->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex(), changed);
}
void ModPage::onSelectionChanged(QModelIndex first, QModelIndex second)
@ -131,7 +164,7 @@ void ModPage::retranslate()
ui->retranslateUi(this);
}
void ModPage::updateModVersions()
void ModPage::updateModVersions(int prev_count)
{
auto packProfile = (dynamic_cast<MinecraftInstance*>(m_instance))->getPackProfile();
@ -141,15 +174,22 @@ void ModPage::updateModVersions()
for (int i = 0; i < current.versions.size(); i++) {
auto version = current.versions[i];
bool valid = false;
for(auto& mcVer : m_filter->versions){
//NOTE: Flame doesn't care about loaderString, so passing it changes nothing.
if (!validateVersion(version, mcVersion, loaderString)) {
continue;
if (validateVersion(version, mcVer.toString(), loaderString)) {
valid = true;
break;
}
}
if(valid || m_filter->versions.size() == 0)
ui->versionSelectionBox->addItem(version.version, QVariant(i));
}
if (ui->versionSelectionBox->count() == 0) { ui->versionSelectionBox->addItem(tr("No valid version found!"), QVariant(-1)); }
if (ui->versionSelectionBox->count() == 0 && prev_count != 0) {
ui->versionSelectionBox->addItem(tr("No valid version found!"), QVariant(-1));
ui->modSelectionButton->setText(tr("Cannot select invalid version :("));
}
updateSelectionButton();
}

View File

@ -7,6 +7,7 @@
#include "modplatform/ModIndex.h"
#include "ui/pages/BasePage.h"
#include "ui/pages/modplatform/ModModel.h"
#include "ui/widgets/ModFilterWidget.h"
class ModDownloadDialog;
@ -39,9 +40,10 @@ class ModPage : public QWidget, public BasePage {
virtual auto validateVersion(ModPlatform::IndexedVersion& ver, QString mineVer, QString loaderVer = "") const -> bool = 0;
auto apiProvider() const -> const ModAPI* { return api.get(); };
auto getFilter() const -> const std::shared_ptr<ModFilterWidget::Filter> { return m_filter; }
auto getCurrent() -> ModPlatform::IndexedPack& { return current; }
void updateModVersions();
void updateModVersions(int prev_count = -1);
void openedImpl() override;
auto eventFilter(QObject* watched, QEvent* event) -> bool override;
@ -52,6 +54,7 @@ class ModPage : public QWidget, public BasePage {
void updateSelectionButton();
protected slots:
virtual void filterMods();
void triggerSearch();
void onSelectionChanged(QModelIndex first, QModelIndex second);
void onVersionSelectionChanged(QString data);
@ -60,6 +63,10 @@ class ModPage : public QWidget, public BasePage {
protected:
Ui::ModPage* ui = nullptr;
ModDownloadDialog* dialog = nullptr;
ModFilterWidget filter_widget;
std::shared_ptr<ModFilterWidget::Filter> m_filter;
ModPlatform::ListModel* listModel = nullptr;
ModPlatform::IndexedPack current;

View File

@ -11,7 +11,7 @@
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="2">
<item row="1" column="0" colspan="4">
<layout class="QGridLayout" name="gridLayout_3">
<item row="1" column="2">
<widget class="QTextBrowser" name="packDescription">
@ -41,7 +41,7 @@
</item>
</layout>
</item>
<item row="0" column="1">
<item row="0" column="3">
<widget class="QPushButton" name="searchButton">
<property name="text">
<string>Search</string>
@ -51,12 +51,12 @@
<item row="0" column="0">
<widget class="QLineEdit" name="searchEdit">
<property name="placeholderText">
<string>Search and filter...</string>
<string>Search for mods...</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout_4" columnstretch="0,0,0,0">
<item row="2" column="0" colspan="4">
<layout class="QGridLayout" name="gridLayout_4" columnstretch="0,0,0">
<item row="0" column="2">
<widget class="QComboBox" name="versionSelectionBox"/>
</item>
@ -82,6 +82,20 @@
</item>
</layout>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="modFilterButton">
<property name="text">
<string>Filter options</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>

View File

@ -0,0 +1,109 @@
#include "ModFilterWidget.h"
#include "ui_ModFilterWidget.h"
ModFilterWidget::ModFilterWidget(Version def, QWidget* parent)
: QTabWidget(parent), m_filter(new Filter()), ui(new Ui::ModFilterWidget)
{
ui->setupUi(this);
m_mcVersion_buttons.addButton(ui->strictVersionButton, VersionButtonID::Strict);
ui->strictVersionButton->click();
m_mcVersion_buttons.addButton(ui->majorVersionButton, VersionButtonID::Major);
m_mcVersion_buttons.addButton(ui->allVersionsButton, VersionButtonID::All);
//m_mcVersion_buttons.addButton(ui->betweenVersionsButton, VersionButtonID::Between);
connect(&m_mcVersion_buttons, SIGNAL(idClicked(int)), this, SLOT(onVersionFilterChanged(int)));
m_filter->versions.push_front(def);
setHidden(true);
}
void ModFilterWidget::setInstance(MinecraftInstance* instance)
{
m_instance = instance;
auto mcVersionSplit = mcVersionStr().split(".");
ui->strictVersionButton->setText(
tr("Strict match (= %1)").arg(mcVersionStr()));
ui->majorVersionButton->setText(
tr("Major version match (= %1.%2.x)").arg(mcVersionSplit[0], mcVersionSplit[1]));
ui->allVersionsButton->setText(
tr("Any version"));
//ui->betweenVersionsButton->setText(
// tr("Between two versions"));
}
auto ModFilterWidget::getFilter() -> std::shared_ptr<Filter>
{
m_last_version_id = m_version_id;
emit filterUnchanged();
return m_filter;
}
void ModFilterWidget::disableVersionButton(VersionButtonID id)
{
switch(id){
case(VersionButtonID::Strict):
ui->strictVersionButton->setEnabled(false);
break;
case(VersionButtonID::Major):
ui->majorVersionButton->setEnabled(false);
break;
case(VersionButtonID::All):
ui->allVersionsButton->setEnabled(false);
break;
case(VersionButtonID::Between):
// ui->betweenVersionsButton->setEnabled(false);
break;
default:
break;
}
}
void ModFilterWidget::onVersionFilterChanged(int id)
{
//ui->lowerVersionComboBox->setEnabled(id == VersionButtonID::Between);
//ui->upperVersionComboBox->setEnabled(id == VersionButtonID::Between);
auto versionSplit = mcVersionStr().split(".");
int index = 0;
auto cast_id = (VersionButtonID) id;
if (cast_id != m_version_id) {
m_version_id = cast_id;
} else {
return;
}
m_filter->versions.clear();
switch(cast_id){
case(VersionButtonID::Strict):
m_filter->versions.push_front(mcVersion());
break;
case(VersionButtonID::Major):
for(auto i = Version(QString("%1.%2").arg(versionSplit[0], versionSplit[1])); i <= mcVersion(); index++){
m_filter->versions.push_front(i);
i = Version(QString("%1.%2.%3").arg(versionSplit[0], versionSplit[1], QString("%1").arg(index)));
}
break;
case(VersionButtonID::All):
// Empty list to avoid enumerating all versions :P
break;
case(VersionButtonID::Between):
// TODO
break;
}
if(changed())
emit filterChanged();
else
emit filterUnchanged();
}
ModFilterWidget::~ModFilterWidget()
{
delete ui;
}

View File

@ -0,0 +1,69 @@
#pragma once
#include <QTabWidget>
#include <QButtonGroup>
#include "Version.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
class MinecraftInstance;
namespace Ui {
class ModFilterWidget;
}
class ModFilterWidget : public QTabWidget
{
Q_OBJECT
public:
enum VersionButtonID {
Strict = 0,
Major = 1,
All = 2,
Between = 3
};
struct Filter {
std::list<Version> versions;
bool operator==(const Filter& other) const { return versions == other.versions; }
bool operator!=(const Filter& other) const { return !(*this == other); }
};
std::shared_ptr<Filter> m_filter;
public:
explicit ModFilterWidget(Version def, QWidget* parent = nullptr);
~ModFilterWidget();
void setInstance(MinecraftInstance* instance);
/// By default all buttons are enabled
void disableVersionButton(VersionButtonID);
auto getFilter() -> std::shared_ptr<Filter>;
auto changed() const -> bool { return m_last_version_id != m_version_id; }
private:
inline auto mcVersionStr() const -> QString { return m_instance ? m_instance->getPackProfile()->getComponentVersion("net.minecraft") : ""; }
inline auto mcVersion() const -> Version { return { mcVersionStr() }; }
private slots:
void onVersionFilterChanged(int id);
public: signals:
void filterChanged();
void filterUnchanged();
private:
Ui::ModFilterWidget* ui;
MinecraftInstance* m_instance = nullptr;
QButtonGroup m_mcVersion_buttons;
/* Used to tell if the filter was changed since the last getFilter() call */
VersionButtonID m_last_version_id = VersionButtonID::Strict;
VersionButtonID m_version_id = VersionButtonID::Strict;
};

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ModFilterWidget</class>
<widget class="QTabWidget" name="ModFilterWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<widget class="QWidget" name="VersionPage">
<attribute name="title">
<string>Minecraft versions</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QFormLayout" name="formLayout">
<item row="2" column="0">
<widget class="QRadioButton" name="allVersionsButton">
<property name="text">
<string>allVersions</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="strictVersionButton">
<property name="text">
<string>strictVersion</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QRadioButton" name="majorVersionButton">
<property name="text">
<string>majorVersion</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>