2017-01-08 03:58:05 +00:00
|
|
|
/* Copyright 2013-2017 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.
|
|
|
|
*/
|
|
|
|
|
2013-11-04 01:53:05 +00:00
|
|
|
#include "MultiMC.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>
|
2013-04-22 21:39:41 +01:00
|
|
|
|
2013-11-04 01:53:05 +00:00
|
|
|
#include "VersionSelectDialog.h"
|
2013-09-17 23:00:35 +01:00
|
|
|
#include "ProgressDialog.h"
|
2013-08-10 17:34:08 +01:00
|
|
|
#include "IconPickerDialog.h"
|
2013-04-22 21:39:41 +01:00
|
|
|
|
2013-02-01 19:07:36 +00:00
|
|
|
#include <QLayout>
|
2013-04-22 21:39:41 +01:00
|
|
|
#include <QPushButton>
|
2015-02-12 21:01:20 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QValidator>
|
|
|
|
|
2017-03-19 23:44:49 +00:00
|
|
|
#include <meta/Index.h>
|
|
|
|
#include <meta/VersionList.h>
|
|
|
|
|
2015-02-12 21:01:20 +00:00
|
|
|
class UrlValidator : public QValidator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using QValidator::QValidator;
|
|
|
|
|
|
|
|
State validate(QString &in, int &pos) const
|
|
|
|
{
|
|
|
|
const QUrl url(in);
|
|
|
|
if (url.isValid() && !url.isRelative() && !url.isEmpty())
|
|
|
|
{
|
|
|
|
return Acceptable;
|
|
|
|
}
|
|
|
|
else if (QFile::exists(in))
|
|
|
|
{
|
|
|
|
return Acceptable;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Intermediate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-02-01 19:07:36 +00:00
|
|
|
|
2017-04-22 05:11:26 +01:00
|
|
|
NewInstanceDialog::NewInstanceDialog(const QString & initialGroup, const QString & url, QWidget *parent)
|
2013-10-06 13:43:46 +01:00
|
|
|
: QDialog(parent), ui(new Ui::NewInstanceDialog)
|
2013-02-01 19:07:36 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
resize(minimumSizeHint());
|
|
|
|
layout()->setSizeConstraint(QLayout::SetFixedSize);
|
2015-02-12 21:01:20 +00:00
|
|
|
|
2017-03-19 23:44:49 +00:00
|
|
|
auto vlist = ENV.metadataIndex()->get("net.minecraft");
|
|
|
|
if(vlist->isLoaded())
|
2017-03-12 18:45:28 +00:00
|
|
|
{
|
|
|
|
setSelectedVersion(vlist->getRecommended());
|
|
|
|
}
|
2017-03-19 23:44:49 +00:00
|
|
|
else
|
|
|
|
{
|
2017-11-11 00:38:31 +00:00
|
|
|
vlist->load(Net::Mode::Online);
|
2017-03-19 23:44:49 +00:00
|
|
|
auto task = vlist->getLoadTask();
|
|
|
|
if(vlist->isLoaded())
|
|
|
|
{
|
|
|
|
setSelectedVersion(vlist->getRecommended());
|
|
|
|
}
|
|
|
|
if(task)
|
|
|
|
{
|
|
|
|
connect(task.get(), &Task::succeeded, this, &NewInstanceDialog::versionListUpdated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 00:19:12 +01:00
|
|
|
InstIconKey = "default";
|
2016-04-10 03:29:29 +01:00
|
|
|
ui->iconButton->setIcon(MMC->icons()->getIcon(InstIconKey));
|
2015-02-12 21:01:20 +00:00
|
|
|
|
|
|
|
ui->modpackEdit->setValidator(new UrlValidator(ui->modpackEdit));
|
2015-08-21 08:00:35 +01:00
|
|
|
|
|
|
|
ui->instNameTextBox->setAlignment(Qt::AlignHCenter);
|
|
|
|
|
2015-02-12 21:01:20 +00:00
|
|
|
connect(ui->modpackEdit, &QLineEdit::textChanged, this, &NewInstanceDialog::updateDialogState);
|
|
|
|
connect(ui->modpackBox, &QRadioButton::clicked, this, &NewInstanceDialog::updateDialogState);
|
|
|
|
connect(ui->versionBox, &QRadioButton::clicked, this, &NewInstanceDialog::updateDialogState);
|
2015-08-21 08:00:35 +01:00
|
|
|
connect(ui->versionTextBox, &QLineEdit::textChanged, this, &NewInstanceDialog::updateDialogState);
|
2015-02-12 21:01:20 +00:00
|
|
|
|
2014-11-08 20:17:28 +00:00
|
|
|
auto groups = MMC->instances()->getGroups().toSet();
|
|
|
|
auto groupList = QStringList(groups.toList());
|
|
|
|
groupList.sort(Qt::CaseInsensitive);
|
|
|
|
groupList.removeOne("");
|
2016-04-11 00:30:50 +01:00
|
|
|
groupList.push_front(initialGroup);
|
2014-11-08 20:17:28 +00:00
|
|
|
groupList.push_front("");
|
|
|
|
ui->groupBox->addItems(groupList);
|
2016-04-11 00:30:50 +01:00
|
|
|
int index = groupList.indexOf(initialGroup);
|
2014-11-08 20:17:28 +00:00
|
|
|
if(index == -1)
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
ui->groupBox->setCurrentIndex(index);
|
|
|
|
ui->groupBox->lineEdit()->setPlaceholderText(tr("No group"));
|
2015-08-21 08:00:35 +01:00
|
|
|
ui->buttonBox->setFocus();
|
|
|
|
|
|
|
|
originalPlaceholderText = ui->instNameTextBox->placeholderText();
|
2017-04-22 05:11:26 +01:00
|
|
|
if(!url.isEmpty())
|
|
|
|
{
|
|
|
|
ui->modpackBox->setChecked(true);
|
|
|
|
ui->modpackEdit->setText(url);
|
|
|
|
}
|
2015-08-21 08:00:35 +01:00
|
|
|
updateDialogState();
|
2013-04-22 21:39:41 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 23:44:49 +00:00
|
|
|
void NewInstanceDialog::versionListUpdated()
|
|
|
|
{
|
|
|
|
if(!m_versionSetByUser)
|
|
|
|
{
|
|
|
|
auto vlist = ENV.metadataIndex()->get("net.minecraft");
|
|
|
|
setSelectedVersion(vlist->getRecommended());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 20:41:37 +01:00
|
|
|
NewInstanceDialog::~NewInstanceDialog()
|
2013-04-22 21:39:41 +01:00
|
|
|
{
|
2013-05-03 20:41:37 +01:00
|
|
|
delete ui;
|
2013-04-22 21:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NewInstanceDialog::updateDialogState()
|
|
|
|
{
|
2015-08-21 08:00:35 +01:00
|
|
|
QString suggestedName;
|
|
|
|
if(ui->versionBox->isChecked())
|
|
|
|
{
|
|
|
|
suggestedName = ui->versionTextBox->text();
|
|
|
|
}
|
|
|
|
else if (ui->modpackBox->isChecked())
|
|
|
|
{
|
|
|
|
auto url = QUrl::fromUserInput(ui->modpackEdit->text());
|
|
|
|
QFileInfo fi(url.fileName());
|
2015-09-12 02:07:41 +01:00
|
|
|
suggestedName = fi.completeBaseName();
|
2015-08-21 08:00:35 +01:00
|
|
|
}
|
|
|
|
if(suggestedName.isEmpty())
|
|
|
|
{
|
|
|
|
ui->instNameTextBox->setPlaceholderText(originalPlaceholderText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->instNameTextBox->setPlaceholderText(suggestedName);
|
|
|
|
}
|
2015-09-26 03:04:09 +01:00
|
|
|
bool allowOK = !instName().isEmpty() && (
|
|
|
|
(ui->versionBox->isChecked() && m_selectedVersion) ||
|
|
|
|
(ui->modpackBox->isChecked() && ui->modpackEdit->hasAcceptableInput())
|
|
|
|
);
|
2015-04-11 11:30:18 +01:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(allowOK);
|
2013-04-22 21:39:41 +01:00
|
|
|
}
|
|
|
|
|
2015-08-21 08:00:35 +01:00
|
|
|
void NewInstanceDialog::setSelectedVersion(BaseVersionPtr version)
|
2013-04-22 21:39:41 +01:00
|
|
|
{
|
|
|
|
m_selectedVersion = version;
|
2013-10-06 13:43:46 +01:00
|
|
|
|
2013-04-22 21:39:41 +01:00
|
|
|
if (m_selectedVersion)
|
|
|
|
{
|
2017-03-19 23:44:49 +00:00
|
|
|
ui->versionTextBox->setText(version->descriptor());
|
2013-04-22 21:39:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->versionTextBox->setText("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 23:15:18 +01:00
|
|
|
QString NewInstanceDialog::instName() const
|
|
|
|
{
|
2015-08-21 08:00:35 +01:00
|
|
|
auto result = ui->instNameTextBox->text();
|
|
|
|
if(result.size())
|
|
|
|
{
|
2016-05-03 23:29:40 +01:00
|
|
|
return result.trimmed();
|
2015-08-21 08:00:35 +01:00
|
|
|
}
|
|
|
|
result = ui->instNameTextBox->placeholderText();
|
|
|
|
if(result.size() && result != originalPlaceholderText)
|
|
|
|
{
|
2016-05-03 23:29:40 +01:00
|
|
|
return result.trimmed();
|
2015-08-21 08:00:35 +01:00
|
|
|
}
|
|
|
|
return QString();
|
2013-04-22 23:15:18 +01:00
|
|
|
}
|
2015-08-21 08:00:35 +01:00
|
|
|
|
2014-11-08 20:17:28 +00:00
|
|
|
QString NewInstanceDialog::instGroup() const
|
|
|
|
{
|
|
|
|
return ui->groupBox->currentText();
|
|
|
|
}
|
2013-04-22 23:15:18 +01:00
|
|
|
QString NewInstanceDialog::iconKey() const
|
|
|
|
{
|
2013-08-10 17:34:08 +01:00
|
|
|
return InstIconKey;
|
2013-04-22 23:15:18 +01:00
|
|
|
}
|
2015-02-12 21:01:20 +00:00
|
|
|
QUrl NewInstanceDialog::modpackUrl() const
|
|
|
|
{
|
|
|
|
if (ui->modpackBox->isChecked())
|
|
|
|
{
|
|
|
|
const QUrl url(ui->modpackEdit->text());
|
|
|
|
if (url.isValid() && !url.isRelative() && !url.host().isEmpty())
|
|
|
|
{
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QUrl::fromLocalFile(ui->modpackEdit->text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QUrl();
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 23:15:18 +01:00
|
|
|
|
2013-09-15 23:54:39 +01:00
|
|
|
BaseVersionPtr NewInstanceDialog::selectedVersion() const
|
2013-04-22 23:15:18 +01:00
|
|
|
{
|
|
|
|
return m_selectedVersion;
|
|
|
|
}
|
|
|
|
|
2013-04-22 21:39:41 +01:00
|
|
|
void NewInstanceDialog::on_btnChangeVersion_clicked()
|
|
|
|
{
|
2017-03-19 23:44:49 +00:00
|
|
|
VersionSelectDialog vselect(ENV.metadataIndex()->get("net.minecraft").get(), tr("Change Minecraft version"), this);
|
2013-08-10 17:34:08 +01:00
|
|
|
vselect.exec();
|
|
|
|
if (vselect.result() == QDialog::Accepted)
|
2013-04-22 21:39:41 +01:00
|
|
|
{
|
2013-09-15 23:54:39 +01:00
|
|
|
BaseVersionPtr version = vselect.selectedVersion();
|
2013-05-03 20:41:37 +01:00
|
|
|
if (version)
|
2017-03-19 23:44:49 +00:00
|
|
|
{
|
|
|
|
m_versionSetByUser = true;
|
2013-05-03 20:41:37 +01:00
|
|
|
setSelectedVersion(version);
|
2017-03-19 23:44:49 +00:00
|
|
|
}
|
2013-04-22 21:39:41 +01:00
|
|
|
}
|
|
|
|
}
|
2013-05-07 03:28:28 +01:00
|
|
|
|
2013-08-10 17:34:08 +01:00
|
|
|
void NewInstanceDialog::on_iconButton_clicked()
|
|
|
|
{
|
2013-08-27 17:27:58 +01:00
|
|
|
IconPickerDialog dlg(this);
|
2015-09-26 03:04:09 +01:00
|
|
|
dlg.execWithSelection(InstIconKey);
|
2013-10-06 13:43:46 +01:00
|
|
|
|
|
|
|
if (dlg.result() == QDialog::Accepted)
|
2013-08-10 17:34:08 +01:00
|
|
|
{
|
|
|
|
InstIconKey = dlg.selectedIconKey;
|
2016-04-10 03:29:29 +01:00
|
|
|
ui->iconButton->setIcon(MMC->icons()->getIcon(InstIconKey));
|
2013-08-10 17:34:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 03:28:28 +01:00
|
|
|
void NewInstanceDialog::on_instNameTextBox_textChanged(const QString &arg1)
|
|
|
|
{
|
|
|
|
updateDialogState();
|
|
|
|
}
|
2014-11-08 20:17:28 +00:00
|
|
|
|
2015-02-12 21:01:20 +00:00
|
|
|
void NewInstanceDialog::on_modpackBtn_clicked()
|
|
|
|
{
|
|
|
|
const QUrl url = QFileDialog::getOpenFileUrl(this, tr("Choose modpack"), modpackUrl(), tr("Zip (*.zip)"));
|
|
|
|
if (url.isValid())
|
|
|
|
{
|
|
|
|
if (url.isLocalFile())
|
|
|
|
{
|
|
|
|
ui->modpackEdit->setText(url.toLocalFile());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->modpackEdit->setText(url.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|