pollymc/launcher/ui/dialogs/NewInstanceDialog.cpp

307 lines
9.1 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
2013-02-02 00:37:36 +05:30
*
* 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.
2013-10-06 18:13:46 +05:30
*
* 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.
2013-02-02 00:37:36 +05:30
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2013-02-02 00:37:36 +05:30
*/
2021-11-20 20:52:22 +05:30
#include "Application.h"
#include "NewInstanceDialog.h"
#include "ui_NewInstanceDialog.h"
2013-02-02 00:37:36 +05:30
2015-02-09 06:21:14 +05:30
#include <BaseVersion.h>
#include <icons/IconList.h>
#include <tasks/Task.h>
#include <InstanceList.h>
#include "VersionSelectDialog.h"
#include "ProgressDialog.h"
#include "IconPickerDialog.h"
2013-02-02 00:37:36 +05:30
#include <QLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QValidator>
2018-03-19 07:06:12 +05:30
#include <QDialogButtonBox>
#include <utility>
#include "ui/widgets/PageContainer.h"
#include "ui/pages/modplatform/VanillaPage.h"
#include "ui/pages/modplatform/atlauncher/AtlPage.h"
#include "ui/pages/modplatform/ftb/FtbPage.h"
#include "ui/pages/modplatform/legacy_ftb/Page.h"
#include "ui/pages/modplatform/flame/FlamePage.h"
#include "ui/pages/modplatform/ImportPage.h"
#include "ui/pages/modplatform/modrinth/ModrinthPage.h"
#include "ui/pages/modplatform/technic/TechnicPage.h"
NewInstanceDialog::NewInstanceDialog(const QString & initialGroup, const QString & url, QWidget *parent)
2018-07-15 18:21:05 +05:30
: QDialog(parent), ui(new Ui::NewInstanceDialog)
2013-02-02 00:37:36 +05:30
{
2018-07-15 18:21:05 +05:30
ui->setupUi(this);
2021-11-20 20:52:22 +05:30
setWindowIcon(APPLICATION->getThemedIcon("new"));
2017-03-20 05:14:49 +05:30
2018-07-15 18:21:05 +05:30
InstIconKey = "default";
2021-11-20 20:52:22 +05:30
ui->iconButton->setIcon(APPLICATION->icons()->getIcon(InstIconKey));
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
auto groupList = APPLICATION->instances()->getGroups();
auto groups = QSet<QString>(groupList.begin(), groupList.end());
groupList = groups.values();
#else
2021-11-20 20:52:22 +05:30
auto groups = APPLICATION->instances()->getGroups().toSet();
2018-07-15 18:21:05 +05:30
auto groupList = QStringList(groups.toList());
#endif
2018-07-15 18:21:05 +05:30
groupList.sort(Qt::CaseInsensitive);
groupList.removeOne("");
groupList.push_front(initialGroup);
groupList.push_front("");
ui->groupBox->addItems(groupList);
int index = groupList.indexOf(initialGroup);
if(index == -1)
{
index = 0;
}
ui->groupBox->setCurrentIndex(index);
ui->groupBox->lineEdit()->setPlaceholderText(tr("No group"));
// 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);
2018-07-15 18:21:05 +05:30
m_container = new PageContainer(this);
m_container->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Expanding);
m_container->layout()->setContentsMargins(0, 0, 0, 0);
ui->verticalLayout->insertWidget(2, m_container);
2018-07-15 18:21:05 +05:30
m_container->addButtons(m_buttons);
// 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);
OkButton->setDefault(true);
OkButton->setAutoDefault(true);
connect(OkButton, &QPushButton::clicked, this, &NewInstanceDialog::accept);
auto CancelButton = m_buttons->button(QDialogButtonBox::Cancel);
CancelButton->setDefault(false);
CancelButton->setAutoDefault(false);
connect(CancelButton, &QPushButton::clicked, this, &NewInstanceDialog::reject);
auto HelpButton = m_buttons->button(QDialogButtonBox::Help);
HelpButton->setDefault(false);
HelpButton->setAutoDefault(false);
connect(HelpButton, &QPushButton::clicked, m_container, &PageContainer::help);
2018-03-19 07:06:12 +05:30
2018-07-15 18:21:05 +05:30
if(!url.isEmpty())
{
QUrl actualUrl(url);
m_container->selectPage("import");
importPage->setUrl(url);
2018-07-15 18:21:05 +05:30
}
2018-07-15 18:21:05 +05:30
updateDialogState();
2018-03-19 07:06:12 +05:30
2021-11-20 20:52:22 +05:30
restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get("NewInstanceGeometry").toByteArray()));
}
void NewInstanceDialog::reject()
{
2021-11-20 20:52:22 +05:30
APPLICATION->settings()->set("NewInstanceGeometry", saveGeometry().toBase64());
2018-07-15 18:21:05 +05:30
QDialog::reject();
}
void NewInstanceDialog::accept()
{
2021-11-20 20:52:22 +05:30
APPLICATION->settings()->set("NewInstanceGeometry", saveGeometry().toBase64());
2018-07-15 18:21:05 +05:30
importIconNow();
QDialog::accept();
}
2018-03-19 07:06:12 +05:30
QList<BasePage *> NewInstanceDialog::getPages()
2017-03-20 05:14:49 +05:30
{
QList<BasePage *> pages;
importPage = new ImportPage(this);
pages.append(new VanillaPage(this));
pages.append(importPage);
pages.append(new AtlPage(this));
if (APPLICATION->capabilities() & Application::SupportsFlame)
pages.append(new FlamePage(this));
pages.append(new FtbPage(this));
pages.append(new LegacyFTB::Page(this));
pages.append(new ModrinthPage(this));
pages.append(new TechnicPage(this));
return pages;
2018-03-19 07:06:12 +05:30
}
QString NewInstanceDialog::dialogTitle()
{
2018-07-15 18:21:05 +05:30
return tr("New Instance");
2017-03-20 05:14:49 +05:30
}
NewInstanceDialog::~NewInstanceDialog()
{
2018-07-15 18:21:05 +05:30
delete ui;
}
void NewInstanceDialog::setSuggestedPack(const QString& name, InstanceTask* task)
{
2018-07-15 18:21:05 +05:30
creationTask.reset(task);
2018-07-15 18:21:05 +05:30
ui->instNameTextBox->setPlaceholderText(name);
importVersion.clear();
if (!task) {
ui->iconButton->setIcon(APPLICATION->icons()->getIcon("default"));
importIcon = false;
}
auto allowOK = task && !instName().isEmpty();
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(allowOK);
}
void NewInstanceDialog::setSuggestedPack(const QString& name, QString version, InstanceTask* task)
{
creationTask.reset(task);
ui->instNameTextBox->setPlaceholderText(name);
importVersion = std::move(version);
if (!task) {
2021-11-20 20:52:22 +05:30
ui->iconButton->setIcon(APPLICATION->icons()->getIcon("default"));
importIcon = false;
}
2018-07-15 18:21:05 +05:30
auto allowOK = task && !instName().isEmpty();
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(allowOK);
}
void NewInstanceDialog::setSuggestedIconFromFile(const QString &path, const QString &name)
{
2018-07-15 18:21:05 +05:30
importIcon = true;
importIconPath = path;
importIconName = name;
2018-07-15 18:21:05 +05:30
//Hmm, for some reason they can be to small
ui->iconButton->setIcon(QIcon(path));
}
void NewInstanceDialog::setSuggestedIcon(const QString &key)
{
2021-11-20 20:52:22 +05:30
auto icon = APPLICATION->icons()->getIcon(key);
importIcon = false;
ui->iconButton->setIcon(icon);
}
2018-03-19 07:06:12 +05:30
InstanceTask * NewInstanceDialog::extractTask()
{
2018-07-15 18:21:05 +05:30
InstanceTask * extracted = creationTask.get();
creationTask.release();
InstanceName inst_name(ui->instNameTextBox->placeholderText().trimmed(), importVersion);
inst_name.setName(ui->instNameTextBox->text().trimmed());
extracted->setName(inst_name);
2018-07-15 18:21:05 +05:30
extracted->setGroup(instGroup());
extracted->setIcon(iconKey());
return extracted;
2018-03-19 07:06:12 +05:30
}
2013-10-06 18:13:46 +05:30
2018-03-19 07:06:12 +05:30
void NewInstanceDialog::updateDialogState()
{
2018-07-15 18:21:05 +05:30
auto allowOK = creationTask && !instName().isEmpty();
auto OkButton = m_buttons->button(QDialogButtonBox::Ok);
if(OkButton->isEnabled() != allowOK)
{
OkButton->setEnabled(allowOK);
}
}
2013-04-23 03:45:18 +05:30
QString NewInstanceDialog::instName() const
{
auto result = ui->instNameTextBox->text().trimmed();
2018-07-15 18:21:05 +05:30
if(result.size())
{
return result;
2018-07-15 18:21:05 +05:30
}
result = ui->instNameTextBox->placeholderText().trimmed();
2018-07-15 18:21:05 +05:30
if(result.size())
{
return result;
2018-07-15 18:21:05 +05:30
}
return QString();
2013-04-23 03:45:18 +05:30
}
QString NewInstanceDialog::instGroup() const
{
2018-07-15 18:21:05 +05:30
return ui->groupBox->currentText();
}
2013-04-23 03:45:18 +05:30
QString NewInstanceDialog::iconKey() const
{
2018-07-15 18:21:05 +05:30
return InstIconKey;
2013-04-23 03:45:18 +05:30
}
2013-05-07 07:58:28 +05:30
void NewInstanceDialog::on_iconButton_clicked()
{
2018-07-15 18:21:05 +05:30
importIconNow(); //so the user can switch back
IconPickerDialog dlg(this);
dlg.execWithSelection(InstIconKey);
2013-10-06 18:13:46 +05:30
2018-07-15 18:21:05 +05:30
if (dlg.result() == QDialog::Accepted)
{
InstIconKey = dlg.selectedIconKey;
2021-11-20 20:52:22 +05:30
ui->iconButton->setIcon(APPLICATION->icons()->getIcon(InstIconKey));
2018-07-15 18:21:05 +05:30
importIcon = false;
}
}
2013-05-07 07:58:28 +05:30
void NewInstanceDialog::on_instNameTextBox_textChanged(const QString &arg1)
{
2018-07-15 18:21:05 +05:30
updateDialogState();
2013-05-07 07:58:28 +05:30
}
void NewInstanceDialog::importIconNow()
{
2018-07-15 18:21:05 +05:30
if(importIcon) {
2021-11-20 20:52:22 +05:30
APPLICATION->icons()->installIcon(importIconPath, importIconName);
2018-07-15 18:21:05 +05:30
InstIconKey = importIconName;
importIcon = false;
}
2021-11-20 20:52:22 +05:30
APPLICATION->settings()->set("NewInstanceGeometry", saveGeometry().toBase64());
}