Enroute Flight Navigation
A navigation app for VFR pilots
TrafficFactor_Abstract.h
1/***************************************************************************
2 * Copyright (C) 2021-2026 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
28using namespace std::chrono_literals;
29
30
31namespace Traffic {
32
33// Plain-data record fed into updateFrom()/replaceBy(); defined in TrafficFactorData.h
35
50
51class TrafficFactor_Abstract : public QObject {
52 Q_OBJECT
53 QML_ELEMENT
54
55public:
77 Q_ENUM(Type) // Register the enum with Qt's meta-object system
78
79
83 explicit TrafficFactor_Abstract(QObject* parent = nullptr);
84
85 // Standard destructor
86 ~TrafficFactor_Abstract() override;
87
88
89 //
90 // Methods
91 //
92
107 [[nodiscard]] bool hasHigherPriorityThan(const TrafficFactor_Abstract& rhs) const;
108
123 [[nodiscard]] static bool isRelevant(Units::Distance hDist, Units::Distance vDist);
124
132
133
134 //
135 // PROPERTIES
136 //
137
158 Q_PROPERTY(int alarmLevel READ alarmLevel WRITE setAlarmLevel BINDABLE bindableAlarmLevel)
159
167 Q_PROPERTY(bool animate READ animate BINDABLE bindableAnimate)
168
174 Q_PROPERTY(QString callSign READ callSign WRITE setCallSign BINDABLE bindableCallSign)
175
184 Q_PROPERTY(QString color READ color BINDABLE bindableColor)
185
192 Q_PROPERTY(QString description READ description BINDABLE bindableDescription)
193
200 Q_PROPERTY(Units::Distance hDist READ hDist WRITE setHDist BINDABLE bindableHDist)
201
208 Q_PROPERTY(QString ID READ ID WRITE setID BINDABLE bindableID)
209
219 Q_PROPERTY(bool relevant READ relevant BINDABLE bindableRelevant)
220
226 Q_PROPERTY(QString relevantString READ relevantString BINDABLE bindableRelevantString)
227
229 Q_PROPERTY(Type type READ type WRITE setType BINDABLE bindableType)
230
235 Q_PROPERTY(QString typeString READ typeString BINDABLE bindableTypeString)
236
243 Q_PROPERTY(bool valid READ valid BINDABLE bindableValid)
244
251 Q_PROPERTY(Units::Distance vDist READ vDist WRITE setVDist BINDABLE bindableVDist)
252
253
254 //
255 // Getter/Setter Methods
256 //
257
262 [[nodiscard]] int alarmLevel() const {return m_alarmLevel.value();}
263
268 [[nodiscard]] QBindable<int> bindableAlarmLevel() {return &m_alarmLevel;}
269
274 [[nodiscard]] bool animate() const {return m_animate.value();}
275
280 [[nodiscard]] QBindable<bool> bindableAnimate() {return &m_animate;}
281
286 [[nodiscard]] QString callSign() const {return m_callSign.value();}
287
292 [[nodiscard]] QBindable<QString> bindableCallSign() const {return &m_callSign;}
293
298 [[nodiscard]] QString color() const {return m_color.value();}
299
304 [[nodiscard]] QBindable<QString> bindableColor() const {return &m_color;}
305
310 [[nodiscard]] QString description() const {return m_description.value();}
311
316 [[nodiscard]] QBindable<QString> bindableDescription() const {return &m_description;}
317
322 [[nodiscard]] Units::Distance hDist() const {return m_hDist.value();}
323
328 [[nodiscard]] QBindable<Units::Distance> bindableHDist() {return &m_hDist;}
329
334 [[nodiscard]] QString ID() const {return m_ID.value();}
335
340 [[nodiscard]] QBindable<QString> bindableID() const {return &m_ID;}
341
346 [[nodiscard]] bool relevant() const {return m_relevant.value();}
347
352 [[nodiscard]] QBindable<bool> bindableRelevant() {return &m_relevant;}
353
358 [[nodiscard]] QString relevantString() const {return m_relevantString.value();}
359
364 [[nodiscard]] QBindable<QString> bindableRelevantString() {return &m_relevantString;}
365
370 [[nodiscard]] TrafficFactor_Abstract::Type type() const {return m_type.value();}
371
376 [[nodiscard]] QBindable<TrafficFactor_Abstract::Type> bindableType() {return &m_type;}
377
382 [[nodiscard]] QString typeString() const {return m_typeString.value();}
383
388 [[nodiscard]] QBindable<QString> bindableTypeString() {return &m_typeString;}
389
394 [[nodiscard]] bool valid() const {return m_valid.value();}
395
400 [[nodiscard]] QBindable<bool> bindableValid() {return &m_valid;}
401
406 [[nodiscard]] Units::Distance vDist() const {return m_vDist.value();}
407
412 [[nodiscard]] QBindable<Units::Distance> bindableVDist() {return &m_vDist;}
413
414
415 //
416 // Setter Methods
417 //
418
423 void setAlarmLevel(int newAlarmLevel)
424 {
425 // Safety checks
426 if ((newAlarmLevel < 0) || (newAlarmLevel > 3)) {
427 return;
428 }
430 m_alarmLevel = newAlarmLevel;
431 }
432
437 void setCallSign(const QString& newCallSign) {m_callSign = newCallSign;}
438
443 void setHDist(Units::Distance newHDist) {m_hDist = newHDist;}
444
449 void setID(const QString& newID) {m_ID = newID;}
450
455 void setType(TrafficFactor_Abstract::Type newType) {m_type = newType;}
456
461 void setVDist(Units::Distance newVDist) {m_vDist = newVDist;}
462
463
464 //
465 // Constants
466 //
467
469 static constexpr auto lifetime = 45s;
470
480
490
491protected:
492 QProperty<bool> m_valid {false};
493
494 QProperty<QString> m_description;
495
496 // Indicates that the instance is valid as an abstract traffic factor
497 QProperty<bool> m_validAbstractTrafficFactor;
498
525 void updateFrom(const TrafficFactorData& data);
526
540 void replaceBy(const TrafficFactorData& data);
541
548 static constexpr qsizetype idMatchLength = 6;
549
560 [[nodiscard]] bool isSameFactorAs(const TrafficFactorData& data) const;
561
562private:
563 Q_DISABLE_COPY_MOVE(TrafficFactor_Abstract)
564
565 //
566 // Property values
567 //
568 QProperty<int> m_alarmLevel {0};
569 QProperty<bool> m_animate {false};
570 QProperty<QString> m_callSign;
571 QProperty<QString> m_color {QStringLiteral("red")};
572 QProperty<Units::Distance> m_hDist;
573 QProperty<QString> m_ID;
574 QProperty<Type> m_type {unknown};
575 QProperty<bool> m_relevant {false};
576 QProperty<QString> m_relevantString;
577 QProperty<QString> m_typeString;
578 QProperty<Units::Distance> m_vDist;
579
580 // Timer for timeout. Traffic objects become invalid if their data has not
581 // been refreshed for longer than "lifetime". The "valid" binding reads
582 // lifetimeCounter.isActive(); QTimer notifies that (bindable) property when
583 // the single-shot timer expires, so the binding reacts to expiry
584 // automatically.
585 QTimer lifetimeCounter;
586};
587
588} // namespace Traffic
QBindable< TrafficFactor_Abstract::Type > bindableType()
Getter method for property with the same name.
static constexpr qsizetype idMatchLength
Number of characters in ICAO HEX IDs.
QString typeString() const
Getter method for property with the same name.
QBindable< QString > bindableRelevantString()
Getter method for property with the same name.
void startLifetime()
Starts or extends the lifetime of this object.
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.
QBindable< QString > bindableDescription() const
Getter method for property with the same name.
Type type
Type of aircraft, as reported by the traffic receiver.
QString description
Description of the traffic, for use in GUI.
static constexpr auto lifetime
Length of lifetime for objects of this class.
void setVDist(Units::Distance newVDist)
Setter function for property with the same name.
QString callSign() const
Getter method for property with the same name.
bool relevant
Indicates relevant traffic.
QString relevantString() const
Getter method for property with the same name.
TrafficFactor_Abstract::Type type() const
Getter method for property with the same name.
QString description() const
Getter method for property with the same name.
QBindable< bool > bindableAnimate()
Getter method for property with the same name.
QString relevantString
Translated string containing the 'relevant' property.
QBindable< QString > bindableCallSign() const
Getter method for property with the same name.
void setHDist(Units::Distance newHDist)
Setter function for property with the same name.
QString ID() const
Getter method 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.
bool hasHigherPriorityThan(const TrafficFactor_Abstract &rhs) const
Estimates if this traffic object has higher priority than other traffic object.
QString color
Suggested color for GUI representation of the traffic.
Units::Distance hDist() const
Getter method for property with the same name.
void setType(TrafficFactor_Abstract::Type newType)
Setter function for property with the same name.
QString color() const
Getter method for property with the same name.
QBindable< QString > bindableColor() const
Getter method for property with the same name.
bool isSameFactorAs(const TrafficFactorData &data) const
Check whether a data record refers to the same factor as *this.
QBindable< bool > bindableValid()
Getter method for property with the same name.
static bool isRelevant(Units::Distance hDist, Units::Distance vDist)
Relevance criterion shared by the "relevant" property and priority logic.
void replaceBy(const TrafficFactorData &data)
Replace this object by a different traffic factor.
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.
QString typeString
Type of aircraft, as reported by the traffic receiver.
TrafficFactor_Abstract(QObject *parent=nullptr)
Default constructor.
void updateFrom(const TrafficFactorData &data)
Update this object with newer data for the same traffic factor.
static constexpr Units::Distance maxHorizontalDistance
Maximal horizontal distance for relevant traffic.
Units::Distance vDist() const
Getter method for property with the same name.
bool valid() const
Getter method for property with the same name.
QBindable< QString > bindableID() const
Getter method for property with the same name.
bool animate() const
Getter method for property with the same name.
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.
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
Plain-data record of a traffic factor.