2022-06-01 21:03:51 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
#include "ModDownloadDialog.h"
|
|
|
|
|
|
|
|
#include <BaseVersion.h>
|
|
|
|
#include <InstanceList.h>
|
2022-07-15 13:34:11 +01:00
|
|
|
#include <icons/IconList.h>
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-05-29 20:18:34 +01:00
|
|
|
#include "Application.h"
|
2022-03-13 14:50:18 +00:00
|
|
|
#include "ReviewMessageBox.h"
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
#include <QDialogButtonBox>
|
2022-01-14 08:43:42 +00:00
|
|
|
#include <QLayout>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QValidator>
|
|
|
|
|
|
|
|
#include "ModDownloadTask.h"
|
2022-07-15 13:34:11 +01:00
|
|
|
#include "ui/pages/modplatform/flame/FlameModPage.h"
|
|
|
|
#include "ui/pages/modplatform/modrinth/ModrinthModPage.h"
|
|
|
|
#include "ui/widgets/PageContainer.h"
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
ModDownloadDialog::ModDownloadDialog(const std::shared_ptr<ModFolderModel>& mods, QWidget* parent, BaseInstance* instance)
|
|
|
|
: QDialog(parent), mods(mods), m_verticalLayout(new QVBoxLayout(this)), m_instance(instance)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
|
|
|
setObjectName(QStringLiteral("ModDownloadDialog"));
|
|
|
|
m_verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
|
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
resize(std::max(0.5 * parent->width(), 400.0), std::max(0.75 * parent->height(), 400.0));
|
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
setWindowIcon(APPLICATION->getThemedIcon("new"));
|
2022-07-15 13:34:11 +01:00
|
|
|
// NOTE: m_buttons must be initialized before PageContainer, because it indirectly accesses m_buttons through setSuggestedPack! Do not
|
|
|
|
// move this below.
|
2022-01-14 08:43:42 +00:00
|
|
|
m_buttons = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
|
|
|
|
|
|
|
m_container = new PageContainer(this);
|
|
|
|
m_container->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Expanding);
|
|
|
|
m_container->layout()->setContentsMargins(0, 0, 0, 0);
|
|
|
|
m_verticalLayout->addWidget(m_container);
|
|
|
|
|
|
|
|
m_container->addButtons(m_buttons);
|
|
|
|
|
2022-07-15 15:20:08 +01:00
|
|
|
connect(m_container, &PageContainer::selectedPageChanged, this, &ModDownloadDialog::selectedPageChanged);
|
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
// Bonk Qt over its stupid head and make sure it understands which button is the default one...
|
|
|
|
// See: https://stackoverflow.com/questions/24556831/qbuttonbox-set-default-button
|
|
|
|
auto OkButton = m_buttons->button(QDialogButtonBox::Ok);
|
2022-02-23 22:17:33 +00:00
|
|
|
OkButton->setEnabled(false);
|
2022-01-14 08:43:42 +00:00
|
|
|
OkButton->setDefault(true);
|
|
|
|
OkButton->setAutoDefault(true);
|
2022-06-17 02:22:14 +01:00
|
|
|
OkButton->setText(tr("Review and confirm"));
|
2022-07-15 13:34:11 +01:00
|
|
|
OkButton->setShortcut(tr("Ctrl+Return"));
|
|
|
|
OkButton->setToolTip(tr("Opens a new popup to review your selected mods and confirm your selection. Shortcut: Ctrl+Return"));
|
2022-02-22 02:00:50 +00:00
|
|
|
connect(OkButton, &QPushButton::clicked, this, &ModDownloadDialog::confirm);
|
2022-01-14 08:43:42 +00:00
|
|
|
|
|
|
|
auto CancelButton = m_buttons->button(QDialogButtonBox::Cancel);
|
|
|
|
CancelButton->setDefault(false);
|
|
|
|
CancelButton->setAutoDefault(false);
|
|
|
|
connect(CancelButton, &QPushButton::clicked, this, &ModDownloadDialog::reject);
|
|
|
|
|
|
|
|
auto HelpButton = m_buttons->button(QDialogButtonBox::Help);
|
|
|
|
HelpButton->setDefault(false);
|
|
|
|
HelpButton->setAutoDefault(false);
|
|
|
|
connect(HelpButton, &QPushButton::clicked, m_container, &PageContainer::help);
|
2022-02-22 00:34:06 +00:00
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
QMetaObject::connectSlotsByName(this);
|
|
|
|
setWindowModality(Qt::WindowModal);
|
2022-06-16 22:25:14 +01:00
|
|
|
setWindowTitle(dialogTitle());
|
2022-07-19 17:50:02 +01:00
|
|
|
|
|
|
|
restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get("ModDownloadGeometry").toByteArray()));
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ModDownloadDialog::dialogTitle()
|
|
|
|
{
|
|
|
|
return tr("Download mods");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModDownloadDialog::reject()
|
|
|
|
{
|
2022-07-19 17:50:02 +01:00
|
|
|
APPLICATION->settings()->set("ModDownloadGeometry", saveGeometry().toBase64());
|
2022-01-14 08:43:42 +00:00
|
|
|
QDialog::reject();
|
|
|
|
}
|
|
|
|
|
2022-02-22 02:00:50 +00:00
|
|
|
void ModDownloadDialog::confirm()
|
|
|
|
{
|
2022-02-22 02:25:33 +00:00
|
|
|
auto keys = modTask.keys();
|
|
|
|
keys.sort(Qt::CaseInsensitive);
|
|
|
|
|
2022-04-22 17:20:31 +01:00
|
|
|
auto confirm_dialog = ReviewMessageBox::create(this, tr("Confirm mods to download"));
|
2022-02-22 02:00:50 +00:00
|
|
|
|
2022-04-22 17:20:31 +01:00
|
|
|
for (auto& task : keys) {
|
|
|
|
confirm_dialog->appendMod({ task, modTask.find(task).value()->getFilename() });
|
2022-03-13 14:50:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 17:20:31 +01:00
|
|
|
if (confirm_dialog->exec()) {
|
|
|
|
auto deselected = confirm_dialog->deselectedMods();
|
|
|
|
for (auto name : deselected) {
|
|
|
|
modTask.remove(name);
|
|
|
|
}
|
2022-02-22 02:00:50 +00:00
|
|
|
|
2022-04-22 17:20:31 +01:00
|
|
|
this->accept();
|
|
|
|
}
|
2022-02-22 02:00:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
void ModDownloadDialog::accept()
|
|
|
|
{
|
2022-07-19 17:50:02 +01:00
|
|
|
APPLICATION->settings()->set("ModDownloadGeometry", saveGeometry().toBase64());
|
2022-01-14 08:43:42 +00:00
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
QList<BasePage*> ModDownloadDialog::getPages()
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-07-15 13:34:11 +01:00
|
|
|
QList<BasePage*> pages;
|
2022-05-29 20:18:34 +01:00
|
|
|
|
|
|
|
pages.append(new ModrinthModPage(this, m_instance));
|
2022-08-08 19:49:49 +01:00
|
|
|
if (APPLICATION->capabilities() & Application::SupportsFlame)
|
2022-05-29 20:18:34 +01:00
|
|
|
pages.append(new FlameModPage(this, m_instance));
|
|
|
|
|
|
|
|
return pages;
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
void ModDownloadDialog::addSelectedMod(QString name, ModDownloadTask* task)
|
2022-02-22 00:34:06 +00:00
|
|
|
{
|
2022-02-23 22:17:33 +00:00
|
|
|
removeSelectedMod(name);
|
|
|
|
modTask.insert(name, task);
|
2022-02-22 00:34:06 +00:00
|
|
|
|
|
|
|
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(!modTask.isEmpty());
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
void ModDownloadDialog::removeSelectedMod(QString name)
|
2022-02-22 00:34:06 +00:00
|
|
|
{
|
2022-07-15 13:34:11 +01:00
|
|
|
if (modTask.contains(name))
|
2022-02-22 00:34:06 +00:00
|
|
|
delete modTask.find(name).value();
|
|
|
|
modTask.remove(name);
|
2022-02-23 22:17:33 +00:00
|
|
|
|
|
|
|
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(!modTask.isEmpty());
|
2022-02-22 00:34:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
bool ModDownloadDialog::isModSelected(QString name, QString filename) const
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-02-22 00:34:06 +00:00
|
|
|
// FIXME: Is there a way to check for versions without checking the filename
|
|
|
|
// as a heuristic, other than adding such info to ModDownloadTask itself?
|
|
|
|
auto iter = modTask.find(name);
|
|
|
|
return iter != modTask.end() && (iter.value()->getFilename() == filename);
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
bool ModDownloadDialog::isModSelected(QString name) const
|
2022-04-29 00:14:03 +01:00
|
|
|
{
|
|
|
|
auto iter = modTask.find(name);
|
|
|
|
return iter != modTask.end();
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:34:11 +01:00
|
|
|
const QList<ModDownloadTask*> ModDownloadDialog::getTasks()
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-02-22 00:34:06 +00:00
|
|
|
return modTask.values();
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 15:20:08 +01:00
|
|
|
void ModDownloadDialog::selectedPageChanged(BasePage* previous, BasePage* selected)
|
|
|
|
{
|
|
|
|
auto* prev_page = dynamic_cast<ModPage*>(previous);
|
|
|
|
if (!prev_page) {
|
|
|
|
qCritical() << "Page '" << previous->displayName() << "' in ModDownloadDialog is not a ModPage!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* selected_page = dynamic_cast<ModPage*>(selected);
|
|
|
|
if (!selected_page) {
|
|
|
|
qCritical() << "Page '" << selected->displayName() << "' in ModDownloadDialog is not a ModPage!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same effect as having a global search bar
|
|
|
|
selected_page->setSearchTerm(prev_page->getSearchTerm());
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|