![]() |
ImFusion SDK 4.3
|
Configuring objects, particularly algorithms, in a generic manner is an important capability for an extensible framework. The ImFusion framework achieves this goal by providing the Configurable interface, which offers methods for saving and loading an object's state to and from generic Properties objects.
Properties objects store arbitrary key-value pairs as strings in a hierarchical fashion and allow you to save and load these properties to and from files. This is extensively used within the framework to save the state of the application, the views, and the algorithms in workspace files. When loading a workspace file, the contained objects are automatically re-created and configured to their previous state. Since a string-based property representation is used, developers are completely free to choose the format in which they store state. Common data types such as matrices, integers, and vectors are directly supported by the framework and do not require custom code.
The Properties class provides a serialization interface for different kinds of values. Each Properties object consists of a list of parameters and a list of further sub-properties. Each parameter is represented by a unique name and stores a single value. Internally, all parameter values are stored as strings but can be implicitly converted to any supported type. The following types are supported:
void
and long double
)std::unordered_set
of fundamental and string typesUse setParam and param to set and retrieve a parameter value. The name can contain any character except '/'. If a parameter with the same name already exists, its value will be overridden.
Example:
Prints:
Each parameter gets a PropertiesDetail::ParamType assigned automatically according to the template type of the last setParam call. Since this type can be overridden, it is merely descriptive: There is no guarantee that Properties::param can convert to that type. Changing the type of a parameter directly does not change its value in any way.
The types Enum
, EnumString
, and Path
are never assigned automatically, since they are specialized versions of other types: Enum
is a specialized version of Integer
, EnumString
and Path
are specialized versions of String
(see PropertiesWidget below for an example).
In general, Properties has some limited support for converting between different types. However, you should not rely on this behavior. An exception to this rule is std::string: since all values are stored as std::string internally, all values can be retrieved as strings. If a parameter cannot be converted to the requested type, param returns false. In this case, the value of the provided variable remains unchanged.
Prints:
In addition to parameters, a Properties object can also hold sub-properties (which can themselves have sub-properties, and so on). All sub-properties instances are owned by their parent Properties instance. Contrary to parameters, sub-properties names are not unique: there can be several sub-properties with the same name.
To access parameters in sub-properties directly, the path syntax can be used (see example). However, if multiple sub-properties share the same name, this syntax retrieves parameters only from the first sub-properties.
Example:
Prints:
The PropertiesWidget provides a convenient way to display and edit a Properties instance in the UI. The "Properties Widget" automatically creates appropriate Qt widgets for each parameter. For example, a parameter with ParamType::Integer will be displayed as a QSpinBox, and a ParamType::String as a QLineEdit. Further customization is possible using the ParamType and the parameter's Attributes, as show in the example below for a widget with a QSpinBox, limited to a range of 1 to 5, and an enum with the values 'One' and 'Two':
Some attributes restrict the number of possible values for a parameter (e.g., min/max for Integer). These restrictions are applied only in the PropertiesWidget and have no effect on the Properties::setParam method.
The PropertiesWidget never holds a reference to the Properties object it was configured with. This means that changes made by the user in the widget are not automatically reflected in the Properties object. To retrieve the changed properties, use the PropertiesWidget::configuration method. Be aware that this will only contain the parameters and their values, but not the ParamType and ParamAttributes of the original Properties object. The PropertiesWidget is designed with the Configurable interface in mind: the Configurable::configure method validates the given properties and updates the object's state accordingly; afterwards, Configurable::configuration returns the object's actual properties.
For example, to display the properties of an Algorithm: