swift
Loading...
Searching...
No Matches
transponder.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 <QRegularExpression>
7#include <Qt>
8
10
11SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::aviation, CTransponder)
12
13namespace swift::misc::aviation
14{
16 {
18 qRegisterMetaType<TransponderMode>();
19 qDBusRegisterMetaType<CTransponder::TransponderMode>();
20 }
21
22 CTransponder::CTransponder(int transponderCode, CTransponder::TransponderMode transponderMode)
23 : m_transponderCode(transponderCode), m_transponderMode(transponderMode)
24 {}
25
26 CTransponder::CTransponder(int transponderCode, const QString &transponderMode)
27 : m_transponderCode(transponderCode), m_transponderMode(StateStandby)
28 {
29 this->setModeAsString(transponderMode);
30 }
31
32 CTransponder::CTransponder(const QString &transponderCode, CTransponder::TransponderMode transponderMode)
33 : m_transponderCode(0), m_transponderMode(transponderMode)
34 {
35 bool ok = false;
36 m_transponderCode = transponderCode.toInt(&ok);
37 if (!ok) m_transponderCode = -1; // will cause assert / exception
38 }
39
40 CTransponder::CTransponder(const QString &transponderCode, const QString &transponderMode)
41 : m_transponderCode(0), m_transponderMode(StateStandby)
42 {
43 bool ok = false;
44 m_transponderCode = transponderCode.toInt(&ok);
45 if (!ok) m_transponderCode = -1; // will cause assert / exception
46 this->setModeAsString(transponderMode);
47 }
48
49 bool CTransponder::validValues() const { return CTransponder::isValidTransponderCode(m_transponderCode); }
50
52 {
53 switch (this->getTransponderMode())
54 {
55 case ModeA:
56 case ModeC:
57 case ModeMil1:
58 case ModeMil2:
59 case ModeMil3:
60 case ModeMil4:
61 case ModeMil5: return true;
62 case StateIdent:
63 case StateStandby:
64 default: return false;
65 }
66 }
67
69 {
70 if (this->getTransponderMode() == StateIdent || this->isInNormalSendingMode())
71 {
73 }
74 else
75 {
76 this->setTransponderMode(ModeC);
77 }
78 }
79
81 {
82 return this->getTransponderCodeFormatted().append(" ").append(this->getModeAsString());
83 }
84
86 {
87 if (modeString.startsWith("I", Qt::CaseInsensitive)) return StateIdent;
88 if (modeString.startsWith("S", Qt::CaseInsensitive)) return StateStandby;
89 if (modeString.startsWith("Mode C", Qt::CaseInsensitive)) return ModeC;
90 if (modeString.startsWith("C", Qt::CaseInsensitive)) return ModeC;
91 if (modeString.startsWith("Mode S", Qt::CaseInsensitive)) return ModeS;
92 if (modeString.contains("1", Qt::CaseInsensitive)) return ModeMil1;
93 if (modeString.contains("2", Qt::CaseInsensitive)) return ModeMil2;
94 if (modeString.contains("3", Qt::CaseInsensitive)) return ModeMil3;
95 if (modeString.contains("4", Qt::CaseInsensitive)) return ModeMil4;
96 if (modeString.contains("5", Qt::CaseInsensitive)) return ModeMil5;
97 return StateStandby;
98 }
99
101 {
102 const int m = static_cast<int>(mode);
103 if (m == m_transponderMode)
104 {
105 return false;
106 }
107 m_transponderMode = m;
108 return true;
109 }
110
112 {
113 QString f("0000");
114 f = f.append(QString::number(m_transponderCode));
115 return f.right(4);
116 }
117
119 {
121 s.append(' ').append(this->getModeAsString());
122 return s;
123 }
124
125 void CTransponder::setTransponderCode(const QString &transponderCode)
126 {
127 if (CTransponder::isValidTransponderCode(transponderCode))
128 {
129 bool ok {};
130 this->setTransponderCode(transponderCode.toInt(&ok));
131 }
132 else
133 {
134 Q_ASSERT_X(false, "CTransponder::setTransponderCode", "illegal transponder value");
135 }
136 }
137
139 {
140 if (transponderCode.isEmpty() || transponderCode.length() > 4) return false;
141 bool number {};
142 int tc = transponderCode.toInt(&number);
143 if (!number) return false;
144 if (tc < 0 || tc > 7777) return false;
145 thread_local const QRegularExpression rx("^[0-7]{1,4}$");
146 return rx.match(transponderCode).hasMatch();
147 }
148
149 bool CTransponder::isValidTransponderCode(qint32 transponderCode)
150 {
151 if (transponderCode < 0 || transponderCode > 7777) return false;
153 }
154
156 {
157 return { transponderCode, mode };
158 }
159
161 {
162 static const QString i("Ident");
163 static const QString s("Standby");
164 static const QString mc("Mode C");
165 static const QString ms("Mode S");
166 static const QString m1("Mil.Mode 1");
167 static const QString m2("Mil.Mode 2");
168 static const QString m3("Mil.Mode 3");
169 static const QString m4("Mil.Mode 4");
170 static const QString m5("Mil.Mode 5");
171
172 switch (mode)
173 {
174 case StateIdent: return i;
175 case StateStandby: return s;
176 case ModeC: return mc;
177 case ModeS: return ms;
178 case ModeMil1: return m1;
179 case ModeMil2: return m2;
180 case ModeMil3: return m3;
181 case ModeMil4: return m4;
182 case ModeMil5: return m5;
183 default: qFatal("Illegal Transponder Mode");
184 }
185 return i;
186 }
187
189 {
190 static const QString i("I");
191 static const QString s("S");
192 static const QString mc("C");
193 static const QString ms("S");
194 static const QString m1("1");
195 static const QString m2("2");
196 static const QString m3("3");
197 static const QString m4("4");
198 static const QString m5("5");
199
200 switch (mode)
201 {
202 case StateIdent: return i;
203 case StateStandby: return s;
204 case ModeC: return mc;
205 case ModeS: return ms;
206 case ModeMil1: return m1;
207 case ModeMil2: return m2;
208 case ModeMil3: return m3;
209 case ModeMil4: return m4;
210 case ModeMil5: return m5;
211 default: qFatal("Illegal Transponder Mode");
212 }
213 return i;
214 }
215
217 {
218 if (index.isMyself())
219 {
220 return QVariant::fromValue(*this);
221 }
222 const auto i = index.frontCasted<ColumnIndex>();
223 switch (i)
224 {
225 case IndexMode: return QVariant::fromValue(this->getTransponderMode());
226 case IndexModeAsString: return QVariant::fromValue(this->getModeAsString());
227 case IndexTransponderCode: return QVariant::fromValue(this->getTransponderCode());
228 case IndexTransponderCodeFormatted: return QVariant::fromValue(this->getTransponderCodeFormatted());
229 case IndexTransponderCodeAndModeFormatted:
231 default: break;
232 }
233
234 Q_ASSERT_X(false, "CTransponder", "index unknown");
235 const QString m = QString("no property, index ").append(index.toQString());
236 return QVariant::fromValue(m);
237 }
238
240 {
241 if (index.isMyself())
242 {
243 (*this) = variant.value<CTransponder>();
244 return;
245 }
246 const auto i = index.frontCasted<ColumnIndex>();
247 switch (i)
248 {
249 case IndexMode: m_transponderMode = variant.toInt(); break;
250 case IndexModeAsString: this->setTransponderMode(modeFromString(variant.toString())); break;
251 case IndexTransponderCode:
252 case IndexTransponderCodeFormatted:
253 if (variant.canConvert<int>())
254 {
255 this->setTransponderCode(variant.toInt());
256 }
257 else
258 {
259 this->setTransponderCode(variant.toString());
260 }
261 break;
262 case IndexTransponderCodeAndModeFormatted:
263 default: Q_ASSERT_X(false, "CTransponder", "index unknown"); break;
264 }
265 }
266} // namespace swift::misc::aviation
Non-owning reference to a CPropertyIndex with a subset of its features.
QString toQString(bool i18n=false) const
Cast as QString.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
QString getTransponderCodeAndModeFormatted() const
Formatted with code / mode.
static bool isValidTransponderCode(const QString &transponderCode)
Is valid transponder code?
static CTransponder getStandardTransponder(qint32 transponderCode, TransponderMode mode)
Transponder unit.
static void registerMetadata()
Register metadata.
void setModeAsString(const QString &mode)
Transponder mode as string.
Definition transponder.h:92
void setTransponderCode(int transponderCode)
Set transponder code.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
static const QString & modeAsShortString(CTransponder::TransponderMode mode)
Transponder mode as short string.
static TransponderMode modeFromString(const QString &modeString)
Mode from string.
int getTransponderCode() const
Transponder code.
bool setTransponderMode(TransponderMode mode)
Set transponder mode.
void toggleTransponderMode()
Transponder mode toggled.
QString convertToQString(bool i18n=false) const
Cast as QString.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
static const QString & modeAsString(TransponderMode mode)
Transponder mode as string.
bool validValues() const
Are set values valid?
bool isInNormalSendingMode() const
In any sending mode such as MIL1 oder ModeS.
QString getModeAsString() const
Transponder mode as string.
Definition transponder.h:77
CTransponder()
Default constructor.
Definition transponder.h:59
@ StateStandby
not a real mode, more a state
Definition transponder.h:33
@ StateIdent
not a real mode, more a state
Definition transponder.h:39
TransponderMode getTransponderMode() const
Transponder mode.
Definition transponder.h:95
QString getTransponderCodeFormatted() const
Transponder code.
static void registerMetadata()
Register metadata.
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
bool hasMatch() const const
QString & append(QChar ch)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString number(double n, char format, int precision)
QString right(qsizetype n) &&
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
CaseInsensitive
bool canConvert() const const
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
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