/
mainwindow.cpp
128 lines (102 loc) · 3.32 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
/*
GUI main class code
Copyright (C) 2015 Pierre Lefebvre
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Set title
this->setWindowTitle(WE_APP_TITLE);
ui->btnApply->setEnabled(false);
// Connect signals/slots
QObject::connect(ui->btnApply, SIGNAL(clicked()), this, SLOT(apply()));
QObject::connect(ui->btnFilePath, SIGNAL(clicked()), this, SLOT(chooseFile()));
QObject::connect(ui->btnQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
void MainWindow::apply(){
// Display error message if text field is empty
if(ui->txtFilePath->text().isEmpty())
{
displayMessage(WE_ERROR, WE_EMPTY_FILE);
return;
}
// Display error message if file doesn't exist
QFile f(ui->txtFilePath->text());
if(!f.exists())
{
displayMessage(WE_ERROR, WE_FILE_DOESNT_EXIST);
return;
}
// Open the game file and write the value from slider
FileWriter fw(ui->txtFilePath->text().toStdString().c_str());
int power = ui->sldPower->value();
// Display error message if update failed or success message otherwise
if(fw.setPower(power) != FW_ERROR)
{
displayMessage(WE_INFO, WE_FILE_UPDATED);
ui->btnApply->setEnabled(false);
}
else
{
displayMessage(WE_ERROR, WE_UNKNOWN_ERROR);
}
}
void MainWindow::chooseFile(){
// Build the warzone game files directory
QString dir(getenv("HOME"));
dir.append(WE_WARZONE_DIR);
// Get the filename from a file chooser dialog
QString s = QFileDialog::getOpenFileName(this, "Open saved game", dir, "Game Files (*.gam)");
if(s.isEmpty())
{
displayMessage(WE_ERROR, WE_EMPTY_FILE);
return;
}
// Update text field
ui->txtFilePath->setText(s);
// Update 'apply' button state
ui->btnApply->setEnabled(true);
// Open the game file
FileWriter fw(s.toStdString().c_str());
// Get the current power resource value
int currentPower = fw.getPower();
// Return if it's failed
if(currentPower == FW_ERROR)
{
displayMessage(WE_INFO, WE_FILE_UPDATED);
ui->btnApply->setEnabled(false);
return;
}
// Update slider and label value
ui->sldPower->setValue(currentPower);
ui->lblPower->setNum((int)currentPower);
}
void MainWindow::displayMessage(int msgType, const char* msg) {
QMessageBox messageBox;
if(msgType == WE_ERROR)
{
messageBox.critical(0,WE_APP_TITLE, msg);
}
else
{
messageBox.information(0,WE_APP_TITLE, msg);
}
}
MainWindow::~MainWindow()
{
delete ui;
}