-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranking.cpp
189 lines (155 loc) · 5.14 KB
/
ranking.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
/*****************************************************************************
* 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 "ranking.hpp"
#include "lbcrexception.hpp"
Ranking::Field RankingSorter::sortingField = Ranking::Field::RTF_FIRST;
Qt::SortOrder RankingSorter::sortingOrder = Qt::AscendingOrder;
Ranking::Ranking(QString const &team)
{
if (team.length() != 1) {
throw(ChronoRaceException(tr("Illegal ranking type - expected 'I' or 'T' - found %1").arg(team)));
} else {
this->team = (team.compare("T", Qt::CaseInsensitive) == 0);
}
}
QDataStream &operator<<(QDataStream &out, Ranking const &ranking)
{
out << ranking.fullDescription
<< ranking.shortDescription
<< qint32(ranking.team)
<< ranking.categories;
return out;
}
QDataStream &operator>>(QDataStream &in, Ranking &ranking)
{
qint32 team32;
in >> ranking.fullDescription
>> ranking.shortDescription
>> team32
>> ranking.categories;
ranking.team = static_cast<bool>(team32);
return in;
}
QString const &Ranking::getFullDescription() const
{
return fullDescription;
}
void Ranking::setFullDescription(QString const &newFullDescription)
{
this->fullDescription = newFullDescription;
}
QString const &Ranking::getShortDescription() const
{
return shortDescription;
}
void Ranking::setShortDescription(QString const &newShortDescription)
{
this->shortDescription = newShortDescription;
}
bool Ranking::isTeam() const
{
return team;
}
void Ranking::setTeam(bool newTeam)
{
this->team = newTeam;
}
QStringList const &Ranking::getCategories() const
{
return categories;
}
void Ranking::setCategories(QStringList const &newCategories)
{
this->categories = newCategories;
}
void Ranking::parseCategories()
{
if (this->categories.count() == 1) {
QStringList list { this->categories[0].split('+') };
this->categories.clear();
this->categories.append(list);
}
}
bool Ranking::isValid() const
{
return (!fullDescription.isEmpty() && !shortDescription.isEmpty());
}
bool Ranking::includes(Category const *category) const
{
return ((category != Q_NULLPTR) && (this->categories.indexOf(category->getShortDescription()) >= 0));
}
bool Ranking::operator< (Ranking const &rhs) const
{
return (!this->isTeam() && rhs.isTeam());
}
bool Ranking::operator> (Ranking const &rhs) const
{
return (this->isTeam() && !rhs.isTeam());
}
bool Ranking::operator<=(Ranking const &rhs) const
{
return !(*this > rhs);
}
bool Ranking::operator>=(Ranking const &rhs) const
{
return !(*this < rhs);
}
bool RankingSorter::operator() (Ranking const &lhs, Ranking const &rhs) const
{
switch(sortingField) {
case Ranking::Field::RTF_FULL_DESCR:
return (sortingOrder == Qt::DescendingOrder) ? (lhs.getFullDescription() > rhs.getFullDescription()) : (lhs.getFullDescription() < rhs.getFullDescription());
case Ranking::Field::RTF_SHORT_DESCR:
return (sortingOrder == Qt::DescendingOrder) ? (lhs.getShortDescription() > rhs.getShortDescription()) : (lhs.getShortDescription() < rhs.getShortDescription());
case Ranking::Field::RTF_TEAM:
[[fallthrough]];
case Ranking::Field::RTF_CATEGORIES:
[[fallthrough]];
default:
return (sortingOrder == Qt::DescendingOrder) ? (lhs > rhs) : (lhs < rhs);
}
return false;
}
Qt::SortOrder RankingSorter::getSortingOrder()
{
return sortingOrder;
}
void RankingSorter::setSortingOrder(Qt::SortOrder const &value)
{
sortingOrder = value;
}
Ranking::Field RankingSorter::getSortingField()
{
return sortingField;
}
void RankingSorter::setSortingField(Ranking::Field const &value)
{
sortingField = value;
}
Ranking::Field &operator++(Ranking::Field &field)
{
field = static_cast<Ranking::Field>(static_cast<int>(field) + 1);
//NOSONAR if (field == Ranking::RTF_COUNT)
//NOSONAR field = Ranking::RTF_FIRST;
return field;
}
Ranking::Field operator++(Ranking::Field &field, int)
{
Ranking::Field tmp = field;
++field;
return tmp;
}