diff --git a/GUI/Models/DoubleDescriptor.h b/GUI/Models/DoubleDescriptor.h index 80fc254783d314a890b17603690287f944b08bcc..9a6f2cfa381efe40d85d8a1ae9b59be58f6d52dd 100644 --- a/GUI/Models/DoubleDescriptor.h +++ b/GUI/Models/DoubleDescriptor.h @@ -76,7 +76,7 @@ public: RealLimits limits; //!< Limits of the value. function<void(double)> set = nullptr; //!< function to set the value function<double()> get = nullptr; //!< function to get the current value - variant<QString, Unit> unit = Unit::undefined; //!< Unit of the value (internal unit only!) + variant<QString, Unit> unit = Unit::unitless; //!< Unit of the value (internal unit only!) function<QString()> path = nullptr; //<! Path describing this value. Used e.g. for undo/redo }; diff --git a/GUI/Models/UIntDescriptor.h b/GUI/Models/UIntDescriptor.h index b7ea127b8f87b2b807e047cb2c28d0d7d76d7715..e0433fde72d575134bda90829358ba0fc198a610 100644 --- a/GUI/Models/UIntDescriptor.h +++ b/GUI/Models/UIntDescriptor.h @@ -60,12 +60,12 @@ public: //! Return the current value of the handled parameter. operator uint() const; - QString label; //!< A label text (short, no trailing colon) - QString tooltip; //!< Tooltip text - RealLimits limits; //!< Limits of the value. - function<void(uint)> set = nullptr; //!< function to set the value - function<uint()> get = nullptr; //!< function to get the current value - variant<QString, Unit> unit = Unit::undefined; //!< Unit of the value (internal unit only!) + QString label; //!< A label text (short, no trailing colon) + QString tooltip; //!< Tooltip text + RealLimits limits; //!< Limits of the value. + function<void(uint)> set = nullptr; //!< function to set the value + function<uint()> get = nullptr; //!< function to get the current value + variant<QString, Unit> unit = Unit::unitless; //!< Unit of the value (internal unit only!) function<QString()> path = nullptr; //<! Path describing this value. Used e.g. for undo/redo }; diff --git a/GUI/Models/Unit.cpp b/GUI/Models/Unit.cpp index 87bf0902c54c8bf4e2c737a445b15b9cad2a3cf9..e49eab8d544d687fb0cc4ecb907f8cf2559dcb54 100644 --- a/GUI/Models/Unit.cpp +++ b/GUI/Models/Unit.cpp @@ -15,10 +15,11 @@ #include "GUI/Models/Unit.h" #include "Base/Const/Units.h" #include "Base/Utils/Assert.h" +#include <QString> double convert(double d, Unit from, Unit to) { - if (from == Unit::undefined || to == Unit::undefined || from == to) + if (from == to) return d; if (from == Unit::angstrom && to == Unit::nanometer) @@ -45,6 +46,41 @@ double convert(double d, Unit from, Unit to) if (from == Unit::degree && to == Unit::radiant) return Units::deg2rad(d); + if (from == Unit::other || to == Unit::other) { + ASSERT(false); // no conversion possible + return d; + } + ASSERT(false); // no conversion implemented return d; } + +QString unitAsString(const Unit& unit) +{ + switch (unit) { + case Unit::unitless: + return ""; + case Unit::nanometer: + return "nm"; + case Unit::nanometerPower2: + return "nm²"; + case Unit::nanometerPowerMinus2: + return "1/nm²"; + case Unit::angstrom: + return "\303\205"; + case Unit::angstromPower2: + return "\303\205²"; + case Unit::angstromPowerMinus2: + return "1/\303\205²"; + case Unit::degree: + return "°"; + case Unit::radiant: + return "rad"; + case Unit::other: + ASSERT(false); // this function should not be called for Unit::other + return ""; + default: + ASSERT(false); // no string implemented + return ""; + } +} diff --git a/GUI/Models/Unit.h b/GUI/Models/Unit.h index 661f6aa0a898810e67b53ac77b3c7b36f75d1bb7..597935db447756e8f0a5db7f6bbe5038edce4b2b 100644 --- a/GUI/Models/Unit.h +++ b/GUI/Models/Unit.h @@ -15,13 +15,15 @@ #ifndef BORNAGAIN_GUI_MODELS_UNIT_H #define BORNAGAIN_GUI_MODELS_UNIT_H +class QString; + //! Defines units, mainly to be able to convert between units. +//! //! E.g. internal unit is nanometer, displayed unit is angstrom. //! Units which do not support conversion do not have to be //! part of the enum, since the relevant code parts support defining a //! unit via enum or via string enum class Unit { - undefined, unitless, nanometer, nanometerPower2, @@ -30,11 +32,15 @@ enum class Unit { angstromPower2, angstromPowerMinus2, degree, - radiant + radiant, + other //!< The unit has no enum value defined in here (e.g. when defined as an explicit string) }; //! Convert the given value d from unit "from" to unit "to" //! Throws an exception if no conversion possible. double convert(double d, Unit from, Unit to); +//! Returns the string for the given unit +QString unitAsString(const Unit& unit); + #endif // BORNAGAIN_GUI_MODELS_UNIT_H