From 2a405c973005e331b93ae2c99fd85f5b80d73d5c Mon Sep 17 00:00:00 2001
From: Mikhail Svechnikov <m.svechnikov@fz-juelich.de>
Date: Fri, 25 Nov 2022 12:49:37 +0100
Subject: [PATCH] MaterialItem: magnetization is derived from VectorProperty

---
 GUI/Model/Descriptor/VectorProperty.cpp |  5 +++
 GUI/Model/Descriptor/VectorProperty.h   |  2 ++
 GUI/Model/Sample/MaterialItem.cpp       | 44 ++++---------------------
 GUI/Model/Sample/MaterialItem.h         |  9 ++---
 4 files changed, 15 insertions(+), 45 deletions(-)

diff --git a/GUI/Model/Descriptor/VectorProperty.cpp b/GUI/Model/Descriptor/VectorProperty.cpp
index 7aa4aaf0e3d..ea166a10b7d 100644
--- a/GUI/Model/Descriptor/VectorProperty.cpp
+++ b/GUI/Model/Descriptor/VectorProperty.cpp
@@ -29,6 +29,11 @@ void VectorProperty::init(const QString& label, const QString& tooltip,
     m_descriptor.uid = [this] { return m_uid; };
 }
 
+bool VectorProperty::operator==(const VectorProperty &other) const
+{
+    return (m_value == other.m_value) && (m_uid == other.m_uid) && (m_persistentTag == other.m_persistentTag);
+}
+
 
 void Serialize::rwProperty(Streamer& s, VectorProperty& d)
 {
diff --git a/GUI/Model/Descriptor/VectorProperty.h b/GUI/Model/Descriptor/VectorProperty.h
index cb62fd9ba4e..a0bf9016212 100644
--- a/GUI/Model/Descriptor/VectorProperty.h
+++ b/GUI/Model/Descriptor/VectorProperty.h
@@ -38,6 +38,8 @@ public:
     VectorDescriptor descriptor() const { return m_descriptor; }
     operator VectorDescriptor() const { return m_descriptor; }
     operator R3() const { return m_value; }
+    bool operator==(const VectorProperty& other) const;
+
     void set(const R3& d) { m_value = d; }
     R3 get() const { return m_value; }
 
diff --git a/GUI/Model/Sample/MaterialItem.cpp b/GUI/Model/Sample/MaterialItem.cpp
index 3b915564b16..144f406c158 100644
--- a/GUI/Model/Sample/MaterialItem.cpp
+++ b/GUI/Model/Sample/MaterialItem.cpp
@@ -51,6 +51,8 @@ MaterialItem::MaterialItem()
 
     m_sldRe.init("SLD, real", "Real part of SLD (SLD = real - i*imag), AA^{-2}", 0.0, Unit::unitless, "sldRe");
     m_sldIm.init("SLD, imaginary", "Imaginary part of SLD (SLD = real - i*imag), AA^{-2}", 0.0, Unit::unitless, "sldIm");
+
+    m_magnetization.init("Magnetization", "Magnetization (A/m)", "A/m", "magnetization");
 }
 
 MaterialItem::MaterialItem(const MaterialItem& other)
@@ -113,36 +115,7 @@ DoubleDescriptor MaterialItem::sldIm()
 
 VectorDescriptor MaterialItem::magnetizationVector()
 {
-    VectorDescriptor d("Magnetization", "Magnetization (A/m)", "A/m");
-
-    d.x.set = [=](double v) {
-        if (m_magnetization.x() != v) {
-            m_magnetization.setX(v);
-            emit dataChanged();
-        }
-    };
-    d.x.get = [=]() { return m_magnetization.x(); };
-    d.x.path = [=] { return uidForDescriptor("mx"); };
-
-    d.y.set = [=](double v) {
-        if (m_magnetization.y() != v) {
-            m_magnetization.setY(v);
-            emit dataChanged();
-        }
-    };
-    d.y.get = [=]() { return m_magnetization.y(); };
-    d.y.path = [=] { return uidForDescriptor("my"); };
-
-    d.z.set = [=](double v) {
-        if (m_magnetization.z() != v) {
-            m_magnetization.setZ(v);
-            emit dataChanged();
-        }
-    };
-    d.z.get = [=]() { return m_magnetization.z(); };
-    d.z.path = [=] { return uidForDescriptor("mz"); };
-    d.uid = [=] { return uidForDescriptor("magnetization"); };
-    return d;
+    return m_magnetization;
 }
 
 bool MaterialItem::hasRefractiveIndex() const
@@ -193,8 +166,8 @@ R3 MaterialItem::magnetization() const
 
 void MaterialItem::setMagnetization(const R3& magnetization)
 {
-    if (m_magnetization != magnetization) {
-        m_magnetization = magnetization;
+    if (m_magnetization.get() != magnetization) {
+        m_magnetization.set(magnetization);
         emit dataChanged();
     }
 }
@@ -215,7 +188,7 @@ void MaterialItem::serialize(Streamer& s)
     Serialize::rwValue(s, Tag::Name, m_name);
     Serialize::rwValue(s, Tag::Id, m_id);
     Serialize::rwValue(s, Tag::Color, m_color);
-    Serialize::rwValue(s, Tag::Magnetization, m_magnetization);
+    Serialize::rwProperty(s, /*Tag::Magnetization,*/ m_magnetization);
     Serialize::rwValue(s, Tag::UseRefractiveIndex, m_useRefractiveIndex);
 
     // read m_useRefractiveIndex before moving on
@@ -246,11 +219,6 @@ void MaterialItem::updateFrom(const MaterialItem& other)
     emit dataChanged();
 }
 
-QString MaterialItem::uidForDescriptor(const QString& postfix) const
-{
-    return "material/" + identifier() + "/" + postfix;
-}
-
 bool MaterialItem::operator==(const MaterialItem& other) const
 {
     if(m_useRefractiveIndex != other.m_useRefractiveIndex)
diff --git a/GUI/Model/Sample/MaterialItem.h b/GUI/Model/Sample/MaterialItem.h
index bde226c438d..22aa0c8e091 100644
--- a/GUI/Model/Sample/MaterialItem.h
+++ b/GUI/Model/Sample/MaterialItem.h
@@ -16,6 +16,7 @@
 #define BORNAGAIN_GUI_MODEL_SAMPLE_MATERIALITEM_H
 
 #include "GUI/Model/Descriptor/DoubleProperty.h"
+#include "GUI/Model/Descriptor/VectorProperty.h"
 #include <QColor>
 #include <QObject>
 #include <heinz/Complex.h>
@@ -84,10 +85,6 @@ public:
     void updateFrom(const MaterialItem& other);
 
 private:
-    //! Returns a unique identifier for descriptors.
-    //! String postfix will be appended for human convenience.
-    QString uidForDescriptor(const QString& postfix) const;
-
     //! Compares all contents. The inactive contents (e.g. SLD in case of refractive) are not taken
     //! into account. Only used by updateFrom.
     bool operator==(const MaterialItem& other) const;
@@ -95,10 +92,8 @@ private:
     QString m_name;
     QString m_id;
     QColor m_color;
-
-    R3 m_magnetization;
+    VectorProperty m_magnetization;
     bool m_useRefractiveIndex = true;
-
     DoubleProperty m_delta;
     DoubleProperty m_beta;
     DoubleProperty m_sldRe;
-- 
GitLab