Refactoring the range into min_value and max_value attributes without action and resolution.

This commit is contained in:
Matthias Koefferlein 2024-03-09 16:14:50 +01:00
parent d811474d8f
commit ce13991542
5 changed files with 110 additions and 88 deletions

View File

@ -70,7 +70,7 @@ public:
* @brief The default constructor
*/
PCellParameterDeclaration ()
: m_hidden (false), m_readonly (false), m_type (t_none), m_range()
: m_hidden (false), m_readonly (false), m_type (t_none)
{
// .. nothing yet ..
}
@ -79,7 +79,7 @@ public:
* @brief The constructor with a name
*/
PCellParameterDeclaration (const std::string &name)
: m_hidden (false), m_readonly (false), m_type (t_none), m_name (name), m_range()
: m_hidden (false), m_readonly (false), m_type (t_none), m_name (name)
{
// .. nothing yet ..
}
@ -88,7 +88,7 @@ public:
* @brief The constructor with a name, type and description
*/
PCellParameterDeclaration (const std::string &name, type t, const std::string &description)
: m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description), m_range()
: m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description)
{
// .. nothing yet ..
}
@ -97,7 +97,7 @@ public:
* @brief The constructor with a name, type, description and default value
*/
PCellParameterDeclaration (const std::string &name, type t, const std::string &description, const tl::Variant &def)
: m_default (def), m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description), m_range()
: m_default (def), m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description)
{
// .. nothing yet ..
}
@ -106,7 +106,7 @@ public:
* @brief The constructor with a name, type, description, default value and unit
*/
PCellParameterDeclaration (const std::string &name, type t, const std::string &description, const tl::Variant &def, const std::string &unit)
: m_default (def), m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description), m_unit (unit), m_range()
: m_default (def), m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description), m_unit (unit)
{
// .. nothing yet ..
}
@ -115,7 +115,7 @@ public:
* @brief The constructor with a name, type and description and choice values / choice descriptions
*/
PCellParameterDeclaration (const std::string &name, type t, const std::string &description, const std::vector<tl::Variant> &choices, const std::vector<std::string> &choice_descriptions)
: m_choices (choices), m_choice_descriptions (choice_descriptions), m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description), m_range()
: m_choices (choices), m_choice_descriptions (choice_descriptions), m_hidden (false), m_readonly (false), m_type (t), m_name (name), m_description (description)
{
// .. nothing yet ..
}
@ -249,53 +249,6 @@ public:
}
/**
* @brief A enum describing the action of range violated
*/
enum Action {
t_Reject = 1, // reject the parameter
t_Accept, // accept the parameter
t_Use_Default // use a default parameter (currently not supported)
};
void set_range (const tl::Variant& low, const tl::Variant& high, const tl::Variant& resolution, Action action = t_Reject)
{
m_range = Range(low, high, resolution, action);
}
typedef struct _Range {
_Range() :
m_low(),
m_high(),
m_resolution(),
m_action(t_Reject) {}
_Range(const tl::Variant& low, const tl::Variant& high, const tl::Variant& resolution, Action action = t_Reject) :
m_low(low),
m_high(high),
m_resolution(resolution),
m_action(action) {}
bool operator== (const _Range& other) const
{
return
m_low == other.m_low &&
m_high == other.m_high &&
m_resolution == other.m_resolution &&
m_action == other.m_action;
}
tl::Variant m_low;
tl::Variant m_high;
tl::Variant m_resolution;
Action m_action;
} Range;
const tl::optional<Range>& get_range() const
{
return m_range;
}
/**
* @brief Getter for the choice descriptions
*
* The choice descriptions correspond to choice values. The descriptions
@ -315,6 +268,56 @@ public:
m_choice_descriptions = choice_descriptions;
}
/**
* @brief Sets the minimum value
*
* The minimum value is a visual feature and limits the allowed values for numerical
* entry boxes. This applies to parameters of type int or double. The minimum value
* is not effective if choices are present.
*
* The minimum value is not enforced - for example there is no restriction implemented
* when setting values programmatically.
*
* Setting this attribute to "nil" (the default) implies "no limit".
*/
void set_min_value (const tl::Variant &min)
{
m_min_value = min;
}
/**
* @brief Gets the minimum value (see \set_min_value)
*/
const tl::Variant &min_value () const
{
return m_min_value;
}
/**
* @brief Sets the maximum value
*
* The maximum value is a visual feature and limits the allowed values for numerical
* entry boxes. This applies to parameters of type int or double. The maximum value
* is not effective if choices are present.
*
* The maximum value is not enforced - for example there is no restriction implemented
* when setting values programmatically.
*
* Setting this attribute to "nil" (the default) implies "no limit".
*/
void set_max_value (const tl::Variant &max)
{
m_max_value = max;
}
/**
* @brief Gets the maximum value (see \set_max_value)
*/
const tl::Variant &max_value () const
{
return m_max_value;
}
/**
* @brief Equality
*/
@ -329,7 +332,8 @@ public:
m_name == d.m_name &&
m_description == d.m_description &&
m_unit == d.m_unit &&
m_range == d.m_range;
m_min_value == d.m_min_value &&
m_max_value == d.m_max_value;
}
private:
@ -340,7 +344,7 @@ private:
type m_type;
std::string m_name;
std::string m_description, m_unit;
tl::optional<Range> m_range;
tl::Variant m_min_value, m_max_value;
};
/**

View File

@ -921,17 +921,43 @@ Class<db::PCellParameterDeclaration> decl_PCellParameterDeclaration ("db", "PCel
"entry field in the parameter user interface.\n"
"If a range is already set for this parameter the choice will not be added and a warning message is showed.\n"
) +
gsi::method_ext ("set_range", &set_range, gsi::arg ("low"), gsi::arg ("high"), gsi::arg ("resolution"), gsi::arg ("action"),
"@brief Set a range constraint\n"
"This method will set a range constraint to the parameter with 'low' and 'high' as minimum and maximum value.\n"
"This range constraint will only be set if the parameter-, the low- and the high-type are numeric.\n"
"If a choice is already set for this parameter the range will not be set and a warning message is showed.\n"
"The optional parameter 'resolution' will give a desired resolution value (currently not used).\n"
"The optional parameter 'action' determines the action to be invoked.\n"
"This action can be one of three values: REJECT, ACCEPT, USE_DEFAULT. If this failure action\n"
"parameter is not specified, then it will be REJECT by default.\n"
"If a range constraint is violated this parameter is marked wrong with violation hint in the\n"
"parameter user interface.\n"
gsi::method ("min_value", &db::PCellParameterDeclaration::min_value,
"@brief Gets the minimum value allowed\n"
"See \\min_value= for a description of this attribute.\n"
"\n"
"This attribute has been added in version 0.29."
) +
gsi::method ("min_value=", &db::PCellParameterDeclaration::set_min_value, gsi::arg ("value"),
"@brief Sets the minimum value allowed\n"
"The minimum value is a visual feature and limits the allowed values for numerical\n"
"entry boxes. This applies to parameters of type int or double. The minimum value\n"
"is not effective if choices are present.\n"
"\n"
"The minimum value is not enforced - for example there is no restriction implemented\n"
"when setting values programmatically.\n"
"\n"
"Setting this attribute to \"nil\" (the default) implies \"no limit\".\n"
"\n"
"This attribute has been added in version 0.29."
) +
gsi::method ("max_value", &db::PCellParameterDeclaration::max_value,
"@brief Gets the maximum value allowed\n"
"See \\max_value= for a description of this attribute.\n"
"\n"
"This attribute has been added in version 0.29."
) +
gsi::method ("max_value=", &db::PCellParameterDeclaration::set_max_value, gsi::arg ("value"),
"@brief Sets the maximum value allowed\n"
"The maximum value is a visual feature and limits the allowed values for numerical\n"
"entry boxes. This applies to parameters of type int or double. The maximum value\n"
"is not effective if choices are present.\n"
"\n"
"The maximum value is not enforced - for example there is no restriction implemented\n"
"when setting values programmatically.\n"
"\n"
"Setting this attribute to \"nil\" (the default) implies \"no limit\".\n"
"\n"
"This attribute has been added in version 0.29."
) +
gsi::method ("choice_values", &db::PCellParameterDeclaration::get_choices,
"@brief Returns a list of choice values\n"

View File

@ -398,17 +398,15 @@ PCellParametersPage::setup (lay::LayoutViewBase *view, int cv_index, const db::P
inner_grid->addWidget (icon_label, row, 0);
m_icon_widgets.push_back (icon_label);
m_all_widgets.back ().push_back (icon_label);
std::string range;
if (p->get_range().has_value())
{
const tl::Variant& low(p->get_range().value().m_low);
const tl::Variant& high(p->get_range().value().m_high);
range = tl::sprintf(
" [%s, %s]" ,
low.is_nil() ? "-\u221e" : low.to_string(),
high.is_nil() ? "\u221e" : high.to_string());
if (! p->min_value ().is_nil () || ! p->max_value ().is_nil ()) {
range = tl::sprintf (
" [%s, %s]" ,
p->min_value ().is_nil () ? "-\u221e" /*infinity*/ : p->min_value ().to_string (),
p->max_value ().is_nil () ? "\u221e" /*infinity*/ : p->max_value ().to_string ()
);
}
if (p->get_type () != db::PCellParameterDeclaration::t_callback) {
@ -1109,19 +1107,14 @@ PCellParametersPage::states_from_parameters (db::ParameterStates &states, const
}
void
PCellParametersPage::check_range(const tl::Variant& value, const db::PCellParameterDeclaration::Range& range)
PCellParametersPage::check_range (const tl::Variant &value, const db::PCellParameterDeclaration &decl)
{
if (db::PCellParameterDeclaration::Action(range.m_action) == db::PCellParameterDeclaration::t_Reject)
{
if (!range.m_low.is_nil() && value < range.m_low)
{
throw tl::Exception(tl::to_string (tr("Range violation: value < low")));
}
if (! decl.min_value ().is_nil () && value < decl.min_value ()) {
throw tl::Exception (tl::sprintf (tl::to_string (tr ("The value is below the minimum allowed value: given value is %s, minimum value is %s")), value.to_string (), decl.min_value ().to_string ()));
}
if (!range.m_high.is_nil() && range.m_high < value)
{
throw tl::Exception(tl::to_string (tr("Range violation: value > high")));
}
if (! decl.max_value ().is_nil () && value > decl.max_value ()) {
throw tl::Exception (tl::sprintf (tl::to_string (tr ("The value is above the maximum allowed value: given value is %s, maximum value is %s")), value.to_string (), decl.max_value ().to_string ()));
}
}

View File

@ -181,7 +181,7 @@ private:
void get_parameters_internal (db::ParameterStates &states, bool &edit_error);
std::vector<tl::Variant> parameter_from_states (const db::ParameterStates &states) const;
void states_from_parameters (db::ParameterStates &states, const std::vector<tl::Variant> &parameters);
void check_range(const tl::Variant& value, const db::PCellParameterDeclaration::Range& range);
void check_range (const tl::Variant& value, const db::PCellParameterDeclaration &decl);
};
}

View File

@ -35,7 +35,6 @@
#include <set>
#include <stdexcept>
#include <cstdint>
#include <optional>
#if defined(HAVE_QT)
#include <QString>