swift
Loading...
Searching...
No Matches
airporticaocode.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
7
8#include <Qt>
9#include <QtGlobal>
10
12#include "misc/stringutils.h"
13
14SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, CAirportIcaoCode)
15
16namespace swift::misc::aviation
17{
18 QString CAirportIcaoCode::convertToQString(bool ) const { return m_icaoCode; }
19
20 bool CAirportIcaoCode::hasValidIcaoCode(bool strict) const
21 {
23 }
24
25 bool CAirportIcaoCode::equalsString(const QString &icaoCode) const
26 {
27 CAirportIcaoCode other(icaoCode);
28 return other == (*this);
29 }
30
32 {
33 const QString code = icaoCode.trimmed().toUpper();
34 if (!validCodeLength(icaoCode.length(), false)) return {};
35 if (containsChar(code, [](QChar c) { return !c.isLetterOrNumber(); }))
36 {
37 return {};
38 }
39 return code;
40 }
41
42 bool CAirportIcaoCode::isValidIcaoDesignator(const QString &icaoCode, bool strict)
43 {
44 const QString icao = unifyAirportCode(icaoCode);
45 return validCodeLength(icao.length(), strict);
46 }
47
49 {
50 return (containsChar(icaoCode, [](QChar c) { return c.isDigit(); }));
51 }
52
54 {
55 if (index.isMyself())
56 {
57 return QVariant::fromValue(*this);
58 }
60 }
61
63 {
64 if (index.isMyself())
65 {
66 (*this) = variant.value<CAirportIcaoCode>();
67 return;
68 }
70 }
71
73 {
74 Q_UNUSED(index);
75 return m_icaoCode.compare(compareValue.getIcaoCode(), Qt::CaseInsensitive);
76 }
77
78 bool CAirportIcaoCode::validCodeLength(int l, bool strict)
79 {
80 // FAA code 3
81 // ICAO code 4
82 if (strict)
83 {
84 return l == 4;
85 }
86 return l >= 3 && l <= 6;
87
88 // https://en.wikipedia.org/wiki/Location_identifier#FAA_identifier says can be up to 5 characters
89 // https://en.wikipedia.org/wiki/ICAO_airport_code#Pseudo_ICAO-codes says France has some 6-character airport
90 // codes and ZZZZ can be used in a flight plan as ICAO code for any airport that doesn't have one
91 }
92} // namespace swift::misc::aviation
Non-owning reference to a CPropertyIndex with a subset of its features.
bool isMyself() const
Myself index, used with nesting.
int comparePropertyByIndex(CPropertyIndexRef index, const CAirportIcaoCode &compareValue) const
Compare for index.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
static bool containsNumbers(const QString &icaoCode)
Containing numbers (normally indicator for small airfield/strip).
bool hasValidIcaoCode(bool strict) const
Has valid code?
static bool isValidIcaoDesignator(const QString &icaoCode, bool strict)
Valid ICAO designator.
QString getIcaoCode() const
Get ICAO code.
CAirportIcaoCode()=default
Default constructor.
bool equalsString(const QString &icaoCode) const
Equals callsign string?
QString convertToQString(bool i18n=false) const
Cast as QString.
static QString unifyAirportCode(const QString &icaoCode)
Unify code.
static bool validCodeLength(int l, bool strict)
Valid code lenght.
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
bool containsChar(const QString &s, F predicate)
True if any character in the string matches the given predicate.
Definition stringutils.h:64
bool isDigit(char32_t ucs4)
bool isLetterOrNumber(char32_t ucs4)
qsizetype length() const const
QString toUpper() const const
QString trimmed() const const
CaseInsensitive
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