From f2be35a1f002343cf8de947e97a68d44af6de050 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Fri, 17 Dec 2021 11:02:44 +0100
Subject: [PATCH] add property classes

---
 GUI/Model/Types/DoubleProperty.cpp  | 41 ++++++++++++++++++++
 GUI/Model/Types/DoubleProperty.h    | 51 +++++++++++++++++++++++++
 GUI/Model/Types/SelectionProperty.h | 56 ++++++++++++++++++++++++++++
 GUI/Model/Types/UIntProperty.cpp    | 39 +++++++++++++++++++
 GUI/Model/Types/UIntProperty.h      | 58 +++++++++++++++++++++++++++++
 GUI/Model/Types/VectorProperty.cpp  | 26 +++++++++++++
 GUI/Model/Types/VectorProperty.h    | 46 +++++++++++++++++++++++
 7 files changed, 317 insertions(+)
 create mode 100644 GUI/Model/Types/DoubleProperty.cpp
 create mode 100644 GUI/Model/Types/DoubleProperty.h
 create mode 100644 GUI/Model/Types/SelectionProperty.h
 create mode 100644 GUI/Model/Types/UIntProperty.cpp
 create mode 100644 GUI/Model/Types/UIntProperty.h
 create mode 100644 GUI/Model/Types/VectorProperty.cpp
 create mode 100644 GUI/Model/Types/VectorProperty.h

