-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lbchronorace.cpp
766 lines (658 loc) · 33.5 KB
/
lbchronorace.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*****************************************************************************
* Copyright (C) 2021 by Lorenzo Buzzi ([email protected]) *
* *
* 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 <https://www.gnu.org/licenses/>. *
*****************************************************************************/
#include <QString>
#include <QStandardPaths>
#include <QFile>
#include <QFileInfo>
#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QScreen>
//NOSONAR #include <QDebug>
#include "lbchronorace.hpp"
#include "lbcrexception.hpp"
#include "rankingswizard.hpp"
#include "crhelper.hpp"
// static members initialization
QDir LBChronoRace::lastSelectedPath(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
uint LBChronoRace::binFormat = LBCHRONORACE_BIN_FMT;
QRegularExpression LBChronoRace::csvFilter("[,;]");
LBChronoRace::LBChronoRace(QWidget *parent, QGuiApplication const *app) :
QMainWindow(parent),
raceInfo(parent),
startListTable(parent),
teamsTable(parent),
rankingsTable(parent),
categoriesTable(parent),
timingsTable(parent),
sexDelegate(&startListTable),
clubDelegate(&startListTable),
rankingTypeDelegate(&rankingsTable),
rankingCatsDelegate(&rankingsTable),
categoryTypeDelegate(&categoriesTable),
timingStatusDelegate(&timingsTable)
{
startListFileName = lastSelectedPath.filePath(LBCHRONORACE_STARTLIST_DEFAULT);
timingsFileName = lastSelectedPath.filePath(LBCHRONORACE_TIMINGS_DEFAULT);
rankingsFileName = lastSelectedPath.filePath(LBCHRONORACE_RANKINGS_DEFAULT);
categoriesFileName = lastSelectedPath.filePath(LBCHRONORACE_CATEGORIES_DEFAULT);
teamsFileName = lastSelectedPath.filePath(LBCHRONORACE_TEAMLIST_DEFAULT);
ui->setupUi(this);
startListTable.setWindowTitle(tr("Start List"));
startListTable.setModel(CRLoader::getStartListModel());
QObject::connect(&startListTable, &ChronoRaceTable::modelImported, this, &LBChronoRace::importStartList);
QObject::connect(&startListTable, &ChronoRaceTable::modelExported, this, &LBChronoRace::exportStartList);
QObject::connect(&startListTable, &ChronoRaceTable::saveRaceData, this, &LBChronoRace::saveRace);
QObject::connect(&startListTable, &ChronoRaceTable::newRowCount, this, &LBChronoRace::setCounterCompetitors);
teamsTable.disableButtons();
teamsTable.setWindowTitle(tr("Clubs List"));
teamsTable.setModel(CRLoader::getTeamsListModel());
QObject::connect(&teamsTable, &ChronoRaceTable::modelExported, this, &LBChronoRace::exportTeamList);
QObject::connect(&teamsTable, &ChronoRaceTable::saveRaceData, this, &LBChronoRace::saveRace);
QObject::connect(&teamsTable, &ChronoRaceTable::newRowCount, this, &LBChronoRace::setCounterTeams);
auto const *startListModel = dynamic_cast<StartListModel const *>(CRLoader::getStartListModel());
auto const *teamsListModel = dynamic_cast<TeamsListModel const *>(CRLoader::getTeamsListModel());
QObject::connect(startListModel, &StartListModel::newClub, teamsListModel, &TeamsListModel::addTeam);
QObject::connect(startListModel, &StartListModel::error, this, &LBChronoRace::appendErrorMessage);
auto *rankingsModel = dynamic_cast<RankingsModel *>(CRLoader::getRankingsModel());
rankingsTable.setWindowTitle(tr("Rankings"));
rankingsTable.setModel(rankingsModel);
QObject::connect(&rankingsTable, &ChronoRaceTable::modelImported, this, &LBChronoRace::importRankingsList);
QObject::connect(&rankingsTable, &ChronoRaceTable::modelExported, this, &LBChronoRace::exportRankingsList);
QObject::connect(&rankingsTable, &ChronoRaceTable::saveRaceData, this, &LBChronoRace::saveRace);
QObject::connect(&rankingsTable, &ChronoRaceTable::newRowCount, this, &LBChronoRace::setCounterRankings);
QObject::connect(rankingsModel, &RankingsModel::error, this, &LBChronoRace::appendErrorMessage);
auto *categoriesModel = dynamic_cast<CategoriesModel *>(CRLoader::getCategoriesModel());
categoriesTable.setWindowTitle(tr("Categories"));
categoriesTable.setModel(categoriesModel);
QObject::connect(&categoriesTable, &ChronoRaceTable::modelImported, this, &LBChronoRace::importCategoriesList);
QObject::connect(&categoriesTable, &ChronoRaceTable::modelExported, this, &LBChronoRace::exportCategoriesList);
QObject::connect(&categoriesTable, &ChronoRaceTable::saveRaceData, this, &LBChronoRace::saveRace);
QObject::connect(&categoriesTable, &ChronoRaceTable::newRowCount, this, &LBChronoRace::setCounterCategories);
QObject::connect(categoriesModel, &CategoriesModel::error, this, &LBChronoRace::appendErrorMessage);
rankingCatsDelegate.setCategories(categoriesModel);
auto *timingsModel = dynamic_cast<TimingsModel *>(CRLoader::getTimingsModel());
timingsTable.setWindowTitle(tr("Timings List"));
timingsTable.setModel(timingsModel);
QObject::connect(&timingsTable, &ChronoRaceTable::modelImported, this, &LBChronoRace::importTimingsList);
QObject::connect(&timingsTable, &ChronoRaceTable::modelExported, this, &LBChronoRace::exportTimingsList);
QObject::connect(&timingsTable, &ChronoRaceTable::saveRaceData, this, &LBChronoRace::saveRace);
QObject::connect(&timingsTable, &ChronoRaceTable::newRowCount, this, &LBChronoRace::setCounterTimings);
QObject::connect(timingsModel, &TimingsModel::error, this, &LBChronoRace::appendErrorMessage);
QObject::connect(&timings, &ChronoRaceTimings::newTimingsCount, this, &LBChronoRace::setCounterTimings);
QObject::connect(&timings, &ChronoRaceTimings::error, this, &LBChronoRace::appendErrorMessage);
// react to screen change and resize
QObject::connect(app, &QGuiApplication::primaryScreenChanged, this, &LBChronoRace::resizeDialogs);
resizeDialogs(QGuiApplication::primaryScreen());
//NOSONAR ui->makeRankingsPDF->setEnabled(false);
QObject::connect(ui->loadRace, &QPushButton::clicked, this, &LBChronoRace::loadRace);
QObject::connect(ui->saveRace, &QPushButton::clicked, this, &LBChronoRace::saveRace);
QObject::connect(ui->editRace, &QPushButton::clicked, &raceInfo, &ChronoRaceData::show);
QObject::connect(ui->editStartList, &QPushButton::clicked, &startListTable, &ChronoRaceTable::show);
QObject::connect(ui->editClubsList, &QPushButton::clicked, &teamsTable, &ChronoRaceTable::show);
QObject::connect(ui->editRankings, &QPushButton::clicked, &rankingsTable, &ChronoRaceTable::show);
QObject::connect(ui->editCategories, &QPushButton::clicked, &categoriesTable, &ChronoRaceTable::show);
QObject::connect(ui->editTimings, &QPushButton::clicked, &timingsTable, &ChronoRaceTable::show);
QObject::connect(ui->importTimings, &QPushButton::clicked, &timingsTable, &ChronoRaceTable::modelImport);
QObject::connect(ui->exportTimings, &QPushButton::clicked, &timingsTable, &ChronoRaceTable::modelExport);
QObject::connect(ui->makeStartList, &QPushButton::clicked, this, &LBChronoRace::makeStartList);
QObject::connect(ui->collectTimings, &QPushButton::clicked, &timings, &ChronoRaceTimings::show);
QObject::connect(ui->makeRankings, &QPushButton::clicked, this, &LBChronoRace::makeRankings);
QObject::connect(ui->actionLoadRace, &QAction::triggered, this, &LBChronoRace::loadRace);
QObject::connect(ui->actionSaveRace, &QAction::triggered, this, &LBChronoRace::saveRace);
QObject::connect(ui->actionSaveRaceAs, &QAction::triggered, this, &LBChronoRace::saveRaceAs);
QObject::connect(ui->actionQuit, &QAction::triggered, this, &QApplication::quit);
QObject::connect(ui->actionEditRace, &QAction::triggered, &raceInfo, &ChronoRaceData::show);
QObject::connect(ui->actionEditStartList, &QAction::triggered, &startListTable, &ChronoRaceTable::show);
QObject::connect(ui->actionEditTeams, &QAction::triggered, &teamsTable, &ChronoRaceTable::show);
QObject::connect(ui->actionEditRankings, &QAction::triggered, &rankingsTable, &ChronoRaceTable::show);
QObject::connect(ui->actionEditCategories, &QAction::triggered, &categoriesTable, &ChronoRaceTable::show);
QObject::connect(ui->actionEditTimings, &QAction::triggered, &timingsTable, &ChronoRaceTable::show);
QObject::connect(ui->actionImportTimings, &QAction::triggered, &timingsTable, &ChronoRaceTable::modelImport);
QObject::connect(ui->actionExportTimings, &QAction::triggered, &timingsTable, &ChronoRaceTable::modelExport);
QObject::connect(ui->actionSetEncoding, &QAction::triggered, this, &LBChronoRace::setEncoding);
QObject::connect(ui->actionMakeStartList, &QAction::triggered, this, &LBChronoRace::makeStartList);
QObject::connect(ui->actionCollectTimings, &QAction::triggered, &timings, &ChronoRaceTimings::show);
QObject::connect(ui->actionMakeRankings, &QAction::triggered, this, &LBChronoRace::makeRankings);
QObject::connect(ui->actionAbout, &QAction::triggered, this, &LBChronoRace::actionAbout);
QObject::connect(ui->actionAboutQt, &QAction::triggered, this, &LBChronoRace::actionAboutQt);
// tie the views with the related delegate instances
startListTable.setItemDelegateForColumn(static_cast<int>(Competitor::Field::CMF_SEX), &sexDelegate);
startListTable.setItemDelegateForColumn(static_cast<int>(Competitor::Field::CMF_CLUB), &clubDelegate);
rankingsTable.setItemDelegateForColumn(static_cast<int>(Ranking::Field::RTF_TEAM), &rankingTypeDelegate);
rankingsTable.setItemDelegateForColumn(static_cast<int>(Ranking::Field::RTF_CATEGORIES), &rankingCatsDelegate);
categoriesTable.setItemDelegateForColumn(static_cast<int>(Category::Field::CTF_TYPE), &categoryTypeDelegate);
timingsTable.setItemDelegateForColumn(static_cast<int>(Timing::Field::TMF_STATUS), &timingStatusDelegate);
}
void LBChronoRace::setCounterTeams(int count) const
{
ui->counterTeams->display(count);
}
void LBChronoRace::setCounterCompetitors(int count) const
{
ui->counterCompetitors->display(count);
// also update the counter for the list of clubs
setCounterTeams(CRLoader::getTeamsListModel()->rowCount());
}
void LBChronoRace::setCounterRankings(int count) const
{
ui->counterRankings->display(count);
}
void LBChronoRace::setCounterCategories(int count) const
{
ui->counterCategories->display(count);
}
void LBChronoRace::setCounterTimings(int count) const
{
ui->counterTimings->display(count);
}
void LBChronoRace::appendInfoMessage(QString const &message) const
{
if (auto sep = message.indexOf(':'); sep < 0) {
ui->infoDisplay->appendHtml("<p><font color=\"blue\">" + message + "</font></p>");
} else {
ui->infoDisplay->appendHtml("<p><font color=\"blue\">" + message.first(sep) + "</font>" + message.sliced(sep) + "</p>");
}
}
void LBChronoRace::appendErrorMessage(QString const &message) const
{
if (auto sep = message.indexOf(':'); sep < 0) {
ui->errorDisplay->appendHtml("<p><font color=\"red\">" + message + "</font></p>");
} else if (message.at(sep + 1) == QChar(':')) {
ui->errorDisplay->appendHtml("<p><font color=\"orange\">" + message.first(sep) + "</font>" + message.sliced(sep + 1) + "</p>");
} else {
ui->errorDisplay->appendHtml("<p><font color=\"red\">" + message.first(sep) + "</font>" + message.sliced(sep) + "</p>");
}
}
void LBChronoRace::importStartList()
{
startListFileName = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(this, tr("Select Start List"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!startListFileName.isEmpty()) {
QPair<int, int> count(0, 0);
appendInfoMessage(tr("Start List File: %1").arg(startListFileName));
try {
count = CRLoader::importStartList(startListFileName);
appendInfoMessage(tr("Loaded: %n competitor(s)", "", count.first));
appendInfoMessage(tr("Loaded: %n team(s)", "", count.second));
lastSelectedPath = QFileInfo(startListFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
setCounterCompetitors(count.first);
setCounterTeams(count.second);
}
}
void LBChronoRace::importRankingsList()
{
rankingsFileName = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(this, tr("Select Rankings File"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!rankingsFileName.isEmpty()) {
int count = 0;
appendInfoMessage(tr("Rankings File: %1").arg(rankingsFileName));
try {
count = CRLoader::importModel(CRLoader::Model::RANKINGS, rankingsFileName);
appendInfoMessage(tr("Loaded: %n ranking(s)", "", count));
lastSelectedPath = QFileInfo(rankingsFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
setCounterRankings(count);
}
}
void LBChronoRace::importCategoriesList()
{
categoriesFileName = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(this, tr("Select Categories File"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!categoriesFileName.isEmpty()) {
int count = 0;
appendInfoMessage(tr("Categories File: %1").arg(categoriesFileName));
try {
count = CRLoader::importModel(CRLoader::Model::CATEGORIES, categoriesFileName);
appendInfoMessage(tr("Loaded: %n category(s)", "", count));
lastSelectedPath = QFileInfo(categoriesFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
setCounterCategories(count);
}
}
void LBChronoRace::importTimingsList()
{
timingsFileName = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(this, tr("Select Timings File"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!timingsFileName.isEmpty()) {
int count = 0;
appendInfoMessage(tr("Timings File: %1").arg(timingsFileName));
try {
count = CRLoader::importModel(CRLoader::Model::TIMINGS, timingsFileName);
appendInfoMessage(tr("Loaded: %n timing(s)", "", count));
lastSelectedPath = QFileInfo(timingsFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
setCounterTimings(count);
}
}
void LBChronoRace::exportStartList()
{
startListFileName = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(this, tr("Select Start List"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!startListFileName.isEmpty()) {
if (!startListFileName.endsWith(".csv", Qt::CaseInsensitive))
startListFileName.append(".csv");
try {
CRLoader::exportModel(CRLoader::Model::STARTLIST, startListFileName);
appendInfoMessage(tr("Start List File saved: %1").arg(startListFileName));
lastSelectedPath = QFileInfo(startListFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
}
}
void LBChronoRace::exportTeamList()
{
teamsFileName = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(this, tr("Select Clubs List"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!teamsFileName.isEmpty()) {
if (!teamsFileName.endsWith(".csv", Qt::CaseInsensitive))
teamsFileName.append(".csv");
try {
CRLoader::exportModel(CRLoader::Model::TEAMSLIST, teamsFileName);
appendInfoMessage(tr("Teams File saved: %1").arg(teamsFileName));
lastSelectedPath = QFileInfo(teamsFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
}
}
void LBChronoRace::exportRankingsList()
{
rankingsFileName = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(this, tr("Select Rankings File"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!rankingsFileName.isEmpty()) {
if (!rankingsFileName.endsWith(".csv", Qt::CaseInsensitive))
rankingsFileName.append(".csv");
try {
CRLoader::exportModel(CRLoader::Model::RANKINGS, rankingsFileName);
appendInfoMessage(tr("Rankings File saved: %1").arg(rankingsFileName));
lastSelectedPath = QFileInfo(rankingsFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
}
}
void LBChronoRace::exportCategoriesList()
{
categoriesFileName = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(this, tr("Select Categories File"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!categoriesFileName.isEmpty()) {
if (!categoriesFileName.endsWith(".csv", Qt::CaseInsensitive))
categoriesFileName.append(".csv");
try {
CRLoader::exportModel(CRLoader::Model::CATEGORIES, categoriesFileName);
appendInfoMessage(tr("Categories File saved: %1").arg(categoriesFileName));
lastSelectedPath = QFileInfo(categoriesFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
}
}
void LBChronoRace::exportTimingsList()
{
timingsFileName = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(this, tr("Select Timings File"),
lastSelectedPath.absolutePath(),
tr("CSV (*.csv)")));
if (!timingsFileName.isEmpty()) {
if (!timingsFileName.endsWith(".csv", Qt::CaseInsensitive))
timingsFileName.append(".csv");
try {
CRLoader::exportModel(CRLoader::Model::TIMINGS, timingsFileName);
appendInfoMessage(tr("Timings File saved: %1").arg(timingsFileName));
lastSelectedPath = QFileInfo(timingsFileName).absoluteDir();
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
}
}
void LBChronoRace::initialize() {
QStringList arguments = QCoreApplication::arguments();
auto argument = arguments.constBegin();
if (++argument != arguments.constEnd()) {
if (loadRaceFile(*argument))
raceDataFileName = *argument;
while (++argument != arguments.constEnd())
appendErrorMessage(tr("Warning: skipping file %1").arg(*argument));
}
}
void LBChronoRace::show()
{
ui->retranslateUi(this);
QWidget::show();
}
void LBChronoRace::resizeDialogs(QScreen const *screen)
{
QRect screenGeometry = screen->availableGeometry();
//NOSONAR this->setMaximumHeight(screenGeometry.height());
raceInfo.setMaximumHeight(screenGeometry.height());
// 15/16 is about 94% of the height; this is an ugly
// workaround since at present time on high resolution
// screen Qt::AA_EnableHighDpiScaling still does not
// scale nicely
startListTable.setMaximumHeight(screenGeometry.height() * 15 / 16);
teamsTable.setMaximumHeight(screenGeometry.height() * 15 / 16);
categoriesTable.setMaximumHeight(screenGeometry.height() * 15 / 16);
timingsTable.setMaximumHeight(screenGeometry.height() * 15 / 16);
}
void LBChronoRace::encodingSelector(int idx) const
{
switch (idx) {
case 1:
CRLoader::setEncoding(QStringConverter::Encoding::Utf8);
break;
case 0:
CRLoader::setEncoding(QStringConverter::Encoding::Latin1);
break;
default:
appendInfoMessage(tr("Unknown encoding %1; loaded default").arg(idx));
CRLoader::setEncoding(QStringConverter::Encoding::Latin1);
break;
}
appendInfoMessage(tr("Selected encoding: %1").arg(CRHelper::encodingToLabel(CRLoader::getEncoding())));
}
void LBChronoRace::formatSelector(int idx) const
{
switch (idx) {
case static_cast<int>(CRLoader::Format::PDF):
CRLoader::setFormat(CRLoader::Format::PDF);
break;
case static_cast<int>(CRLoader::Format::TEXT):
CRLoader::setFormat(CRLoader::Format::TEXT);
break;
case static_cast<int>(CRLoader::Format::CSV):
CRLoader::setFormat(CRLoader::Format::CSV);
break;
default:
CRLoader::setFormat(CRLoader::Format::PDF);
break;
}
appendInfoMessage(tr("Selected format: %1").arg(CRHelper::formatToLabel(CRLoader::getFormat())));
}
bool LBChronoRace::loadRaceFile(QString const &fileName)
{
bool retval = false;
if (!fileName.isEmpty()) {
QFile raceDataFile(fileName);
lastSelectedPath = QFileInfo(fileName).absoluteDir();
if (raceDataFile.open(QIODevice::ReadOnly)) {
quint32 binFmt;
QFileInfo raceDataFileInfo(fileName);
QDataStream in(&raceDataFile);
in.setVersion(QDataStream::Qt_5_15);
in >> binFmt;
switch (binFmt) {
case LBCHRONORACE_BIN_FMT_v1:
[[fallthrough]];
case LBCHRONORACE_BIN_FMT_v2:
[[fallthrough]];
case LBCHRONORACE_BIN_FMT_v3:
QMessageBox::warning(this, tr("Race Data File Format"), tr("This Race Data File was saved with a previous release of the application.\nThe definitions of Categories and Rankings must be reviewed and corrected."));
[[fallthrough]];
case LBCHRONORACE_BIN_FMT_v4:
QAbstractTableModel const *table;
qint16 encodingIdx;
qint16 formatIdx;
int tableCount;
binFormat = static_cast<int>(binFmt);
in >> encodingIdx;
encodingSelector(encodingIdx);
in >> formatIdx;
formatSelector(formatIdx);
raceInfo.setBinFormat(binFmt);
in >> raceInfo;
CRLoader::loadRaceData(in);
// Set useful information
lastSelectedPath = raceDataFileInfo.absoluteDir();
setWindowTitle(QString(tr(LBCHRONORACE_NAME) + " - " + raceDataFileInfo.fileName()));
table = CRLoader::getStartListModel();
tableCount = table->rowCount();
setCounterCompetitors(tableCount);
appendInfoMessage(tr("Loaded: %n competitor(s)", "", tableCount));
table = CRLoader::getTeamsListModel();
tableCount = table->rowCount();
setCounterTeams(tableCount);
appendInfoMessage(tr("Loaded: %n team(s)", "", tableCount));
table = CRLoader::getRankingsModel();
tableCount = table->rowCount();
setCounterRankings(tableCount);
appendInfoMessage(tr("Loaded: %n ranking(s)", "", tableCount));
table = CRLoader::getCategoriesModel();
tableCount = table->rowCount();
setCounterCategories(tableCount);
appendInfoMessage(tr("Loaded: %n category(s)", "", tableCount));
table = CRLoader::getTimingsModel();
tableCount = table->rowCount();
setCounterTimings(tableCount);
appendInfoMessage(tr("Loaded: %n timing(s)", "", tableCount));
appendInfoMessage(tr("Race loaded: %1").arg(fileName));
retval = true;
break;
default:
QMessageBox::information(this, tr("Race Data File Error"), tr("Data format %1 not supported.\nPlease update the application.").arg(binFmt));
break;
}
} else {
QMessageBox::information(this, tr("Unable to open file"), raceDataFile.errorString());
}
}
return retval;
}
void LBChronoRace::loadRace()
{
QString fileName = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(this, tr("Select Race Data File"),
lastSelectedPath.absolutePath(),
tr("ChronoRace Data (*.crd)")));
if (loadRaceFile(fileName))
raceDataFileName = fileName;
}
void LBChronoRace::saveRace()
{
if (raceDataFileName.isEmpty()) {
raceDataFileName = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(this, tr("Select Race Data File"),
lastSelectedPath.absolutePath(),
tr("ChronoRace Data (*.crd)")));
if (!raceDataFileName.isEmpty() && !raceDataFileName.endsWith(".crd", Qt::CaseInsensitive))
raceDataFileName.append(".crd");
}
if (!raceDataFileName.isEmpty()) {
QFile raceDataFile(raceDataFileName);
if (raceDataFile.open(QIODevice::WriteOnly)) {
QFileInfo raceDataFileInfo(raceDataFileName);
QDataStream out(&raceDataFile);
out.setVersion(QDataStream::Qt_5_15);
out << quint32(LBCHRONORACE_BIN_FMT);
switch (CRLoader::getEncoding()) {
case QStringConverter::Encoding::Utf8:
out << qint16(1);
break;
case QStringConverter::Encoding::Latin1:
out << qint16(0);
break;
default:
appendInfoMessage(tr("Unknown encoding %1; default saved").arg(static_cast<int>(CRLoader::getEncoding())));
out << qint16(0);
break;
}
out << qint16(CRLoader::getFormat())
<< raceInfo;
CRLoader::saveRaceData(out);
// Set useful information
lastSelectedPath = raceDataFileInfo.absoluteDir();
setWindowTitle(QString(tr(LBCHRONORACE_NAME) + " - " + raceDataFileInfo.fileName()));
appendInfoMessage(tr("Race saved: %1").arg(raceDataFileName));
} else {
QMessageBox::information(this, tr("Unable to open file"), raceDataFile.errorString());
raceDataFileName.clear();
}
}
}
void LBChronoRace::saveRaceAs()
{
QString oldRaceDataFileName(raceDataFileName);
raceDataFileName.clear();
saveRace();
if (raceDataFileName.isEmpty())
raceDataFileName = oldRaceDataFileName;
}
void LBChronoRace::setEncoding()
{
bool ok = false;
int current = 0;
QStringList items = {
CRHelper::encodingToLabel(QStringConverter::Encoding::Latin1),
CRHelper::encodingToLabel(QStringConverter::Encoding::Utf8)
};
switch (CRLoader::getEncoding()) {
case QStringConverter::Encoding::Latin1:
current = 0;
break;
case QStringConverter::Encoding::Utf8:
current = 1;
break;
default:
appendErrorMessage(tr("Unexpected encoding value (fall back to the default)"));
break;
}
QString item = QInputDialog::getItem(this,
tr("Settings"),
tr("CSV and Plain Text Encoding"),
items,
current,
false,
&ok);
if (ok) {
if (item == items[0])
CRLoader::setEncoding(QStringConverter::Encoding::Latin1);
else if (item == items[1])
CRLoader::setEncoding(QStringConverter::Encoding::Utf8);
else
appendErrorMessage(tr("Unexpected encoding value (encoding not changed)"));
}
}
void LBChronoRace::makeStartList()
{
ui->errorDisplay->clear();
try {
RankingsWizard wizard(&raceInfo, &lastSelectedPath, RankingsWizard::RankingsWizardTarget::StartList);
auto const &wizardInfoMessages = QObject::connect(&wizard, &RankingsWizard::info, this, &LBChronoRace::appendInfoMessage);
auto const &wizardErrorMessages = QObject::connect(&wizard, &RankingsWizard::error, this, &LBChronoRace::appendErrorMessage);
wizard.exec();
QObject::disconnect(wizardErrorMessages);
QObject::disconnect(wizardInfoMessages);
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
}
void LBChronoRace::makeRankings()
{
ui->errorDisplay->clear();
try {
RankingsWizard wizard(&raceInfo, &lastSelectedPath, RankingsWizard::RankingsWizardTarget::Rankings);
auto const &wizardInfoMessages = QObject::connect(&wizard, &RankingsWizard::info, this, &LBChronoRace::appendInfoMessage);
auto const &wizardErrorMessages = QObject::connect(&wizard, &RankingsWizard::error, this, &LBChronoRace::appendErrorMessage);
wizard.exec();
QObject::disconnect(wizardErrorMessages);
QObject::disconnect(wizardInfoMessages);
} catch (ChronoRaceException &e) {
appendErrorMessage(e.getMessage());
}
}
void LBChronoRace::actionAbout()
{
/* The purpose of the following object is to prevent the "About Qt" *
* translation from being removed from the .tr files when refreshed. */
QString const translatedTextAboutQtMessage = QMessageBox::tr(
"<p>Qt is a C++ toolkit for cross-platform application development.</p>"
"<p>Qt provides single-source portability across all major desktop operating systems. "
"It is also available for embedded Linux and other embedded and mobile operating systems.</p>"
"<p>Qt is available under multiple licensing options designed to accommodate the needs of our various users.</p>"
"<p>Qt licensed under our commercial license agreement is appropriate for development of "
"proprietary/commercial software where you do not want to share any source code with "
"third parties or otherwise cannot comply with the terms of GNU (L)GPL.</p>"
"<p>Qt licensed under GNU (L)GPL is appropriate for the development of Qt applications "
"provided you can comply with the terms and conditions of the respective licenses.</p>"
"<p>Please see <a href=\"http://%2/\">%2</a> for an overview of Qt licensing.</p>"
"<p>Copyright (C) %1 The Qt Company Ltd and other contributors.</p>"
"<p>Qt and the Qt logo are trademarks of The Qt Company Ltd.</p>"
"<p>Qt is The Qt Company Ltd product developed as an open source project. "
"See <a href=\"http://%3/\">%3</a> for more information.</p>"
);
std::ignore = translatedTextAboutQtMessage;
QString const translatedTextAboutCaption = QMessageBox::tr(
"<h3>About %1</h3>"
"<p>Software for producing the results of footraces.</p>"
).arg(QStringLiteral(LBCHRONORACE_NAME));
QString const translatedTextAboutText = QMessageBox::tr(
"<p>Copyright© 2021-2022</p>"
"<p>Version: %1 (source code on <a href=\"http://github.com/flinco/LBChronoRace\">GitHub</a>)</p>"
"<p>Author: Lorenzo Buzzi (<a href=\"mailto:[email protected]\">[email protected]</a>)</p>"
"<p>Site: <a href=\"http://www.buzzi.pro/\">http://www.buzzi.pro/</a></p>"
"<p>%2 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.</p>"
"<p>%2 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.</p>"
"<p>You should have received a copy of the GNU General Public License along with %2. "
"If not, see: <a href=\"https://www.gnu.org/licenses/\">https://www.gnu.org/licenses/</a>.</p>"
"<p><table><tbody><tr>"
"<td>If you found this application useful<br>and want to support its development,<br>you can make a donation:</td>"
"<td><a href=\"https://www.paypal.com/donate/?hosted_button_id=8NZWAMWPKCA7C\"><img src=\":/images/PayPal_Donate_en.gif\" /></a></td>"
"</tr></tbody></table></p>"
).arg(QStringLiteral(LBCHRONORACE_VERSION), QStringLiteral(LBCHRONORACE_NAME));
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("About %1").arg(QStringLiteral(LBCHRONORACE_NAME)));
msgBox.setText(translatedTextAboutCaption);
msgBox.setInformativeText(translatedTextAboutText);
if (QPixmap pm(QStringLiteral(":/icons/LBChronoRace.png")); !pm.isNull()) {
msgBox.setIconPixmap(pm);
}
msgBox.exec();
}
void LBChronoRace::actionAboutQt()
{
QMessageBox::aboutQt(this, tr("About Qt"));
}