From 930cb1aa05330e45508e688226989050c17c25c7 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Mon, 31 Jan 2022 21:56:43 +0100
Subject: [PATCH] adapt editors to changed item classes

---
 GUI/View/Instrument/AxisPropertyEditor.cpp    | 71 +++++++++++++++++++
 GUI/View/Instrument/AxisPropertyEditor.h      | 47 ++++++++++++
 .../Instrument/DepthProbeInstrumentEditor.cpp | 12 ++--
 GUI/View/Instrument/GISASBeamEditor.cpp       | 17 +++--
 GUI/View/Instrument/OffSpecularBeamEditor.cpp | 11 ++-
 .../Instrument/PolarizationAnalysisEditor.cpp |  4 +-
 GUI/View/Instrument/SpecularBeamEditor.cpp    |  6 +-
 .../Instrument/SphericalDetectorEditor.cpp    | 11 ++-
 8 files changed, 147 insertions(+), 32 deletions(-)
 create mode 100644 GUI/View/Instrument/AxisPropertyEditor.cpp
 create mode 100644 GUI/View/Instrument/AxisPropertyEditor.h

diff --git a/GUI/View/Instrument/AxisPropertyEditor.cpp b/GUI/View/Instrument/AxisPropertyEditor.cpp
new file mode 100644
index 00000000000..bde22ddd05d
--- /dev/null
+++ b/GUI/View/Instrument/AxisPropertyEditor.cpp
@@ -0,0 +1,71 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/View/Instrument/AxisPropertyEditor.cpp
+//! @brief     Implement class AxisPropertyEditor
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2022
+//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
+//
+//  ************************************************************************************************
+
+#include "GUI/View/Instrument/AxisPropertyEditor.h"
+#include "GUI/Model/Item/AxesItems.h"
+#include "GUI/Model/Types/AxisProperty.h"
+#include "GUI/View/Edit/DoubleSpinBox.h"
+#include "GUI/View/Tool/WidgetUtils.h"
+#include <QFormLayout>
+#include <QGroupBox>
+#include <QSpinBox>
+
+AxisPropertyEditor::AxisPropertyEditor(QWidget* parent, const QString& groupTitle,
+                                       AxisProperty* axisProperty)
+    : QGroupBox(groupTitle, parent)
+    , m_axisProperty(axisProperty)
+{
+    auto* formLayout = new QFormLayout(this);
+
+    nbinsSpinBox = GUI::Util::createSpinBox(formLayout, axisProperty->nbins());
+    m_minSpinBox = GUI::Util::createSpinBox(formLayout, axisProperty->min());
+    m_maxSpinBox = GUI::Util::createSpinBox(formLayout, axisProperty->max());
+
+    connect(nbinsSpinBox, qOverload<int>(&QSpinBox::valueChanged), [=](int v) {
+        if (axisProperty->nbins() != v) {
+            axisProperty->setNbins(v);
+            emit dataChanged();
+        }
+    });
+
+    connect(m_minSpinBox, qOverload<double>(&DoubleSpinBox::baseValueChanged), [=](double v) {
+        if (m_axisProperty->min() != v) {
+            m_axisProperty->setMin(v);
+            emit dataChanged();
+            if (m_axisProperty->max() < v) {
+                m_axisProperty->setMax(v);
+                m_maxSpinBox->updateValue();
+            }
+        }
+    });
+
+    connect(m_maxSpinBox, qOverload<double>(&DoubleSpinBox::baseValueChanged), [=](double v) {
+        if (m_axisProperty->max() != v) {
+            m_axisProperty->setMax(v);
+            emit dataChanged();
+            if (m_axisProperty->min() > v) {
+                m_axisProperty->setMin(v);
+                m_minSpinBox->updateValue();
+            }
+        }
+    });
+}
+
+void AxisPropertyEditor::updateData()
+{
+    QSignalBlocker b(nbinsSpinBox);
+    nbinsSpinBox->setValue(m_axisProperty->nbins());
+    m_minSpinBox->updateValue();
+    m_maxSpinBox->updateValue();
+}
diff --git a/GUI/View/Instrument/AxisPropertyEditor.h b/GUI/View/Instrument/AxisPropertyEditor.h
new file mode 100644
index 00000000000..2c3bb3c5b3b
--- /dev/null
+++ b/GUI/View/Instrument/AxisPropertyEditor.h
@@ -0,0 +1,47 @@
+//  ************************************************************************************************
+//
+//  BornAgain: simulate and fit reflection and scattering
+//
+//! @file      GUI/View/Instrument/AxisPropertyEditor.h
+//! @brief     Defines class AxisPropertyEditor
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2022
+//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
+//
+//  ************************************************************************************************
+
+#ifndef BORNAGAIN_GUI_VIEW_INSTRUMENT_AxisPropertyEditor_H
+#define BORNAGAIN_GUI_VIEW_INSTRUMENT_AxisPropertyEditor_H
+
+#include <QGroupBox>
+
+class QSpinBox;
+class AxisProperty;
+class DoubleSpinBox;
+
+/// The form for a spherical axis: contains the bare bone widgets for the user to enter
+/// the number of bins and to select the range in degrees (minimum & maximum)
+/// The input widgets will be inserted in the form given in the constructor
+
+class AxisPropertyEditor : public QGroupBox {
+    Q_OBJECT
+public:
+    AxisPropertyEditor(QWidget* parent, const QString& groupTitle, AxisProperty* axisProperty);
+
+    //! Reload UI from data
+    void updateData();
+
+signals:
+    void dataChanged();
+
+private:
+    QSpinBox* nbinsSpinBox;
+    AxisProperty* m_axisProperty;
+    DoubleSpinBox* m_minSpinBox;
+    DoubleSpinBox* m_maxSpinBox;
+};
+
+
+#endif // BORNAGAIN_GUI_VIEW_INSTRUMENT_AxisPropertyEditor_H
diff --git a/GUI/View/Instrument/DepthProbeInstrumentEditor.cpp b/GUI/View/Instrument/DepthProbeInstrumentEditor.cpp
index 5ca30903605..75fd2da2181 100644
--- a/GUI/View/Instrument/DepthProbeInstrumentEditor.cpp
+++ b/GUI/View/Instrument/DepthProbeInstrumentEditor.cpp
@@ -15,6 +15,7 @@
 #include "GUI/View/Instrument/DepthProbeInstrumentEditor.h"
 #include "GUI/Model/Item/BeamWavelengthItem.h"
 #include "GUI/Model/Item/InstrumentItems.h"
