BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
DoubleProperty.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Descriptor/DoubleProperty.cpp
6 //! @brief Implements 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 
18 #include <QUuid>
19 
20 void DoubleProperty::init(const QString& label, const QString& tooltip, double value,
21  const variant<QString, Unit>& unit, const QString& persistentTag)
22 {
23  init(label, tooltip, value, unit, 3, RealLimits::nonnegative(), persistentTag);
24 }
25 
26 void DoubleProperty::init(const QString& label, const QString& tooltip, double value,
27  const variant<QString, Unit>& unit, int decimals,
28  const RealLimits& limits, const QString& persistentTag)
29 {
30  m_value = value;
33  m_uid = QUuid::createUuid().toString(QUuid::WithoutBraces);
34 
35  m_descriptor.label = label;
36  m_descriptor.tooltip = tooltip;
38  m_descriptor.limits = limits;
39  m_descriptor.unit = unit;
40  m_descriptor.set = [=](double v) { m_value = v; };
41  m_descriptor.get = [=] { return m_value; };
42  m_descriptor.path = [=] { return m_uid; };
43 }
44 
45 void DoubleProperty::setTooltip(const QString& tooltip)
46 {
47  m_descriptor.tooltip = tooltip;
48 }
49 
50 void DoubleProperty::setUnit(const variant<QString, Unit>& unit)
51 {
52  m_descriptor.unit = unit;
53 }
54 
55 void DoubleProperty::setLimits(const RealLimits& limits)
56 {
57  m_descriptor.limits = limits;
58 }
59 
61 {
62  return !m_uid.isEmpty();
63 }
64 
66 {
67  if (QXmlStreamWriter* w = s.xmlWriter()) {
68  w->writeStartElement(d.persistentTag());
70  w->writeAttribute(XML::Tags::Id, d.uid());
72  w->writeEndElement();
73  } else if (QXmlStreamReader* r = s.xmlReader()) {
74  r->readNextStartElement();
76  d.set(r->attributes().value(XML::Tags::Value).toDouble());
77  d.setUid(r->attributes().value(XML::Tags::Id).toString());
78  d.setDecimals(r->attributes().value(XML::Tags::Decimals).toUInt());
80  } else
81  ASSERT(0);
82 }
Defines class DoubleProperty.
Defines class Streamer.
Defines.
variant< QString, Unit > unit
Unit of the value (internal unit only!)
QString label
A label text (short, no trailing colon)
function< void(double)> set
function to set the value
RealLimits limits
Limits of the value.
function< double()> get
function to get the current value
QString tooltip
Tooltip text.
int decimals
numbers of decimals to be shown in an edit control
function< QString()> path
Path describing this value. Used e.g. for undo/redo.
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.
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
void gotoEndElementOfTag(const QString &tag)
Definition: Streamer.cpp:39
void assertCurrentTag(const QString &expectedTag) const
Definition: Streamer.cpp:62
QXmlStreamWriter * xmlWriter()
Returns stream writer or nullptr.
Definition: Streamer.h:47
QXmlStreamReader * xmlReader()
Returns stream reader or nullptr.
Definition: Streamer.h:48
void writeAttribute(QXmlStreamWriter *writer, const QString &attributeName, const QVariant &variant)
Write the variant's value as an attribute.
Definition: UtilXML.cpp:65
void rwProperty(Streamer &s, DoubleProperty &d)
constexpr auto Decimals("decimals")
constexpr auto Id("id")
constexpr auto Value("value")