swift
Loading...
Searching...
No Matches
atcstation.cpp
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
6#include <QCoreApplication>
7#include <QStringBuilder>
8#include <QtGlobal>
9
14#include "misc/pq/units.h"
16#include "misc/propertyindexvariantmap.h" // needed for mixin::Index::apply
17#include "misc/stringutils.h"
18
19using namespace swift::misc;
20using namespace swift::misc::physical_quantities;
21using namespace swift::misc::geo;
22using namespace swift::misc::network;
23
24SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, CAtcStation)
25
26namespace swift::misc::aviation
27{
28 CAtcStation::CAtcStation(const QString &callsign) : m_callsign(callsign) { m_callsign.setTypeHint(CCallsign::Atc); }
29
30 CAtcStation::CAtcStation(const CCallsign &callsign, const CUser &controller, const CFrequency &frequency,
31 const CCoordinateGeodetic &pos, const CLength &range, bool isOnline,
32 const QDateTime &logoffTimeUtc, const CInformationMessage &atis,
33 const CInformationMessage &metar)
34 : m_callsign(callsign), m_controller(controller), m_frequency(frequency), m_position(pos), m_range(range),
35 m_isOnline(isOnline), m_logoffTimeUtc(logoffTimeUtc), m_atis(atis), m_metar(metar)
36 {
37 // sync callsigns
38 m_callsign.setTypeHint(CCallsign::Atc);
39 if (!m_controller.hasCallsign() && !callsign.isEmpty())
40 {
41 m_controller.setCallsign(m_callsign);
42 }
43 }
44
45 bool CAtcStation::hasLogoffTimeUtc() const { return !m_logoffTimeUtc.isNull(); }
46
47 bool CAtcStation::hasMetar() const { return m_metar.hasMessage(); }
48
50 {
51 if (!this->isAfvCrossCoupled())
52 {
53 return this->getCallsignAsString();
54 }
55 return QStringLiteral("*") % this->getCallsignAsString();
56 }
57
58 QString CAtcStation::getCallsignSuffix() const { return m_callsign.getSuffix(); }
59
60 int CAtcStation::getSuffixSortOrder() const { return m_callsign.getSuffixSortOrder(); }
61
62 void CAtcStation::setCallsign(const CCallsign &callsign)
63 {
64 m_callsign = callsign;
65 m_controller.setCallsign(callsign);
66 m_isAfvCrossCoupled = callsign.getStringAsSet().startsWith('*');
67 }
68
70 {
71 if (m_callsign.isEmpty())
72 {
73 return this->getControllerRealName();
74 }
75 if (!m_controller.hasRealName())
76 {
77 return m_callsign.asString();
78 }
79 return m_callsign.asString() % u' ' % this->getControllerRealName();
80 }
81
82 void CAtcStation::setController(const CUser &controller)
83 {
84 m_controller = controller;
85 m_controller.setCallsign(m_callsign);
86 }
87
89 {
90 static const QString atcI18n(QCoreApplication::translate("Aviation", "ATC station"));
91 static const QString rangeI18n(QCoreApplication::translate("Aviation", "range"));
92 static const QString fromUtcI18n(QCoreApplication::translate("Aviation", "from(UTC)"));
93 static const QString untilUtcI18n(QCoreApplication::translate("Aviation", "until(UTC)"));
94
95 const QString s = (i18n ? atcI18n : QStringLiteral("ATC station")) % u' ' % m_callsign.toQString(i18n) % u' ' %
96 m_position.toQString(i18n) % u" online: " % boolToYesNo(m_isOnline) %
97
98 // controller
99 (m_controller.isNull() ? QString() : u' ' % m_controller.toQString(i18n)) %
100
101 // frequency
102 u' ' % m_frequency.valueRoundedWithUnit(3, i18n) %
103
104 // ATIS
105 (!this->hasAtis() ? QString() : u' ' % m_atis.toQString(i18n)) %
106
107 // METAR
108 (!this->hasMetar() ? QString() : u' ' % m_metar.toQString(i18n)) %
109
110 // range
111 u' ' % (i18n ? rangeI18n : QStringLiteral("range")) % u' ' % m_range.toQString(i18n) %
112
113 // distance / bearing
115
116 return s;
117 }
118
120 {
121 m_frequency = frequency;
122 m_frequency.setUnit(CFrequencyUnit::MHz());
123 }
124
126 {
127 if (m_range.isNull() || !hasValidRelativeDistance())
128 {
129 return false;
130 }
131 return (this->getRelativeDistance() <= m_range);
132 }
133
134 bool CAtcStation::setOnline(bool online)
135 {
136 if (online == m_isOnline)
137 {
138 return false;
139 }
140 m_isOnline = online;
141 return true;
142 }
143
145 {
146 return comUnit.isActiveFrequencySameFrequency(this->getFrequency());
147 }
148
150 {
151 return CComSystem::isSameFrequency(frequency, this->getFrequency());
152 }
153
155 {
156 switch (type)
157 {
158 case CInformationMessage::ATIS: return this->getAtis();
159 case CInformationMessage::METAR: return this->getMetar();
160 default: break;
161 }
163 }
164
166 {
167 switch (message.getType())
168 {
169 case CInformationMessage::ATIS: this->setAtis(message); return true;
170 case CInformationMessage::METAR: this->setMetar(message); return true;
171 default: break;
172 }
173 return false;
174 }
175
177
179
180 const CAltitude &CAtcStation::geodeticHeight() const { return m_position.geodeticHeight(); }
181
182 QVector3D CAtcStation::normalVector() const { return m_position.normalVector(); }
183
184 std::array<double, 3> CAtcStation::normalVectorDouble() const { return m_position.normalVectorDouble(); }
185
187 {
188 if (index.isMyself())
189 {
190 return QVariant::fromValue(*this);
191 }
192 const auto i = index.frontCasted<ColumnIndex>();
193 switch (i)
194 {
195 case IndexLogoffTime: return QVariant::fromValue(m_logoffTimeUtc);
196 case IndexCallsign: return m_callsign.propertyByIndex(index.copyFrontRemoved());
197 case IndexCallsignString: return this->getCallsignAsString();
198 case IndexCallsignStringCrossCopuled: return this->getCallsignAsStringCrossCoupled();
199 case IndexController: return m_controller.propertyByIndex(index.copyFrontRemoved());
200 case IndexFrequency: return m_frequency.propertyByIndex(index.copyFrontRemoved());
201 case IndexIsOnline: return QVariant::fromValue(m_isOnline);
202 case IndexLatitude: return this->latitude().propertyByIndex(index.copyFrontRemoved());
203 case IndexLongitude: return this->longitude().propertyByIndex(index.copyFrontRemoved());
204 case IndexPosition: return m_position.propertyByIndex(index.copyFrontRemoved());
205 case IndexRange: return m_range.propertyByIndex(index.copyFrontRemoved());
206 case IndexIsInRange: return QVariant::fromValue(isInRange());
207 case IndexAtis: return m_atis.propertyByIndex(index.copyFrontRemoved());
208 case IndexMetar: return m_metar.propertyByIndex(index.copyFrontRemoved());
209 case IndexIsAfvCrossCoupled: return QVariant::fromValue(m_isAfvCrossCoupled);
210 default:
214 }
215 }
216
218 {
219 if (index.isMyself())
220 {
221 (*this) = variant.value<CAtcStation>();
222 return;
223 }
224 const auto i = index.frontCasted<ColumnIndex>();
225 switch (i)
226 {
227 case IndexLogoffTime: this->setLogoffTimeUtc(variant.value<QDateTime>()); break;
228 case IndexCallsign: m_callsign.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
229 case IndexController: m_controller.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
230 case IndexFrequency: m_frequency.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
231 case IndexIsOnline: this->setOnline(variant.value<bool>()); break;
232 case IndexPosition: m_position.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
233 case IndexRange: m_range.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
234 case IndexAtis: m_atis.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
235 case IndexMetar: m_metar.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
236 case IndexIsAfvCrossCoupled: this->setAfvCrossCoupled(variant.value<bool>()); break;
237 case IndexCallsignString:
238 case IndexCallsignStringCrossCopuled:
239 {
240 const QString cs = variant.toString();
241 *this = CAtcStation();
242 this->setAfvCrossCoupled(cs.startsWith('*'));
243 }
244 break;
245 default:
247 {
249 }
250 else
251 {
252 CValueObject::setPropertyByIndex(index, variant);
253 }
254 break;
255 }
256 }
257
259 {
260 if (index.isMyself())
261 {
263 }
264 const auto i = index.frontCasted<ColumnIndex>();
265 switch (i)
266 {
267 case IndexLogoffTime: return Compare::compare(this->getLogoffTimeUtc(), compareValue.getLogoffTimeUtc());
268 case IndexCallsignString:
269 case IndexCallsignStringCrossCopuled:
270 return m_callsign.comparePropertyByIndex(CPropertyIndexRef::empty(), compareValue.getCallsign());
271 case IndexCallsign:
272 return m_callsign.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getCallsign());
273 case IndexController:
274 return m_controller.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getController());
275 case IndexFrequency:
276 return m_frequency.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getFrequency());
277 case IndexIsOnline: return Compare::compare(this->isOnline(), compareValue.isOnline());
278 case IndexLatitude:
279 return this->latitude().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.latitude());
280 case IndexLongitude:
281 return this->longitude().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.longitude());
282 case IndexPosition:
283 return m_position.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getPosition());
284 case IndexRange: return m_range.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getRange());
285 case IndexIsInRange: return Compare::compare(this->isInRange(), compareValue.isInRange());
286 case IndexAtis: return m_atis.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getAtis());
287 case IndexMetar: return m_metar.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getMetar());
288 case IndexIsAfvCrossCoupled:
289 return Compare::compare(this->isAfvCrossCoupled(), compareValue.isAfvCrossCoupled());
290 default:
292 {
294 }
295 return CValueObject::comparePropertyByIndex(index, *this);
296 }
297 Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
298 return 0;
299 }
300} // namespace swift::misc::aviation
static CFrequencyUnit MHz()
Megahertz.
Definition units.h:380
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
static CPropertyIndexRef empty()
an empty property index
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Altitude as used in aviation, can be AGL or MSL altitude.
Definition altitude.h:52
void setController(const network::CUser &controller)
Set controller.
geo::CLatitude latitude() const
Latitude.
const physical_quantities::CFrequency & getFrequency() const
Get frequency.
Definition atcstation.h:135
void setFrequency(const swift::misc::physical_quantities::CFrequency &frequency)
Set frequency.
const geo::CCoordinateGeodetic & getPosition() const
Get the position of the center of the controller's area of visibility.
Definition atcstation.h:141
void setAfvCrossCoupled(bool coupled)
Set AFV cross coupled.
Definition atcstation.h:165
ColumnIndex
Properties by index.
Definition atcstation.h:42
bool isOnline() const
Is station online?
Definition atcstation.h:156
const CInformationMessage & getMetar() const
Get METAR.
Definition atcstation.h:187
void setMetar(const CInformationMessage &metar)
Set METAR.
Definition atcstation.h:193
geo::CLongitude longitude() const
Longitude.
bool hasAtis() const
Has ATIS?
Definition atcstation.h:78
QString getCallsignSuffix() const
Callsign suffix (e.g. TWR).
void setCallsign(const CCallsign &callsign)
Set callsign.
const aviation::CAltitude & geodeticHeight() const
Height, ellipsoidal or geodetic height (used in GPS).
QString getCallsignAsString() const
Get callsign as string.
Definition atcstation.h:90
bool isComUnitTunedToFrequency(const aviation::CComSystem &comUnit) const
Is Com unit tuned to this stations frequency.
const CInformationMessage & getInformationMessage(CInformationMessage::InformationType type) const
Message per type.
QString getCallsignAsStringCrossCoupled() const
Get callsign.
QString getCallsignAndControllerRealName() const
Callsign and controller's name if available.
std::array< double, 3 > normalVectorDouble() const
Normal vector with double precision.
CAtcStation()=default
Default constructor.
bool setOnline(bool online)
Set online.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
const CCallsign & getCallsign() const
Get callsign.
Definition atcstation.h:84
const CInformationMessage & getAtis() const
Get ATIS.
Definition atcstation.h:178
bool isAtcStationFrequency(const physical_quantities::CFrequency &frequency) const
Is passed frequency the frequency of this station.
QString getControllerRealName() const
Get controller name.
Definition atcstation.h:108
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
const physical_quantities::CLength & getRange() const
Get the radius of the controller's area of visibility.
Definition atcstation.h:147
bool setMessage(const CInformationMessage &message)
Set given message.
int getSuffixSortOrder() const
Callsign suffix sort order.
bool isAfvCrossCoupled() const
Is AFV cross coupled?
Definition atcstation.h:162
const swift::misc::network::CUser & getController() const
Get controller.
Definition atcstation.h:105
bool hasMetar() const
Has METAR?
QVector3D normalVector() const
Normal vector.
const QDateTime & getLogoffTimeUtc() const
Return the expected logoff time (UTC). This data comes from the controller through its ATIS line.
Definition atcstation.h:169
bool isInRange() const
In range? If range and distance to own aircraft are not available false.
bool hasLogoffTimeUtc() const
Has expected logoff time?
void setAtis(const CInformationMessage &atis)
Set ATIS.
Definition atcstation.h:181
QString convertToQString(bool i18n=false) const
Cast as QString.
int comparePropertyByIndex(CPropertyIndexRef index, const CAtcStation &compareValue) const
Compare for index.
void setLogoffTimeUtc(const QDateTime &logoffTimeUtc)
Set expected logoff time (UTC).
Definition atcstation.h:202
Value object encapsulating information of a callsign.
Definition callsign.h:30
int comparePropertyByIndex(CPropertyIndexRef index, const CCallsign &compareValue) const
Compare for index.
Definition callsign.cpp:491
const QString & getStringAsSet() const
Get callsign.
Definition callsign.h:99
bool isEmpty() const
Is empty?
Definition callsign.h:63
COM system (aka "radio").
Definition comsystem.h:37
static bool isSameFrequency(const physical_quantities::CFrequency &freq1, const physical_quantities::CFrequency &freq2)
Compare frequencies under consideration that on VATSIM frequencies .x20/.x25 and ....
bool isActiveFrequencySameFrequency(const physical_quantities::CFrequency &comFrequency) const
Is active frequency the same frequency.
Definition comsystem.cpp:59
Value object encapsulating information message (ATIS, METAR, TAF).
static const CInformationMessage & unspecified()
Unspecified object.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled?
const physical_quantities::CLength & getRelativeDistance() const
Get the distance.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const ICoordinateWithRelativePosition &compareValue) const
Compare for index.
QString convertToQString(bool i18n=false) const
Cast as QString.
void setPropertyByIndex(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
Value object encapsulating information of a user.
Definition user.h:28
bool setCallsign(const aviation::CCallsign &callsign)
Set associated callsign.
Definition user.cpp:71
Physical unit length (length).
Definition length.h:18
void setUnit(const MU &unit)
Simply set unit, do no calclulate conversion.
int compare(T a, T b)
Compare arithmetic values.
Free functions in swift::misc.
SWIFT_MISC_EXPORT const QString & boolToYesNo(bool v)
Bool to yes/no.
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QVariant fromValue(T &&value)
QString toString() const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67