+#include "GUI/View/Instrument/AxisPropertyEditor.h"
 #include "GUI/View/Instrument/Detail/CreateDetails.h"
 #include "GUI/View/Instrument/DistributionEditor.h"
 #include "GUI/View/Instrument/InclinationAnglesEditor.h"
@@ -39,15 +40,16 @@ DepthProbeInstrumentEditor::DepthProbeInstrumentEditor(QWidget* parent,
     MeanConfig wave_cfg{boost::numeric::interval<double>(std::numeric_limits<double>::min(),
                                                          std::numeric_limits<double>::max()),
                         4, true};
-    auto* m_wavelengthEditor = new DistributionEditor("Wavelength [nm]", wave_cfg,
-                                                      GUI::ID::Distributions::Symmetric, this);
+    auto* m_wavelengthEditor =
+        new DistributionEditor("Wavelength [nm]", wave_cfg, GUI::ID::Distributions::Symmetric, this,
+                               instrument->beamItem()->wavelengthItem());
     grid->addWidget(m_wavelengthEditor, 1, 0);
 
     auto* inclinationEditor =
         new InclinationAnglesEditor(this, instrument->beamItem()->inclinationAngleItem());
     grid->addWidget(inclinationEditor, 1, 1);
 
-    auto* depthAxisEditor = new SphericalAxisEditor("Depth axis [nm]", this, instrument->zAxis());
+    auto* depthAxisEditor = new AxisPropertyEditor(this, "Depth axis [nm]", &instrument->zAxis());
     grid->addWidget(depthAxisEditor, 1, 2);
 
     layout->addWidget(GUI::Detail::createDetailsWidget(grid, "Parameters"));
@@ -58,13 +60,11 @@ DepthProbeInstrumentEditor::DepthProbeInstrumentEditor(QWidget* parent,
 
     layout->addStretch();
 
-    m_wavelengthEditor->setItem(instrument->beamItem()->wavelengthItem());
-
     connect(m_wavelengthEditor, &DistributionEditor::distributionChanged, this,
             &DepthProbeInstrumentEditor::dataChanged);
     connect(inclinationEditor, &InclinationAnglesEditor::dataChanged, this,
             &DepthProbeInstrumentEditor::dataChanged);
-    connect(depthAxisEditor, &SphericalAxisEditor::dataChanged, this,
+    connect(depthAxisEditor, &AxisPropertyEditor::dataChanged, this,
             &DepthProbeInstrumentEditor::dataChanged);
     connect(polpairAnalysisEditor, &PolarizationAnalysisEditor::dataChanged, this,
             &DepthProbeInstrumentEditor::dataChanged);
diff --git a/GUI/View/Instrument/GISASBeamEditor.cpp b/GUI/View/Instrument/GISASBeamEditor.cpp
index c1577363934..d855f496f4d 100644
--- a/GUI/View/Instrument/GISASBeamEditor.cpp
+++ b/GUI/View/Instrument/GISASBeamEditor.cpp
@@ -43,26 +43,25 @@ GISASBeamEditor::GISASBeamEditor(QWidget* parent, BeamItem* item)
     MeanConfig wave_cfg{boost::numeric::interval<double>(std::numeric_limits<double>::min(),
                                                          std::numeric_limits<double>::max()),
                         4, true};
-    auto* wavelengthEditor =
-        new DistributionEditor("Wavelength [nm]", wave_cfg, GUI::ID::Distributions::All, this);
+    auto* wavelengthEditor = new DistributionEditor(
+        "Wavelength [nm]", wave_cfg, GUI::ID::Distributions::All, this, item->wavelengthItem());
     grid->addWidget(wavelengthEditor, 1, 0);
 
     // valid input range is [0,90]
     MeanConfig incl_cfg{boost::numeric::interval<double>(0.0, 90.0), 3, false};
-    auto* inclinationEditor = new DistributionEditor("Inclination angle [deg]", incl_cfg,
-                                                     GUI::ID::Distributions::All, this);
+    auto* inclinationEditor =
+        new DistributionEditor("Inclination angle [deg]", incl_cfg, GUI::ID::Distributions::All,
+                               this, item->inclinationAngleItem());
     grid->addWidget(inclinationEditor, 1, 1);
 
     // valid input range is [-90,90]
     MeanConfig azim_cfg{boost::numeric::interval<double>(-90.0, 90.0), 3, false};
-    auto* azimuthalEditor = new DistributionEditor("Azimuthal angle [deg]", azim_cfg,
-                                                   GUI::ID::Distributions::All, this);
+    auto* azimuthalEditor =
+        new DistributionEditor("Azimuthal angle [deg]", azim_cfg, GUI::ID::Distributions::All, this,
+                               item->azimuthalAngleItem());
     grid->addWidget(azimuthalEditor, 1, 2);
 
     intensityEditor->setText(QString::number(item->intensity()));
-    wavelengthEditor->setItem(item->wavelengthItem());
-    inclinationEditor->setItem(item->inclinationAngleItem());
-    azimuthalEditor->setItem(item->azimuthalAngleItem());
 
     connect(wavelengthEditor, &DistributionEditor::distributionChanged, this,
             &GISASBeamEditor::dataChanged);
diff --git a/GUI/View/Instrument/OffSpecularBeamEditor.cpp b/GUI/View/Instrument/OffSpecularBeamEditor.cpp
index 0fb02dfef0d..9b3424084bf 100644
--- a/GUI/View/Instrument/OffSpecularBeamEditor.cpp
+++ b/GUI/View/Instrument/OffSpecularBeamEditor.cpp
@@ -49,7 +49,8 @@ OffSpecularBeamEditor::OffSpecularBeamEditor(QWidget* parent, OffSpecularInstrum
                                                          std::numeric_limits<double>::max()),
                         4, true};
     auto* wavelengthEditor =
-        new DistributionEditor("Wavelength [nm]", wave_cfg, GUI::ID::Distributions::All, this);
+        new DistributionEditor("Wavelength [nm]", wave_cfg, GUI::ID::Distributions::All, this,
+                               item->beamItem()->wavelengthItem());
     grid->addWidget(wavelengthEditor, 1, 0);
 
     auto* inclinationEditor =
@@ -58,15 +59,13 @@ OffSpecularBeamEditor::OffSpecularBeamEditor(QWidget* parent, OffSpecularInstrum
 
     // valid input range is [-90,90]
     MeanConfig azim_cfg{boost::numeric::interval<double>(-90.0, 90.0), 3, false};
-    auto* azimuthalEditor = new DistributionEditor("Azimuthal angle [deg]", azim_cfg,
-                                                   GUI::ID::Distributions::All, this);
+    auto* azimuthalEditor =
+        new DistributionEditor("Azimuthal angle [deg]", azim_cfg, GUI::ID::Distributions::All, this,
+                               item->beamItem()->azimuthalAngleItem());
     grid->addWidget(azimuthalEditor, 1, 2);
 
     intensityEditor->setText(QString::number(item->beamItem()->intensity()));
 
-    wavelengthEditor->setItem(item->beamItem()->wavelengthItem());
-    azimuthalEditor->setItem(item->beamItem()->azimuthalAngleItem());
-
     connect(wavelengthEditor, &DistributionEditor::distributionChanged, this,
             &OffSpecularBeamEditor::dataChanged);
     connect(inclinationEditor, &SphericalAxisEditor::dataChanged, this,
diff --git a/GUI/View/Instrument/PolarizationAnalysisEditor.cpp b/GUI/View/Instrument/PolarizationAnalysisEditor.cpp
index d9b4e74e3a2..ec1443cb0d1 100644
--- a/GUI/View/Instrument/PolarizationAnalysisEditor.cpp
+++ b/GUI/View/Instrument/PolarizationAnalysisEditor.cpp
@@ -82,8 +82,8 @@ PolarizationAnalysisEditor::PolarizationAnalysisEditor(QWidget* parent, Instrume
     onCheckBoxToggled();
 
     m_checkbox->setChecked(m_instrument->withPolarizerAnalyzer());
-    m_polarizerEditor->setVector(m_instrument->polarizationVector());
-    m_analyzerEditor->setVector(m_instrument->analyzerDirectionVector());
+    m_polarizerEditor->setVector(m_instrument->polarization());
+    m_analyzerEditor->setVector(m_instrument->analyzerDirection());
 
     connect(m_polarizerEditor, &VectorEditor::vectorChanged, this,
             &PolarizationAnalysisEditor::dataChanged);
diff --git a/GUI/View/Instrument/SpecularBeamEditor.cpp b/GUI/View/Instrument/SpecularBeamEditor.cpp
index db8ae4f8c00..c73a2e6e1d4 100644
--- a/GUI/View/Instrument/SpecularBeamEditor.cpp
+++ b/GUI/View/Instrument/SpecularBeamEditor.cpp
@@ -50,8 +50,9 @@ SpecularBeamEditor::SpecularBeamEditor(QWidget* parent, SpecularBeamItem* item,
     MeanConfig wave_cfg{boost::numeric::interval<double>(std::numeric_limits<double>::min(),
                                                          std::numeric_limits<double>::max()),
                         4, true};
-    auto* wavelengthEditor = new DistributionEditor("Wavelength [nm]", wave_cfg,
-                                                    GUI::ID::Distributions::Symmetric, this);
+    auto* wavelengthEditor =
+        new DistributionEditor("Wavelength [nm]", wave_cfg, GUI::ID::Distributions::Symmetric, this,
+                               item->wavelengthItem());
     grid->addWidget(wavelengthEditor, 1, 0);
 
     auto* inclinationEditor = new InclinationAnglesEditor(this, item->inclinationAngleItem());
@@ -61,7 +62,6 @@ SpecularBeamEditor::SpecularBeamEditor(QWidget* parent, SpecularBeamItem* item,
     grid->addWidget(footprintEditor, 1, 2);
 
     intensityLineEdit->setText(QString::number(item->intensity()));
-    wavelengthEditor->setItem(item->wavelengthItem());
 
     // order of the next two connections is important! Indicators have to be recalculated first,
     // then updated (recalculation is done in EditController)
diff --git a/GUI/View/Instrument/SphericalDetectorEditor.cpp b/GUI/View/Instrument/SphericalDetectorEditor.cpp
index d307d21d350..bac7e55729c 100644
--- a/GUI/View/Instrument/SphericalDetectorEditor.cpp
+++ b/GUI/View/Instrument/SphericalDetectorEditor.cpp
@@ -14,9 +14,8 @@
 
 #include "GUI/View/Instrument/SphericalDetectorEditor.h"
 #include "GUI/Model/Item/SphericalDetectorItem.h"
+#include "GUI/View/Instrument/AxisPropertyEditor.h"
 #include "GUI/View/Instrument/ResolutionFunctionEditor.h"
-#include "GUI/View/Instrument/SphericalAxisEditor.h"
-
 #include <QGridLayout>
 
 SphericalDetectorEditor::SphericalDetectorEditor(QWidget* parent, SphericalDetectorItem* item)
@@ -28,10 +27,10 @@ SphericalDetectorEditor::SphericalDetectorEditor(QWidget* parent, SphericalDetec
     grid->setColumnStretch(1, 1);
     grid->setColumnStretch(2, 1);
 
-    auto* phiAxisEditor = new SphericalAxisEditor(u8"\u03c6 axis", this, item->phiAxisItem());
+    auto* phiAxisEditor = new AxisPropertyEditor(this, u8"\u03c6 axis", &item->phiAxis());
     grid->addWidget(phiAxisEditor, 1, 0);
 
-    auto* alphaAxisEditor = new SphericalAxisEditor(u8"\u03b1 axis", this, item->alphaAxisItem());
+    auto* alphaAxisEditor = new AxisPropertyEditor(this, u8"\u03b1 axis", &item->alphaAxis());
     grid->addWidget(alphaAxisEditor, 1, 1);
 
     auto* resolutionFunctionEditor =
@@ -40,10 +39,10 @@ SphericalDetectorEditor::SphericalDetectorEditor(QWidget* parent, SphericalDetec
 
     grid->setRowStretch(2, 1);
 
-    connect(phiAxisEditor, &SphericalAxisEditor::dataChanged, this,
+    connect(phiAxisEditor, &AxisPropertyEditor::dataChanged, this,
             &SphericalDetectorEditor::dataChanged);
 
-    connect(alphaAxisEditor, &SphericalAxisEditor::dataChanged, this,
+    connect(alphaAxisEditor, &AxisPropertyEditor::dataChanged, this,
             &SphericalDetectorEditor::dataChanged);
 
     connect(resolutionFunctionEditor, &ResolutionFunctionEditor::dataChanged, this,
-- 
GitLab