/
mainwindow.cpp
440 lines (368 loc) · 15.3 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
#include "mainwindow.h"
#include <QFont>
#include <QFileDialog>
#include <QString>
#include <QMessageBox>
#include <QGridLayout>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), logoWidget(0), classificaWidget(0), toolBar(0), newWizard(0),
tabs(0), editor(0), squadre(new SquadreModel(this)), arbitri(new ArbitriModel(this)),
fileOpen(""), confFile("preference.conf"), xml(squadre, arbitri), modificato(false)
{
createLogoWidget();
createActions();
createMenus();
createToolBar();
setCentralWidget(logoWidget);
setWindowTitle("HandBall Stats by Beatrice Guerra");
setMinimumSize(sizeHint());
if(loadPreference() && !fileOpen.isEmpty()){
open(false);
}
}
void MainWindow::createLogoWidget(){
logoWidget = new QWidget(this);
QLabel* logoLabel = new QLabel(logoWidget);
logoLabel->setPixmap(QPixmap(":/images/images/HBStats_Icon_Sfondo.png").scaled(500, 500, Qt::KeepAspectRatio));
QWidget* topFiller = new QWidget(logoWidget);
topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QWidget* bottomFiller = new QWidget(logoWidget);
bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(topFiller);
layout->addWidget(logoLabel);
layout->addWidget(bottomFiller);
layout->setAlignment(logoLabel, Qt::AlignHCenter);
logoWidget->setLayout(layout);
}
void MainWindow::createActions(){
newAct = new QAction(QIcon(":/images/images/new.png"), tr("&Nuovo"), this);
newAct->setShortcut(QKeySequence::New);
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
openAct = new QAction(QIcon(":/images/images/open.png"), tr("&Apri"), this);
openAct->setShortcut(QKeySequence::Open);
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(QIcon(":/images/images/save.png"), tr("&Salva"), this);
saveAct->setShortcut(QKeySequence::Save);
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
saveAsAct = new QAction(tr("Salva &come"), this);
saveAsAct->setShortcut(QKeySequence::SaveAs);
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
exportAct = new QAction(QIcon(":/images/images/export.png"), tr("Esporta"), this);
exportAct->setShortcut(QKeySequence(tr("Ctrl+E")));
exportAct->setEnabled(false);
editAct = new QAction(QIcon(":/images/images/edit.png"), tr("E&dita"), this);
editAct->setEnabled(false);
connect(editAct, SIGNAL(triggered()), this, SLOT(edit()));
exitAct = new QAction(QIcon(":/images/images/quit.png"), tr("&Esci"), this);
exitAct->setShortcut(QKeySequence::Quit);
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
aboutAct = new QAction(tr("Informazioni su HBStats"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt()));
resetPartitaAct = new QAction(QIcon(":/images/images/reset.png"), tr("Reset Partita"), this);
resetPartitaAct->setEnabled(false);
closePartitaAct = new QAction(QIcon(":/images/images/termina.png"), tr("Termina Partita"), this);
closePartitaAct->setEnabled(false);
connect(closePartitaAct, SIGNAL(triggered()), this, SLOT(terminaPartita()));
classificaAct = new QAction(QIcon(":/images/images/cup.png"), tr("Mostra classifica"), this);
classificaAct->setEnabled(false);
connect(classificaAct, SIGNAL(triggered()), this, SLOT(creaClassifica()));
closeTabs = new QAction(QIcon(":/images/images/close_red.png"), tr("Chiudi"), this);
connect(closeTabs, SIGNAL(triggered()), this, SLOT(chiudiTabs()));
}
void MainWindow::createMenus(){
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
fileMenu->addAction(editAct);
fileMenu->addAction(exportAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Finestra"));
editMenu->addAction(resetPartitaAct);
editMenu->addAction(closePartitaAct);
editMenu->addAction(closeTabs);
editMenu->addSeparator();
editMenu->addAction(classificaAct);
helpMenu = menuBar()->addMenu(tr("&Aiuto"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
}
void MainWindow::createToolBar(){
toolBar = addToolBar(tr("Partita"));
toolBar->addAction(exportAct);
toolBar->addAction(editAct);
toolBar->addSeparator();
toolBar->addAction(classificaAct);
toolBar->addSeparator();
toolBar->addAction(resetPartitaAct);
toolBar->addAction(closePartitaAct);
toolBar->addAction(closeTabs);
toolBar->setMovable(false);
}
bool MainWindow::maybeSave(){
if(modificato){
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Salvare le modfifiche?"),
tr("Le modifiche non sono state salvate.\n"
"Si desidera salvare prima di chiudere?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if(ret == QMessageBox::Save){
save();
return true;
}
else if(ret == QMessageBox::Discard){
return true;
}
else if(ret == QMessageBox::Cancel){
return false;
}
}
return true;
}
bool MainWindow::loadPreference(){
if (confFile.exists() && confFile.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&confFile);
while (!in.atEnd()) {
QString line = in.readLine();
if(line == "Last Session:"){
fileOpen = in.readLine();
confFile.close();
return true;
}
}
}
return false;
}
void MainWindow::writePreference(){
if(!confFile.exists()){
confFile.setFileName("preference.conf");
}
if(confFile.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&confFile);
out << "Last Session:\r\n" << fileOpen;
}
confFile.close();
}
void MainWindow::newFile(){
newWizard = new NewWizard(squadre, arbitri, this);
newWizard->setAttribute(Qt::WA_DeleteOnClose);
newWizard->setModal(true);
newWizard->show();
connect(newWizard, SIGNAL(partitaCreata()), this, SLOT(showPartita()));
connect(newWizard, SIGNAL(squadraCreata()), this, SLOT(creaClassifica()));
connect(newWizard, SIGNAL(dataChanged(bool)), this, SLOT(wasModified(bool)));
if(!editAct->isEnabled() && !squadre->isEmpty()){
editAct->setEnabled(true);
}
}
void MainWindow::open(bool showDialog){
QString fileName(fileOpen);
if(showDialog){
fileName = QFileDialog::getOpenFileName(this, tr("Scegli un file da aprire"), QDir::currentPath(), "*.hbs");
}
if(!fileName.isNull()){
if(squadre && !squadre->isEmpty()){
squadre->clear();
}
if(arbitri && !arbitri->isEmpty()){
arbitri->clear();
}
try{
xml.readFile(fileName);
fileOpen = fileName;
editAct->setEnabled(true);
classificaAct->setEnabled(true);
}
catch(Err_Open e){
QMessageBox::critical(this, tr("Errore!"), tr("Si è verificato un errore nell'apertura del file"), QMessageBox::Ok);
fileOpen = "";
}
}
}
void MainWindow::save(){
if(!squadre->isEmpty() || !arbitri->isEmpty()){
if(!fileOpen.isEmpty()){
QFile file(fileOpen);
if(file.open(QIODevice::WriteOnly)){
try{
xml.writeFile(file);
}
catch(Err_Save e){
QMessageBox::critical(this, tr("Errore!"), tr("Si è verificato un errore nel salvataggio del file"), QMessageBox::Ok);
}
}
else{
QMessageBox::warning(this, tr("Errore!"), tr("Impossibile salvare il file (modalità solo lettura)"), QMessageBox::Ok);
}
file.close();
wasModified(false);
}
else{
saveAs();
}
}
}
void MainWindow::saveAs(){
QString fileName = QFileDialog::getSaveFileName(this, tr("Salva come"), QDir::currentPath(), "*.hbs");
if(!fileName.isEmpty()){
QFile file(fileName);
if(file.open(QIODevice::WriteOnly)){
try{
xml.writeFile(file);
fileOpen = fileName;
}
catch(Err_Save e){
QMessageBox::critical(this, tr("Errore!"), tr("Si è verificato un errore nel salvataggio del file"), QMessageBox::Ok);
}
}
else{
QMessageBox::warning(this, tr("Errore!"), tr("Impossibile salavre il file (modalità solo lettura)"), QMessageBox::Ok);
}
file.close();
wasModified(false);
}
}
void MainWindow::exportPng(){
tabs->exportPng();
}
void MainWindow::edit(){
editor = new Editor(squadre, arbitri, this);
editor->setAttribute(Qt::WA_DeleteOnClose);
editor->setModal(true);
for(unsigned int i=0; i<squadre->size(); ++i){
squadre->at(i)->sortByName();
}
connect(editor, SIGNAL(squadraChanged()), this, SLOT(creaClassifica()));
connect(editor, SIGNAL(dataChanged(bool)), this, SLOT(wasModified(bool)));
editor->setModal(true);
editor->show();
}
void MainWindow::about(){
QMessageBox::about(this, "HandBall Stats", tr("HandBallStats è un applicativo per la gestione "
"di partite di pallamano, che permette di mantenere traccia "
"delle statistiche individuali e di squadra per ogni partita "
"giocata.\n\n"
"Creato da Beatrice Guerra"));
}
void MainWindow::aboutQt(){
QMessageBox::aboutQt(this, "About Qt");
}
void MainWindow::showPartita(){
Squadra* home = newWizard->getHomeTeam();
Squadra* guest = newWizard->getGuestTeam();
Arbitro* a1 = 0; /*newWizard->getArbitro1();*/
Arbitro* a2 = 0; /*newWizard->getArbitro2();*/
Arbitro::Categoria cat= newWizard->getCategoria();
int numPlS1 = newWizard->getNumberOf(PartitaPage::home, PartitaPage::gioc);
int numAllS1 = newWizard->getNumberOf(PartitaPage::home, PartitaPage::all);
int numPlS2 = newWizard->getNumberOf(PartitaPage::guest, PartitaPage::gioc);
int numAllS2 = newWizard->getNumberOf(PartitaPage::guest, PartitaPage::all);
tabs = new Tabs(home, guest, numPlS1, numAllS1, numPlS2, numAllS2, a1, a2, cat, this);
classificaAct->setEnabled(false);
exportAct->setEnabled(true);
resetPartitaAct->setEnabled(true);
closePartitaAct->setEnabled(true);
closeTabs->setEnabled(false);
connect(exportAct, SIGNAL(triggered()), tabs, SLOT(exportPng()));
connect(resetPartitaAct, SIGNAL(triggered()), tabs, SLOT(reset()));
connect(tabs, SIGNAL(resetted(bool)), this, SLOT(enable_closeTabs(bool)));
setCentralWidget(tabs);
}
void MainWindow::creaClassifica(){
exportAct->setEnabled(false);
resetPartitaAct->setEnabled(false);
closePartitaAct->setEnabled(false);
classificaWidget = new QWidget(this);
QFont font;
font.setBold(true);
font.setPointSize(16);
QLabel* header[8];
header[0] = new QLabel("N°", classificaWidget);
header[1] = new QLabel(tr("Nome"), classificaWidget);
header[2] = new QLabel(tr("Punti"), classificaWidget);
header[3] = new QLabel(tr("V"), classificaWidget); //Vittorie
header[4] = new QLabel(tr("P"), classificaWidget); //Pareggi
header[5] = new QLabel(tr("S"), classificaWidget); //Sconfitte
header[6] = new QLabel(tr("Pen"), classificaWidget); //Penalità
header[7] = new QLabel(tr("DR"), classificaWidget); //Differenza Reti
squadre->sort();
QLabel* teamsLabel[squadre->size()][8];
for(unsigned int i=0; i<squadre->size(); ++i){
teamsLabel[i][0] = new QLabel(QString::number(i+1), classificaWidget);
teamsLabel[i][1] = new QLabel(squadre->at(i)->getNome(), classificaWidget);
teamsLabel[i][2] = new QLabel(QString::number(squadre->at(i)->getPunti()), classificaWidget);
teamsLabel[i][3] = new QLabel(QString::number(squadre->at(i)->getVittorie()), classificaWidget);
teamsLabel[i][4] = new QLabel(QString::number(squadre->at(i)->getPareggi()), classificaWidget);
teamsLabel[i][5] = new QLabel(QString::number(squadre->at(i)->getSconfitte()), classificaWidget);
teamsLabel[i][6] = new QLabel(QString::number(squadre->at(i)->getPenalita()), classificaWidget);
teamsLabel[i][7] = new QLabel(QString::number(squadre->at(i)->getDifferenzaReti()), classificaWidget);
}
QGridLayout* classifica = new QGridLayout;
for(int i=0; i<8; ++i){
header[i]->setFont(font);
header[i]->setAlignment(Qt::AlignHCenter);
classifica->addWidget(header[i], 1, i+1);
}
font.setBold(false);
for(unsigned int i=0; i<squadre->size(); ++i){
for(int j=0; j<8; ++j){
teamsLabel[i][j]->setFont(font);
teamsLabel[i][j]->setAlignment(Qt::AlignHCenter);
classifica->addWidget(teamsLabel[i][j], i+2, j+1);
}
}
QWidget* topFiller = new QWidget(classificaWidget);
topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QWidget* bottomFiller = new QWidget(classificaWidget);
bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QHBoxLayout* classificaLayout = new QHBoxLayout;
classificaLayout->addStretch(20);
classificaLayout->addLayout(classifica);
classificaLayout->addStretch(20);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(topFiller);
layout->addLayout(classificaLayout);
layout->addWidget(bottomFiller);
classifica->setAlignment(layout, Qt::AlignHCenter);
classificaWidget->setLayout(layout);
setCentralWidget(classificaWidget);
}
void MainWindow::terminaPartita(){
classificaAct->setEnabled(true);
resetPartitaAct->setEnabled(false);
closePartitaAct->setEnabled(false);
closeTabs->setEnabled(true);
tabs->termina();
modificato = true;
}
void MainWindow::chiudiTabs(){
createLogoWidget();
setCentralWidget(logoWidget);
exportAct->setEnabled(false);
resetPartitaAct->setEnabled(false);
closePartitaAct->setEnabled(false);
classificaAct->setEnabled(true);
}
void MainWindow::enable_closeTabs(bool e){
closeTabs->setEnabled(e);
}
void MainWindow::wasModified(bool mod){
modificato = mod;
}
void MainWindow::closeEvent(QCloseEvent *event){
if(maybeSave()){
save();
writePreference();
event->accept();
}
else{
event->ignore();
}
}