swift
Loading...
Searching...
No Matches
aircraftcategorylist.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QJsonValue>
7#include <QMap>
8#include <Qt>
9
10#include "misc/setbuilder.h"
11
12SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::aviation, CAircraftCategory, CAircraftCategoryList)
13
14namespace swift::misc::aviation
15{
19
21 {
22 return this->findBy([&](const CAircraftCategory &category) { return category.matchesName(name, cs); });
23 }
24
26
28 {
29 this->sort([](const CAircraftCategory &a, const CAircraftCategory &b) {
30 const int c = a.compareByLevel(b);
31 return c < 0;
32 });
33 }
34
36 {
38 for (const CAircraftCategory &category : *this)
39 {
40 levels.insert(category.getLevelString());
41 }
42 return levels;
43 }
44
46 {
47 const QSet<QString> levels = this->getLevelStrings();
48 if (levels.isEmpty())
49 {
50 return {};
51 }
52 return levels.values().join(separator);
53 }
54
56 {
57 CSetBuilder<int> levels;
58 for (const CAircraftCategory &category : *this)
59 {
60 levels.insert(category.getFirstLevel());
61 }
62 QList<int> ll = levels;
63 ll.removeOne(0);
64 return ll;
65 }
66
68 {
69 CSetBuilder<int> levels;
70 for (const CAircraftCategory &category : *this)
71 {
72 levels.insert(category.getSecondLevel());
73 }
74 QList<int> ll = levels;
75 ll.removeOne(0);
76 return ll;
77 }
78
80 {
81 if (categories.isEmpty())
82 {
83 return {};
84 }
85 QMap<int, CAircraftCategory> highestLevels;
86 for (const CAircraftCategory &category : *this)
87 {
88 const int fl = category.getFirstLevel();
89 if (highestLevels.contains(fl))
90 {
91 if (highestLevels[fl].isHigherLevel(category))
92 {
93 highestLevels[fl] = category;
94 }
95 }
96 else
97 {
98 highestLevels[fl] = category;
99 }
100 }
101
102 CAircraftCategoryList topLevels;
103 for (const CAircraftCategory &category : highestLevels.values())
104 {
105 topLevels.push_back(category);
106 }
107 return topLevels;
108 }
109
111 {
112 CAircraftCategoryList categories;
113 for (const CAircraftCategory &category : *this)
114 {
115 if (category.getFirstLevel() == level)
116 {
117 categories.push_back(category);
118 }
119 }
120 return categories;
121 }
122
124 {
125 CAircraftCategoryList categories;
126 if (level.isEmpty())
127 {
128 return categories;
129 }
130 const int ls = level.size();
131 for (const CAircraftCategory &category : *this)
132 {
133 if (noRootNode && ls == category.getDepth())
134 {
135 continue;
136 } // ignore root nodes
137 if (category.matchesLevel(level))
138 {
139 categories.push_back(category);
140 }
141 }
142 return categories;
143 }
144
146 {
147 if (level.size() != 3)
148 {
149 return {};
150 }
151 for (const CAircraftCategory &category : *this)
152 {
153 if (category.isLevel(level[0], level[1], level[2]))
154 {
155 return category;
156 }
157 }
158 return {};
159 }
160
165
167 {
168 QList<int> levels = category.getLevel();
169 CAircraftCategoryList categories;
170 if (levels.size() < 2)
171 {
172 categories = this->findFirstLevels();
173 }
174 else
175 {
176 levels.removeLast();
177 categories = this->findByLevel(levels, true);
178 }
179 categories.remove(category);
180 return categories;
181 }
182
184 {
185 if (category.isNull() || this->isEmpty())
186 {
187 return {};
188 }
189 if (category.isFirstLevel())
190 {
191 return {};
192 }
193 const bool isL2 = category.getDepth() == 2;
194 const QList<int> loopLevels = isL2 ? this->getFirstLevels() : this->getSecondLevels();
195 if (loopLevels.isEmpty())
196 {
197 return {};
198 }
199
200 QList<int> level = category.getLevel();
202 for (int l = loopLevels.front(); loopLevels.back() >= l; ++l)
203 {
204 level[isL2 ? 0 : 1] = l;
205 if (category.isLevel(level))
206 {
207 continue;
208 } // ignore category itself
209 const CAircraftCategory cat = this->findByFullLevel(level);
210 if (!cat.isNull())
211 {
212 result.push_back(cat);
213 }
214 }
215 return result;
216 }
217
219 {
220 if (level.isEmpty())
221 {
222 return 0;
223 }
224 const int c = this->size();
225 const CAircraftCategoryList removed = this->removedLevel(level);
226 const int delta = c - removed.size();
227 if (delta > 0)
228 {
229 *this = removed;
230 }
231 return delta;
232 }
233
235 {
236 if (level.isEmpty())
237 {
238 return *this;
239 } // nothing removed
240 CAircraftCategoryList removed;
241 for (const CAircraftCategory &category : *this)
242 {
243 if (category.matchesLevel(level))
244 {
245 continue;
246 }
247 removed.push_back(category);
248 }
249 return removed;
250 }
251
253 {
254 CAircraftCategoryList categories;
255 for (const QJsonValue &value : array)
256 {
257 const CAircraftCategory category(CAircraftCategory::fromDatabaseJson(value.toObject()));
258 categories.push_back(category);
259 }
260 return categories;
261 }
262
263} // namespace swift::misc::aviation
void sortBy(K1 key1, Keys... keys)
Definition sequence.h:603
CSequence findBy(Predicate p) const
Definition sequence.h:407
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
int remove(const T &object)
Remove all elements equal to the given object, if it is contained.
Definition sequence.h:447
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Build a QSet more efficiently when calling insert() in a for loop.
Definition setbuilder.h:25
void insert(const T &value)
Add an element to the set. Runs in amortized constant time.
Definition setbuilder.h:29
Value object for aircraft categories.
const QString & getPath() const
Path.
int compareByLevel(const CAircraftCategory &other) const
Level compare.
bool isLevel(int l1, int l2, int l3) const
Is that given level?
QString getLevelString() const
Level string.
bool matchesName(const QString &name, Qt::CaseSensitivity cs) const
Matching name?
QList< int > getLevel() const
Levels depending on depth, 3.2 -> 3,2 / 1.0 -> 1 / 4.3.1 -> 4,3,1.
static CAircraftCategory fromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
From our database JSON format.
bool matchesLevel(int l1, int l2=0, int l3=0) const
Is matching the level, 0 ignored.
CAircraftCategoryList findByLevel(const QList< int > &level, bool noRootNode=false) const
Find by levels.
QList< int > getFirstLevels() const
All levels sorted.
CAircraftCategoryList()=default
Default constructor.
CSequence()=default
Default constructor.
QString getLevelsString(const QString &separator=", ") const
Get all level strings.
int removeIfLevel(const QList< int > &level)
Remove by level.
CAircraftCategoryList findInParallelBranch(const CAircraftCategory &category) const
Find siblings.
CAircraftCategoryList findByFirstLevel(int level) const
Find by first level.
CAircraftCategoryList findFirstLevels() const
Find first levels.
static CAircraftCategoryList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
CAircraftCategoryList findByName(const QString &name, Qt::CaseSensitivity cs=Qt::CaseInsensitive) const
Find by name.
CAircraftCategory findByFullLevel(const QList< int > &level) const
Find by exact levels.
QList< int > getSecondLevels() const
All levels sorted.
QSet< QString > getLevelStrings() const
Get all level strings.
CAircraftCategoryList removedLevel(const QList< int > &level) const
With removed categories.
CAircraftCategoryList findHighestLevels(const CAircraftCategoryList &categories)
Find highest (top) level of categories.
CAircraftCategoryList findSiblings(const CAircraftCategory &category) const
Find siblings.
QList< T >::reference back()
QList< T >::reference front()
bool isEmpty() const const
void removeLast()
bool removeOne(const AT &t)
qsizetype size() const const
bool contains(const Key &key) const const
QList< T > values() const const
bool isEmpty() const const
QList< T > values() const const
CaseSensitivity
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition sequence.h:63