Merge pull request #997 from flowln/fix_major_version_filter

This commit is contained in:
flow 2022-09-07 10:34:50 -03:00 committed by GitHub
commit f65d506f26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 130 additions and 33 deletions

View File

@ -140,6 +140,13 @@ VersionPtr VersionList::getVersion(const QString &version)
return out; return out;
} }
bool VersionList::hasVersion(QString version) const
{
auto ver = std::find_if(m_versions.constBegin(), m_versions.constEnd(),
[&](Meta::VersionPtr const& a){ return a->version() == version; });
return (ver != m_versions.constEnd());
}
void VersionList::setName(const QString &name) void VersionList::setName(const QString &name)
{ {
m_name = name; m_name = name;

View File

@ -66,6 +66,7 @@ public:
QString humanReadable() const; QString humanReadable() const;
VersionPtr getVersion(const QString &version); VersionPtr getVersion(const QString &version);
bool hasVersion(QString version) const;
QVector<VersionPtr> versions() const QVector<VersionPtr> versions() const
{ {

View File

@ -127,9 +127,9 @@ QList<BasePage*> ModDownloadDialog::getPages()
{ {
QList<BasePage*> pages; QList<BasePage*> pages;
pages.append(new ModrinthModPage(this, m_instance)); pages.append(ModrinthModPage::create(this, m_instance));
if (APPLICATION->capabilities() & Application::SupportsFlame) if (APPLICATION->capabilities() & Application::SupportsFlame)
pages.append(new FlameModPage(this, m_instance)); pages.append(FlameModPage::create(this, m_instance));
return pages; return pages;
} }

View File

@ -47,12 +47,12 @@
#include "ui/dialogs/ModDownloadDialog.h" #include "ui/dialogs/ModDownloadDialog.h"
#include "ui/widgets/ProjectItem.h" #include "ui/widgets/ProjectItem.h"
ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api) ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api)
: QWidget(dialog) : QWidget(dialog)
, m_instance(instance) , m_instance(instance)
, ui(new Ui::ModPage) , ui(new Ui::ModPage)
, dialog(dialog) , dialog(dialog)
, filter_widget(static_cast<MinecraftInstance*>(instance)->getPackProfile()->getComponentVersion("net.minecraft"), this)
, m_fetch_progress(this, false) , m_fetch_progress(this, false)
, api(api) , api(api)
{ {
@ -76,17 +76,6 @@ ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api)
m_fetch_progress.progressFormat(""); m_fetch_progress.progressFormat("");
ui->gridLayout_3->addWidget(&m_fetch_progress, 0, 0, 1, ui->gridLayout_3->columnCount()); ui->gridLayout_3->addWidget(&m_fetch_progress, 0, 0, 1, ui->gridLayout_3->columnCount());
ui->gridLayout_3->addWidget(&filter_widget, 1, 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");
});
ui->packView->setItemDelegate(new ProjectItemDelegate(this)); ui->packView->setItemDelegate(new ProjectItemDelegate(this));
ui->packView->installEventFilter(this); ui->packView->installEventFilter(this);
@ -97,6 +86,26 @@ ModPage::~ModPage()
delete ui; delete ui;
} }
void ModPage::setFilterWidget(unique_qobject_ptr<ModFilterWidget>& widget)
{
if (m_filter_widget)
disconnect(m_filter_widget.get(), nullptr, nullptr, nullptr);
m_filter_widget.swap(widget);
ui->gridLayout_3->addWidget(m_filter_widget.get(), 0, 0, 1, ui->gridLayout_3->columnCount());
m_filter_widget->setInstance(static_cast<MinecraftInstance*>(m_instance));
m_filter = m_filter_widget->getFilter();
connect(m_filter_widget.get(), &ModFilterWidget::filterChanged, this, [&]{
ui->searchButton->setStyleSheet("text-decoration: underline");
});
connect(m_filter_widget.get(), &ModFilterWidget::filterUnchanged, this, [&]{
ui->searchButton->setStyleSheet("text-decoration: none");
});
}
/******** Qt things ********/ /******** Qt things ********/
@ -141,13 +150,13 @@ auto ModPage::eventFilter(QObject* watched, QEvent* event) -> bool
void ModPage::filterMods() void ModPage::filterMods()
{ {
filter_widget.setHidden(!filter_widget.isHidden()); m_filter_widget->setHidden(!m_filter_widget->isHidden());
} }
void ModPage::triggerSearch() void ModPage::triggerSearch()
{ {
auto changed = filter_widget.changed(); auto changed = m_filter_widget->changed();
m_filter = filter_widget.getFilter(); m_filter = m_filter_widget->getFilter();
if(changed){ if(changed){
ui->packView->clearSelection(); ui->packView->clearSelection();

View File

@ -21,7 +21,17 @@ class ModPage : public QWidget, public BasePage {
Q_OBJECT Q_OBJECT
public: public:
explicit ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api); template<typename T>
static T* create(ModDownloadDialog* dialog, BaseInstance* instance)
{
auto page = new T(dialog, instance);
auto filter_widget = ModFilterWidget::create(static_cast<MinecraftInstance*>(instance)->getPackProfile()->getComponentVersion("net.minecraft"), page);
page->setFilterWidget(filter_widget);
return page;
}
~ModPage() override; ~ModPage() override;
/* Affects what the user sees */ /* Affects what the user sees */
@ -51,6 +61,8 @@ class ModPage : public QWidget, public BasePage {
/** Programatically set the term in the search bar. */ /** Programatically set the term in the search bar. */
void setSearchTerm(QString); void setSearchTerm(QString);
void setFilterWidget(unique_qobject_ptr<ModFilterWidget>&);
auto getCurrent() -> ModPlatform::IndexedPack& { return current; } auto getCurrent() -> ModPlatform::IndexedPack& { return current; }
void updateModVersions(int prev_count = -1); void updateModVersions(int prev_count = -1);
@ -60,6 +72,7 @@ class ModPage : public QWidget, public BasePage {
BaseInstance* m_instance; BaseInstance* m_instance;
protected: protected:
ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api);
void updateSelectionButton(); void updateSelectionButton();
protected slots: protected slots:
@ -73,7 +86,7 @@ class ModPage : public QWidget, public BasePage {
Ui::ModPage* ui = nullptr; Ui::ModPage* ui = nullptr;
ModDownloadDialog* dialog = nullptr; ModDownloadDialog* dialog = nullptr;
ModFilterWidget filter_widget; unique_qobject_ptr<ModFilterWidget> m_filter_widget;
std::shared_ptr<ModFilterWidget::Filter> m_filter; std::shared_ptr<ModFilterWidget::Filter> m_filter;
ProgressWidget m_fetch_progress; ProgressWidget m_fetch_progress;

View File

@ -44,7 +44,12 @@ class FlameModPage : public ModPage {
Q_OBJECT Q_OBJECT
public: public:
explicit FlameModPage(ModDownloadDialog* dialog, BaseInstance* instance); static FlameModPage* create(ModDownloadDialog* dialog, BaseInstance* instance)
{
return ModPage::create<FlameModPage>(dialog, instance);
}
FlameModPage(ModDownloadDialog* dialog, BaseInstance* instance);
~FlameModPage() override = default; ~FlameModPage() override = default;
inline auto displayName() const -> QString override { return "CurseForge"; } inline auto displayName() const -> QString override { return "CurseForge"; }

View File

@ -44,7 +44,12 @@ class ModrinthModPage : public ModPage {
Q_OBJECT Q_OBJECT
public: public:
explicit ModrinthModPage(ModDownloadDialog* dialog, BaseInstance* instance); static ModrinthModPage* create(ModDownloadDialog* dialog, BaseInstance* instance)
{
return ModPage::create<ModrinthModPage>(dialog, instance);
}
ModrinthModPage(ModDownloadDialog* dialog, BaseInstance* instance);
~ModrinthModPage() override = default; ~ModrinthModPage() override = default;
inline auto displayName() const -> QString override { return "Modrinth"; } inline auto displayName() const -> QString override { return "Modrinth"; }

View File

@ -1,6 +1,39 @@
#include "ModFilterWidget.h" #include "ModFilterWidget.h"
#include "ui_ModFilterWidget.h" #include "ui_ModFilterWidget.h"
#include "Application.h"
unique_qobject_ptr<ModFilterWidget> ModFilterWidget::create(Version default_version, QWidget* parent)
{
auto filter_widget = new ModFilterWidget(default_version, parent);
if (!filter_widget->versionList()->isLoaded()) {
QEventLoop load_version_list_loop;
QTimer time_limit_for_list_load;
time_limit_for_list_load.setTimerType(Qt::TimerType::CoarseTimer);
time_limit_for_list_load.setSingleShot(true);
time_limit_for_list_load.callOnTimeout(&load_version_list_loop, &QEventLoop::quit);
time_limit_for_list_load.start(4000);
auto task = filter_widget->versionList()->getLoadTask();
connect(task.get(), &Task::failed, [filter_widget]{
filter_widget->disableVersionButton(VersionButtonID::Major, tr("failed to get version index"));
});
connect(task.get(), &Task::finished, &load_version_list_loop, &QEventLoop::quit);
if (!task->isRunning())
task->start();
load_version_list_loop.exec();
if (time_limit_for_list_load.isActive())
time_limit_for_list_load.stop();
}
return unique_qobject_ptr<ModFilterWidget>(filter_widget);
}
ModFilterWidget::ModFilterWidget(Version def, QWidget* parent) ModFilterWidget::ModFilterWidget(Version def, QWidget* parent)
: QTabWidget(parent), m_filter(new Filter()), ui(new Ui::ModFilterWidget) : QTabWidget(parent), m_filter(new Filter()), ui(new Ui::ModFilterWidget)
{ {
@ -16,6 +49,7 @@ ModFilterWidget::ModFilterWidget(Version def, QWidget* parent)
m_filter->versions.push_front(def); m_filter->versions.push_front(def);
m_version_list = APPLICATION->metadataIndex()->get("net.minecraft");
setHidden(true); setHidden(true);
} }
@ -51,24 +85,30 @@ auto ModFilterWidget::getFilter() -> std::shared_ptr<Filter>
return m_filter; return m_filter;
} }
void ModFilterWidget::disableVersionButton(VersionButtonID id) void ModFilterWidget::disableVersionButton(VersionButtonID id, QString reason)
{ {
QAbstractButton* btn = nullptr;
switch(id){ switch(id){
case(VersionButtonID::Strict): case(VersionButtonID::Strict):
ui->strictVersionButton->setEnabled(false); btn = ui->strictVersionButton;
break; break;
case(VersionButtonID::Major): case(VersionButtonID::Major):
ui->majorVersionButton->setEnabled(false); btn = ui->majorVersionButton;
break; break;
case(VersionButtonID::All): case(VersionButtonID::All):
ui->allVersionsButton->setEnabled(false); btn = ui->allVersionsButton;
break; break;
case(VersionButtonID::Between): case(VersionButtonID::Between):
// ui->betweenVersionsButton->setEnabled(false);
break;
default: default:
break; break;
} }
if (btn) {
btn->setEnabled(false);
if (!reason.isEmpty())
btn->setText(btn->text() + QString(" (%1)").arg(reason));
}
} }
void ModFilterWidget::onVersionFilterChanged(int id) void ModFilterWidget::onVersionFilterChanged(int id)
@ -76,7 +116,7 @@ void ModFilterWidget::onVersionFilterChanged(int id)
//ui->lowerVersionComboBox->setEnabled(id == VersionButtonID::Between); //ui->lowerVersionComboBox->setEnabled(id == VersionButtonID::Between);
//ui->upperVersionComboBox->setEnabled(id == VersionButtonID::Between); //ui->upperVersionComboBox->setEnabled(id == VersionButtonID::Between);
int index = 0; int index = 1;
auto cast_id = (VersionButtonID) id; auto cast_id = (VersionButtonID) id;
if (cast_id != m_version_id) { if (cast_id != m_version_id) {
@ -93,10 +133,15 @@ void ModFilterWidget::onVersionFilterChanged(int id)
break; break;
case(VersionButtonID::Major): { case(VersionButtonID::Major): {
auto versionSplit = mcVersionStr().split("."); auto versionSplit = mcVersionStr().split(".");
for(auto i = Version(QString("%1.%2").arg(versionSplit[0], versionSplit[1])); i <= mcVersion(); index++){
m_filter->versions.push_front(i); auto major_version = QString("%1.%2").arg(versionSplit[0], versionSplit[1]);
i = Version(QString("%1.%2.%3").arg(versionSplit[0], versionSplit[1], QString("%1").arg(index))); QString version_str = major_version;
while (m_version_list->hasVersion(version_str)) {
m_filter->versions.emplace_back(version_str);
version_str = QString("%1.%2").arg(major_version, QString::number(index++));
} }
break; break;
} }
case(VersionButtonID::All): case(VersionButtonID::All):

View File

@ -4,6 +4,10 @@
#include <QButtonGroup> #include <QButtonGroup>
#include "Version.h" #include "Version.h"
#include "meta/Index.h"
#include "meta/VersionList.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
@ -34,18 +38,22 @@ public:
std::shared_ptr<Filter> m_filter; std::shared_ptr<Filter> m_filter;
public: public:
explicit ModFilterWidget(Version def, QWidget* parent = nullptr); static unique_qobject_ptr<ModFilterWidget> create(Version default_version, QWidget* parent = nullptr);
~ModFilterWidget(); ~ModFilterWidget();
void setInstance(MinecraftInstance* instance); void setInstance(MinecraftInstance* instance);
/// By default all buttons are enabled /// By default all buttons are enabled
void disableVersionButton(VersionButtonID); void disableVersionButton(VersionButtonID, QString reason = {});
auto getFilter() -> std::shared_ptr<Filter>; auto getFilter() -> std::shared_ptr<Filter>;
auto changed() const -> bool { return m_last_version_id != m_version_id; } auto changed() const -> bool { return m_last_version_id != m_version_id; }
Meta::VersionListPtr versionList() { return m_version_list; }
private: private:
ModFilterWidget(Version def, QWidget* parent = nullptr);
inline auto mcVersionStr() const -> QString { return m_instance ? m_instance->getPackProfile()->getComponentVersion("net.minecraft") : ""; } inline auto mcVersionStr() const -> QString { return m_instance ? m_instance->getPackProfile()->getComponentVersion("net.minecraft") : ""; }
inline auto mcVersion() const -> Version { return { mcVersionStr() }; } inline auto mcVersion() const -> Version { return { mcVersionStr() }; }
@ -61,8 +69,12 @@ private:
MinecraftInstance* m_instance = nullptr; MinecraftInstance* m_instance = nullptr;
/* Version stuff */
QButtonGroup m_mcVersion_buttons; QButtonGroup m_mcVersion_buttons;
Meta::VersionListPtr m_version_list;
/* Used to tell if the filter was changed since the last getFilter() call */ /* Used to tell if the filter was changed since the last getFilter() call */
VersionButtonID m_last_version_id = VersionButtonID::Strict; VersionButtonID m_last_version_id = VersionButtonID::Strict;
VersionButtonID m_version_id = VersionButtonID::Strict; VersionButtonID m_version_id = VersionButtonID::Strict;