forked from sxxx/Qt-CK2-Launcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
201 lines (171 loc) · 6 KB
/
mainwindow.cpp
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
191
192
193
194
195
196
197
198
199
200
201
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "firstdialog.h"
#include "QDir"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
settings(new QSettings),
ui(new Ui::MainWindow)
{
// Check if this first run
if (! settings->value("internal/isConfigured").toBool()) {
// Open modal dialog
FirstDialog dialog(this);
dialog.exec();
// Quit if user press Cancel or close dialog window
if (!dialog.dialogCompleted) {
parent->close();
}
}
ui->setupUi(this);
// Load Styles
loadStyles();
// Load launcher page
ui->webView->load(QUrl(settings->value("internal/urlLauncher").toString()));
// Disable context menu
ui->webView->setContextMenuPolicy(Qt::CustomContextMenu);
foreach(QString key, settings->allKeys())
{
if (key.right(4) == ".mod")
{
QDir dir(settings->value("userOptions/pathConfig").toString() + "mod/");
if(!dir.exists(key))
settings->remove(key);
}
}
foreach(QString modName, listFiles(settings->value("userOptions/pathConfig").toString() + "mod/", "*.mod"))
{
QString modIniName = extractName(settings->value("userOptions/pathConfig").toString() + "/mod/", modName);
if (!settings->contains(modName))
settings->setValue(modName, Qt::Unchecked);
QListWidgetItem *itemOne = new QListWidgetItem(
modIniName,
ui->listMod);
if (settings->value(modName) == Qt::Checked)
itemOne->setCheckState(Qt::Checked);
else
itemOne->setCheckState(Qt::Unchecked);
itemOne->setData(Qt::UserRole, modName);
}
foreach(QString dlcName, listFiles(settings->value("userOptions/pathGame").toString() + "/dlc/", "*.dlc"))
{
QString dlcIniName = extractName(settings->value("userOptions/pathGame").toString() + "/dlc/", dlcName);
if (!settings->contains(dlcName))
{
settings->setValue(dlcName, Qt::Checked);
}
QListWidgetItem *itemOne = new QListWidgetItem(
dlcIniName,
ui->listDLC);
if (settings->value(dlcName) == Qt::Checked)
itemOne->setCheckState(Qt::Checked);
else
itemOne->setCheckState(Qt::Unchecked);
itemOne->setData(Qt::UserRole, dlcName);
}
}
QStringList MainWindow::listFiles(QString directory, QString extension)
{
QDir modDir(directory);
QStringList filters;
filters << extension;
return modDir.entryList(filters, QDir::Files);
}
QString MainWindow::extractName(QString iniDir, QString iniName)
{
QSettings ini(iniDir.append(iniName), QSettings::IniFormat);
if (ini.value("name").toString() == "")
return iniName;
else
return ini.value("name").toString();
}
/**
Launcher use original game (proprietary) content which can't going bundled with this code.
For now don't find any better way to change path to background images and icon.
*/
void MainWindow::loadStyles()
{
QString launcherPath = settings->value("userOptions/pathGame").toString()+"launcher/";
QIcon icon;
icon.addFile(launcherPath+"logo.ico", QSize(), QIcon::Normal, QIcon::Off);
setWindowIcon(icon);
ui->widget->setStyleSheet(
"#widget { background-image: url("+launcherPath+"background.jpg); }"
"#buttonRegister { background-image: url("+launcherPath+"button_small.jpg);border-width: 1px; }"
"#buttonRun { background-image: url("+launcherPath+"button_big.jpg);border-width: 1px; }"
);
}
/* * * * * * * * * *
Slots
* * * * * * * * * */
void MainWindow::listItemChanged(QListWidgetItem* changed)
{
bool checked = changed->checkState() == Qt::Checked;
int index = 0;
for (; changed->listWidget()->item(index) != changed; index++) ;
QString modName = changed->data(Qt::UserRole).toString();
if (checked)
{
settings->setValue(modName, Qt::Checked);
}
else
{
settings->setValue(modName, Qt::Unchecked);
}
settings->sync();
}
void MainWindow::buttonClickedRegister()
{
QDesktopServices::openUrl(QUrl(settings->value("urlRegister").toString(), QUrl::TolerantMode));
}
void MainWindow::buttonClickedRun()
{
execPath = settings->value("userOptions/pathGame").toString()+settings->value("internal/executableName").toString();
QStringList arguments;
QStringList keysList = settings->allKeys();
foreach(QString key, keysList) {
if (key.right(4) == ".mod" && settings->value(key) == Qt::Checked) {
arguments << "-mod=mod/" + key;
}
else if (key.right(4) == ".dlc" && settings->value(key) == Qt::Unchecked) {
arguments << "-exclude_dlc=dlc/" + key;
}
}
QProcess *gameProcess = new QProcess();
connect(gameProcess, SIGNAL(started()), SLOT(gameStarted()));
connect(gameProcess, SIGNAL(error(QProcess::ProcessError)), SLOT(gameError()));
gameProcess->start(execPath, arguments);
gameProcess->closeWriteChannel();
}
void MainWindow::gameError()
{
QProcess *gameProcess = qobject_cast<QProcess *>(sender());
QMessageBox error(this);
QPushButton *configure = error.addButton(tr("Configure..."), QMessageBox::RejectRole);
QPushButton *ok = error.addButton(tr("OK"), QMessageBox::AcceptRole);
error.setDefaultButton(ok);
error.setEscapeButton(ok);
error.setWindowTitle(tr("Crusader Kings 2"));
error.setText(tr("Failed to start %1: %2").arg(execPath, gameProcess->errorString()));
error.setIcon(QMessageBox::Critical);
error.exec();
if (error.clickedButton() == configure) {
FirstDialog dialog(this);
dialog.exec();
}
}
void MainWindow::gameStarted()
{
// Quit after CK2 running
QApplication::quit();
}
void MainWindow::webviewLoadFinished(bool status)
{
if (!status) {
ui->webView->load(QUrl(settings->value("userOptions/pathGame").toString()+"launcher/launcher.html"));
}
}
MainWindow::~MainWindow()
{
delete ui;
}