1
0
Fork 0

Units model knows about frequency units

This commit is contained in:
James Turner 2018-08-11 22:02:19 +02:00
parent cd0b493426
commit f0c48110d3
2 changed files with 35 additions and 3 deletions

View file

@ -79,7 +79,10 @@ std::vector<UnitData> static_unitData = {
{ "UTC", "Universal coordinated time", ""},
{ "Local", "Local time", ""},
{ "Nm", "Nautical miles", "00000", false, 0, 999999, 1.0, false /* no wrap */, 1 /* decimal places */},
{ "Km", "Kilometers", "00000", false, 0, 999999, 1.0, false /* no wrap */, 1 /* decimal places */}
{ "Km", "Kilometers", "00000", false, 0, 999999, 1.0, false /* no wrap */, 1 /* decimal places */},
{ "MHz", "MHz", "00000", false, 105, 140, 0.025, false /* no wrap */, 3 /* decimal places */},
{ "KHz", "KHz", "00000", false, 200, 400, 1.0, false /* no wrap */, 0 /* decimal places */}
};
@ -295,7 +298,6 @@ QuantityValue::QuantityValue(Units::Type u, int v) :
assert(static_unitData.at(u).decimals == 0);
}
QuantityValue QuantityValue::convertToUnit(Units::Type u) const
{
// special case a no-change
@ -388,6 +390,30 @@ QuantityValue QuantityValue::convertToUnit(int u) const
return convertToUnit(static_cast<Units::Type>(u));
}
QString QuantityValue::toString() const
{
if (unit == Units::NoUnits)
return "<no unit>";
const auto& data = static_unitData.at(unit);
int dp = data.decimals;
QString prefix;
QString suffix = data.shortName;
if (data.isPrefix)
std::swap(prefix, suffix);
if (dp == 0) {
return prefix + QString::number(static_cast<int>(value)) + suffix;
}
return prefix + QString::number(value, 'f', dp) + suffix;
}
bool QuantityValue::isValid() const
{
return unit != Units::NoUnits;
}
bool QuantityValue::operator==(const QuantityValue &v) const
{
if (v.unit != unit)

View file

@ -54,7 +54,9 @@ public:
TimeUTC,
TimeLocal,
NauticalMiles,
Kilometers
Kilometers,
FreqMHz, // for VORs, LOCs
FreqKHz // for NDBs
};
enum Mode
@ -92,6 +94,10 @@ public:
Q_INVOKABLE QuantityValue convertToUnit(int u) const;
Q_INVOKABLE QString toString() const;
Q_INVOKABLE bool isValid() const;
// precision aware comparisom
bool operator==(const QuantityValue& v) const;
bool operator!=(const QuantityValue& v) const;