From 05c18e5857cd6ac09e9616c00b806eb617c225e3 Mon Sep 17 00:00:00 2001
From: Dmitry Yurov <d.yurov@fz-juelich.de>
Date: Sun, 22 Oct 2017 19:43:55 +0200
Subject: [PATCH] Replace RefractionIndexItem with MaterialDataItem in GUI

Redmine: #1858
---
 Core/Export/ExportToPython.cpp                | 14 ++--
 GUI/coregui/Models/GUIObjectBuilder.cpp       |  4 +-
 GUI/coregui/Models/ItemFactory.cpp            |  4 +-
 GUI/coregui/Models/MaterialDataItem.cpp       | 72 +++++++++++++++++++
 ...fractiveIndexItem.h => MaterialDataItem.h} | 24 +++----
 GUI/coregui/Models/MaterialItem.cpp           | 20 +++---
 GUI/coregui/Models/MaterialItem.h             |  2 +-
 GUI/coregui/Models/MaterialModel.cpp          | 14 ++--
 GUI/coregui/Models/MaterialModel.h            |  2 +-
 GUI/coregui/Models/RefractiveIndexItem.cpp    | 71 ------------------
 GUI/coregui/Models/item_constants.h           |  2 +-
 .../Views/MaterialEditor/MaterialUtils.cpp    |  3 +-
 .../Views/PropertyEditor/ComponentEditor.cpp  |  2 +-
 Tests/UnitTests/GUI/TestMaterialModel.h       | 18 ++---
 14 files changed, 126 insertions(+), 126 deletions(-)
 create mode 100644 GUI/coregui/Models/MaterialDataItem.cpp
 rename GUI/coregui/Models/{RefractiveIndexItem.h => MaterialDataItem.h} (65%)
 delete mode 100644 GUI/coregui/Models/RefractiveIndexItem.cpp

diff --git a/Core/Export/ExportToPython.cpp b/Core/Export/ExportToPython.cpp
index 4e82e5a5e61..f60b81dede6 100644
--- a/Core/Export/ExportToPython.cpp
+++ b/Core/Export/ExportToPython.cpp
@@ -200,14 +200,14 @@ std::string ExportToPython::defineMaterials() const
             continue;
         visitedMaterials.insert(it->second);
         const HomogeneousMaterial* p_material = it->first;
-        complex_t ri = p_material->refractiveIndex();
-        double delta = 1.0 - std::real(ri);
-        double beta = std::imag(ri);
+        complex_t material_data = p_material->materialData();
+        double real = std::real(material_data);
+        double imag = std::imag(material_data);
         if (p_material->isScalarMaterial()) {
             result << indent() << m_label->labelMaterial(p_material)
                    << " = ba.HomogeneousMaterial(\"" << p_material->getName()
-                   << "\", " << printDouble(delta) << ", "
-                   << printDouble(beta) << ")\n";
+                   << "\", " << printDouble(real) << ", "
+                   << printDouble(imag) << ")\n";
         } else {
             kvector_t magnetic_field = p_material->magnetization();
             result << indent() << "magnetic_field = kvector_t(" << magnetic_field.x() << ", "
@@ -215,8 +215,8 @@ std::string ExportToPython::defineMaterials() const
                    << ")\n";
             result << indent() << m_label->labelMaterial(p_material)
                    << " = ba.HomogeneousMaterial(\"" << p_material->getName();
-            result << "\", " << printDouble(delta) << ", "
-                   << printDouble(beta) << ", "
+            result << "\", " << printDouble(real) << ", "
+                   << printDouble(imag) << ", "
                    << "magnetic_field)\n";
         }
     }
