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 if (other.type() != Traffic::AircraftType::unknown)
81 {
82 setType(other.type());
83 }
84 setVDist(other.vDist());
85 updateDescription();
86 }
87
101 [[nodiscard]] auto hasHigherPriorityThan(const TrafficFactor_Abstract& rhs) const -> bool;
102
110
111
112 //
113 // PROPERTIES
114 //
115
135 Q_PROPERTY(int alarmLevel READ alarmLevel WRITE setAlarmLevel NOTIFY alarmLevelChanged BINDABLE bindableAlarmLevel)
136
137
144 Q_PROPERTY(bool animate READ animate WRITE setAnimate NOTIFY animateChanged)
145
150 Q_PROPERTY(QString callSign READ callSign WRITE setCallSign NOTIFY callSignChanged)
151
160 Q_PROPERTY(QString color READ color NOTIFY colorChanged)
161
168 Q_PROPERTY(QString description READ description NOTIFY descriptionChanged)
169
176 Q_PROPERTY(Units::Distance hDist READ hDist WRITE setHDist NOTIFY hDistChanged BINDABLE bindableHDist)
177
184 Q_PROPERTY(QString ID READ ID WRITE setID NOTIFY IDChanged)
185
191 Q_PROPERTY(bool relevant READ relevant BINDABLE bindableRelevant)
192
194 Q_PROPERTY(QString relevantString READ relevantString BINDABLE bindableRelevantString)
195
197 Q_PROPERTY(AircraftType type READ type WRITE setType NOTIFY typeChanged BINDABLE bindableType)
198
203 Q_PROPERTY(QString typeString READ typeString BINDABLE bindableTypeString)
204
210 Q_PROPERTY(bool valid READ valid NOTIFY validChanged BINDABLE bindableValid)
211
218 Q_PROPERTY(Units::Distance vDist READ vDist WRITE setVDist NOTIFY vDistChanged BINDABLE bindableVDist)
219
220
221 //
222 // Getter/Setter Methods
223 //
224
229 [[nodiscard]] int alarmLevel() const {return m_alarmLevel.value();}
230
235 [[nodiscard]] QBindable<int> bindableAlarmLevel() {return &m_alarmLevel;}
236
241 [[nodiscard]] auto animate() const -> bool
242 {
243 return m_animate;
244 }
245
250 [[nodiscard]] auto callSign() const -> QString
251 {
252 return m_callSign;
253 }
254
259 [[nodiscard]] auto color() const -> QString
260 {
261 if (m_alarmLevel == 0) {
262 return QStringLiteral("green");
263 }
264 if (m_alarmLevel == 1) {
265 return QStringLiteral("yellow");
266 }
267 return QStringLiteral("red");
268 }
269
274 [[nodiscard]] auto description() const -> QString
275 {
276 return m_description;
277 }
278
283 [[nodiscard]] Units::Distance hDist() const {return m_hDist.value();}
284
289 [[nodiscard]] QBindable<Units::Distance> bindableHDist() {return &m_hDist;}
290
295 [[nodiscard]] auto ID() const -> QString
296 {
297 return m_ID;
298 }
299
304 [[nodiscard]] bool relevant() const {return m_relevant.value();}
305
310 [[nodiscard]] QBindable<bool> bindableRelevant() {return &m_relevant;}
311
316 [[nodiscard]] QString relevantString() const {return m_relevantString.value();}
317
322 [[nodiscard]] QBindable<bool> bindableRelevantString() {return &m_relevantString;}
323
328 [[nodiscard]] Traffic::AircraftType type() const {return m_type.value();}
329
334 [[nodiscard]] QBindable<Traffic::AircraftType> bindableType() {return &m_type;}
335
340 [[nodiscard]] QString typeString() const {return m_typeString.value();}
341
346 [[nodiscard]] QBindable<QString> bindableTypeString() {return &m_typeString;}
347
352 [[nodiscard]] bool valid() const {return m_valid.value();}
353
358 [[nodiscard]] QBindable<bool> bindableValid() {return &m_valid;}
359
364 [[nodiscard]] Units::Distance vDist() const {return m_vDist.value();}
365
370 [[nodiscard]] QBindable<Units::Distance> bindableVDist() {return &m_vDist;}
371
372
373 //
374 // Setter Methods
375 //
376
381 void setAlarmLevel(int newAlarmLevel)
382 {
383 // Safety checks
384 if ((newAlarmLevel < 0) || (newAlarmLevel > 3)) {
385 return;
386 }
388 m_alarmLevel = newAlarmLevel;
389 }
390
395 void setAnimate(bool newAnimate) {
396 if (m_animate == newAnimate) {
397 return;
398 }
399 m_animate = newAnimate;
400 emit animateChanged();
401 }
402
407 void setCallSign(const QString& newCallSign) {
408 if (m_callSign == newCallSign) {
409 return;
410 }
411 m_callSign = newCallSign;
412 emit callSignChanged();
413 }
414
419 void setHDist(Units::Distance newHDist) {m_hDist = newHDist;}
420
425 void setID(const QString& newID) {
426 if (m_ID == newID) {
427 return;
428 }
429 m_ID = newID;
430 emit IDChanged();
431 }
432
437 void setType(Traffic::AircraftType newType) {m_type = newType;}
438
443 void setVDist(Units::Distance newVDist) {m_vDist = newVDist;}
444
445
446 //
447 // Constants
448 //
449
456
463
464signals:
467
470
473
476
479
482
484 void IDChanged();
485
488
491
494
495
496protected:
497 // Setter function for the property valid. This function is virtual and must not be
498 // called or accessed from the constructor. For this reason, we have a special function
499 // "dispatchUpdateValid", which whose address is already known to the constructor.
500 virtual void updateValid();
501 void dispatchUpdateValid();
502 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(Traffic::TrafficFactor_Abstract, bool, m_valid, false, &Traffic::TrafficFactor_Abstract::validChanged);
503
504 // Setter function for the property "description". This function is virtual and must not be
505 // called or accessed from the constructor. For this reason, we have a special function
506 // "dispatchUpdateDescription", which whose address is already known to the constructor.
507 virtual void updateDescription();
508 void dispatchUpdateDescription();
509 QString m_description;
510
511private:
512 Q_DISABLE_COPY_MOVE(TrafficFactor_Abstract)
513
514 //
515 // Property values
516 //
517 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(Traffic::TrafficFactor_Abstract, int, m_alarmLevel, 0, &Traffic::TrafficFactor_Abstract::alarmLevelChanged);
518 bool m_animate {false};
519 QString m_callSign;
520 QString m_color{QStringLiteral("red")};
522 QString m_ID;
523 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(Traffic::TrafficFactor_Abstract, Traffic::AircraftType, m_type, unknown, &Traffic::TrafficFactor_Abstract::typeChanged);
524 QProperty<bool> m_relevant {false};
525 QProperty<QString> m_relevantString;
526 QProperty<QString> m_typeString;
527 Q_OBJECT_BINDABLE_PROPERTY(Traffic::TrafficFactor_Abstract, Units::Distance, m_vDist, &Traffic::TrafficFactor_Abstract::vDistChanged);
528
529 // Timer for timeout. Traffic objects become invalid if their data has not been
530 // refreshed for longer than timeout.
531 QTimer lifeTimeCounter;
532};
533
534} // 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