BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
DoubleProperty.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Descriptor/DoubleProperty.h
6 //! @brief Defines class DoubleProperty
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_GUI_MODEL_DESCRIPTOR_DOUBLEPROPERTY_H
16 #define BORNAGAIN_GUI_MODEL_DESCRIPTOR_DOUBLEPROPERTY_H
17 
19 
20 class Streamer;
21 
22 //! Class for representing a double value, its attributes and its accessors.
23 //!
24 //! Contained attributes are
25 //! * the value itself
26 //! * label: a label of e.g. a spin box
27 //! * tooltip: tooltip for e.g. a spin box
28 //! * unit: unit of this value (not the representation on UI, but the unit of the contained value).
29 //! Use this to show the unit in the UI as well as allow a representation in a different unit
30 //! (e.g. Angstrom instead of nanometer)
31 //! * decimals: how many decimals shall be shown in a spin box
32 //! * limits: which limits shall be set in a spin box or other validator
33 //! * persistent tag: a name to serialize this property. Do not change this tag if it was already
34 //! used for serialization, otherwise you may break backward compatibility of project reading.
35 //! * uid: a unique id which represents this property. This is used for linking to this property,
36 //! also when serializing the link. Right now, this uid is a UUID. It could also be refactored and
37 //! changed to a simple (e.g. integer) id which is always unique throughout one project.
38 //!
39 //! This property class supports also the descriptor mechanism by directly returning a
40 //! DoubleDescriptor. This descriptor contains all the relevant information taken from this class,
41 //! and provides a getter and a setter to the contained value.
42 //!
43 //! \sa DoubleDescriptor
45 public:
46  void init(const QString& label, const QString& tooltip, double value,
47  const variant<QString, Unit>& unit, const QString& persistentTag);
48  void init(const QString& label, const QString& tooltip, double value,
49  const variant<QString, Unit>& unit, int decimals, const RealLimits& limits,
50  const QString& persistentTag);
51 
52  //! Return a descriptor (information provider) for this double property.
54 
55  //! Cast to a descriptor (information provider)
56  operator DoubleDescriptor() const { return m_descriptor; }
57 
58  //! Cast to the contained double value.
59  operator double() const { return m_value; }
60 
61  //! Set the contained value.
62  void set(double d) { m_value = d; }
63 
64  //! The contained value.
65  double get() const { return m_value; }
66 
67  //! Persistent tag for serializing.
68  QString persistentTag() const { return m_persistentTag; }
69 
70  //! Unique id of this double property.
71  QString uid() const { return m_uid; }
72 
73  //! Set the unique id of this double property.
74  void setUid(const QString& uid) { m_uid = uid; }
75 
76  //! Number of decimals to be shown in an edit field.
77  unsigned decimals() const { return m_decimals; }
78 
79  //! Set number of decimals to be shown in an edit field.
80  void setDecimals(unsigned decimals) { m_decimals = decimals; }
81 
82  //! Set the tooltip
83  void setTooltip(const QString& tooltip);
84 
85  //! Set the unit
86  void setUnit(const variant<QString, Unit>& unit);
87 
88  //! Set the limits
89  void setLimits(const RealLimits& limits);
90 
91  //! True if one of the init methods has been called (checks for a valid uid).
92  bool isInitialized() const;
93 
94 private:
95  double m_value = 0.0; //!< Current value
96  unsigned m_decimals = 3; //!< Number of decimals to be shown in an edit field
97  QString m_persistentTag; //!< Persistent tag for serializing
98  QString m_uid; //!< Unique id of this double property.
99  DoubleDescriptor m_descriptor; //!< descriptor, holding more attributes like label, tooltip etc.
100 };
101 
102 namespace Serialize {
103 void rwProperty(Streamer& s, DoubleProperty& d);
104 } // namespace Serialize
105 
106 //! Add a member, a getter and a setter for a DoubleProperty
107 #define DOUBLE_PROPERTY(nameLower, nameUpper) \
108 protected: \
109  DoubleProperty m_##nameLower; \
110  \
111 public: \
112  DoubleDescriptor nameLower() const { return m_##nameLower; } \
113  void set##nameUpper(double v) { m_##nameLower.set(v); }
114 
115 
116 #endif // BORNAGAIN_GUI_MODEL_DESCRIPTOR_DOUBLEPROPERTY_H
Defines class DoubleDescriptor.
Describes properties of a double value which are necessary to allow GUI representation,...
Class for representing a double value, its attributes and its accessors.
void setTooltip(const QString &tooltip)
Set the tooltip.
void setLimits(const RealLimits &limits)
Set the limits.
DoubleDescriptor m_descriptor
descriptor, holding more attributes like label, tooltip etc.
QString m_uid
Unique id of this double property.
QString uid() const
Unique id of this double property.
QString persistentTag() const
Persistent tag for serializing.
unsigned m_decimals
Number of decimals to be shown in an edit field.
void setDecimals(unsigned decimals)
Set number of decimals to be shown in an edit field.
double m_value
Current value.
void setUnit(const variant< QString, Unit > &unit)
Set the unit.
unsigned decimals() const
Number of decimals to be shown in an edit field.
void set(double d)
Set the contained value.
DoubleDescriptor descriptor() const
Return a descriptor (information provider) for this double property.
double get() const
The contained value.
QString m_persistentTag
Persistent tag for serializing.
void init(const QString &label, const QString &tooltip, double value, const variant< QString, Unit > &unit, const QString &persistentTag)
bool isInitialized() const
True if one of the init methods has been called (checks for a valid uid).
void setUid(const QString &uid)
Set the unique id of this double property.
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
Functions to serialize various data types.
Definition: SessionItem.h:339
void rwProperty(Streamer &s, DoubleProperty &d)