27#include <simgear/constants.h>
29#include <QIntValidator>
30#include <QDoubleValidator>
33#include <QGuiApplication>
40 UnitData(
const char* sn,
const char* ln, QString metrics,
bool pfx =
false) :
41 shortName(sn), longName(ln),
42 maxTextForMetrics(metrics),
45 UnitData(
const char* sn,
const char* ln,
48 double min,
double max,
52 shortName(sn), longName(ln),
53 maxTextForMetrics(metrics),
56 minValue(
min), maxValue(max), stepSize(step),
60 const char* shortName;
62 QString maxTextForMetrics;
63 bool isPrefix =
false;
64 bool valueWraps =
false;
65 double minValue = 0.0;
66 double maxValue = 9999999.0;
67 double stepSize = 1.0;
71std::vector<UnitData> static_unitData = {
73 { QT_TRANSLATE_NOOP(
"UnitsModel",
"ft"), QT_TRANSLATE_NOOP(
"UnitsModel",
"feet above sea-level (MSL)"),
"000000",
false, -2000, 180000, 50},
74 { QT_TRANSLATE_NOOP(
"UnitsModel",
"ft AGL"), QT_TRANSLATE_NOOP(
"UnitsModel",
"feet above ground level (AGL)"),
"000000",
false, 0, 180000, 50},
75 { QT_TRANSLATE_NOOP(
"UnitsModel",
"ft above field"), QT_TRANSLATE_NOOP(
"UnitsModel",
"feet above airfield"),
"000000",
false, 0, 180000, 50},
76 { QT_TRANSLATE_NOOP(
"UnitsModel",
"FL"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Flight-level"),
"000",
true , 0.0, 500.0, 5.0},
77 { QT_TRANSLATE_NOOP(
"UnitsModel",
"m"), QT_TRANSLATE_NOOP(
"UnitsModel",
"meters above sea-level (MSL)"),
"000000",
false, -500, 100000, 50},
78 { QT_TRANSLATE_NOOP(
"UnitsModel",
"kts"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Knots"),
"9999",
false, 0, 999999, 10.0},
79 { QT_TRANSLATE_NOOP(
"UnitsModel",
"M"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Mach"),
"00.000",
true , 0.0, 99.0, 0.05,
false , 3 },
80 { QT_TRANSLATE_NOOP(
"UnitsModel",
"KM/H"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Kilometers/hour"),
"9999",
false, 0, 999999, 10.0},
81 { QT_TRANSLATE_NOOP(
"UnitsModel",
"°True"), QT_TRANSLATE_NOOP(
"UnitsModel",
"degrees true"),
"000",
false, 0, 359, 5.0,
true },
82 { QT_TRANSLATE_NOOP(
"UnitsModel",
"°Mag"), QT_TRANSLATE_NOOP(
"UnitsModel",
"degrees magnetic"),
"000",
false, 0, 359, 5.0,
true },
83 { QT_TRANSLATE_NOOP(
"UnitsModel",
"UTC"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Universal coordinated time"),
""},
84 { QT_TRANSLATE_NOOP(
"UnitsModel",
"Local"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Local time"),
""},
85 { QT_TRANSLATE_NOOP(
"UnitsModel",
"Nm"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Nautical miles"),
"00000",
false, 0, 999999, 1.0,
false , 1 },
86 { QT_TRANSLATE_NOOP(
"UnitsModel",
"Km"), QT_TRANSLATE_NOOP(
"UnitsModel",
"Kilometers"),
"00000",
false, 0, 999999, 1.0,
false , 1 },
88 { QT_TRANSLATE_NOOP(
"UnitsModel",
"MHz"), QT_TRANSLATE_NOOP(
"UnitsModel",
"MHz"),
"00000",
false, 105, 140, 0.025,
false , 3 },
89 { QT_TRANSLATE_NOOP(
"UnitsModel",
"kHz"), QT_TRANSLATE_NOOP(
"UnitsModel",
"kHz"),
"00000",
false, 200, 400, 1.0,
false , 0 }
94std::vector<UnitsModel::UnitVec> static_modeData = {
107const int UnitLongNameRole = Qt::UserRole + 1;
108const int UnitIsPrefixRole = Qt::UserRole + 2;
109const int UnitMinValueRole = Qt::UserRole + 3;
110const int UnitMaxValueRole = Qt::UserRole + 4;
111const int UnitStepSizeRole = Qt::UserRole + 5;
112const int UnitDecimalsRole = Qt::UserRole + 6;
113const int UnitValueWrapsRole = Qt::UserRole + 7;
121 m_enabledUnits = static_modeData.at(m_mode);
126 return static_cast<int>(m_enabledUnits.size());
131 int row = index.row();
132 if ((row < 0) || (
static_cast<size_t>(row) >= m_enabledUnits.size()))
136 const UnitData& ud = static_unitData.at(u);
139 case Qt::DisplayRole:
140 return qApp->translate(
"UnitsModel", ud.shortName);
142 case UnitLongNameRole:
143 return qApp->translate(
"UnitsModel", ud.longName);
145 case UnitIsPrefixRole:
return ud.isPrefix;
146 case UnitMinValueRole:
return ud.minValue;
147 case UnitMaxValueRole:
return ud.maxValue;
148 case UnitStepSizeRole:
return ud.stepSize;
149 case UnitValueWrapsRole:
return ud.valueWraps;
150 case UnitDecimalsRole:
return ud.decimals;
160 const auto u = m_enabledUnits.at(m_activeIndex);
161 const UnitData& ud = static_unitData.at(u);
162 if (ud.decimals > 0) {
163 return new QDoubleValidator(ud.minValue, ud.maxValue, ud.decimals);
170 return new QIntValidator(
static_cast<int>(ud.minValue),
171 static_cast<int>(ud.maxValue));
176 const auto u = m_enabledUnits.at(m_activeIndex);
177 const UnitData& ud = static_unitData.at(u);
178 return ud.maxTextForMetrics;
183 const auto u = m_enabledUnits.at(m_activeIndex);
184 const UnitData& ud = static_unitData.at(u);
190 const auto u = m_enabledUnits.at(m_activeIndex);
191 const UnitData& ud = static_unitData.at(u);
192 return ud.valueWraps;
197 const auto u = m_enabledUnits.at(m_activeIndex);
198 const UnitData& ud = static_unitData.at(u);
199 return qApp->translate(
"UnitsModel",ud.shortName);
204 return m_enabledUnits.at(m_activeIndex);
209 return static_cast<int>(m_enabledUnits.size());
214 auto it = std::find(m_enabledUnits.begin(), m_enabledUnits.end(), unit);
215 return it != m_enabledUnits.end();
220 QHash<int, QByteArray> result = QAbstractListModel::roleNames();
222 result[Qt::DisplayRole] =
"shortName";
223 result[UnitLongNameRole] =
"longName";
224 result[UnitIsPrefixRole] =
"isPrefix";
225 result[UnitValueWrapsRole] =
"valueDoesWrap";
226 result[UnitMinValueRole] =
"minValue";
227 result[UnitMaxValueRole] =
"maxValue";
228 result[UnitStepSizeRole] =
"stepSize";
229 result[UnitDecimalsRole] =
"decimalPlaces";
236 const auto u = m_enabledUnits.at(m_activeIndex);
237 const UnitData& ud = static_unitData.at(u);
243 const auto u = m_enabledUnits.at(m_activeIndex);
244 const UnitData& ud = static_unitData.at(u);
250 const auto u = m_enabledUnits.at(m_activeIndex);
251 const UnitData& ud = static_unitData.at(u);
257 const auto u = m_enabledUnits.at(m_activeIndex);
258 const UnitData& ud = static_unitData.at(u);
271 m_enabledUnits = static_modeData.at(
mode);
289 auto it = std::find(m_enabledUnits.begin(), m_enabledUnits.end(),
static_cast<Units::Type>(u));
290 if (it == m_enabledUnits.end()) {
291 qWarning() << Q_FUNC_INFO <<
"unit" << u <<
"not enabled for mode" << m_mode;
295 auto index = std::distance(m_enabledUnits.begin(), it);
296 if (index != m_activeIndex) {
297 m_activeIndex =
static_cast<quint32
>(index);
313 value(static_cast<double>(v)),
316 assert(static_unitData.at(u).decimals == 0);
333 return {u,
value * SG_METER_TO_NM * 1000};
341 return {u,
value * SG_NM_TO_METER * 0.001};
349 return {u,
value * 100};
351 return {u,
value * SG_METER_TO_FEET};
371 return {u,
value / 667.0};
383 return {u,
value * 667.0};
385 return {u,
value * SG_KMH_TO_MPS * SG_MPS_TO_KT};
392 return {u,
value * SG_KT_TO_MPS * SG_MPS_TO_KMH};
411 return {u,
static_cast<double>(
static_cast<int>(
value / 100))};
417 qWarning() << Q_FUNC_INFO <<
"unhandled case:" << u <<
"from" <<
unit;
434 const auto& data = static_unitData.at(
unit);
435 int dp = data.decimals;
437 QString suffix = qApp->translate(
"UnitsModel", data.shortName);
439 std::swap(prefix, suffix);
442 return prefix + QString::number(
static_cast<int>(
value)) + suffix;
445 return prefix + QString::number(
value,
'f', dp) + suffix;
458 int dp = static_unitData.at(
unit).decimals;
459 const auto aInt =
static_cast<qlonglong
>(
value * pow(10, dp));
460 const auto bInt =
static_cast<qlonglong
>(v.
value * pow(10, dp));
466 return !(*
this == v);
471 out << static_cast<quint8>(value.
unit);
QDataStream & operator<<(QDataStream &out, const QuantityValue &value)
QDataStream & operator>>(QDataStream &in, QuantityValue &value)
Q_INVOKABLE QString toString() const
bool operator!=(const QuantityValue &v) const
bool operator==(const QuantityValue &v) const
QuantityValue convertToUnit(Units::Type u) const
Q_INVOKABLE bool isValid() const
int rowCount(const QModelIndex &parent) const override
Q_INVOKABLE bool isUnitInMode(int unit) const
QString maxTextForMetrics
void selectionChanged(int selectedIndex)
void setSelectedUnit(int u)
void setSelectedIndex(int selectedIndex)
QVariant data(const QModelIndex &index, int role) const override
void modeChanged(Units::Mode mode)
QHash< int, QByteArray > roleNames() const override
void setMode(Units::Mode mode)
Type
This enum stores units / types of values used in the simulator.
@ FeetAboveFieldElevation