swift
aircraftparts.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QtGlobal>
7 
8 #include "QStringBuilder"
9 
11 #include "misc/comparefunctions.h"
12 #include "misc/propertyindex.h"
13 #include "misc/stringutils.h"
14 
15 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, CAircraftParts)
16 
17 namespace swift::misc::aviation
18 {
19  CAircraftParts::CAircraftParts(int flapsPercent) : m_flapsPercentage(flapsPercent) {}
20 
21  CAircraftParts::CAircraftParts(const CAircraftLights &lights, bool gearDown, int flapsPercent, bool spoilersOut,
22  const CAircraftEngineList &engines, bool onGround)
23  : m_lights(lights), m_engines(engines), m_flapsPercentage(flapsPercent), m_gearDown(gearDown),
24  m_spoilersOut(spoilersOut), m_isOnGround(onGround)
25  {}
26 
27  CAircraftParts::CAircraftParts(const CAircraftLights &lights, bool gearDown, int flapsPercent, bool spoilersOut,
28  const CAircraftEngineList &engines, bool onGround, qint64 timestamp)
29  : m_lights(lights), m_engines(engines), m_flapsPercentage(flapsPercent), m_gearDown(gearDown),
30  m_spoilersOut(spoilersOut), m_isOnGround(onGround)
31  {
32  this->setMSecsSinceEpoch(timestamp);
33  }
34 
35  QString CAircraftParts::convertToQString(bool i18n) const
36  {
37  return u"ts: " % this->getFormattedTimestampAndOffset(true) % u" details: " % this->getPartsDetailsAsString() %
38  (m_guessingDetails.isEmpty() ? QString() : u" - " % m_guessingDetails) % u" | on ground: " %
39  swift::misc::boolToYesNo(m_isOnGround) % u" | lights: " % m_lights.toQString(i18n) % u" | gear down: " %
40  swift::misc::boolToYesNo(m_gearDown) % u" | flaps pct: " % QString::number(m_flapsPercentage) %
41  u" | spoilers out: " % swift::misc::boolToYesNo(m_spoilersOut) % u" | engines on: " %
42  m_engines.toQString(i18n);
43  }
44 
46  {
47  QJsonObject json = this->toJson();
48  json.remove(attributeNameIsFullJson());
49  json.insert(attributeNameIsFullJson(), QJsonValue(false));
50  return json;
51  }
52 
53  QJsonObject CAircraftParts::toFullJson() const
54  {
55  QJsonObject json = this->toJson();
56  json.remove(attributeNameIsFullJson());
57  json.insert(attributeNameIsFullJson(), QJsonValue(true));
58  return json;
59  }
60 
61  bool CAircraftParts::isNull() const { return this->getPartsDetails() == NotSet && m_flapsPercentage < 0; }
62 
64  {
65  // currently same as some values are diabled for comparison
66  // but that could change in future
67  return other == *this;
68  }
69 
71  {
72  static const CAircraftParts null(-1);
73  return null;
74  }
75 
77  {
78  static const QString guessed("guessed");
79  static const QString notset("not set");
80  static const QString fsd("FSD parts");
81 
82  switch (details)
83  {
84  case GuessedParts: return guessed;
85  case FSDAircraftParts: return fsd;
86  case NotSet: break;
87  default: break;
88  }
89  return notset;
90  }
91 
93  {
94  static const QString a("is_full_data");
95  return a;
96  }
97 
99  {
100  if (index.isMyself()) { return QVariant::fromValue(*this); }
102  {
104  }
105 
106  const ColumnIndex i = index.frontCasted<ColumnIndex>();
107  switch (i)
108  {
109  case IndexEngines: return QVariant::fromValue(m_engines);
110  case IndexEnginesAsString: return QVariant::fromValue(m_engines.toQString(true));
111  case IndexFlapsPercentage: return QVariant::fromValue(m_flapsPercentage);
112  case IndexGearDown: return QVariant::fromValue(m_gearDown);
113  case IndexLights: return m_lights.propertyByIndex(index.copyFrontRemoved());
114  case IndexSpoilersOut: return QVariant::fromValue(m_spoilersOut);
115  case IndexIsOnGround: return QVariant::fromValue(m_isOnGround);
116  default: return CValueObject::propertyByIndex(index);
117  }
118  }
119 
120  void CAircraftParts::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
121  {
122  if (index.isMyself())
123  {
124  (*this) = variant.value<CAircraftParts>();
125  return;
126  }
128  {
130  return;
131  }
132 
133  const ColumnIndex i = index.frontCasted<ColumnIndex>();
134  switch (i)
135  {
136  case IndexEngines: m_engines = variant.value<decltype(m_engines)>(); break;
137  case IndexFlapsPercentage: m_flapsPercentage = variant.toInt(); break;
138  case IndexGearDown: m_gearDown = variant.toBool(); break;
139  case IndexLights: m_lights.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
140  case IndexSpoilersOut: m_spoilersOut = variant.toBool(); break;
141  case IndexIsOnGround: m_isOnGround = variant.toBool(); break;
142  default: CValueObject::setPropertyByIndex(index, variant); break;
143  }
144  }
145 
147  {
148  if (index.isMyself())
149  {
151  }
153  {
154  return ITimestampWithOffsetBased::comparePropertyByIndex(index, compareValue);
155  }
156 
157  const ColumnIndex i = index.frontCasted<ColumnIndex>();
158  switch (i)
159  {
160  case IndexEngines: return Compare::compare(this->getEnginesCount(), compareValue.getEnginesCount());
161  case IndexFlapsPercentage: return Compare::compare(m_flapsPercentage, compareValue.getFlapsPercent());
162  case IndexGearDown: return Compare::compare(m_gearDown, compareValue.isGearDown());
163  case IndexSpoilersOut: return Compare::compare(m_spoilersOut, compareValue.isSpoilersOut());
164  case IndexIsOnGround: return Compare::compare(m_isOnGround, compareValue.isOnGround());
165  case IndexLights: return m_lights.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getLights());
166  default: break;
167  }
168  Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison");
169  return 0;
170  }
171 
173  {
174  CAircraftLights lights = this->getLights();
175  const bool anyEngine = this->isAnyEngineOn();
176  lights.setRecognitionOn(anyEngine);
177  lights.setCabinOn(anyEngine);
178  return lights;
179  }
180 
182 
184 
185  bool CAircraftParts::isFixedGearDown() const { return this->isGearDown() || this->isOnGround(); }
186 
187  CAircraftEngine CAircraftParts::getEngine(int number) const { return m_engines.getEngine(number); }
188 
189  bool CAircraftParts::isEngineOn(int number) const { return m_engines.isEngineOn(number); }
190 
191  bool CAircraftParts::isAnyEngineOn() const { return m_engines.isAnyEngineOn(); }
192 
193  void CAircraftParts::setEngines(const CAircraftEngine &engine, int engineNumber)
194  {
196  engines.setEngines(engine, engineNumber);
197  m_engines = engines;
198  }
199 } // namespace swift::misc::aviation
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
void setMSecsSinceEpoch(qint64 mSecsSinceEpoch)
Timestamp as ms value.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampWithOffsetBased &compareValue) const
Compare for index.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
QString getFormattedTimestampAndOffset(bool includeRawTimestamp) const
Timestamp and offset.
Value object encapsulating information about aircraft's engines.
Value object encapsulating a list of aircraft engines.
void setEngines(const CAircraftEngine &engine, int engineNumber)
Copy one engine multiple times (number) and set the number of engines to number.
CAircraftEngine getEngine(int engineNumber) const
Get engine 1..n.
bool isEngineOn(int engineNumber) const
Engine number 1..x on?
bool isAnyEngineOn() const
Is any engine on?
Value object encapsulating information about aircraft's lights.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const CAircraftLights &compareValue) const
Compare by index.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
void setCabinOn(bool on)
Set cabin lights.
void setRecognitionOn(bool on)
Set recognition lights.
Value object encapsulating information of aircraft's parts.
Definition: aircraftparts.h:26
CAircraftParts()
Default constructor.
Definition: aircraftparts.h:49
static const QString & attributeNameIsFullJson()
Attribute name.
bool isGearDown() const
Is gear down?
Definition: aircraftparts.h:90
CAircraftLights getLights() const
Get aircraft lights.
Definition: aircraftparts.h:72
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
bool isEngineOn(int number) const
Is engine with number 1..n on?
bool isSpoilersOut() const
Are spoilers out?
bool isNull() const
NULL parts object?
QJsonObject toIncrementalJson() const
Incremental JSON object.
CAircraftLights getAdjustedLights() const
Lights adjusted depending on engines.
CAircraftEngineList & engines()
Direct access to engines, meant for simple value modifications.
static const CAircraftParts & null()
NULL parts object.
bool isFixedGearDown() const
Is fixed gear down?
void setEngines(const CAircraftEngineList &engines)
Set engines.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
CAircraftLights & lights()
Reference to lights, meant for easy direct changes of the values.
Definition: aircraftparts.h:78
bool isOnGround() const
Is aircraft on ground?
bool isAnyEngineOn() const
Any engine on?
PartsDetails getPartsDetails() const
Get parts details.
CAircraftEngine getEngine(int number) const
Engine with number.
int comparePropertyByIndex(CPropertyIndexRef index, const CAircraftParts &compareValue) const
Compare for index.
static const QString & partsDetailsToString(PartsDetails details)
Convert to QString.
void setAllLightsOn()
Set all lights on.
QString convertToQString(bool i18n=false) const
Cast as QString.
const QString & getPartsDetailsAsString() const
Parts details as string.
int getFlapsPercent() const
Get flaps position in percent.
void setAllLightsOff()
Set all lights off.
bool equalValues(const CAircraftParts &other) const
Equal values, but not comparing timestamp etc.
int getEnginesCount() const
Number of engines.
QJsonObject toFullJson() const
Full JSON Object.
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
QJsonObject toJson() const
Cast to JSON object.
Definition: mixinjson.h:132
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
SWIFT_MISC_EXPORT const QString & boolToYesNo(bool v)
Bool to yes/no.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67