swift
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 
14 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, CAirportIcaoCode)
15 
16 namespace 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 
31  QString CAirportIcaoCode::unifyAirportCode(const QString &icaoCode)
32  {
33  const QString code = icaoCode.trimmed().toUpper();
34  if (!validCodeLength(icaoCode.length(), false)) return {};
35  if (containsChar(code, [](QChar c) { return !c.isLetterOrNumber(); })) { return {}; }
36  return code;
37  }
38 
39  bool CAirportIcaoCode::isValidIcaoDesignator(const QString &icaoCode, bool strict)
40  {
41  const QString icao = unifyAirportCode(icaoCode);
42  return validCodeLength(icao.length(), strict);
43  }
44 
45  bool CAirportIcaoCode::containsNumbers(const QString &icaoCode)
46  {
47  return (containsChar(icaoCode, [](QChar c) { return c.isDigit(); }));
48  }
49 
51  {
52  if (index.isMyself()) { return QVariant::fromValue(*this); }
53  return CValueObject::propertyByIndex(index);
54  }
55 
56  void CAirportIcaoCode::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
57  {
58  if (index.isMyself())
59  {
60  (*this) = variant.value<CAirportIcaoCode>();
61  return;
62  }
63  CValueObject::setPropertyByIndex(index, variant);
64  }
65 
67  {
68  Q_UNUSED(index);
69  return m_icaoCode.compare(compareValue.getIcaoCode(), Qt::CaseInsensitive);
70  }
71 
72  bool CAirportIcaoCode::validCodeLength(int l, bool strict)
73  {
74  // FAA code 3
75  // ICAO code 4
76  if (strict) { return l == 4; }
77  return l >= 3 && l <= 6;
78 
79  // https://en.wikipedia.org/wiki/Location_identifier#FAA_identifier says can be up to 5 characters
80  // https://en.wikipedia.org/wiki/ICAO_airport_code#Pseudo_ICAO-codes says France has some 6-character airport
81  // codes and ZZZZ can be used in a flight plan as ICAO code for any airport that doesn't have one
82  }
83 } // namespace swift::misc::aviation
Non-owning reference to a CPropertyIndex with a subset of its features.
bool isMyself() const
Myself index, used with nesting.
Value object encapsulating information of airport ICAO data.
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.
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:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
bool containsChar(const QString &s, F predicate)
True if any character in the string matches the given predicate.
Definition: stringutils.h:65
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67