Skip to content
Snippets Groups Projects
DoubleProperty.h 5.09 KiB
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Descriptor/DoubleProperty.h
//! @brief     Defines class DoubleProperty
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_GUI_MODEL_DESCRIPTOR_DOUBLEPROPERTY_H
#define BORNAGAIN_GUI_MODEL_DESCRIPTOR_DOUBLEPROPERTY_H

#include "GUI/Model/Descriptor/DoubleDescriptor.h"

class Streamer;

//! Class for representing a double value, its attributes and its accessors.
//!
//! Contained attributes are
//! * the value itself
//! * label: a label of e.g. a spin box
//! * tooltip: tooltip for e.g. a spin box
//! * unit: unit of this value (not the representation on UI, but the unit of the contained value).
//!   Use this to show the unit in the UI as well as allow a representation in a different unit
//!   (e.g. Angstrom instead of nanometer)
//! * decimals: how many decimals shall be shown in a spin box
//! * limits: which limits shall be set in a spin box or other validator
//! * persistent tag: a name to serialize this property. Do not change this tag if it was already
//!   used for serialization, otherwise you may break backward compatibility of project reading.
//! * uid: a unique id which represents this property. This is used for linking to this property,
//!   also when serializing the link. Right now, this uid is a UUID. It could also be refactored and
//!   changed to a simple (e.g. integer) id which is always unique throughout one project.
//!
//! This property class supports also the descriptor mechanism by directly returning a
//! DoubleDescriptor. This descriptor contains all the relevant information taken from this class,
//! and provides a getter and a setter to the contained value.
//!
//! \sa DoubleDescriptor
class DoubleProperty {
public:
    void init(const QString& label, const QString& tooltip, double value,
              const std::variant<QString, Unit>& unit, const QString& persistentTag);
    void init(const QString& label, const QString& tooltip, double value,
              const std::variant<QString, Unit>& unit, int decimals, const RealLimits& limits,
              const QString& persistentTag);

    //! Return a descriptor (information provider) for this double property.
    DoubleDescriptor descriptor() const { return m_descriptor; }

    //! Cast to a descriptor (information provider)
    operator DoubleDescriptor() const { return m_descriptor; }

    //! Cast to the contained double value.
    operator double() const { return m_value; }

    //! Set the contained value.
    void set(double d) { m_value = d; }

    //! The contained value.
    double get() const { return m_value; }

    //! Persistent tag for serializing.
    QString persistentTag() const { return m_persistentTag; }

    //! Unique id of this double property.
    QString uid() const { return m_uid; }

    //! Set the unique id of this double property.
    void setUid(const QString& uid) { m_uid = uid; }

    //! Number of decimals to be shown in an edit field.
    unsigned decimals() const { return m_decimals; }

    //! Set number of decimals to be shown in an edit field.
    void setDecimals(unsigned decimals) { m_decimals = decimals; }

    //! Set the tooltip
    void setTooltip(const QString& tooltip);

    //! Set the unit
    void setUnit(const std::variant<QString, Unit>& unit);

    //! Set the limits
    void setLimits(const RealLimits& limits);

    //! True if one of the init methods has been called (checks for a valid uid).
    bool isInitialized() const;

private:
    double m_value = 0.0;          //!< Current value
    unsigned m_decimals = 3;       //!< Number of decimals to be shown in an edit field
    QString m_persistentTag;       //!< Persistent tag for serializing
    QString m_uid;                 //!< Unique id of this double property.
    DoubleDescriptor m_descriptor; //!< descriptor, holding more attributes like label, tooltip etc.
};

namespace Serialize {
void rwProperty(Streamer& s, DoubleProperty& d);
} // namespace Serialize

//! Add a member, a getter and a setter for a DoubleProperty
#define DOUBLE_PROPERTY(nameLower, nameUpper)                                                      \
protected:                                                                                         \
    DoubleProperty m_##nameLower;                                                                  \
                                                                                                   \
public:                                                                                            \
    DoubleDescriptor nameLower() const { return m_##nameLower; }                                   \
    void set##nameUpper(double v) { m_##nameLower.set(v); }


#endif // BORNAGAIN_GUI_MODEL_DESCRIPTOR_DOUBLEPROPERTY_H