Enroute Flight Navigation
A navigation app for VFR pilots
TrafficFactor_Abstract.h
1/***************************************************************************
2 * Copyright (C) 2021-2025 by Stefan Kebekus *
3 * stefan.kebekus@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#pragma once
22
23#include <QObjectBindableProperty>
24#include <QTimer>
25
26#include "units/Distance.h"
27#include "TrafficFactorAircraftType.h"
28
29using namespace std::chrono_literals;
30
31
32namespace Traffic {
33
44
45class TrafficFactor_Abstract : public QObject {
46 Q_OBJECT
47
48public:
50 static constexpr auto lifeTime = 10s;
51
56 explicit TrafficFactor_Abstract(QObject* parent = nullptr);
57
58 // Standard destructor
59 ~TrafficFactor_Abstract() override = default;
60
61 //
62 // Methods
63 //
64
75 {
77 setCallSign(other.callSign());
78 setHDist(other.hDist());
79 setID(other.ID());
80 setType(other.type());
81 setVDist(other.vDist());
82 updateDescription();
83 }
84
98 [[nodiscard]] auto hasHigherPriorityThan(const TrafficFactor_Abstract& rhs) const -> bool;
99
107
108
109 //
110 // PROPERTIES
111 //
112
132 Q_PROPERTY(int alarmLevel READ alarmLevel WRITE setAlarmLevel NOTIFY alarmLevelChanged BINDABLE bindableAlarmLevel)
133
134
141 Q_PROPERTY(bool animate READ animate WRITE setAnimate NOTIFY animateChanged)
142
147 Q_PROPERTY(QString callSign READ callSign WRITE setCallSign NOTIFY callSignChanged)
148
157 Q_PROPERTY(QString color READ color NOTIFY colorChanged)
158
165 Q_PROPERTY(QString description READ description NOTIFY descriptionChanged)
166
173 Q_PROPERTY(Units::Distance hDist READ hDist WRITE setHDist NOTIFY hDistChanged BINDABLE bindableHDist)
174
181 Q_PROPERTY(QString ID READ ID WRITE setID NOTIFY IDChanged)
182
188 Q_PROPERTY(bool relevant READ relevant BINDABLE bindableRelevant)
189
191 Q_PROPERTY(QString relevantString READ relevantString BINDABLE bindableRelevantString)
192
194 Q_PROPERTY(AircraftType type READ type WRITE setType NOTIFY typeChanged BINDABLE bindableType)
195
200 Q_PROPERTY(QString typeString READ typeString BINDABLE bindableTypeString)
201
207 Q_PROPERTY(bool valid READ valid NOTIFY validChanged BINDABLE bindableValid)
208
215 Q_PROPERTY(Units::Distance vDist READ vDist WRITE setVDist NOTIFY vDistChanged BINDABLE bindableVDist)
216
217
218 //
219 // Getter/Setter Methods
220 //
221
226 [[nodiscard]] int alarmLevel() const {return m_alarmLevel.value();}
227
232 [[nodiscard]] QBindable<int> bindableAlarmLevel() {return &m_alarmLevel;}
233
238 [[nodiscard]] auto animate() const -> bool
239 {
240 return m_animate;
241 }
242
247 [[nodiscard]] auto callSign() const -> QString
248 {
249 return m_callSign;
250 }
251
256 [[nodiscard]] auto color() const -> QString
257 {
258 if (m_alarmLevel == 0) {
259 return QStringLiteral("green");
260 }
261 if (m_alarmLevel == 1) {
262 return QStringLiteral("yellow");
263 }
264 return QStringLiteral("red");
265 }
266
271 [[nodiscard]] auto description() const -> QString
272 {
273 return m_description;
274 }
275
280 [[nodiscard]] Units::Distance hDist() const {return m_hDist.value();}
281
286 [[nodiscard]] QBindable<Units::Distance> bindableHDist() {return &m_hDist;}
287
292 [[nodiscard]] auto ID() const -> QString
293 {
294 return m_ID;
295 }
296
301 [[nodiscard]] bool relevant() const {return m_relevant.value();}
302
307 [[nodiscard]] QBindable<bool> bindableRelevant() {return &m_relevant;}
308
313 [[nodiscard]] QString relevantString() const {return m_relevantString.value();}
314
319 [[nodiscard]] QBindable<bool> bindableRelevantString() {return &m_relevantString;}
320
325 [[nodiscard]] Traffic::AircraftType type() const {return m_type.value();}
326
331 [[nodiscard]] QBindable<Traffic::AircraftType> bindableType() {return &m_type;}
332
337 [[nodiscard]] QString typeString() const {return m_typeString.value();}
338
343 [[nodiscard]] QBindable<QString> bindableTypeString() {return &m_typeString;}
344
349 [[nodiscard]] bool valid() const {return m_valid.value();}
350
355 [[nodiscard]] QBindable<bool> bindableValid() {return &m_valid;}
356
361 [[nodiscard]] Units::Distance vDist() const {return m_vDist.value();}
362
367 [[nodiscard]] QBindable<Units::Distance> bindableVDist() {return &m_vDist;}
368
369
370 //
371 // Setter Methods
372 //
373
378 void setAlarmLevel(int newAlarmLevel)
379 {
380 // Safety checks
381 if ((newAlarmLevel < 0) || (newAlarmLevel > 3)) {
382 return;
383 }
385 m_alarmLevel = newAlarmLevel;
386 }
387
392 void setAnimate(bool newAnimate) {
393 if (m_animate == newAnimate) {
394 return;
395 }
396 m_animate = newAnimate;
397 emit animateChanged();
398 }
399
404 void setCallSign(const QString& newCallSign) {
405 if (m_callSign == newCallSign) {
406 return;
407 }
408 m_callSign = newCallSign;
409 emit callSignChanged();
410 }
411
416 void setHDist(Units::Distance newHDist) {m_hDist = newHDist;}
417
422 void setID(const QString& newID) {
423 if (m_ID == newID) {
424 return;
425 }
426 m_ID = newID;
427 emit IDChanged();
428 }
429
434 void setType(Traffic::AircraftType newType) {m_type = newType;}
435
440 void setVDist(Units::Distance newVDist) {m_vDist = newVDist;}
441
442
443 //
444 // Constants
445 //
446
453
460
461signals:
464
467
470
473
476
479
481 void IDChanged();
482
485
488
491
492
493protected:
494 // Setter function for the property valid. This function is virtual and must not be
495 // called or accessed from the constructor. For this reason, we have a special function
496 // "dispatchUpdateValid", which whose address is already known to the constructor.
497 virtual void updateValid();
498 void dispatchUpdateValid();
499 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(Traffic::TrafficFactor_Abstract, bool, m_valid, false, &Traffic::TrafficFactor_Abstract::validChanged);
500
501 // Setter function for the property "description". This function is virtual and must not be
502 // called or accessed from the constructor. For this reason, we have a special function
503 // "dispatchUpdateDescription", which whose address is already known to the constructor.
504 virtual void updateDescription();
505 void dispatchUpdateDescription();
506 QString m_description;
507
508private:
509 Q_DISABLE_COPY_MOVE(TrafficFactor_Abstract)
510
511 //
512 // Property values
513 //
514 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(Traffic::TrafficFactor_Abstract, int, m_alarmLevel, 0, &Traffic::TrafficFactor_Abstract::alarmLevelChanged);
515 bool m_animate {false};
516 QString m_callSign;
517 QString m_color{QStringLiteral("red")};
519 QString m_ID;
520 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(Traffic::TrafficFactor_Abstract, Traffic::AircraftType, m_type, unknown, &Traffic::TrafficFactor_Abstract::typeChanged);
521 QProperty<bool> m_relevant {false};
522 QProperty<QString> m_relevantString;
523 QProperty<QString> m_typeString;
524 Q_OBJECT_BINDABLE_PROPERTY(Traffic::TrafficFactor_Abstract, Units::Distance, m_vDist, &Traffic::TrafficFactor_Abstract::vDistChanged);
525
526 // Timer for timeout. Traffic objects become invalid if their data has not been
527 // refreshed for longer than timeout.
528 QTimer lifeTimeCounter;
529};
530
531} // namespace Traffic
Abstract base class for traffic factors.
QString typeString() const
Getter method for property with the same name.
void typeChanged()
Notifier signal.
QBindable< QString > bindableTypeString()
Getter method for property with the same name.
QString ID
Identifier string of the traffic.
Units::Distance vDist
Vertical distance from own position to the traffic, at the time of report.
void setCallSign(const QString &newCallSign)
Setter function for property with the same name.
auto description() const -> QString
Getter method for property with the same name.
void callSignChanged()
Notifier signal.
auto ID() const -> QString
Getter method for property with the same name.
QString description
Description of the traffic, for use in GUI.
auto hasHigherPriorityThan(const TrafficFactor_Abstract &rhs) const -> bool
Estimates if this traffic object has higher priority than other traffic object.
auto color() const -> QString
Getter method for property with the same name.
void startLiveTime()
Starts or extends the lifetime of this object.
void setVDist(Units::Distance newVDist)
Setter function for property with the same name.
void alarmLevelChanged()
Notifier signal.
void vDistChanged()
Notifier signal.
static constexpr auto lifeTime
Length of lifetime for objects of this class.
bool relevant
Indicates relevant traffic.
QString relevantString() const
Getter method for property with the same name.
auto callSign() const -> QString
Getter method for property with the same name.
AircraftType type
Type of aircraft, as reported by the traffic receiver.
QString relevantString
Translated string containing the 'relevant' property.
void colorChanged()
Notifier signal.
void setHDist(Units::Distance newHDist)
Setter function for property with the same name.
void setAlarmLevel(int newAlarmLevel)
Setter function for property with the same name.
bool relevant() const
Getter method for property with the same name.
QBindable< Units::Distance > bindableVDist()
Getter method for property with the same name.
Units::Distance hDist
Horizontal distance from own position to the traffic, at the time of report.
void animateChanged()
Notifier signal.
QString color
Suggested color for GUI representation of the traffic.
Units::Distance hDist() const
Getter method for property with the same name.
void setAnimate(bool newAnimate)
Setter function for property with the same name.
QBindable< bool > bindableValid()
Getter method for property with the same name.
QBindable< bool > bindableRelevantString()
Getter method for property with the same name.
QBindable< Units::Distance > bindableHDist()
Getter method for property with the same name.
QBindable< int > bindableAlarmLevel()
Getter method for property with the same name.
static constexpr Units::Distance maxVerticalDistance
Maximal vertical distance for relevant traffic.
QBindable< bool > bindableRelevant()
Getter method for property with the same name.
QBindable< Traffic::AircraftType > bindableType()
Getter method for property with the same name.
QString typeString
Type of aircraft, as reported by the traffic receiver.
TrafficFactor_Abstract(QObject *parent=nullptr)
Default constructor.
void descriptionChanged()
Notifier signal.
void validChanged()
Notifier signal.
void setType(Traffic::AircraftType newType)
Setter function for property with the same name.
static constexpr Units::Distance maxHorizontalDistance
Maximal horizontal distance for relevant traffic.
Units::Distance vDist() const
Getter method for property with the same name.
void hDistChanged()
Notifier signal.
bool valid() const
Getter method for property with the same name.
auto animate() const -> bool
Getter method for property with the same name.
Traffic::AircraftType type() const
Getter method for property with the same name.
void copyFrom(const TrafficFactor_Abstract &other)
Copy data from other object.
bool animate
Indicates if changes in properties should be animated in the GUI.
void setID(const QString &newID)
Setter function for property with the same name.
void IDChanged()
Notifier signal.
Convenience class for distance computations.
Definition Distance.h:35
static Q_INVOKABLE constexpr Units::Distance fromNM(double distanceInNM)
Constructs a distance.
Definition Distance.h:82
static Q_INVOKABLE constexpr Units::Distance fromM(double distanceInM)
Constructs a distance.
Definition Distance.h:56
Conversion between units used in aviation.
Definition Angle.h:34