Current “Support” of Units
In SMTK 23.01, a designer can set the units of any ValueItemDefinition which is the base class used to represent String, Integer, and DoubleItem Definitions. The units are represented as a string and is used in the UI to indicate the units the value is assumed to be specified in.
There is now a call to take this to the next level and formally support units and unit conversion.
How Units Could Be Supported
When setting the value of the DoubleItem with the current setValue(double) method, it would be assumed that the value is in the default units specified by the designer. If the value is specified with its own units then if the units are different from the default, a unit conversion would take place. In either case, the value() method would always return the value in the default units. In the case of unit conversion there would be the need to access the value the user entered as well as the units specified.
Specifying a value with units
We could support either specifying the value and units using two separate line edit widgets (line edit and combobox) or with a single line edit with a validator and completer.
Note that the second approach would require the UI to somehow display the default units (in this case I’ve added them to the label),
The UI should also provide a mechanism to display the value in the default/preferred units. This could be as simple as having over the widget and using a tooltip or providing a checkbox to display the converted form. The former would take up less space.
Note: if there are no default units specified then SMTK would assume the value is unites and no unit information would be displayed.
Integration of a unit conversion library
SMTK would delegate the task of doing the conversion to an existing Open-Source library such as:
Proposed API Changes
- Move the units specification to DoubleItemDefinition instead of ValueItemDefinition, unless folks can think of use cases where Integers and/or Strings can also benefit from having unit support.
- Add one of the two possible APIs to DoubleItem to specify a value with units:
- setValue(double val, const std::string& units)
- setValue(const std::string& valueWithUnits)
- Add one of the two possible API’s to DoubleItem to retrieve the original information:
- double value(std::string& units) const
- std::string value() const - Note that we could also use the existing valueAsString method
- Add the ability to save the original entries in the XML/JSON file formats