add min max def to properties
This commit is contained in:
@@ -36,6 +36,13 @@ public:
|
||||
}
|
||||
virtual const std::string& GetGroup() const = 0;
|
||||
virtual void SetGroup(const std::string& group) = 0;
|
||||
|
||||
virtual bool HasRange() const { return false; }
|
||||
virtual double GetMin() const { return 0; }
|
||||
virtual double GetMax() const { return 0; }
|
||||
virtual bool HasDefault() const { return false; }
|
||||
virtual std::string GetDefaultValueAsString() const { return ""; }
|
||||
|
||||
std::string GetQualifiedName() const {
|
||||
if (GetGroup().empty()) return GetName();
|
||||
return GetGroup() + "." + GetName();
|
||||
@@ -63,7 +70,8 @@ class Property : public PropertyBase {
|
||||
public:
|
||||
// PROXY: Use an existing variable as back-end storage
|
||||
Property(Object* owner, const std::string& name, T* valuePtr, const std::string& units = "", const std::string& group = "")
|
||||
: m_owner(owner), m_name(name), m_units(units), m_group(group), m_value(valuePtr), m_own(false) {
|
||||
: m_owner(owner), m_name(name), m_units(units), m_group(group), m_value(valuePtr), m_own(false),
|
||||
m_HasRange(false), m_HasDefault(false) {
|
||||
if (m_owner) {
|
||||
m_owner->RegisterProperty(this);
|
||||
}
|
||||
@@ -71,7 +79,8 @@ public:
|
||||
|
||||
// MANAGED: Create and own internal storage
|
||||
Property(Object* owner, const std::string& name, const T& defaultValue = T(), const std::string& units = "", const std::string& group = "")
|
||||
: m_owner(owner), m_name(name), m_units(units), m_group(group), m_value(new T(defaultValue)), m_own(true) {
|
||||
: m_owner(owner), m_name(name), m_units(units), m_group(group), m_value(new T(defaultValue)), m_own(true),
|
||||
m_HasRange(false), m_HasDefault(true), m_Default(defaultValue) {
|
||||
if (m_owner) {
|
||||
m_owner->RegisterProperty(this);
|
||||
}
|
||||
@@ -103,15 +112,61 @@ public:
|
||||
|
||||
// Accessors
|
||||
const T& Get() const { return *m_value; }
|
||||
template<typename U = T>
|
||||
typename std::enable_if<std::is_arithmetic<U>::value, void>::type
|
||||
ValidateT(T& val) {
|
||||
if (m_HasRange) {
|
||||
if (val < m_Min) val = m_Min;
|
||||
if (val > m_Max) val = m_Max;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
typename std::enable_if<!std::is_arithmetic<U>::value, void>::type
|
||||
ValidateT(T& val) {
|
||||
}
|
||||
|
||||
void Set(const T& value) {
|
||||
if (*m_value != value) {
|
||||
*m_value = value;
|
||||
T val = value;
|
||||
ValidateT<T>(val);
|
||||
if (*m_value != val) {
|
||||
*m_value = val;
|
||||
ULIB_SIGNAL_EMIT(Property<T>::PropertyChanged);
|
||||
this->Updated();
|
||||
if (m_owner) m_owner->Updated();
|
||||
}
|
||||
}
|
||||
|
||||
void SetRange(const T& min, const T& max) { m_Min = min; m_Max = max; m_HasRange = true; }
|
||||
void SetDefault(const T& def) { m_Default = def; m_HasDefault = true; }
|
||||
|
||||
virtual bool HasRange() const override { return m_HasRange; }
|
||||
|
||||
template<typename U = T>
|
||||
typename std::enable_if<std::is_arithmetic<U>::value, double>::type
|
||||
GetMinT() const { return (double)m_Min; }
|
||||
|
||||
template<typename U = T>
|
||||
typename std::enable_if<!std::is_arithmetic<U>::value, double>::type
|
||||
GetMinT() const { return 0.0; }
|
||||
|
||||
template<typename U = T>
|
||||
typename std::enable_if<std::is_arithmetic<U>::value, double>::type
|
||||
GetMaxT() const { return (double)m_Max; }
|
||||
|
||||
template<typename U = T>
|
||||
typename std::enable_if<!std::is_arithmetic<U>::value, double>::type
|
||||
GetMaxT() const { return 0.0; }
|
||||
|
||||
virtual double GetMin() const override { return GetMinT<T>(); }
|
||||
virtual double GetMax() const override { return GetMaxT<T>(); }
|
||||
|
||||
virtual bool HasDefault() const override { return m_HasDefault; }
|
||||
virtual std::string GetDefaultValueAsString() const override {
|
||||
try { return boost::lexical_cast<std::string>(m_Default); }
|
||||
catch (...) { return ""; }
|
||||
}
|
||||
|
||||
// Operators for seamless usage
|
||||
operator const T&() const { return *m_value; }
|
||||
Property& operator=(const T& value) {
|
||||
@@ -149,6 +204,11 @@ private:
|
||||
T* m_value;
|
||||
bool m_own;
|
||||
Object* m_owner;
|
||||
bool m_HasRange;
|
||||
T m_Min;
|
||||
T m_Max;
|
||||
bool m_HasDefault;
|
||||
T m_Default;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -242,6 +302,8 @@ public:
|
||||
void save_override(const boost::serialization::hrp<T> &t) {
|
||||
if (m_Object) {
|
||||
Property<T>* p = new Property<T>(m_Object, t.name(), &const_cast<boost::serialization::hrp<T>&>(t).value(), t.units() ? t.units() : "", GetCurrentGroup());
|
||||
if (t.has_range()) p->SetRange(t.min_val(), t.max_val());
|
||||
if (t.has_default()) p->SetDefault(t.default_val());
|
||||
m_Object->RegisterDynamicProperty(p);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,15 +77,30 @@ class hrp : public boost::serialization::wrapper_traits<hrp<T>> {
|
||||
const char *m_name;
|
||||
const char *m_units;
|
||||
T &m_value;
|
||||
bool m_has_range;
|
||||
T m_min;
|
||||
T m_max;
|
||||
bool m_has_default;
|
||||
T m_default;
|
||||
|
||||
public:
|
||||
explicit hrp(const char *name_, T &t, const char* units_ = nullptr) : m_name(name_), m_units(units_), m_value(t) {}
|
||||
explicit hrp(const char *name_, T &t, const char* units_ = nullptr)
|
||||
: m_name(name_), m_units(units_), m_value(t), m_has_range(false), m_has_default(false) {}
|
||||
|
||||
hrp& range(const T& min_val, const T& max_val) { m_min = min_val; m_max = max_val; m_has_range = true; return *this; }
|
||||
hrp& set_default(const T& def_val) { m_default = def_val; m_has_default = true; return *this; }
|
||||
|
||||
const char *name() const { return this->m_name; }
|
||||
const char *units() const { return this->m_units; }
|
||||
T &value() { return this->m_value; }
|
||||
const T &const_value() const { return this->m_value; }
|
||||
|
||||
bool has_range() const { return m_has_range; }
|
||||
const T& min_val() const { return m_min; }
|
||||
const T& max_val() const { return m_max; }
|
||||
bool has_default() const { return m_has_default; }
|
||||
const T& default_val() const { return m_default; }
|
||||
|
||||
BOOST_SERIALIZATION_SPLIT_MEMBER()
|
||||
|
||||
template <class Archivex>
|
||||
@@ -110,16 +125,23 @@ class hrp_enum : public boost::serialization::wrapper_traits<hrp_enum<T>> {
|
||||
const char *m_units;
|
||||
T &m_value;
|
||||
std::vector<std::string> m_labels;
|
||||
bool m_has_default;
|
||||
T m_default;
|
||||
|
||||
public:
|
||||
explicit hrp_enum(const char *name_, T &t, const std::vector<std::string>& labels, const char* units_ = nullptr)
|
||||
: m_name(name_), m_units(units_), m_value(t), m_labels(labels) {}
|
||||
: m_name(name_), m_units(units_), m_value(t), m_labels(labels), m_has_default(false) {}
|
||||
|
||||
hrp_enum& set_default(const T& def_val) { m_default = def_val; m_has_default = true; return *this; }
|
||||
|
||||
const char *name() const { return this->m_name; }
|
||||
const char *units() const { return this->m_units; }
|
||||
T &value() { return this->m_value; }
|
||||
const std::vector<std::string>& labels() const { return m_labels; }
|
||||
|
||||
bool has_default() const { return m_has_default; }
|
||||
const T& default_val() const { return m_default; }
|
||||
|
||||
BOOST_SERIALIZATION_SPLIT_MEMBER()
|
||||
|
||||
template <class Archivex>
|
||||
|
||||
@@ -86,13 +86,13 @@ public:
|
||||
m_ShowBoundingBox(false),
|
||||
m_ShowScaleMeasures(false),
|
||||
m_Representation(Puppet::Surface),
|
||||
m_Opacity(-1.0),
|
||||
m_Opacity(1.0),
|
||||
m_Selectable(true),
|
||||
m_Selected(false),
|
||||
m_Visibility(true),
|
||||
m_Dragable(true)
|
||||
{
|
||||
m_Color[0] = m_Color[1] = m_Color[2] = -1.0;
|
||||
m_Color = Vector3d(-1, -1, -1);
|
||||
}
|
||||
|
||||
~PuppetData() {
|
||||
@@ -111,7 +111,7 @@ public:
|
||||
bool m_ShowBoundingBox;
|
||||
bool m_ShowScaleMeasures;
|
||||
int m_Representation;
|
||||
double m_Color[3];
|
||||
Vector3d m_Color;
|
||||
double m_Opacity;
|
||||
|
||||
bool m_Selectable;
|
||||
@@ -137,8 +137,9 @@ public:
|
||||
actor->GetProperty()->SetEdgeVisibility(0);
|
||||
}
|
||||
}
|
||||
if (m_Color[0] != -1.0) {
|
||||
actor->GetProperty()->SetColor(m_Color);
|
||||
if (m_Color.x() != -1.0) {
|
||||
double c[3] = {m_Color.x(), m_Color.y(), m_Color.z()};
|
||||
actor->GetProperty()->SetColor(c);
|
||||
}
|
||||
|
||||
if (m_Opacity != -1.0) {
|
||||
@@ -280,8 +281,11 @@ void Puppet::SetProp(vtkProp *prop)
|
||||
pd->m_Representation = vp->GetRepresentation();
|
||||
if (pd->m_Opacity < 0)
|
||||
pd->m_Opacity = vp->GetOpacity();
|
||||
if (pd->m_Color[0] < 0)
|
||||
vp->GetColor(pd->m_Color);
|
||||
if (pd->m_Color.x() < 0) {
|
||||
double c[3];
|
||||
vp->GetColor(c);
|
||||
pd->m_Color = Vector3d(c[0], c[1], c[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -622,10 +626,8 @@ struct AppearanceProxy {
|
||||
PuppetData* pd;
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int version) {
|
||||
ar & boost::serialization::make_hrp("ColorR", pd->m_Color[0]);
|
||||
ar & boost::serialization::make_hrp("ColorG", pd->m_Color[1]);
|
||||
ar & boost::serialization::make_hrp("ColorB", pd->m_Color[2]);
|
||||
ar & boost::serialization::make_hrp("Opacity", pd->m_Opacity);
|
||||
ar & boost::serialization::make_hrp("Color", pd->m_Color, "color");
|
||||
ar & boost::serialization::make_hrp("Opacity", pd->m_Opacity).range(0.0, 1.0).set_default(1.0);
|
||||
ar & boost::serialization::make_hrp_enum("Representation", pd->m_Representation, {"Points", "Wireframe", "Surface", "SurfaceWithEdges", "Volume", "Outline", "Slice"});
|
||||
ar & boost::serialization::make_hrp("Visibility", pd->m_Visibility);
|
||||
ar & boost::serialization::make_hrp("Pickable", pd->m_Selectable);
|
||||
|
||||
@@ -177,6 +177,9 @@ public:
|
||||
void save_override(const boost::serialization::hrp<T> &t) {
|
||||
if (m_Puppet) {
|
||||
uLib::Property<T>* p = new uLib::Property<T>(m_Puppet, t.name(), &const_cast<boost::serialization::hrp<T>&>(t).value(), t.units() ? t.units() : "", GetCurrentGroup());
|
||||
if (t.has_range()) p->SetRange(t.min_val(), t.max_val());
|
||||
if (t.has_default()) p->SetDefault(t.default_val());
|
||||
|
||||
m_Puppet->RegisterDisplayProperty(p);
|
||||
Vtk::Puppet* puppet = m_Puppet;
|
||||
uLib::Object::connect(p, &uLib::PropertyBase::Updated, [puppet](){ puppet->Update(); });
|
||||
|
||||
Reference in New Issue
Block a user