From f0c48110d3e3462ee3f7eeda4ba67808e70ae4c8 Mon Sep 17 00:00:00 2001 From: James Turner Date: Sat, 11 Aug 2018 22:02:19 +0200 Subject: [PATCH] Units model knows about frequency units --- src/GUI/UnitsModel.cxx | 30 ++++++++++++++++++++++++++++-- src/GUI/UnitsModel.hxx | 8 +++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/GUI/UnitsModel.cxx b/src/GUI/UnitsModel.cxx index 525babcd2..5e9930038 100644 --- a/src/GUI/UnitsModel.cxx +++ b/src/GUI/UnitsModel.cxx @@ -79,7 +79,10 @@ std::vector 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(u)); } +QString QuantityValue::toString() const +{ + if (unit == Units::NoUnits) + return ""; + + 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(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) diff --git a/src/GUI/UnitsModel.hxx b/src/GUI/UnitsModel.hxx index cfc44afcf..aed3419d2 100644 --- a/src/GUI/UnitsModel.hxx +++ b/src/GUI/UnitsModel.hxx @@ -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;