swift
Loading...
Searching...
No Matches
modulator.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 <type_traits>
7
8#include <QtGlobal>
9
13#include "misc/pq/units.h"
15
18
19namespace swift::misc::aviation
20{
21 template <class AVIO>
23 {
24 const CFrequency a = m_frequencyActive;
25 m_frequencyActive = m_frequencyStandby;
26 m_frequencyStandby = a;
27 }
28
29 template <class AVIO>
31 {
32 return m_frequencyActive;
33 }
34
35 template <class AVIO>
37 {
38 return m_frequencyStandby;
39 }
40
41 template <class AVIO>
43 {
44 m_frequencyActive = frequency;
45 }
46
47 template <class AVIO>
49 {
50 m_frequencyStandby = frequency;
51 }
52
53 template <class AVIO>
55 {
56 return m_volumeReceive;
57 }
58
59 template <class AVIO>
61 {
62 return m_volumeTransmit;
63 }
64
65 template <class AVIO>
67 {
68 if (volume >= 100)
69 {
70 m_volumeReceive = 100;
71 }
72 else if (volume <= 0)
73 {
74 m_volumeReceive = 0;
75 }
76 else
77 {
78 m_volumeReceive = volume;
79 }
80 }
81
82 template <class AVIO>
84 {
85 if (volume >= 100)
86 {
87 m_volumeTransmit = 100;
88 }
89 else if (volume <= 0)
90 {
91 m_volumeTransmit = 0;
92 }
93 else
94 {
95 m_volumeTransmit = volume;
96 }
97 }
98
99 template <class AVIO>
101 {
102 return m_transmitEnabled;
103 }
104
105 template <class AVIO>
107 {
108 return m_receiveEnabled;
109 }
110
111 template <class AVIO>
113 {
114 m_transmitEnabled = enable;
115 }
116
117 template <class AVIO>
119 {
120 m_receiveEnabled = enable;
121 }
122
123 template <class AVIO>
125 {
126 return m_name;
127 }
128
129 template <class AVIO>
131 {
132 if (index.isMyself())
133 {
134 return QVariant::fromValue(*derived());
135 }
136 const auto i = index.frontCasted<ColumnIndex>();
137 switch (i)
138 {
139 case IndexActiveFrequency: return this->getFrequencyActive().propertyByIndex(index.copyFrontRemoved());
140 case IndexStandbyFrequency: return this->getFrequencyStandby().propertyByIndex(index.copyFrontRemoved());
141 case IndexEnabledTransmit: return QVariant::fromValue(this->isTransmitEnabled());
142 case IndexEnabledReceive: return QVariant::fromValue(this->isReceiveEnabled());
143 case IndexTransmitVolume: return QVariant::fromValue(this->getVolumeTransmit());
144 case IndexReceiveVolume: return QVariant::fromValue(this->getVolumeReceive());
146 }
147 }
148
149 template <class AVIO>
151 {
152 if (index.isMyself())
153 {
154 Q_ASSERT_X(false, Q_FUNC_INFO, "Wrong index to base template");
155 return;
156 }
157 const auto i = index.frontCasted<ColumnIndex>();
158 switch (i)
159 {
160 case IndexActiveFrequency: m_frequencyActive.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
161 case IndexStandbyFrequency: m_frequencyStandby.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
162 case IndexEnabledTransmit: this->setTransmitEnabled(variant.toBool()); break;
163 case IndexEnabledReceive: this->setReceiveEnabled(variant.toBool()); break;
164 case IndexTransmitVolume: this->setVolumeTransmit(variant.toInt()); break;
165 case IndexReceiveVolume: this->setVolumeReceive(variant.toInt()); break;
166 default: CValueObject<CModulator<AVIO>>::setPropertyByIndex(index, variant); break;
167 }
168 }
169
170 template <class AVIO>
171 int CModulator<AVIO>::comparePropertyByIndex(CPropertyIndexRef index, const AVIO &compareValue) const
172 {
173 if (index.isMyself())
174 {
175 return m_frequencyActive.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.m_frequencyActive);
176 }
177 const auto i = index.frontCasted<ColumnIndex>();
178 switch (i)
179 {
180 case IndexActiveFrequency:
181 return m_frequencyActive.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.m_frequencyActive);
182 case IndexStandbyFrequency:
183 return m_frequencyStandby.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.m_frequencyStandby);
184 case IndexEnabledTransmit: return Compare::compare(this->isTransmitEnabled(), compareValue.isTransmitEnabled());
185 case IndexEnabledReceive: return Compare::compare(this->isReceiveEnabled(), compareValue.isReceiveEnabled());
186 case IndexTransmitVolume: return Compare::compare(this->getVolumeTransmit(), compareValue.getVolumeTransmit());
187 case IndexReceiveVolume: return Compare::compare(this->getVolumeReceive(), compareValue.getVolumeReceive());
188 default: break;
189 }
190 Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
191 return 0;
192 }
193
194 template <class AVIO>
195 CModulator<AVIO>::CModulator() : m_name("default")
196 {
197 static_assert(!std::is_polymorphic_v<AVIO>, "Must not use virtual functions for value classes");
198 }
199
200 template <class AVIO>
203 const swift::misc::physical_quantities::CFrequency &standbyFrequency)
204 : m_name(name), m_frequencyActive(activeFrequency), m_frequencyStandby(standbyFrequency)
205 {
206 static_assert(!std::is_polymorphic_v<AVIO>, "Must not use virtual functions for value classes");
207 }
208
209 template <class AVIO>
211 {
212 QString s(this->getName());
213 s.append(" Active: ").append(m_frequencyActive.valueRoundedWithUnit(3, i18n));
214 s.append(" Standby: ").append(m_frequencyStandby.valueRoundedWithUnit(3, i18n));
215 return s;
216 }
217
218 template <class AVIO>
224
225 template <class AVIO>
231
232 template <class AVIO>
234 {
235 static QString n("COM1");
236 return n;
237 }
238
239 template <class AVIO>
241 {
242 static QString n("COM2");
243 return n;
244 }
245
246 template <class AVIO>
248 {
249 static QString n("COM3");
250 return n;
251 }
252
253 template <class AVIO>
255 {
256 static QString n("NAV1");
257 return n;
258 }
259
260 template <class AVIO>
262 {
263 static QString n("NAV2");
264 return n;
265 }
266
267 template <class AVIO>
269 {
270 static QString n("NAV3");
271 return n;
272 }
273
274 // see here for the reason of the forward instantiations
275 // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
277 template class CModulator<CComSystem>;
278 template class CModulator<CNavSystem>;
280
281} // namespace swift::misc::aviation
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Base class for COM, NAV, Squawk units.
Definition modulator.h:17
bool isTransmitEnabled() const
Enabled?
static const QString & NameNav2()
NAV2.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
void setVolumeReceive(int volume)
Output volume 0.100.
Definition modulator.cpp:66
void setFrequencyStandbyKHz(double frequencyKHz)
Set standby frequency.
static const QString & NameCom3()
COM3.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
int getVolumeTransmit() const
Input volume 0..100.
Definition modulator.cpp:60
void setTransmitEnabled(bool enable)
Enabled?
swift::misc::physical_quantities::CFrequency getFrequencyStandby() const
Standby frequency.
Definition modulator.cpp:36
int getVolumeReceive() const
Output volume 0..100.
Definition modulator.cpp:54
static const QString & NameNav3()
NAV3.
static const QString & NameCom1()
COM1.
void setVolumeTransmit(int volume)
Input volume 0..100.
Definition modulator.cpp:83
static const QString & NameNav1()
NAV1.
bool isReceiveEnabled() const
Enabled?
void setFrequencyStandby(const swift::misc::physical_quantities::CFrequency &frequency)
Set standby frequency.
Definition modulator.cpp:48
QString convertToQString(bool i18n=false) const
Cast as QString.
swift::misc::physical_quantities::CFrequency getFrequencyActive() const
Active frequency.
Definition modulator.cpp:30
int comparePropertyByIndex(swift::misc::CPropertyIndexRef index, const AVIO &compareValue) const
Compare by property index.
void toggleActiveStandby()
Toggle active and standby frequencies.
Definition modulator.cpp:22
CModulator()
Default constructor.
void setFrequencyActive(const swift::misc::physical_quantities::CFrequency &frequency)
Set active frequency.
Definition modulator.cpp:42
void setReceiveEnabled(bool enable)
Enabled?
static const QString & NameCom2()
COM2.
void setFrequencyActiveKHz(double frequencyKHz)
Set active frequency.
Specialized class for frequency (hertz, mega hertz, kilo hertz).
Definition units.h:334
static CFrequencyUnit kHz()
Kilohertz.
Definition units.h:371
int compare(T a, T b)
Compare arithmetic values.
QString & append(QChar ch)
QVariant fromValue(T &&value)
bool toBool() const const
int toInt(bool *ok) const const