-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
232 lines (194 loc) · 6.87 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <stdio.h>
#ifdef __linux__
#include <unistd.h>
#define GetCurrentDir getcwd
#define PATH_SEPARATOR "/"
#elif defined _WIN32 //We are on Windows, THIS BRANCH IS NOT TESTED
#include <direct.h>
#define GetCurrentDir _getcwd
#define PATH_SEPARATOR "\\"
#endif
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), count(countStart)
{
setWindowIcon(QIcon(":/tusliLogo_quadratisch.png"));
ui->setupUi(this);
timer = new QBasicTimer();
statusBar()->showMessage(tr("Created timer %1").arg(timer->timerId()), 1000);
connect(ui->homeNameEdit, SIGNAL(textChanged(const QString&)), this, SLOT(WriteHomeTeamToFile()));
connect(ui->guestNameEdit, SIGNAL(textChanged(const QString&)), this, SLOT(WriteGuestTeamToFile()));
connect(ui->legComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(WriteLegToFile()));
connect(ui->homeGoalsSpinBox, SIGNAL(editingFinished()), this, SLOT(WriteHomeTeamGoalsToFile()));
connect(ui->guestGoalsSpinBox, SIGNAL(editingFinished()), this, SLOT(WriteGuestTeamGoalsToFile()));
connect(ui->homeIncreaseGoalsButton, SIGNAL(clicked(bool)), this, SLOT(IncreaseHomeGoals()));
connect(ui->homeDecreaseGoalsButton, SIGNAL(clicked(bool)), this, SLOT(DecreaseHomeGoals()));
connect(ui->guestIncreaseGoalsButton, SIGNAL(clicked(bool)), this, SLOT(IncreaseGuestGoals()));
connect(ui->guestDecreaseGoalsButton, SIGNAL(clicked(bool)), this, SLOT(DecreaseGuestGoals()));
connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::Start);
connect(ui->setButton, &QPushButton::clicked, this, &MainWindow::Set);
connect(ui->set5Button, &QPushButton::clicked, this, &MainWindow::Set5);
connect(ui->set10Button, &QPushButton::clicked, this, &MainWindow::Set10);
connect(ui->set15Button, &QPushButton::clicked, this, &MainWindow::Set15);
connect(ui->set30Button, &QPushButton::clicked, this, &MainWindow::Set30);
connect(ui->set35Button, &QPushButton::clicked, this, &MainWindow::Set35);
connect(ui->clearButton, &QPushButton::clicked, this, &MainWindow::Clear);
QPixmap image(QString::fromStdString(this->getCurrentWorkingDir() + PATH_SEPARATOR + "tusliLogo.png"));
ui->logoLabel->setPixmap(image);
this->Clear();
}
MainWindow::~MainWindow()
{
delete timer;
delete ui;
}
void MainWindow::Clear()
{
count = countStart;
ui->lcdNumber->display(this->formattedTime());
ui->homeNameEdit->setText("");
ui->guestNameEdit->setText("");
ui->homeGoalsSpinBox->setValue(0);
ui->guestGoalsSpinBox->setValue(0);
ui->legComboBox->setCurrentIndex(0);
this->WriteHomeTeamToFile();
this->WriteGuestTeamToFile();
this->WriteLegToFile();
this->WriteHomeTeamGoalsToFile();
this->WriteGuestTeamGoalsToFile();
this->writeTimeToFile();
}
void MainWindow::Set()
{
auto timeToBeSet = ui->timeEdit->time();
count = timeToBeSet.minute() * 60 + timeToBeSet.second();
ui->lcdNumber->display(this->formattedTime());
this->writeTimeToFile();
}
void MainWindow::Set5()
{
count = 300; //300 seconds = 5 minutes
ui->lcdNumber->display(this->formattedTime());
this->writeTimeToFile();
}
void MainWindow::Set10()
{
count = 600; //600 seconds = 10 minutes
ui->lcdNumber->display(this->formattedTime());
this->writeTimeToFile();
}
void MainWindow::Set15()
{
count = 900; //900 seconds = 15 minutes
ui->lcdNumber->display(this->formattedTime());
this->writeTimeToFile();
}
void MainWindow::Set30()
{
count = 1800; //1800 seconds = 30 minutes
ui->lcdNumber->display(this->formattedTime());
this->writeTimeToFile();
}
void MainWindow::Set35()
{
count = 2100; //2100 seconds = 35 minutes
ui->lcdNumber->display(this->formattedTime());
this->writeTimeToFile();
}
void MainWindow::Start()
{
if (timer->isActive()) {
timer->stop();
ui->startButton->setText(tr("Start"));
} else {
Qt::TimerType type = Qt::PreciseTimer;
timer->start(1000, type, this);
ui->startButton->setText(tr("Stop"));
}
}
void MainWindow::IncreaseHomeGoals()
{
ui->homeGoalsSpinBox->setValue(ui->homeGoalsSpinBox->value() + 1);
this->WriteHomeTeamGoalsToFile();
}
void MainWindow::DecreaseHomeGoals()
{
ui->homeGoalsSpinBox->setValue(ui->homeGoalsSpinBox->value() - 1);
this->WriteHomeTeamGoalsToFile();
}
void MainWindow::IncreaseGuestGoals()
{
ui->guestGoalsSpinBox->setValue(ui->guestGoalsSpinBox->value() + 1);
this->WriteGuestTeamGoalsToFile();
}
void MainWindow::DecreaseGuestGoals()
{
ui->guestGoalsSpinBox->setValue(ui->guestGoalsSpinBox->value() - 1);
this->WriteGuestTeamGoalsToFile();
}
void MainWindow::WriteHomeTeamToFile()
{
this->writeToFile(this->getCurrentWorkingDir() + PATH_SEPARATOR + "home.txt", ui->homeNameEdit->text().toStdString());
}
void MainWindow::WriteGuestTeamToFile()
{
this->writeToFile(this->getCurrentWorkingDir() + PATH_SEPARATOR + "guest.txt", ui->guestNameEdit->text().toStdString());
}
void MainWindow::WriteLegToFile()
{
this->writeToFile(this->getCurrentWorkingDir() + PATH_SEPARATOR + "leg.txt", ui->legComboBox->currentText().toStdString());
}
void MainWindow::WriteHomeTeamGoalsToFile()
{
this->writeToFile(this->getCurrentWorkingDir() + PATH_SEPARATOR + "homeGoals.txt", ui->homeGoalsSpinBox->text().toStdString());
}
void MainWindow::WriteGuestTeamGoalsToFile()
{
this->writeToFile(this->getCurrentWorkingDir() + PATH_SEPARATOR + "guestGoals.txt", ui->guestGoalsSpinBox->text().toStdString());
}
void MainWindow::writeTimeToFile()
{
std::string formattedTime = this->formattedTimeAsNormalString();
if (count >= 0) ui->lcdNumber->display(QString::fromStdString(formattedTime));
this->writeToFile(this->getCurrentWorkingDir() + PATH_SEPARATOR + "timer.txt", formattedTime);
}
QString MainWindow::formattedTime()
{
return QString::fromStdString(this->formattedTimeAsNormalString());
}
std::string MainWindow::formattedTimeAsNormalString()
{
std::stringstream stringStream;
int remainingMinutes = count / 60;
int remainingSeconds = count % 60;
if (remainingMinutes < 10) stringStream << std::string("0");
stringStream << remainingMinutes << ":";
if (remainingSeconds < 10) stringStream << std::string("0");
stringStream << remainingSeconds;
return stringStream.str();
}
void MainWindow::writeToFile(std::string file, std::string content)
{
std::ofstream outputFile(file);
outputFile << content;
outputFile.close();
}
std::string MainWindow::getCurrentWorkingDir()
{
char buff[FILENAME_MAX];
GetCurrentDir(buff, FILENAME_MAX);
std::string current_working_dir(buff);
return current_working_dir;
}
void MainWindow::timerEvent(QTimerEvent *event)
{
count--;
this->writeTimeToFile();
event->accept();
}