swift
Loading...
Searching...
No Matches
aircraftcategory.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 <QRegularExpression>
7#include <QStringBuilder>
8#include <Qt>
9#include <QtGlobal>
10
13#include "misc/logcategories.h"
15#include "misc/statusmessage.h"
16#include "misc/stringutils.h"
17
18using namespace swift::misc;
19using namespace swift::misc::db;
20
21SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, CAircraftCategory)
22
23namespace swift::misc::aviation
24{
25 CAircraftCategory::CAircraftCategory(const QString &name, const QString &description, const QString &path,
26 bool assignable)
27 : m_name(name), m_description(description), m_path(path), m_assignable(assignable)
28 {}
29
31 {
32 return (this->isLoadedFromDb()) ? this->getName() % u' ' % this->getDbKeyAsStringInParentheses() :
33 this->getName();
34 }
35
37 {
38 Q_UNUSED(i18n);
39 return QStringLiteral("%1 %2").arg(this->getNameDbKey(), this->getDescription());
40 }
41
43
44 bool CAircraftCategory::isNull() const { return m_name.isEmpty() && m_description.isEmpty(); }
45
47 {
48 static const CAircraftCategory null;
49 return null;
50 }
51
52 bool CAircraftCategory::hasName() const { return !m_name.isEmpty(); }
53
55 {
56 return stringCompare(name, this->getName(), cs);
57 }
58
59 void CAircraftCategory::setLevel(int l1, int l2, int l3)
60 {
61 m_l1 = l1;
62 m_l2 = l2;
63 m_l3 = l3;
64 }
65
66 bool CAircraftCategory::isLevel(int l1, int l2, int l3) const { return l1 == m_l1 && l2 == m_l2 && l3 == m_l3; }
67
68 bool CAircraftCategory::isLevel(const QList<int> &level) const
69 {
70 if (level.size() != 3)
71 {
72 return false;
73 }
74 return m_l1 == level[0] && m_l2 == level[1] && m_l3 == level[2];
75 }
76
78 {
79 if (category.isNull())
80 {
81 return false;
82 }
83 return category.m_l1 == m_l1 && category.m_l2 == m_l2 && category.m_l3 == m_l3;
84 }
85
87 {
88 QList<int> l;
89 if (m_l1 > 0) l << m_l1;
90 if (m_l2 > 0) l << m_l2;
91 if (m_l3 > 0) l << m_l3;
92 return l;
93 }
94
95 bool CAircraftCategory::isFirstLevel() const { return (m_l3 == 0 && m_l2 == 0 && m_l1 > 0); }
96
98 {
99 if (this->isFirstLevel())
100 {
101 return 1;
102 }
103 if (m_l3 == 0 && m_l2 > 0 && m_l1 > 0)
104 {
105 return 2;
106 }
107 return 3;
108 }
109
111 {
112 if (this->isNull())
113 {
114 return {};
115 }
116 return QStringLiteral("%1.%2.%3").arg(m_l1).arg(m_l2).arg(m_l3);
117 }
118
120 {
121 if (this->isNull())
122 {
123 return {};
124 }
125 return QStringLiteral("%1 %2").arg(this->getLevelString(), this->getName());
126 }
127
129 {
130 if (this->isNull())
131 {
132 return {};
133 }
134 return QStringLiteral("%1 %2").arg(this->getLevelString(), this->getPath());
135 }
136
138 {
139 return stringCompare(path, this->getPath(), cs);
140 }
141
142 bool CAircraftCategory::matchesLevel(int l1, int l2, int l3) const
143 {
144 if (l1 != m_l1)
145 {
146 return false;
147 }
148 if (l2 > 0 && l2 != m_l2)
149 {
150 return false;
151 }
152 if (l3 > 0 && l3 != m_l3)
153 {
154 return false;
155 }
156 return true;
157 }
158
160 {
161 if (level.isEmpty())
162 {
163 return false;
164 }
165 const int l1 = level.first();
166 const int l2 = level.size() > 1 ? level.at(1) : 0;
167 const int l3 = level.size() > 2 ? level.at(2) : 0;
168 return this->matchesLevel(l1, l2, l3);
169 }
170
172 {
173 if (index.isMyself())
174 {
175 return QVariant::fromValue(*this);
176 }
178 {
180 }
181 const auto i = index.frontCasted<ColumnIndex>();
182 switch (i)
183 {
184 case IndexName: return QVariant::fromValue(m_name);
185 case IndexDescription: return QVariant::fromValue(m_description);
186 case IndexAssignable: return QVariant::fromValue(m_assignable);
187 case IndexPath: return QVariant::fromValue(m_path);
188 case IndexLevelString: return QVariant::fromValue(this->getLevelString());
189 case IndexLevelStringAndName: return QVariant::fromValue(this->getLevelAndName());
190 case IndexLevelStringAndPath: return QVariant::fromValue(this->getLevelAndPath());
191 default: return CValueObject::propertyByIndex(index);
192 }
193 }
194
196 {
197 if (index.isMyself())
198 {
199 (*this) = variant.value<CAircraftCategory>();
200 return;
201 }
203 {
205 return;
206 }
207 const auto i = index.frontCasted<ColumnIndex>();
208 switch (i)
209 {
210 case IndexName: this->setName(variant.value<QString>()); break;
211 case IndexDescription: this->setDescription(variant.value<QString>()); break;
212 case IndexAssignable: this->setAssignable(variant.value<bool>()); break;
213 default: CValueObject::setPropertyByIndex(index, variant); break;
214 }
215 }
216
218 {
219 if (index.isMyself())
220 {
221 return m_path.compare(compareValue.getPath(), Qt::CaseInsensitive);
222 }
224 {
226 }
227 const auto i = index.frontCasted<ColumnIndex>();
228 switch (i)
229 {
230 case IndexName: return m_name.compare(compareValue.getName(), Qt::CaseInsensitive);
231 case IndexPath: return m_path.compare(compareValue.getPath(), Qt::CaseInsensitive);
232 case IndexDescription: return m_description.compare(compareValue.getDescription(), Qt::CaseInsensitive);
233 case IndexAssignable: return Compare::compare(this->isAssignable(), compareValue.isAssignable());
234 case IndexLevelStringAndName:
235 case IndexLevelStringAndPath:
236 case IndexLevelString: return this->compareByLevel(compareValue);
237 default: return CValueObject::comparePropertyByIndex(index, *this);
238 }
239 Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison");
240 return 0;
241 }
242
244 {
245 const QString name(json.value(prefix % u"name").toString());
246 const QString description(json.value(prefix % u"description").toString());
247 const QString path(json.value(prefix % u"path").toString());
248 const bool assignable = CDatastoreUtility::dbBoolStringToBool(json.value(prefix % u"assignable").toString());
249 const int l1 = json.value(prefix % u"l1").toInt();
250 const int l2 = json.value(prefix % u"l2").toInt();
251 const int l3 = json.value(prefix % u"l3").toInt();
252
253 CAircraftCategory cat(name, description, path, assignable);
254 cat.setLevel(l1, l2, l3);
256 return cat;
257 }
258
260 {
261 // any faster or better way to compare can be used here
262 int c = Compare::compare(m_l1, other.m_l1);
263 if (c != 0)
264 {
265 return c;
266 }
267 c = Compare::compare(m_l2, other.m_l2);
268 if (c != 0)
269 {
270 return c;
271 }
272 c = Compare::compare(m_l3, other.m_l3);
273 return c;
274 }
275
277 {
278 const int c = this->compareByLevel(other);
279 return c < 0;
280 }
281} // namespace swift::misc::aviation
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Status messages, e.g. from Core -> GUI.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
void setAssignable(bool assignable)
Mark/set assignable.
bool isHigherLevel(const CAircraftCategory &other) const
Higher level?
int comparePropertyByIndex(CPropertyIndexRef index, const CAircraftCategory &compareValue) const
Compare for index.
void setDescription(const QString &description)
Set description.
const QString & getName() const
Get name.
const QString & getPath() const
Path.
int compareByLevel(const CAircraftCategory &other) const
Level compare.
QString getLevelAndName() const
Level and name.
CAircraftCategory()=default
Default constructor.
swift::misc::CStatusMessageList validate() const
Validate data.
bool isLevel(int l1, int l2, int l3) const
Is that given level?
const QString & getDescription() const
Description.
QString getLevelString() const
Level string.
bool matchesName(const QString &name, Qt::CaseSensitivity cs) const
Matching name?
QString getNameDbKey() const
Designator and DB key.
QList< int > getLevel() const
Levels depending on depth, 3.2 -> 3,2 / 1.0 -> 1 / 4.3.1 -> 4,3,1.
QString convertToQString(bool i18n=false) const
Cast as QString.
static CAircraftCategory fromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
From our database JSON format.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
QString getLevelAndPath() const
Level and path.
bool matchesPath(const QString &path, Qt::CaseSensitivity cs)
Matching path?
void setLevel(int l1, int l2, int l3)
Level.
static const CAircraftCategory & null()
NULL object.
bool hasName() const
Aircraft designator?
bool matchesLevel(int l1, int l2=0, int l3=0) const
Is matching the level, 0 ignored.
void setName(const QString &name)
Set name.
static bool dbBoolStringToBool(const QString &dbBool)
DB Bool value to bool.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
bool isLoadedFromDb() const
Loaded from DB.
Definition datastore.cpp:58
static bool canHandleIndex(swift::misc::CPropertyIndexRef index)
Can given index be handled?
QString getDbKeyAsStringInParentheses(const QString &prefix={}) const
Db key in parentheses, e.g. "(3)".
Definition datastore.cpp:42
int comparePropertyByIndex(CPropertyIndexRef index, const IDatastoreObjectWithIntegerKey &compareValue) const
Compare for index.
void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Set key and timestamp values.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
Compare for index.
Definition mixinindex.h:203
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
int compare(T a, T b)
Compare arithmetic values.
Free functions in swift::misc.
SWIFT_MISC_EXPORT bool stringCompare(const QString &c1, const QString &c2, Qt::CaseSensitivity cs)
String compare.
QList< T >::const_reference at(qsizetype i) const const
T & first()
bool isEmpty() const const
qsizetype size() const const
QString arg(Args &&... args) const const
CaseSensitivity
QVariant fromValue(T &&value)
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67