summaryrefslogtreecommitdiffstats
path: root/application/dialogs/NewInstanceDialog.cpp
blob: f0783603cbddde02bdd56d6a9e1323a79c6e83a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright 2013-2018 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.
 */

#include "MultiMC.h"
#include "NewInstanceDialog.h"
#include "ui_NewInstanceDialog.h"

#include <BaseVersion.h>
#include <icons/IconList.h>
#include <tasks/Task.h>
#include <InstanceList.h>

#include "VersionSelectDialog.h"
#include "ProgressDialog.h"
#include "IconPickerDialog.h"

#include <QLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QValidator>
#include <QDialogButtonBox>

#include "widgets/PageContainer.h"
#include <pages/modplatform/VanillaPage.h>
#include <pages/modplatform/FTBPage.h>
#include <pages/modplatform/TwitchPage.h>
#include <pages/modplatform/ImportPage.h>
#include <pages/modplatform/TechnicPage.h>

NewInstanceDialog::NewInstanceDialog(const QString & initialGroup, const QString & url, QWidget *parent)
	: QDialog(parent), ui(new Ui::NewInstanceDialog)
{
	ui->setupUi(this);

	setWindowIcon(MMC->getThemedIcon("new"));

	InstIconKey = "default";
	ui->iconButton->setIcon(MMC->icons()->getIcon(InstIconKey));

	auto groups = MMC->instances()->getGroups().toSet();
	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"));

	m_buttons = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok);
	m_buttons->button(QDialogButtonBox::Ok)->setDefault(true);

	connect(m_buttons->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &QDialog::accept);
	connect(m_buttons->button(QDialogButtonBox::Help), &QPushButton::clicked, m_container, &PageContainer::help);

	m_container = new PageContainer(this);
	m_container->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Expanding);
	ui->verticalLayout->insertWidget(2, m_container);

	m_container->addButtons(m_buttons);
	m_buttons->setFocus();

	if(!url.isEmpty())
	{
		m_container->selectPage("import");
		importPage->setUrl(url);
	}

	updateDialogState();

	restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("NewInstanceGeometry").toByteArray()));
}

QList<BasePage *> NewInstanceDialog::getPages()
{
	importPage = new ImportPage(this);
	return
	{
		new VanillaPage(this),
		new FTBPage(this),
		importPage,
		new TwitchPage(this),
		new TechnicPage(this)
	};
}

QString NewInstanceDialog::dialogTitle()
{
	return tr("New Instance");
}

NewInstanceDialog::~NewInstanceDialog()
{
	delete ui;
}

void NewInstanceDialog::setSuggestedPack(const QString& name, InstanceTask* task)
{
	creationTask.reset(task);
	ui->instNameTextBox->setPlaceholderText(name);

	auto allowOK = task && !instName().isEmpty();
	m_buttons->button(QDialogButtonBox::Ok)->setEnabled(allowOK);
}

InstanceTask * NewInstanceDialog::extractTask()
{
	InstanceTask * extracted = creationTask.get();
	creationTask.release();
	extracted->setName(instName());
	extracted->setGroup(instGroup());
	extracted->setIcon(iconKey());
	return extracted;
}

void NewInstanceDialog::updateDialogState()
{
	auto allowOK = creationTask && !instName().isEmpty();
	m_buttons->button(QDialogButtonBox::Ok)->setEnabled(allowOK);
}

QString NewInstanceDialog::instName() const
{
	auto result = ui->instNameTextBox->text();
	if(result.size())
	{
		return result.trimmed();
	}
	result = ui->instNameTextBox->placeholderText();
	if(result.size())
	{
		return result.trimmed();
	}
	return QString();
}

QString NewInstanceDialog::instGroup() const
{
	return ui->groupBox->currentText();
}
QString NewInstanceDialog::iconKey() const
{
	return InstIconKey;
}

void NewInstanceDialog::on_iconButton_clicked()
{
	IconPickerDialog dlg(this);
	dlg.execWithSelection(InstIconKey);

	if (dlg.result() == QDialog::Accepted)
	{
		InstIconKey = dlg.selectedIconKey;
		ui->iconButton->setIcon(MMC->icons()->getIcon(InstIconKey));
	}
}

void NewInstanceDialog::on_instNameTextBox_textChanged(const QString &arg1)
{
	updateDialogState();
}

void NewInstanceDialog::closeEvent(QCloseEvent* event)
{
	qDebug() << "New instance dialog close requested";
	if (m_container->prepareToClose())
	{
		qDebug() << "New instance dialog close approved";
		MMC->settings()->set("NewInstanceGeometry", saveGeometry().toBase64());
		qDebug() << "New instance dialog geometry saved";
		QDialog::closeEvent(event);
	}
}