PrismLauncher/launcher/dialogs/NewInstanceDialog.cpp

256 lines
7.2 KiB
C++
Raw Normal View History

2021-01-18 07:28:54 +00:00
/* Copyright 2013-2021 MultiMC Contributors
2013-02-01 19:07:36 +00:00
*
* 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
2013-10-06 13:43:46 +01:00
*
2013-02-01 19:07:36 +00:00
* 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.
*/
#include "Launcher.h"
#include "NewInstanceDialog.h"
#include "ui_NewInstanceDialog.h"
2013-02-01 19:07:36 +00:00
2015-02-09 00:51:14 +00:00
#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-01 19:07:36 +00:00
#include <QLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QValidator>
2018-03-19 01:36:12 +00:00
#include <QDialogButtonBox>
2018-03-19 01:36:12 +00:00
#include "widgets/PageContainer.h"
#include <pages/modplatform/VanillaPage.h>
2020-08-24 23:13:43 +01:00
#include <pages/modplatform/atlauncher/AtlPage.h>
#include <pages/modplatform/ftb/FtbPage.h>
#include <pages/modplatform/legacy_ftb/Page.h>
#include <pages/modplatform/flame/FlamePage.h>
#include <pages/modplatform/ImportPage.h>
#include <pages/modplatform/technic/TechnicPage.h>
NewInstanceDialog::NewInstanceDialog(const QString & initialGroup, const QString & url, QWidget *parent)
2018-07-15 13:51:05 +01:00
: QDialog(parent), ui(new Ui::NewInstanceDialog)
2013-02-01 19:07:36 +00:00
{
2018-07-15 13:51:05 +01:00
ui->setupUi(this);
setWindowIcon(LAUNCHER->getThemedIcon("new"));
2017-03-19 23:44:49 +00:00
2018-07-15 13:51:05 +01:00
InstIconKey = "default";
ui->iconButton->setIcon(LAUNCHER->icons()->getIcon(InstIconKey));
auto groups = LAUNCHER->instances()->getGroups().toSet();
2018-07-15 13:51:05 +01:00
auto groupList = QStringList(groups.toList());
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 13:51:05 +01:00
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 13:51:05 +01:00
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 01:36:12 +00:00
2018-07-15 13:51:05 +01:00
if(!url.isEmpty())
{
QUrl actualUrl(url);
m_container->selectPage("import");
importPage->setUrl(url);
2018-07-15 13:51:05 +01:00
}
2018-07-15 13:51:05 +01:00
updateDialogState();
2018-03-19 01:36:12 +00:00
restoreGeometry(QByteArray::fromBase64(LAUNCHER->settings()->get("NewInstanceGeometry").toByteArray()));
}
void NewInstanceDialog::reject()
{
LAUNCHER->settings()->set("NewInstanceGeometry", saveGeometry().toBase64());
2018-07-15 13:51:05 +01:00
QDialog::reject();
}
void NewInstanceDialog::accept()
{
LAUNCHER->settings()->set("NewInstanceGeometry", saveGeometry().toBase64());
2018-07-15 13:51:05 +01:00
importIconNow();
QDialog::accept();
}
2018-03-19 01:36:12 +00:00
QList<BasePage *> NewInstanceDialog::getPages()
2017-03-19 23:44:49 +00:00
{
importPage = new ImportPage(this);
flamePage = new FlamePage(this);
auto technicPage = new TechnicPage(this);
2018-07-15 13:51:05 +01:00
return
{
new VanillaPage(this),
importPage,
2020-08-24 23:13:43 +01:00
new AtlPage(this),
flamePage,
new FtbPage(this),
new LegacyFTB::Page(this),
technicPage
2018-07-15 13:51:05 +01:00
};
2018-03-19 01:36:12 +00:00
}
QString NewInstanceDialog::dialogTitle()
{
2018-07-15 13:51:05 +01:00
return tr("New Instance");
2017-03-19 23:44:49 +00:00
}
NewInstanceDialog::~NewInstanceDialog()
{
2018-07-15 13:51:05 +01:00
delete ui;
}
2018-03-19 01:36:12 +00:00
void NewInstanceDialog::setSuggestedPack(const QString& name, InstanceTask* task)
{
2018-07-15 13:51:05 +01:00
creationTask.reset(task);
ui->instNameTextBox->setPlaceholderText(name);
if(!task)
{
ui->iconButton->setIcon(LAUNCHER->icons()->getIcon("default"));
importIcon = false;
}
2018-07-15 13:51:05 +01:00
auto allowOK = task && !instName().isEmpty();
m_buttons->button(QDialogButtonBox::Ok)->setEnabled(allowOK);
}
void NewInstanceDialog::setSuggestedIconFromFile(const QString &path, const QString &name)
{
2018-07-15 13:51:05 +01:00
importIcon = true;
importIconPath = path;
importIconName = name;
2018-07-15 13:51:05 +01:00
//Hmm, for some reason they can be to small
ui->iconButton->setIcon(QIcon(path));
}
void NewInstanceDialog::setSuggestedIcon(const QString &key)
{
auto icon = LAUNCHER->icons()->getIcon(key);
importIcon = false;
ui->iconButton->setIcon(icon);
}
2018-03-19 01:36:12 +00:00
InstanceTask * NewInstanceDialog::extractTask()
{
2018-07-15 13:51:05 +01:00
InstanceTask * extracted = creationTask.get();
creationTask.release();
extracted->setName(instName());
extracted->setGroup(instGroup());
extracted->setIcon(iconKey());
return extracted;
2018-03-19 01:36:12 +00:00
}
2013-10-06 13:43:46 +01:00
2018-03-19 01:36:12 +00:00
void NewInstanceDialog::updateDialogState()
{
2018-07-15 13:51:05 +01:00
auto allowOK = creationTask && !instName().isEmpty();
auto OkButton = m_buttons->button(QDialogButtonBox::Ok);
if(OkButton->isEnabled() != allowOK)
{
OkButton->setEnabled(allowOK);
}
}
2013-04-22 23:15:18 +01:00
QString NewInstanceDialog::instName() const
{
auto result = ui->instNameTextBox->text().trimmed();
2018-07-15 13:51:05 +01:00
if(result.size())
{
return result;
2018-07-15 13:51:05 +01:00
}
result = ui->instNameTextBox->placeholderText().trimmed();
2018-07-15 13:51:05 +01:00
if(result.size())
{
return result;
2018-07-15 13:51:05 +01:00
}
return QString();
2013-04-22 23:15:18 +01:00
}
QString NewInstanceDialog::instGroup() const
{
2018-07-15 13:51:05 +01:00
return ui->groupBox->currentText();
}
2013-04-22 23:15:18 +01:00
QString NewInstanceDialog::iconKey() const
{
2018-07-15 13:51:05 +01:00
return InstIconKey;
2013-04-22 23:15:18 +01:00
}
2013-05-07 03:28:28 +01:00
void NewInstanceDialog::on_iconButton_clicked()
{
2018-07-15 13:51:05 +01:00
importIconNow(); //so the user can switch back
IconPickerDialog dlg(this);
dlg.execWithSelection(InstIconKey);
2013-10-06 13:43:46 +01:00
2018-07-15 13:51:05 +01:00
if (dlg.result() == QDialog::Accepted)
{
InstIconKey = dlg.selectedIconKey;
ui->iconButton->setIcon(LAUNCHER->icons()->getIcon(InstIconKey));
2018-07-15 13:51:05 +01:00
importIcon = false;
}
}
2013-05-07 03:28:28 +01:00
void NewInstanceDialog::on_instNameTextBox_textChanged(const QString &arg1)
{
2018-07-15 13:51:05 +01:00
updateDialogState();
2013-05-07 03:28:28 +01:00
}
void NewInstanceDialog::importIconNow()
{
2018-07-15 13:51:05 +01:00
if(importIcon) {
LAUNCHER->icons()->installIcon(importIconPath, importIconName);
2018-07-15 13:51:05 +01:00
InstIconKey = importIconName;
importIcon = false;
}
LAUNCHER->settings()->set("NewInstanceGeometry", saveGeometry().toBase64());
}