diff --git a/GUI/Model/Types/DoubleProperty.cpp b/GUI/Model/Types/DoubleProperty.cpp
new file mode 100644
index 00000000000..fa93ad9ce2f
--- /dev/null
+++ b/GUI/Model/Types/DoubleProperty.cpp
@@ -0,0 +1,41 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Model/Types/DoubleProperty.cpp
+//! @brief     Implements 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)
+//
+//  ************************************************************************************************
+
+#include "GUI/Model/Types/DoubleProperty.h"
+#include <QUuid>
+
+void DoubleProperty::init(const QString& label, const QString& tooltip, double value,
+                          const variant<QString, Unit>& unit, const QString& persistentTag)
+{
+    init(label, tooltip, value, unit, 3, RealLimits::nonnegative(), persistentTag);
+}
+
+void DoubleProperty::init(const QString& label, const QString& tooltip, double value,
+                          const variant<QString, Unit>& unit, int decimals,
+                          const RealLimits& limits, const QString& persistentTag)
+{
+    m_value = value;
+    m_decimals = decimals;
+    m_persistentTag = persistentTag;
+    m_uid = QUuid::createUuid().toString(QUuid::WithoutBraces);
+
+    m_descriptor.label = label;
+    m_descriptor.tooltip = tooltip;
+    m_descriptor.decimals = m_decimals;
+    m_descriptor.limits = limits;
+    m_descriptor.unit = unit;
+    m_descriptor.set = [=](double v) { m_value = v; };
+    m_descriptor.get = [=] { return m_value; };
+    m_descriptor.path = [=] { return m_uid; };
+}
diff --git a/GUI/Model/Types/DoubleProperty.h b/GUI/Model/Types/DoubleProperty.h
new file mode 100644
index 00000000000..924de77c8c8
--- /dev/null
+++ b/GUI/Model/Types/DoubleProperty.h
@@ -0,0 +1,51 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Model/Types/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_TYPES_DOUBLEPROPERTY_H
+#define BORNAGAIN_GUI_MODEL_TYPES_DOUBLEPROPERTY_H
+
+#include "GUI/Model/Types/DoubleDescriptor.h"
+
+// #baMigration docu
+class DoubleProperty {
+public:
+    void init(const QString& label, const QString& tooltip, double value,
+              const variant<QString, Unit>& unit, const QString& persistentTag);
+    void init(const QString& label, const QString& tooltip, double value,
+              const variant<QString, Unit>& unit, int decimals, const RealLimits& limits,
+              const QString& persistentTag);
+
+    DoubleDescriptor descriptor() const { return m_descriptor; }
+    operator DoubleDescriptor() const { return m_descriptor; }
+    operator double() const { return m_value; }
+    void set(double d) { m_value = d; }
+    double get() const { return m_value; }
+
+    QString persistentTag() const { return m_persistentTag; }
+    QString uid() const { return m_uid; }
+    void setUid(const QString& uid) { m_uid = uid; }
+    unsigned decimals() const { return m_decimals; }
+    void setDecimals(unsigned decimals) { m_decimals = decimals; }
+
+private:
+    double m_value = 0.0;
+    unsigned m_decimals = 3;
+    QString m_persistentTag;
+    QString m_uid;
+
+    DoubleDescriptor m_descriptor;
+};
+
+
+#endif // BORNAGAIN_GUI_MODEL_TYPES_DOUBLEPROPERTY_H
diff --git a/GUI/Model/Types/SelectionProperty.h b/GUI/Model/Types/SelectionProperty.h
new file mode 100644
index 00000000000..0327f6bfe4a
--- /dev/null
+++ b/GUI/Model/Types/SelectionProperty.h
@@ -0,0 +1,56 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Model/Types/SelectionProperty.h
+//! @brief     Defines class SelectionProperty
+//!
+//! @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_TYPES_SELECTIONPROPERTY_H
+#define BORNAGAIN_GUI_MODEL_TYPES_SELECTIONPROPERTY_H
+
+#include "GUI/Model/Group/SelectionDescriptor.h"
+
+// #baMigration docu
+template <typename T> class SelectionProperty {
+public:
+    ~SelectionProperty() { delete m_p; }
+
+    template <typename Catalog>
+    void init(const QString& label, const QString& tooltip, const QString& persistentTag)
+    {
+        m_persistentTag = persistentTag;
+        m_descriptor.init<Catalog>(label, tooltip, &m_p);
+        m_p = Catalog::create(Catalog::types()[0]);
+    }
+
+    SelectionDescriptor<T> descriptor() const { return m_descriptor; }
+
+    operator SelectionDescriptor<T>() const { return m_descriptor; }
+    T operator->() const { return m_p; }
+
+    void set(T t)
+    {
+        delete m_p;
+        m_p = t;
+    }
+
+    T get() const { return m_p; }
+
+    QString persistentTag() const { return m_persistentTag; }
+
+private:
+    QString m_persistentTag;
+    SelectionDescriptor<T> m_descriptor;
+
+    T m_p = nullptr;
+};
+
+
+#endif // BORNAGAIN_GUI_MODEL_TYPES_SELECTIONPROPERTY_H
diff --git a/GUI/Model/Types/UIntProperty.cpp b/GUI/Model/Types/UIntProperty.cpp
new file mode 100644
index 00000000000..e4092e8d22e
--- /dev/null
+++ b/GUI/Model/Types/UIntProperty.cpp
@@ -0,0 +1,39 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Model/Types/UIntProperty.cpp
+//! @brief     Implements class UIntProperty
+//!
+//! @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)
+//
+//  ************************************************************************************************
+
+#include "GUI/Model/Types/UIntProperty.h"
+#include <QUuid>
+
+void UIntProperty::init(const QString& label, const QString& tooltip, uint value,
+                        const variant<QString, Unit>& unit, const QString& persistentTag)
+{
+    init(label, tooltip, value, unit, RealLimits::nonnegative(), persistentTag);
+}
+
+void UIntProperty::init(const QString& label, const QString& tooltip, uint value,
+                        const variant<QString, Unit>& unit, const RealLimits& limits,
+                        const QString& persistentTag)
+{
+    m_value = value;
+    m_persistentTag = persistentTag;
+    m_uid = QUuid::createUuid().toString();
+
+    m_descriptor.label = label;
+    m_descriptor.tooltip = tooltip;
+    m_descriptor.limits = limits;
+    m_descriptor.unit = unit;
+    m_descriptor.set = [=](double v) { m_value = v; };
+    m_descriptor.get = [=] { return m_value; };
+    m_descriptor.path = [=] { return m_uid; };
+}
diff --git a/GUI/Model/Types/UIntProperty.h b/GUI/Model/Types/UIntProperty.h
new file mode 100644
index 00000000000..0e543020ed3
--- /dev/null
+++ b/GUI/Model/Types/UIntProperty.h
@@ -0,0 +1,58 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Model/Types/UIntProperty.h
+//! @brief     Defines class UIntProperty
+//!
+//! @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_TYPES_UINTPROPERTY_H
+#define BORNAGAIN_GUI_MODEL_TYPES_UINTPROPERTY_H
+
+#include "Fit/Param/RealLimits.h"
+#include "GUI/Model/Types/UIntDescriptor.h"
+#include "GUI/Model/Types/Unit.h"
+#include <QString>
+#include <functional>
+#include <variant>
+
+class SessionItem;
+
+using std::function;
+using std::variant;
+
+// #baMigration docu
+class UIntProperty {
+public:
+    void init(const QString& label, const QString& tooltip, uint value,
+              const variant<QString, Unit>& unit, const QString& persistentTag);
+    void init(const QString& label, const QString& tooltip, uint value,
+              const variant<QString, Unit>& unit, const RealLimits& limits,
+              const QString& persistentTag);
+
+    UIntDescriptor descriptor() const { return m_descriptor; }
+    operator UIntDescriptor() const { return m_descriptor; }
+    operator uint() const { return m_value; }
+    void set(uint d) { m_value = d; }
+    uint get() const { return m_value; }
+
+    QString persistentTag() const { return m_persistentTag; }
+    QString uid() const { return m_uid; }
+    void setUid(const QString& uid) { m_uid = uid; }
+
+private:
+    uint m_value = 0;
+    QString m_persistentTag;
+    QString m_uid;
+
+    UIntDescriptor m_descriptor;
+};
+
+
+#endif // BORNAGAIN_GUI_MODEL_TYPES_UINTPROPERTY_H
diff --git a/GUI/Model/Types/VectorProperty.cpp b/GUI/Model/Types/VectorProperty.cpp
new file mode 100644
index 00000000000..1222a668daf
--- /dev/null
+++ b/GUI/Model/Types/VectorProperty.cpp
@@ -0,0 +1,26 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Model/Types/VectorProperty.cpp
+//! @brief     Implements class VectorProperty
+//!
+//! @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)
+//
+//  ************************************************************************************************
+
+#include "GUI/Model/Types/VectorProperty.h"
+#include <QUuid>
+
+void VectorProperty::init(const QString& label, const QString& tooltip,
+                          const variant<QString, Unit>& unit, const QString& persistentTag)
+{
+    m_persistentTag = persistentTag;
+    m_uid = QUuid::createUuid().toString();
+
+    m_descriptor.init(label, tooltip, &m_value, unit);
+    m_descriptor.uid = [this] { return m_uid; };
+}
diff --git a/GUI/Model/Types/VectorProperty.h b/GUI/Model/Types/VectorProperty.h
new file mode 100644
index 00000000000..6d24a5fe401
--- /dev/null
+++ b/GUI/Model/Types/VectorProperty.h
@@ -0,0 +1,46 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/Model/Types/VectorProperty.h
+//! @brief     Defines class VectorProperty
+//!
+//! @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_TYPES_VectorProperty_H
+#define BORNAGAIN_GUI_MODEL_TYPES_VectorProperty_H
+
+#include "GUI/Model/Types/VectorDescriptor.h"
+#include <heinz/Vectors3D.h>
+
+// #baMigration docu
+class VectorProperty {
+public:
+    void init(const QString& label, const QString& tooltip, const variant<QString, Unit>& unit,
+              const QString& persistentTag);
+
+    VectorDescriptor descriptor() const { return m_descriptor; }
+    operator VectorDescriptor() const { return m_descriptor; }
+    operator R3() const { return m_value; }
+    void set(const R3& d) { m_value = d; }
+    R3 get() const { return m_value; }
+
+    QString persistentTag() const { return m_persistentTag; }
+    QString uid() const { return m_uid; }
+    void setUid(const QString& uid) { m_uid = uid; }
+
+private:
+    R3 m_value;
+    QString m_persistentTag;
+    QString m_uid;
+
+    VectorDescriptor m_descriptor;
+};
+
+
+#endif // BORNAGAIN_GUI_MODEL_TYPES_VectorProperty_H
-- 
GitLab