diff --git a/GUI/coregui/Models/GUIObjectBuilder.cpp b/GUI/coregui/Models/GUIObjectBuilder.cpp
index b76898e6746..cfb0cc14323 100644
--- a/GUI/coregui/Models/GUIObjectBuilder.cpp
+++ b/GUI/coregui/Models/GUIObjectBuilder.cpp
@@ -610,9 +610,9 @@ MaterialProperty GUIObjectBuilder::createMaterialFromDomain(
     MaterialModel* model = MaterialSvc::getMaterialModel();
 
     if(material->isScalarMaterial()) {
-        complex_t rindex = material->refractiveIndex();
+        complex_t material_data = material->materialData();
         MaterialItem* materialItem  =
-            model->addMaterial(materialName, 1-rindex.real(),rindex.imag());
+            model->addMaterial(materialName, material_data.real(),material_data.imag());
         return MaterialProperty(materialItem->getIdentifier());
     } else {
         throw GUIHelpers::Error("GUIObjectBuilder::createMaterialFromDomain()"
diff --git a/GUI/coregui/Models/ItemFactory.cpp b/GUI/coregui/Models/ItemFactory.cpp
index 3be70463ea4..3e65af73cea 100644
--- a/GUI/coregui/Models/ItemFactory.cpp
+++ b/GUI/coregui/Models/ItemFactory.cpp
@@ -50,13 +50,13 @@
 #include "ParticleLayoutItem.h"
 #include "PropertyItem.h"
 #include "RealDataItem.h"
-#include "RefractiveIndexItem.h"
 #include "ResolutionFunctionItems.h"
 #include "RotationItems.h"
 #include "SimulationOptionsItem.h"
 #include "TransformationItem.h"
 #include "VectorItem.h"
 #include "LinkInstrumentItem.h"
+#include "MaterialDataItem.h"
 #include "RealLimitsItems.h"
 #include "ProjectionItems.h"
 
@@ -154,7 +154,7 @@ ItemFactory::ItemMap_t initializeItemMap() {
 
     result[Constants::HomogeneousMaterialType] = &createInstance<MaterialItem>;
 
-    result[Constants::RefractiveIndexType] = &createInstance<RefractiveIndexItem>;
+    result[Constants::MaterialDataType] = &createInstance<MaterialDataItem>;
 
     result[Constants::MagneticFieldType] = &createInstance<MagneticFieldItem>;
 
diff --git a/GUI/coregui/Models/MaterialDataItem.cpp b/GUI/coregui/Models/MaterialDataItem.cpp
new file mode 100644
index 00000000000..f310c61a95f
--- /dev/null
+++ b/GUI/coregui/Models/MaterialDataItem.cpp
@@ -0,0 +1,72 @@
+// ************************************************************************** //
+//
+//  BornAgain: simulate and fit scattering at grazing incidence
+//
+//! @file      GUI/coregui/Models/RefractiveIndexItem.cpp
+//! @brief     Implements class RefractiveIndexItem
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2016
+//! @authors   Scientific Computing Group at MLZ Garching
+//! @authors   Céline Durniak, Marina Ganeva, David Li, Gennady Pospelov
+//! @authors   Walter Van Herck, Joachim Wuttke
+//
+// ************************************************************************** //
+
+#include "MaterialDataItem.h"
+
+#include "ScientificDoubleProperty.h"
+
+
+const QString MaterialDataItem::P_REAL = "real";
+const QString MaterialDataItem::P_IMAG = "imag";
+
+
+MaterialDataItem::MaterialDataItem()
+    : SessionItem(Constants::MaterialDataType)
+{
+    ScientificDoubleProperty real(0.0);
+    addProperty(P_REAL, real.getVariant());
+
+    ScientificDoubleProperty imag(0.0);
+    addProperty(P_IMAG, imag.getVariant());
+
+    mapper()->setOnPropertyChange(
+        [this](const QString &){
+            setValue(itemLabel());
+        }
+    );
+
+    setValue(itemLabel());
+    setEditable(false);
+}
+
+QString MaterialDataItem::itemLabel() const
+{
+    return QString("(1 - %1, %2)").arg(getReal()).arg(getImag());
+}
+
+double MaterialDataItem::getReal() const
+{
+    return getItemValue(P_REAL).value<ScientificDoubleProperty>().getValue();
+}
+
+void MaterialDataItem::setReal(double real)
+{
+    ScientificDoubleProperty property = getItemValue(P_REAL).value<ScientificDoubleProperty>();
+    property.setValue(real);
+    setItemValue(P_REAL, property.getVariant());
+}
+
+double MaterialDataItem::getImag() const
+{
+    return getItemValue(P_IMAG).value<ScientificDoubleProperty>().getValue();
+}
+
+void MaterialDataItem::setImag(double imag)
+{
+    ScientificDoubleProperty property = getItemValue(P_IMAG).value<ScientificDoubleProperty>();
+    property.setValue(imag);
+    setItemValue(P_IMAG, property.getVariant());
+}
diff --git a/GUI/coregui/Models/RefractiveIndexItem.h b/GUI/coregui/Models/MaterialDataItem.h
similarity index 65%
rename from GUI/coregui/Models/RefractiveIndexItem.h
rename to GUI/coregui/Models/MaterialDataItem.h
index 13a02f98c0c..218667fcebe 100644
--- a/GUI/coregui/Models/RefractiveIndexItem.h
+++ b/GUI/coregui/Models/MaterialDataItem.h
@@ -14,26 +14,26 @@
 //
 // ************************************************************************** //
 
-#ifndef REFRACTIVEINDEXITEM_H
-#define REFRACTIVEINDEXITEM_H
+#ifndef MATERIALDATAITEM_H
+#define MATERIALDATAITEM_H
 
 #include "SessionItem.h"
 
-class BA_CORE_API_ RefractiveIndexItem : public SessionItem
+class BA_CORE_API_ MaterialDataItem : public SessionItem
 {
 
 public:
-    static const QString P_DELTA;
-    static const QString P_BETA;
-    explicit RefractiveIndexItem();
-    virtual ~RefractiveIndexItem(){}
+    static const QString P_REAL;
+    static const QString P_IMAG;
+    explicit MaterialDataItem();
+    virtual ~MaterialDataItem(){}
     virtual QString itemLabel() const;
 
-    double getDelta() const;
-    void setDelta(double delta);
+    double getReal() const;
+    void setReal(double real);
 
-    double getBeta() const;
-    void setBeta(double beta);
+    double getImag() const;
+    void setImag(double imag);
 };
 
-#endif // REFRACTIVEINDEXITEM_H
+#endif // MATERIALDATAITEM_H
diff --git a/GUI/coregui/Models/MaterialItem.cpp b/GUI/coregui/Models/MaterialItem.cpp
index d6c5cfb1017..d47d3b436dd 100644
--- a/GUI/coregui/Models/MaterialItem.cpp
+++ b/GUI/coregui/Models/MaterialItem.cpp
@@ -17,8 +17,8 @@
 #include "MaterialItem.h"
 #include "GUIHelpers.h"
 #include "HomogeneousMaterial.h"
+#include "MaterialDataItem.h"
 #include "MaterialUtils.h"
-#include "RefractiveIndexItem.h"
 
 
 namespace {
@@ -27,7 +27,7 @@ const QString magnetization_tooltip =
 }
 
 const QString MaterialItem::P_COLOR = "Color";
-const QString MaterialItem::P_REFRACTIVE_INDEX = "Refractive index";
+const QString MaterialItem::P_MATERIAL_DATA = "Material data";
 const QString MaterialItem::P_MAGNETIZATION = "Magnetization";
 const QString MaterialItem::P_IDENTIFIER = "Identifier";
 
@@ -38,7 +38,7 @@ MaterialItem::MaterialItem()
 
     ColorProperty color;
     addProperty(P_COLOR, color.getVariant());
-    addGroupProperty(P_REFRACTIVE_INDEX, Constants::RefractiveIndexType);
+    addGroupProperty(P_MATERIAL_DATA, Constants::MaterialDataType);
     addGroupProperty(P_MAGNETIZATION, Constants::VectorType)->setToolTip(magnetization_tooltip);
     addProperty(P_IDENTIFIER, GUIHelpers::createUuid());
     getItem(P_IDENTIFIER)->setVisible(false);
@@ -57,15 +57,13 @@ QColor MaterialItem::getColor() const
 
 std::unique_ptr<HomogeneousMaterial> MaterialItem::createMaterial() const
 {
-    const RefractiveIndexItem *refractiveIndexItem
-        = dynamic_cast<const RefractiveIndexItem *>(
-            getItem(MaterialItem::P_REFRACTIVE_INDEX));
+    const MaterialDataItem* materialDataItem
+        = dynamic_cast<const MaterialDataItem*>(getItem(MaterialItem::P_MATERIAL_DATA));
 
-    Q_ASSERT(refractiveIndexItem);
+    Q_ASSERT(materialDataItem);
 
-    double delta = refractiveIndexItem->getDelta();
-    double beta = refractiveIndexItem->getBeta();
+    double real = materialDataItem->getReal();
+    double imag = materialDataItem->getImag();
 
-    return std::make_unique<HomogeneousMaterial>(
-                itemName().toStdString(), delta, beta);
+    return std::make_unique<HomogeneousMaterial>(itemName().toStdString(), real, imag);
 }
diff --git a/GUI/coregui/Models/MaterialItem.h b/GUI/coregui/Models/MaterialItem.h
index 12d4f0f8e3c..517b530c1a5 100644
--- a/GUI/coregui/Models/MaterialItem.h
+++ b/GUI/coregui/Models/MaterialItem.h
@@ -25,7 +25,7 @@ class BA_CORE_API_ MaterialItem : public SessionItem
 {
 public:
     static const QString P_COLOR;
-    static const QString P_REFRACTIVE_INDEX;
+    static const QString P_MATERIAL_DATA;
     static const QString P_MAGNETIZATION;
     static const QString P_IDENTIFIER;
     explicit MaterialItem();
diff --git a/GUI/coregui/Models/MaterialModel.cpp b/GUI/coregui/Models/MaterialModel.cpp
index 7af9a064679..45fed4afe18 100644
--- a/GUI/coregui/Models/MaterialModel.cpp
+++ b/GUI/coregui/Models/MaterialModel.cpp
@@ -16,8 +16,8 @@
 
 #include "MaterialModel.h"
 #include "MaterialUtils.h"
-#include "RefractiveIndexItem.h"
 #include "GUIHelpers.h"
+#include "MaterialDataItem.h"
 
 MaterialModel::MaterialModel(QObject* parent) : SessionModel(SessionXML::MaterialModelTag, parent)
 {
@@ -31,18 +31,18 @@ MaterialModel* MaterialModel::createCopy(SessionItem* parent)
     return result;
 }
 
-MaterialItem* MaterialModel::addMaterial(const QString& name, double delta, double beta)
+MaterialItem* MaterialModel::addMaterial(const QString& name, double material_data_real, double material_data_imag)
 {
     MaterialItem* materialItem
         = dynamic_cast<MaterialItem*>(insertNewItem(Constants::HomogeneousMaterialType));
     materialItem->setItemName(name);
 
-    RefractiveIndexItem* refractiveIndexItem = dynamic_cast<RefractiveIndexItem*>(
-        materialItem->getItem(MaterialItem::P_REFRACTIVE_INDEX));
-    Q_ASSERT(refractiveIndexItem);
+    MaterialDataItem* materialDataItem = dynamic_cast<MaterialDataItem*>(
+        materialItem->getItem(MaterialItem::P_MATERIAL_DATA));
+    Q_ASSERT(materialDataItem);
 
-    refractiveIndexItem->setDelta(delta);
-    refractiveIndexItem->setBeta(beta);
+    materialDataItem->setReal(material_data_real);
+    materialDataItem->setImag(material_data_imag);
 
     materialItem->setItemValue(MaterialItem::P_COLOR,
                                MaterialUtils::suggestMaterialColorProperty(name).getVariant());
diff --git a/GUI/coregui/Models/MaterialModel.h b/GUI/coregui/Models/MaterialModel.h
index 4f248cc7f92..50ba0ad69dd 100644
--- a/GUI/coregui/Models/MaterialModel.h
+++ b/GUI/coregui/Models/MaterialModel.h
@@ -32,7 +32,7 @@ public:
 
     virtual MaterialModel* createCopy(SessionItem* parent = 0);
 
-    MaterialItem* addMaterial(const QString& name, double delta = 0.0, double beta = 0.0);
+    MaterialItem* addMaterial(const QString& name, double material_data_real = 0.0, double material_data_imag = 0.0);
     void removeMaterial(MaterialItem*);
 
     MaterialItem* getMaterial(const QModelIndex& index);
diff --git a/GUI/coregui/Models/RefractiveIndexItem.cpp b/GUI/coregui/Models/RefractiveIndexItem.cpp
deleted file mode 100644
index 7f8d7cedd40..00000000000
--- a/GUI/coregui/Models/RefractiveIndexItem.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-// ************************************************************************** //
-//
-//  BornAgain: simulate and fit scattering at grazing incidence
-//
-//! @file      GUI/coregui/Models/RefractiveIndexItem.cpp
-//! @brief     Implements class RefractiveIndexItem
-//!
-//! @homepage  http://www.bornagainproject.org
-//! @license   GNU General Public License v3 or higher (see COPYING)
-//! @copyright Forschungszentrum Jülich GmbH 2016
-//! @authors   Scientific Computing Group at MLZ Garching
-//! @authors   Céline Durniak, Marina Ganeva, David Li, Gennady Pospelov
-//! @authors   Walter Van Herck, Joachim Wuttke
-//
-// ************************************************************************** //
-
-#include "RefractiveIndexItem.h"
-#include "ScientificDoubleProperty.h"
-
-
-const QString RefractiveIndexItem::P_DELTA = "delta";
-const QString RefractiveIndexItem::P_BETA = "beta";
-
-
-RefractiveIndexItem::RefractiveIndexItem()
-    : SessionItem(Constants::RefractiveIndexType)
-{
-    ScientificDoubleProperty delta(0.0);
-    addProperty(P_DELTA, delta.getVariant());
-
-    ScientificDoubleProperty beta(0.0);
-    addProperty(P_BETA, beta.getVariant());
-
-    mapper()->setOnPropertyChange(
-        [this](const QString &){
-            setValue(itemLabel());
-        }
-    );
-
-    setValue(itemLabel());
-    setEditable(false);
-}
-
-QString RefractiveIndexItem::itemLabel() const
-{
-    return QString("(1 - %1, %2)").arg(getDelta()).arg(getBeta());
-}
-
-double RefractiveIndexItem::getDelta() const
-{
-    return getItemValue(P_DELTA).value<ScientificDoubleProperty>().getValue();
-}
-
-void RefractiveIndexItem::setDelta(double delta)
-{
-    ScientificDoubleProperty property = getItemValue(P_DELTA).value<ScientificDoubleProperty>();
-    property.setValue(delta);
-    setItemValue(P_DELTA, property.getVariant());
-}
-
-double RefractiveIndexItem::getBeta() const
-{
-    return getItemValue(P_BETA).value<ScientificDoubleProperty>().getValue();
-}
-
-void RefractiveIndexItem::setBeta(double beta)
-{
-    ScientificDoubleProperty property = getItemValue(P_BETA).value<ScientificDoubleProperty>();
-    property.setValue(beta);
-    setItemValue(P_BETA, property.getVariant());
-}
diff --git a/GUI/coregui/Models/item_constants.h b/GUI/coregui/Models/item_constants.h
index 4f49604b1f8..bc1160bc448 100644
--- a/GUI/coregui/Models/item_constants.h
+++ b/GUI/coregui/Models/item_constants.h
@@ -120,7 +120,7 @@ const ModelType HexagonalLatticeType = "HexagonalLattice";
 
 const ModelType HomogeneousMaterialType = "HomogeneousMaterial";
 
-const ModelType RefractiveIndexType = "RefractiveIndex";
+const ModelType MaterialDataType = "MaterialData";
 
 const ModelType MagneticFieldType = "MagneticField";
 
diff --git a/GUI/coregui/Views/MaterialEditor/MaterialUtils.cpp b/GUI/coregui/Views/MaterialEditor/MaterialUtils.cpp
index 3b38b083f68..dae92786ec0 100644
--- a/GUI/coregui/Views/MaterialEditor/MaterialUtils.cpp
+++ b/GUI/coregui/Views/MaterialEditor/MaterialUtils.cpp
@@ -15,6 +15,8 @@
 // ************************************************************************** //
 
 #include "MaterialUtils.h"
+
+#include "MaterialDataItem.h"
 #include "ComboProperty.h"
 #include "DesignerHelper.h"
 #include "GUIHelpers.h"
@@ -22,7 +24,6 @@
 #include "MagneticFieldItem.h"
 #include "MaterialModel.h"
 #include "MaterialSvc.h"
-#include "RefractiveIndexItem.h"
 #include "ParticleItem.h"
 #include "LayerItem.h"
 
diff --git a/GUI/coregui/Views/PropertyEditor/ComponentEditor.cpp b/GUI/coregui/Views/PropertyEditor/ComponentEditor.cpp
index 5811295139f..8b0f76bf986 100644
--- a/GUI/coregui/Views/PropertyEditor/ComponentEditor.cpp
+++ b/GUI/coregui/Views/PropertyEditor/ComponentEditor.cpp
@@ -193,7 +193,7 @@ ComponentEditor::componentItems(SessionItem *item) const
             result.append(child);
         }
 
-        else if (child->modelType() == Constants::RefractiveIndexType) {
+        else if (child->modelType() == Constants::MaterialDataType) {
             result.append(child);
         }
 
diff --git a/Tests/UnitTests/GUI/TestMaterialModel.h b/Tests/UnitTests/GUI/TestMaterialModel.h
index 90a4c516418..c641c36b7aa 100644
--- a/Tests/UnitTests/GUI/TestMaterialModel.h
+++ b/Tests/UnitTests/GUI/TestMaterialModel.h
@@ -1,6 +1,6 @@
 #include "MaterialModel.h"
 #include "MaterialItem.h"
-#include "RefractiveIndexItem.h"
+#include "MaterialDataItem.h"
 #include <QtTest>
 #include <memory>
 
@@ -27,10 +27,10 @@ inline void TestMaterialModel::test_addMaterial()
     QCOMPARE(model->rowCount(QModelIndex()), 1);
 
     QCOMPARE(item->itemName(), name);
-    const RefractiveIndexItem *refIndex = dynamic_cast<const RefractiveIndexItem *>(
-        item->getItem(MaterialItem::P_REFRACTIVE_INDEX));
-    QCOMPARE(refIndex->getDelta(), delta);
-    QCOMPARE(refIndex->getBeta(), beta);
+    const MaterialDataItem *refIndex = dynamic_cast<const MaterialDataItem *>(
+        item->getItem(MaterialItem::P_MATERIAL_DATA));
+    QCOMPARE(refIndex->getReal(), delta);
+    QCOMPARE(refIndex->getImag(), beta);
 
 }
 
@@ -57,10 +57,10 @@ inline void TestMaterialModel::test_cloneMaterial()
     // checking name of cloned material
     QCOMPARE(item->itemName()+" (clone)", clonedMaterial->itemName());
 
-    const RefractiveIndexItem *refIndex = dynamic_cast<const RefractiveIndexItem *>(
-        clonedMaterial->getItem(MaterialItem::P_REFRACTIVE_INDEX));
-    QCOMPARE(refIndex->getDelta(), delta);
-    QCOMPARE(refIndex->getBeta(), beta);
+    const MaterialDataItem *refIndex = dynamic_cast<const MaterialDataItem *>(
+        clonedMaterial->getItem(MaterialItem::P_MATERIAL_DATA));
+    QCOMPARE(refIndex->getReal(), delta);
+    QCOMPARE(refIndex->getImag(), beta);
 
 
 }
-- 
GitLab