feat+refactor: add shortcuts to mod downloader and clean up

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-07-15 09:34:11 -03:00
parent 127b558f95
commit a8bcd85c93
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
4 changed files with 47 additions and 36 deletions

View File

@ -19,36 +19,33 @@
#include "ModDownloadDialog.h" #include "ModDownloadDialog.h"
#include <BaseVersion.h> #include <BaseVersion.h>
#include <icons/IconList.h>
#include <InstanceList.h> #include <InstanceList.h>
#include <icons/IconList.h>
#include "Application.h" #include "Application.h"
#include "ProgressDialog.h"
#include "ReviewMessageBox.h" #include "ReviewMessageBox.h"
#include <QDialogButtonBox>
#include <QLayout> #include <QLayout>
#include <QPushButton> #include <QPushButton>
#include <QValidator> #include <QValidator>
#include <QDialogButtonBox>
#include "ui/widgets/PageContainer.h"
#include "ui/pages/modplatform/modrinth/ModrinthModPage.h"
#include "ModDownloadTask.h" #include "ModDownloadTask.h"
#include "ui/pages/modplatform/flame/FlameModPage.h"
#include "ui/pages/modplatform/modrinth/ModrinthModPage.h"
#include "ui/widgets/PageContainer.h"
ModDownloadDialog::ModDownloadDialog(const std::shared_ptr<ModFolderModel>& mods, QWidget* parent, BaseInstance* instance)
ModDownloadDialog::ModDownloadDialog(const std::shared_ptr<ModFolderModel> &mods, QWidget *parent, : QDialog(parent), mods(mods), m_verticalLayout(new QVBoxLayout(this)), m_instance(instance)
BaseInstance *instance)
: QDialog(parent), mods(mods), m_instance(instance)
{ {
setObjectName(QStringLiteral("ModDownloadDialog")); setObjectName(QStringLiteral("ModDownloadDialog"));
m_verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
resize(std::max(0.5 * parent->width(), 400.0), std::max(0.75 * parent->height(), 400.0)); 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"));
setWindowIcon(APPLICATION->getThemedIcon("new")); setWindowIcon(APPLICATION->getThemedIcon("new"));
// NOTE: m_buttons must be initialized before PageContainer, because it indirectly accesses m_buttons through setSuggestedPack! Do not move this below. // NOTE: m_buttons must be initialized before PageContainer, because it indirectly accesses m_buttons through setSuggestedPack! Do not
// move this below.
m_buttons = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel); m_buttons = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
m_container = new PageContainer(this); m_container = new PageContainer(this);
@ -65,6 +62,8 @@ ModDownloadDialog::ModDownloadDialog(const std::shared_ptr<ModFolderModel> &mods
OkButton->setDefault(true); OkButton->setDefault(true);
OkButton->setAutoDefault(true); OkButton->setAutoDefault(true);
OkButton->setText(tr("Review and confirm")); OkButton->setText(tr("Review and confirm"));
OkButton->setShortcut(tr("Ctrl+Return"));
OkButton->setToolTip(tr("Opens a new popup to review your selected mods and confirm your selection. Shortcut: Ctrl+Return"));
connect(OkButton, &QPushButton::clicked, this, &ModDownloadDialog::confirm); connect(OkButton, &QPushButton::clicked, this, &ModDownloadDialog::confirm);
auto CancelButton = m_buttons->button(QDialogButtonBox::Cancel); auto CancelButton = m_buttons->button(QDialogButtonBox::Cancel);
@ -129,7 +128,7 @@ QList<BasePage *> ModDownloadDialog::getPages()
return pages; return pages;
} }
void ModDownloadDialog::addSelectedMod(const QString& name, ModDownloadTask* task) void ModDownloadDialog::addSelectedMod(QString name, ModDownloadTask* task)
{ {
removeSelectedMod(name); removeSelectedMod(name);
modTask.insert(name, task); modTask.insert(name, task);
@ -137,7 +136,7 @@ void ModDownloadDialog::addSelectedMod(const QString& name, ModDownloadTask* tas
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(!modTask.isEmpty()); m_buttons->button(QDialogButtonBox::Ok)->setEnabled(!modTask.isEmpty());
} }
void ModDownloadDialog::removeSelectedMod(const QString &name) void ModDownloadDialog::removeSelectedMod(QString name)
{ {
if (modTask.contains(name)) if (modTask.contains(name))
delete modTask.find(name).value(); delete modTask.find(name).value();
@ -146,7 +145,7 @@ void ModDownloadDialog::removeSelectedMod(const QString &name)
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(!modTask.isEmpty()); m_buttons->button(QDialogButtonBox::Ok)->setEnabled(!modTask.isEmpty());
} }
bool ModDownloadDialog::isModSelected(const QString &name, const QString& filename) const bool ModDownloadDialog::isModSelected(QString name, QString filename) const
{ {
// FIXME: Is there a way to check for versions without checking the filename // FIXME: Is there a way to check for versions without checking the filename
// as a heuristic, other than adding such info to ModDownloadTask itself? // as a heuristic, other than adding such info to ModDownloadTask itself?
@ -154,16 +153,13 @@ bool ModDownloadDialog::isModSelected(const QString &name, const QString& filena
return iter != modTask.end() && (iter.value()->getFilename() == filename); return iter != modTask.end() && (iter.value()->getFilename() == filename);
} }
bool ModDownloadDialog::isModSelected(const QString &name) const bool ModDownloadDialog::isModSelected(QString name) const
{ {
auto iter = modTask.find(name); auto iter = modTask.find(name);
return iter != modTask.end(); return iter != modTask.end();
} }
ModDownloadDialog::~ModDownloadDialog() const QList<ModDownloadTask*> ModDownloadDialog::getTasks()
{ {
}
const QList<ModDownloadTask*> ModDownloadDialog::getTasks() {
return modTask.values(); return modTask.values();
} }

View File

@ -21,11 +21,9 @@
#include <QDialog> #include <QDialog>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "BaseVersion.h"
#include "ui/pages/BasePageProvider.h"
#include "minecraft/mod/ModFolderModel.h"
#include "ModDownloadTask.h" #include "ModDownloadTask.h"
#include "ui/pages/modplatform/flame/FlameModPage.h" #include "minecraft/mod/ModFolderModel.h"
#include "ui/pages/BasePageProvider.h"
namespace Ui namespace Ui
{ {
@ -42,15 +40,15 @@ class ModDownloadDialog : public QDialog, public BasePageProvider
public: public:
explicit ModDownloadDialog(const std::shared_ptr<ModFolderModel>& mods, QWidget* parent, BaseInstance* instance); explicit ModDownloadDialog(const std::shared_ptr<ModFolderModel>& mods, QWidget* parent, BaseInstance* instance);
~ModDownloadDialog(); ~ModDownloadDialog() override = default;
QString dialogTitle() override; QString dialogTitle() override;
QList<BasePage*> getPages() override; QList<BasePage*> getPages() override;
void addSelectedMod(const QString & name = QString(), ModDownloadTask * task = nullptr); void addSelectedMod(QString name = QString(), ModDownloadTask* task = nullptr);
void removeSelectedMod(const QString & name = QString()); void removeSelectedMod(QString name = QString());
bool isModSelected(const QString & name, const QString & filename) const; bool isModSelected(QString name, QString filename) const;
bool isModSelected(const QString & name) const; bool isModSelected(QString name) const;
const QList<ModDownloadTask*> getTasks(); const QList<ModDownloadTask*> getTasks();
const std::shared_ptr<ModFolderModel> &mods; const std::shared_ptr<ModFolderModel> &mods;

View File

@ -2,6 +2,7 @@
#include "BuildConfig.h" #include "BuildConfig.h"
#include "Json.h" #include "Json.h"
#include "ModPage.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
#include "ui/dialogs/ModDownloadDialog.h" #include "ui/dialogs/ModDownloadDialog.h"

View File

@ -76,6 +76,7 @@ ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api)
}); });
ui->packView->setItemDelegate(new ProjectItemDelegate(this)); ui->packView->setItemDelegate(new ProjectItemDelegate(this));
ui->packView->installEventFilter(this);
} }
ModPage::~ModPage() ModPage::~ModPage()
@ -98,6 +99,18 @@ auto ModPage::eventFilter(QObject* watched, QEvent* event) -> bool
auto* keyEvent = dynamic_cast<QKeyEvent*>(event); auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Return) { if (keyEvent->key() == Qt::Key_Return) {
triggerSearch(); triggerSearch();
keyEvent->accept();
return true;
}
} else if (watched == ui->packView && event->type() == QEvent::KeyPress) {
auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Return) {
onModSelected();
// To have the 'select mod' button outlined instead of the 'review and confirm' one
ui->modSelectionButton->setFocus(Qt::FocusReason::ShortcutFocusReason);
ui->packView->setFocus(Qt::FocusReason::NoFocusReason);
keyEvent->accept(); keyEvent->accept();
return true; return true;
} }
@ -172,6 +185,9 @@ void ModPage::onVersionSelectionChanged(QString data)
void ModPage::onModSelected() void ModPage::onModSelected()
{ {
if (selectedVersion < 0)
return;
auto& version = current.versions[selectedVersion]; auto& version = current.versions[selectedVersion];
if (dialog->isModSelected(current.name, version.fileName)) { if (dialog->isModSelected(current.name, version.fileName)) {
dialog->removeSelectedMod(current.name); dialog->removeSelectedMod(current.name);