-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
477 lines (412 loc) · 17.1 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "inisyntaxhighlighter.h"
#include <mapcraftercore/version.h>
#include <mapcraftercore/config/mapcrafterconfig.h>
#include <sstream>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QFont>
#include <QIcon>
#include <QMessageBox>
#include <QSettings>
#include <QStringList>
#include <QTextStream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
currentTab(0), currentlyRendering(false), currentFile(""), currentFileDirty(false),
manager(NULL)
{
ui->setupUi(this);
readSettings();
setCurrentFile("", false);
// configure file menu
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(open()));
connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
connect(ui->actionSaveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
recentFilesSeparator = new QAction(this);
recentFilesSeparator->setSeparator(true);
ui->menuFile->addAction(recentFilesSeparator);
for (int i = 0; i < MAX_RECENT_FILES; i++) {
recentFileActions[i] = new QAction(this);
recentFileActions[i]->setVisible(false);
ui->menuFile->addAction(recentFileActions[i]);
connect(recentFileActions[i], SIGNAL(triggered()), this, SLOT(openRecentFile()));
}
updateRecentFiles();
// configure config file tab
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
ui->textEdit->setFont(font);
INISyntaxHighlighter* highlighter = new INISyntaxHighlighter(ui->textEdit->document());
connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(handleTextChanged()));
connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(validateConfig()));
// configure tab widget
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(handleTabChanged(int)));
// configure rendering tab
QMenu* menu = new QMenu(this);
QAction* action1 = new QAction("auto", this);
QAction* action2 = new QAction("force", this);
QAction* action3 = new QAction("skip", this);
connect(action1, SIGNAL(triggered()), this, SLOT(handleSetRenderBehaviorsTo()));
connect(action2, SIGNAL(triggered()), this, SLOT(handleSetRenderBehaviorsTo()));
connect(action3, SIGNAL(triggered()), this, SLOT(handleSetRenderBehaviorsTo()));
menu->addAction(action1);
menu->addAction(action2);
menu->addAction(action3);
ui->buttonSetAllRenderBehaviorsTo->setMenu(menu);
ui->buttonSetAllRenderBehaviorsTo->setPopupMode(QToolButton::InstantPopup);
worker = new RenderWorker();
worker->moveToThread(&thread);
thread.start();
connect(ui->buttonRender, SIGNAL(clicked()), this, SLOT(handleRender()));
connect(this, SIGNAL(startRendering()), worker, SLOT(scanWorlds()));
connect(worker, SIGNAL(scanWorldsFinished()), worker, SLOT(renderMaps()));
connect(worker, SIGNAL(renderMapsFinished()), this, SLOT(handleRenderFinished()));
connect(worker, SIGNAL(labelMapsProgressChanged(QString)), ui->labelMapsProgress, SLOT(setText(QString)));
connect(worker, SIGNAL(progressMapsMaxChanged(int)), ui->progressMaps, SLOT(setMaximum(int)));
connect(worker, SIGNAL(progressMapsValueChanged(int)), ui->progressMaps, SLOT(setValue(int)));
connect(worker, SIGNAL(labelTilesProgressLeftChanged(QString)), ui->labelTilesProgressLeft, SLOT(setText(QString)));
connect(worker, SIGNAL(labelTilesProgressCenterChanged(QString)), ui->labelTilesProgressCenter, SLOT(setText(QString)));
connect(worker, SIGNAL(labelTilesProgressRightChanged(QString)), ui->labelTilesProgressRight, SLOT(setText(QString)));
connect(worker, SIGNAL(progressTilesMaxChanged(int)), ui->progressTiles, SLOT(setMaximum(int)));
connect(worker, SIGNAL(progressTilesValueChanged(int)), ui->progressTiles, SLOT(setValue(int)));
// configure command line rendering tab
connect(ui->customMapcrafterBinary, SIGNAL(toggled(bool)), this, SLOT(updateMapcrafterCommand()));
connect(ui->mapcrafterBinary, SIGNAL(textChanged(QString)), this, SLOT(updateMapcrafterCommand()));
connect(ui->threads, SIGNAL(valueChanged(int)), this, SLOT(updateMapcrafterCommand()));
connect(ui->mapsSkipAll, SIGNAL(toggled(bool)), this, SLOT(updateMapcrafterCommand()));
connect(ui->mapsSkip, SIGNAL(textChanged(QString)), this, SLOT(updateMapcrafterCommand()));
connect(ui->mapsAuto, SIGNAL(textChanged(QString)), this, SLOT(updateMapcrafterCommand()));
connect(ui->mapsForce, SIGNAL(textChanged(QString)), this, SLOT(updateMapcrafterCommand()));
connect(ui->mapsForceAll, SIGNAL(toggled(bool)), this, SLOT(updateMapcrafterCommand()));
updateMapcrafterCommand();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newFile() {
if (currentFileDirty) {
QMessageBox dialog(this);
dialog.setWindowTitle("Save file");
dialog.setText("Do you want to save the file?");
dialog.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
dialog.setDefaultButton(QMessageBox::Yes);
int result = dialog.exec();
if (result == QMessageBox::Yes)
save();
if (result == QMessageBox::Cancel)
return;
}
ui->textEdit->setText("");
setCurrentFile("", false);
}
void MainWindow::open()
{
// TODO handle this differently?
if (currentlyRendering) {
QMessageBox::critical(this, "Open file", "Please wait until rendering is finished.");
return;
}
QString filename = QFileDialog::getOpenFileName(this);
if(filename.isEmpty())
return;
loadFile(filename);
}
void MainWindow::openRecentFile() {
// TODO handle this differently?
if (currentlyRendering) {
QMessageBox::critical(this, "Open file", "Please wait until rendering is finished.");
return;
}
QAction* action = qobject_cast<QAction*>(sender());
if (action)
loadFile(action->data().toString());
}
void MainWindow::save()
{
if (currentFile.isEmpty())
saveAs();
else
saveFile(currentFile);
}
void MainWindow::saveAs()
{
QString filename = QFileDialog::getSaveFileName(this);
if(filename.isEmpty())
return;
saveFile(filename);
}
void MainWindow::about()
{
std::stringstream stream;
stream << "<b>Mapcrafter GUI version:</b> " << QCoreApplication::applicationVersion().toStdString() << "<br />";
stream << "<b>Mapcrafter version:</b> " << mapcrafter::MAPCRAFTER_VERSION;
if(!QString(mapcrafter::MAPCRAFTER_GITVERSION).isEmpty())
stream << " (" << mapcrafter::MAPCRAFTER_GITVERSION << ")";
stream << "<br /><br />";
fs::path mapcrafter_bin = mapcrafter::util::findExecutablePath();
stream << "<b>Your home directory:</b> " << mapcrafter::util::findHomeDir().string().c_str() << "<br />";
stream << "<b>Mapcrafter binary:</b> " << mapcrafter_bin.string().c_str() << "<br /><br />";
mapcrafter::util::PathList resources = mapcrafter::util::findResourceDirs(mapcrafter_bin);
stream << "<b>Resource directories:</b><ol>";
for (size_t i = 0; i < resources.size(); i++)
stream << "<li>" << BOOST_FS_ABSOLUTE1(resources[i]).string().c_str() << "</li>";
if (resources.size() == 0)
stream << "<li>Nothing found.</li>";
stream << "</ol>";
mapcrafter::util::PathList templates = mapcrafter::util::findTemplateDirs(mapcrafter_bin);
stream << "<b>Template directories:</b><ol>";
for (size_t i = 0; i < templates.size(); i++)
stream << "<li>" << BOOST_FS_ABSOLUTE1(templates[i]).string().c_str() << "</li>";
if (templates.size() == 0)
stream << "<li>Nothing found.</li>";
stream << "</ol>";
mapcrafter::util::PathList configs = mapcrafter::util::findLoggingConfigFiles(mapcrafter_bin);
stream << "<b>Logging configuration file:</b><ol>";
for (size_t i = 0; i < configs.size(); i++)
stream << "<li>" << BOOST_FS_ABSOLUTE1(configs[i]).string().c_str() << "</li>";
if (configs.size() == 0)
stream << "<li>Nothing found.</li>";
stream << "</ol>";
QMessageBox::information(this, "About", stream.str().c_str());
}
int MainWindow::validateConfig() {
std::string configText = ui->textEdit->document()->toPlainText().toStdString();
fs::path configDir = fs::path(currentFile.toStdString()).parent_path();
config = config::MapcrafterConfig();
config::ValidationMap validation = config.parseString(configText, configDir);
//validation.log();
ui->validationWidget->setValidation(validation);
ui->inputRenderBehaviors->setRenderBehaviors(renderer::RenderBehaviors(renderer::RenderBehavior::AUTO), config);
if (validation.isEmpty()) {
return 0;
} else if (validation.isCritical()) {
return 2;
} else {
return 1;
}
}
void MainWindow::updateMapcrafterCommand()
{
QString mapcrafter;
if (ui->customMapcrafterBinary->isChecked()) {
mapcrafter = ui->mapcrafterBinary->text();
} else {
fs::path mapcrafterDir = mapcrafter::util::findExecutableMapcrafterDir();
mapcrafter = QString::fromStdString((mapcrafterDir / "mapcrafter").string());
ui->mapcrafterBinary->setText(mapcrafter);
}
if (currentFile.isEmpty() || mapcrafter.isEmpty()) {
ui->mapcrafterCommand->setText("");
return;
}
QStringList args;
args << "\"" + mapcrafter + "\"";
args << "-c" << "\"" + currentFile + "\"";
args << "-j" << QString::number(ui->threads->value());
if (ui->mapsSkipAll->isChecked()) {
args << "-r";
} else if (!ui->mapsSkip->text().isEmpty()) {
args << "-s" << ui->mapsSkip->text();
}
if (!ui->mapsAuto->text().isEmpty())
args << "-a" << ui->mapsAuto->text();
if (ui->mapsForceAll->isChecked()) {
args << "-F";
} else if (!ui->mapsForce->text().isEmpty()) {
args << "-f" << ui->mapsForce->text();
}
ui->mapcrafterCommand->setText(args.join(" "));
}
void MainWindow::handleTextChanged()
{
setCurrentFile(currentFile, true);
}
void MainWindow::handleTabChanged(int tab) {
// make sure config is valid when switching to rendering tab
if (currentTab != 1 && tab == 1) {
int status = validateConfig();
if (status == 1) {
QMessageBox dialog(this);
dialog.setWindowTitle("Configuration validation");
dialog.setIcon(QMessageBox::Warning);
dialog.setText("There are warnings about your configuration file. Do still you want to proceed to render your map(s)?");
dialog.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
dialog.setDefaultButton(QMessageBox::Yes);
int result = dialog.exec();
if (result == QMessageBox::No) {
ui->tabWidget->setCurrentIndex(0);
return;
}
} else if (status == 2) {
QMessageBox::critical(this, "Configuration validation", "You have to fix the errors in your configuration file before you can proceed to render your map(s).");
ui->tabWidget->setCurrentIndex(0);
return;
}
}
// TODO handle this differently
if (currentTab == 1 && tab != 1 && currentlyRendering) {
QMessageBox::critical(this, "TODO", "Please wait until rendering is finished.");
ui->tabWidget->setCurrentIndex(1);
return;
}
currentTab = tab;
}
void MainWindow::handleSetRenderBehaviorsTo() {
QAction* action = qobject_cast<QAction*>(sender());
if (action != nullptr) {
renderer::RenderBehavior behavior = renderer::RenderBehavior::AUTO;
if (action->text() == "force")
behavior = renderer::RenderBehavior::FORCE;
else if (action->text() == "skip")
behavior = renderer::RenderBehavior::SKIP;
else if (action->text() != "auto")
return;
ui->inputRenderBehaviors->setRenderBehaviors(renderer::RenderBehaviors(behavior), config);
}
}
void MainWindow::handleRender()
{
if (manager)
delete manager;
currentlyRendering = true;
manager = new renderer::RenderManager(config);
manager->initialize();
manager->setRenderBehaviors(ui->inputRenderBehaviors->getRenderBehaviors());
worker->setConfig(config);
worker->setRenderManager(manager);
worker->setThreadCount(ui->inputThreadCount->value());
emit startRendering();
ui->labelRenderBehaviors->setEnabled(false);
ui->buttonSetAllRenderBehaviorsTo->setEnabled(false);
ui->inputRenderBehaviors->setEnabled(false);
ui->labelThreadCount->setEnabled(false);
ui->inputThreadCount->setEnabled(false);
ui->buttonRender->setEnabled(false);
}
void MainWindow::handleRenderFinished() {
currentlyRendering = false;
ui->labelRenderBehaviors->setEnabled(true);
ui->buttonSetAllRenderBehaviorsTo->setEnabled(true);
ui->inputRenderBehaviors->setEnabled(true);
ui->labelThreadCount->setEnabled(true);
ui->inputThreadCount->setEnabled(true);
ui->buttonRender->setEnabled(true);
ui->labelMapsProgress->setText("-");
ui->progressMaps->setMaximum(1);
ui->progressMaps->setValue(0);
ui->labelTilesProgressLeft->setText("-");
ui->labelTilesProgressCenter->setText("");
ui->labelTilesProgressRight->setText("");
ui->progressTiles->setMaximum(1);
ui->progressTiles->setValue(0);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (currentFileDirty) {
QMessageBox dialog(this);
dialog.setWindowTitle("Save file");
dialog.setText("Do you want to save the file?");
dialog.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
dialog.setDefaultButton(QMessageBox::Yes);
int result = dialog.exec();
if (result == QMessageBox::Yes)
save();
if (result == QMessageBox::Cancel)
event->setAccepted(false);
}
writeSettings();
}
void MainWindow::loadFile(const QString& filename)
{
QFile file(filename);
file.open(QIODevice::ReadOnly);
QTextStream stream(&file);
ui->textEdit->setPlainText(stream.readAll());
file.close();
setCurrentFile(filename, false);
int status = validateConfig();
// goto rendering tab to config tab if config file contains errors
if (status == 2 && ui->tabWidget->currentIndex() == 1)
ui->tabWidget->setCurrentIndex(0);
}
void MainWindow::saveFile(const QString& filename)
{
QFile file(filename);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream stream(&file);
stream << ui->textEdit->toPlainText();
file.close();
setCurrentFile(filename, false);
}
void MainWindow::setCurrentFile(const QString &filename, bool dirty)
{
currentFile = filename;
currentFileDirty = dirty;
QString shortName = QFileInfo(QFile(filename)).fileName();
if (filename.isEmpty())
shortName = "Empty file";
if (dirty)
shortName += "*";
setWindowTitle(shortName + " - " + QCoreApplication::applicationName());
if (filename.isEmpty())
return;
QSettings settings;
QStringList recentFiles = settings.value("recentFiles").toStringList();
recentFiles.removeAll(filename);
recentFiles.prepend(filename);
while (recentFiles.size() > MAX_RECENT_FILES)
recentFiles.removeLast();
settings.setValue("recentFiles", recentFiles);
updateRecentFiles();
updateMapcrafterCommand();
}
void MainWindow::updateRecentFiles()
{
QSettings settings;
QStringList recentFiles = settings.value("recentFiles").toStringList();
int numRecentFiles = qMin(recentFiles.size(), MAX_RECENT_FILES);
for (int i = 0; i < numRecentFiles; i++) {
QString text = tr("&%1 %2").arg(i + 1).arg(QFileInfo(QFile(recentFiles[i])).fileName());
recentFileActions[i]->setText(text);
recentFileActions[i]->setData(recentFiles[i]);
recentFileActions[i]->setVisible(true);
}
for (int j = numRecentFiles; j < MAX_RECENT_FILES; j++)
recentFileActions[j]->setVisible(false);
recentFilesSeparator->setVisible(numRecentFiles != 0);
}
void MainWindow::readSettings()
{
QSettings settings;
restoreGeometry(settings.value("MainWindow/geometry").toByteArray());
restoreState(settings.value("MainWindow/windowState").toByteArray());
ui->splitter->restoreState(settings.value("MainWindow/splitterState").toByteArray());
if (settings.contains("mapcrafterBinary")) {
ui->customMapcrafterBinary->setChecked(true);
ui->mapcrafterBinary->setText(settings.value("mapcrafterBinary").toString());
updateMapcrafterCommand();
}
}
void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue("MainWindow/geometry", saveGeometry());
settings.setValue("MainWindow/windowState", saveState());
settings.setValue("MainWindow/splitterState", ui->splitter->saveState());
if (ui->customMapcrafterBinary->isChecked()) {
settings.setValue("mapcrafterBinary", ui->mapcrafterBinary->text());
} else {
settings.remove("mapcrafterBinary");
}
}
const int MainWindow::MAX_RECENT_FILES;