Skip to content
Snippets Groups Projects
Commit 6ddf8487 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

refactor unit: remove "undefined", add "other" for string-defined units

parent 60b2bbff
No related branches found
No related tags found
1 merge request!412More preparations for layer oriented sample editor
......@@ -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
};
......
......@@ -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
};
......
......@@ -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 "";
}
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment