-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.cpp
313 lines (268 loc) · 8.45 KB
/
timing.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
/*****************************************************************************
* 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 "timing.hpp"
#include "lbchronorace.hpp"
#include "lbcrexception.hpp"
#include "crhelper.hpp"
Timing::Field TimingSorter::sortingField = Timing::Field::TMF_TIME;
Qt::SortOrder TimingSorter::sortingOrder = Qt::AscendingOrder;
QDataStream &operator<<(QDataStream &out, Timing const &timing)
{
out << quint32(timing.bib)
<< quint32(timing.leg)
<< quint32(timing.seconds)
<< qint32(timing.status);
return out;
}
QDataStream &operator>>(QDataStream &in, Timing &timing)
{
quint32 bib32;
quint32 leg32;
quint32 seconds32;
qint32 status32;
in >> bib32
>> leg32
>> seconds32
>> status32;
timing.bib = bib32;
timing.leg = leg32;
timing.seconds = seconds32;
switch (status32) {
case static_cast<qint32>(Timing::Status::DNS):
timing.status = Timing::Status::DNS;
break;
case static_cast<qint32>(Timing::Status::DNF):
timing.status = Timing::Status::DNF;
break;
case static_cast<qint32>(Timing::Status::DSQ):
timing.status = (LBChronoRace::binFormat < LBCHRONORACE_BIN_FMT_v4) ? Timing::Status::CLASSIFIED : Timing::Status::DSQ;
break;
case static_cast<qint32>(Timing::Status::CLASSIFIED):
timing.status = Timing::Status::CLASSIFIED;
break;
default:
/* Since tr() cannot be used here, let's use classified just
* to avoid loosing the recorded timing value (if any). */
timing.status = Timing::Status::CLASSIFIED;
break;
}
return in;
}
uint Timing::getBib() const
{
return bib;
}
void Timing::setBib(uint newBib)
{
this->bib = newBib;
}
uint Timing::getLeg() const
{
return leg;
}
void Timing::setLeg(uint newLeg)
{
this->leg = newLeg;
}
bool Timing::isDnf() const
{
return (status == Status::DNF);
}
bool Timing::isDns() const
{
return (status == Status::DNS);
}
bool Timing::isDsq() const
{
return (status == Status::DSQ);
}
uint Timing::getSeconds() const
{
return seconds;
}
Timing::Status Timing::getStatus() const
{
return status;
}
void Timing::setStatus(Timing::Status newStatus)
{
this->status = newStatus;
}
void Timing::setStatus(QString const &newStatus)
{
this->status = CRHelper::toTimingStatus(newStatus);
if ((this->status == Status::DNF) || (this->status == Status::DNS))
this->seconds = 0u;
}
QString Timing::getTiming() const
{
return CRHelper::toTimeString(this->seconds, Status::CLASSIFIED); //
}
void Timing::setTiming(QString const &timing)
{
uint val;
uint secs = 0u;
bool converted;
QString sep { timing.contains(".") ? "." : ":" };
for (auto const &token : timing.split(sep, Qt::KeepEmptyParts, Qt::CaseInsensitive)) {
converted = true;
val = token.toUInt(&converted);
if (!converted) {
throw(ChronoRaceException(tr("Illegal timing value '%1' for bib '%2'").arg(token).arg(this->bib)));
} else {
secs = (secs * 60) + val;
}
}
if ((this->seconds = secs)) {
this->status = Status::CLASSIFIED;
}
}
void Timing::setTiming(char const *timing)
{
setTiming(QString(timing));
}
bool Timing::isValid() const
{
return ((bib != 0u) && ((status == Status::DNS) || (status == Status::DNF) || (status == Status::DSQ) || ((status == Status::CLASSIFIED) && (seconds != 0u))));
}
bool Timing::operator< (Timing const &rhs) const
{
bool lessThen = false;
//NOSONAR switch (this->status) {
//NOSONAR case Status::CLASSIFIED:
//NOSONAR lessThen = ((rhs.status != Status::CLASSIFIED) || (this->seconds < rhs.seconds));
//NOSONAR break;
//NOSONAR case Status::DSQ:
//NOSONAR lessThen = ((rhs.status == Status::DNF) || (rhs.status == Status::DNS));
//NOSONAR break;
//NOSONAR case Status::DNF:
//NOSONAR lessThen = (rhs.status == Status::DNS);
//NOSONAR break;
//NOSONAR case Status::DNS:
//NOSONAR // nothing to assign
//NOSONAR break;
//NOSONAR default:
//NOSONAR Q_UNREACHABLE();
//NOSONAR break;
//NOSONAR }
switch (this->status) {
case Status::CLASSIFIED:
[[fallthrough]];
case Status::DSQ:
lessThen = (rhs.status == Status::DNS) || (rhs.status == Status::DNF) || (this->seconds < rhs.seconds);
break;
case Status::DNF:
lessThen = (rhs.status == Status::DNS);
break;
case Status::DNS:
// nothing to assign
break;
default:
Q_UNREACHABLE();
break;
}
return lessThen;
}
bool Timing::operator> (Timing const &rhs) const
{
bool greaterThen = false;
//NOSONAR switch (this->status) {
//NOSONAR case Status::CLASSIFIED:
//NOSONAR greaterThen = ((rhs.status == Status::CLASSIFIED) && (this->seconds > rhs.seconds));
//NOSONAR break;
//NOSONAR case Status::DSQ:
//NOSONAR greaterThen = (rhs.status == Status::CLASSIFIED);
//NOSONAR break;
//NOSONAR case Status::DNF:
//NOSONAR greaterThen = ((rhs.status == Status::CLASSIFIED) || (rhs.status == Status::DSQ));
//NOSONAR break;
//NOSONAR case Status::DNS:
//NOSONAR greaterThen = (rhs.status != Status::DNS);
//NOSONAR break;
//NOSONAR default:
//NOSONAR Q_UNREACHABLE();
//NOSONAR break;
//NOSONAR }
switch (this->status) {
case Status::CLASSIFIED:
[[fallthrough]];
case Status::DSQ:
greaterThen = ((rhs.status == Status::CLASSIFIED) || (rhs.status == Status::DSQ)) && (this->seconds > rhs.seconds);
break;
case Status::DNF:
greaterThen = ((rhs.status == Status::CLASSIFIED) || (rhs.status == Status::DSQ));
break;
case Status::DNS:
greaterThen = (rhs.status != Status::DNS);
break;
default:
Q_UNREACHABLE();
break;
}
return greaterThen;
}
bool Timing::operator<=(Timing const &rhs) const
{
return !(*this > rhs);
}
bool Timing::operator>=(Timing const &rhs) const
{
return !(*this < rhs);
}
bool TimingSorter::operator() (Timing const &lhs, Timing const &rhs) const
{
switch(sortingField) {
case Timing::Field::TMF_BIB:
return (sortingOrder == Qt::DescendingOrder) ? (lhs.getBib() > rhs.getBib()) : (lhs.getBib() < rhs.getBib());
case Timing::Field::TMF_LEG:
return (sortingOrder == Qt::DescendingOrder) ? (lhs.getLeg() > rhs.getLeg()) : (lhs.getLeg() < rhs.getLeg());
case Timing::Field::TMF_TIME:
[[fallthrough]];
default:
return (sortingOrder == Qt::DescendingOrder) ? (lhs > rhs) : (lhs < rhs);
}
return false;
}
Qt::SortOrder TimingSorter::getSortingOrder()
{
return sortingOrder;
}
void TimingSorter::setSortingOrder(Qt::SortOrder const &value)
{
sortingOrder = value;
}
Timing::Field TimingSorter::getSortingField()
{
return sortingField;
}
void TimingSorter::setSortingField(Timing::Field const &value)
{
sortingField = value;
}
Timing::Field &operator++(Timing::Field &field)
{
field = static_cast<Timing::Field>(static_cast<int>(field) + 1);
//NOSONAR if (field == Timing::TMF_COUNT)
//NOSONAR field = Timing::TMF_FIRST;
return field;
}
Timing::Field operator++(Timing::Field &field, int)
{
Timing::Field tmp = field;
++field;
return tmp;
}