From ae0746d1589a05e9243dfc60d12da98f2f0b9e27 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Thu, 9 Dec 2021 07:23:15 +0100
Subject: [PATCH] rm SessionItem signaling from MultiLayer (functionality
 changed to signal-free by the last commits)

rm obsolete unit tests
---
 GUI/Model/Job/ParameterTreeUtils.cpp  |  14 ++-
 GUI/Model/Sample/MultiLayerItem.cpp   |  17 ---
 GUI/Model/Sample/MultiLayerItem.h     |   3 -
 Tests/Unit/GUI/TestMapperForItem.cpp  |  19 ----
 Tests/Unit/GUI/TestMultiLayerItem.cpp | 144 --------------------------
 5 files changed, 13 insertions(+), 184 deletions(-)
 delete mode 100644 Tests/Unit/GUI/TestMultiLayerItem.cpp

diff --git a/GUI/Model/Job/ParameterTreeUtils.cpp b/GUI/Model/Job/ParameterTreeUtils.cpp
index 94c00843153..eb0d2a7de90 100644
--- a/GUI/Model/Job/ParameterTreeUtils.cpp
+++ b/GUI/Model/Job/ParameterTreeUtils.cpp
@@ -21,6 +21,7 @@
 #include "GUI/Model/Material/MaterialItem.h"
 #include "GUI/Model/Sample/InterferenceItems.h"
 #include "GUI/Model/Sample/Lattice2DItems.h"
+#include "GUI/Model/Sample/LayerItem.h"
 #include "GUI/Model/Sample/MultiLayerItem.h"
 #include "GUI/Model/Sample/ParticleCoreShellItem.h"
 #include "GUI/Model/Sample/ParticleItem.h"
@@ -134,7 +135,18 @@ void GUI::Model::ParameterTreeUtils::createParameterTree(JobItem* jobItem,
         }
     }
 
-    // add sample
+    // add sample.
+    // #baMigration To ignore thickness/roughness, they are disabled. This has to be changed after
+    // SessionModel migration. Compare also to handling in
+    // LayerForm::updateLayerPositionDependentElements()
+    for (auto* layer : jobItem->sampleItem()->layers()) {
+        const bool isFirstLayer = jobItem->sampleItem()->layers().first() == layer;
+        const bool isLastLayer = jobItem->sampleItem()->layers().last() == layer;
+
+        layer->setRoughnessEnabled(!isFirstLayer);
+        layer->setThicknessEnabled(!isFirstLayer && !isLastLayer);
+    }
+
     populateParameterContainer(container, jobItem->sampleItem(), recreateBackupValues);
 
     // add instrument
diff --git a/GUI/Model/Sample/MultiLayerItem.cpp b/GUI/Model/Sample/MultiLayerItem.cpp
index 5b783ba1e81..f4efbd14577 100644
--- a/GUI/Model/Sample/MultiLayerItem.cpp
+++ b/GUI/Model/Sample/MultiLayerItem.cpp
@@ -36,8 +36,6 @@ MultiLayerItem::MultiLayerItem() : SessionItem(M_TYPE)
 
     registerTag(T_LAYERS, 0, -1, {LayerItem::M_TYPE});
     setDefaultTag(T_LAYERS);
-
-    mapper()->setOnChildrenChange([this](SessionItem*) { updateLayers(); });
 }
 
 QVector<ItemWithMaterial*> MultiLayerItem::itemsWithMaterial() const
@@ -120,7 +118,6 @@ void MultiLayerItem::removeLayer(LayerItem* item)
     model()->removeItem(item);
 }
 
-
 void MultiLayerItem::moveLayer(LayerItem* item, LayerItem* beforeThisLayer)
 {
     int index = -1; // move to end
@@ -129,17 +126,3 @@ void MultiLayerItem::moveLayer(LayerItem* item, LayerItem* beforeThisLayer)
 
     model()->moveItem(item, this, index);
 }
-
-void MultiLayerItem::updateLayers()
-{
-    QVector<LayerItem*> list = childrenOfType<LayerItem>();
-    for (auto* it = list.begin(); it != list.end(); ++it) {
-        (*it)->setRoughnessEnabled(it != list.begin());
-
-        if (it == list.begin() || it == (list.end() - 1)) {
-            (*it)->setThicknessEnabled(false);
-            (*it)->thickness().set(0.0);
-        } else
-            (*it)->setThicknessEnabled(true);
-    }
-}
diff --git a/GUI/Model/Sample/MultiLayerItem.h b/GUI/Model/Sample/MultiLayerItem.h
index b5e42623853..e89ec57a230 100644
--- a/GUI/Model/Sample/MultiLayerItem.h
+++ b/GUI/Model/Sample/MultiLayerItem.h
@@ -62,9 +62,6 @@ public:
 
     void removeLayer(LayerItem* item);
     void moveLayer(LayerItem* item, LayerItem* beforeThisLayer);
-
-private:
-    void updateLayers();
 };
 
 #endif // BORNAGAIN_GUI_MODEL_SAMPLE_MULTILAYERITEM_H
diff --git a/Tests/Unit/GUI/TestMapperForItem.cpp b/Tests/Unit/GUI/TestMapperForItem.cpp
index e5178cd382c..174881cb6ca 100644
--- a/Tests/Unit/GUI/TestMapperForItem.cpp
+++ b/Tests/Unit/GUI/TestMapperForItem.cpp
@@ -176,25 +176,6 @@ TEST_F(TestMapperForItem, onPropertyChange)
                 && MultiLayerItem::isCrossCorrLengthPropertyName(w.m_reported_names[0]));
 }
 
-TEST_F(TestMapperForItem, onChildrenChange)
-{
-    Widget w;
-    SampleModel model;
-    auto* multilayer = model.insertItem<MultiLayerItem>();
-
-    // Mapper is looking on parent; adding new child to parent
-    setItem(multilayer, &w);
-    EXPECT_TRUE(m_mapped_item == multilayer);
-    model.insertItem<LayerItem>(multilayer);
-
-    EXPECT_EQ(w.m_onPropertyChangeCount, 0);
-    EXPECT_EQ(w.m_onChildPropertyChangeCount, 2);
-    EXPECT_EQ(w.m_onParentChangeCount, 0);
-    EXPECT_EQ(w.m_onChildrenChangeCount, 1);
-    EXPECT_EQ(w.m_reported_items.size(), 2);
-    EXPECT_EQ(w.m_reported_names.size(), 2);
-}
-
 TEST_F(TestMapperForItem, onSiblingsChange)
 {
     Widget w;
diff --git a/Tests/Unit/GUI/TestMultiLayerItem.cpp b/Tests/Unit/GUI/TestMultiLayerItem.cpp
deleted file mode 100644
index bf2f2bd60cb..00000000000
--- a/Tests/Unit/GUI/TestMultiLayerItem.cpp
+++ /dev/null
@@ -1,144 +0,0 @@
-#include "GUI/Model/Sample/LayerItem.h"
-#include "GUI/Model/Sample/LayerRoughnessItems.h"
-#include "GUI/Model/Sample/MultiLayerItem.h"
-#include "GUI/Model/Sample/SampleModel.h"
-#include "GUI/Model/Types/DoubleDescriptor.h"
-#include "Tests/GTestWrapper/google_test.h"
-
-class TestMultiLayerItem : public ::testing::Test {
-};
-
-//! Testing layer appearance (enabled, disabled) in a MultiLayer made of two default layers.
-//!
-//! In two layer system top and bottom layers should have disabled thickness and roughness.
-
-TEST_F(TestMultiLayerItem, twoLayerSystem)
-{
-    SampleModel model;
-
-    auto* multilayer = model.addMultiLayer();
-    auto* top = multilayer->addLayer();
-    auto* bottom = multilayer->addLayer();
-
-    // Thickness property should be disabled for top and bottom layers
-    EXPECT_FALSE(top->thicknessItem()->isEnabled());
-    EXPECT_FALSE(bottom->thicknessItem()->isEnabled());
-
-    // Thickness value should be 0.0 for top and bottom layers
-    EXPECT_EQ(top->thickness(), 0.0);
-    EXPECT_EQ(bottom->thickness(), 0.0);
-
-    // Roughness group property should be disabled for top, and enabled for bottom layers
-    EXPECT_FALSE(top->roughnessItem()->isEnabled());
-    EXPECT_TRUE(bottom->roughnessItem()->isEnabled());
-
-    // Default roughness should be "LayerZeroRoughness"
-    EXPECT_TRUE(std::holds_alternative<LayerZeroRoughnessItem*>(top->roughness().currentItem()));
-    EXPECT_TRUE(std::holds_alternative<LayerZeroRoughnessItem*>(bottom->roughness().currentItem()));
-}
-
-//! Testing layer appearance (enabled, disabled) in a MultiLayer made of three default layers.
-//!
-//! In three layer system middle layer's thickness/roughness should be enabled.
-
-TEST_F(TestMultiLayerItem, threeLayerSystem)
-{
-    SampleModel model;
-
-    auto* multilayer = model.addMultiLayer();
-    auto* top = multilayer->addLayer();
-    auto* middle = multilayer->addLayer();
-    auto* bottom = multilayer->addLayer();
-
-    // Thickness property should be disabled for top and bottom layers and enabled for middle
-    EXPECT_FALSE(top->thicknessItem()->isEnabled());
-    EXPECT_TRUE(middle->thicknessItem()->isEnabled());
-    EXPECT_FALSE(bottom->thicknessItem()->isEnabled());
-
-    // Thickness value should be 0.0 for top and bottom layers
-    EXPECT_EQ(top->thickness(), 0.0);
-    EXPECT_EQ(middle->thickness(), 0.0);
-    EXPECT_EQ(bottom->thickness(), 0.0);
-
-    // Roughness group property should be disabled for top, and enabled for other layers
-    EXPECT_FALSE(top->roughnessItem()->isEnabled());
-    EXPECT_TRUE(middle->roughnessItem()->isEnabled());
-    EXPECT_TRUE(bottom->roughnessItem()->isEnabled());
-
-    // Default roughness should be "LayerZeroRoughness"
-    EXPECT_TRUE(std::holds_alternative<LayerZeroRoughnessItem*>(top->roughness().currentItem()));
-    EXPECT_TRUE(std::holds_alternative<LayerZeroRoughnessItem*>(middle->roughness().currentItem()));
-    EXPECT_TRUE(std::holds_alternative<LayerZeroRoughnessItem*>(bottom->roughness().currentItem()));
-}
-
-//! Testing middle layer appearance when it is moved to the top.
-//!
-//! In three layer system, the moving of middle layer on top should lead to the disabling
-//! of roughness/thickness.
-
-TEST_F(TestMultiLayerItem, movingMiddleLayerOnTop)
-{
-    SampleModel model;
-
-    auto* multilayer = model.addMultiLayer();
-    auto* top = multilayer->addLayer();
-    auto* middle = multilayer->addLayer();
-    auto* bottom = multilayer->addLayer();
-
-    const double thickness = 10.0;
-    middle->thickness().set(thickness);
-
-    // Thickness property should be disabled for top and bottom layers and enabled for middle
-    EXPECT_FALSE(top->thicknessItem()->isEnabled());
-    EXPECT_TRUE(middle->thicknessItem()->isEnabled());
-    EXPECT_FALSE(bottom->thicknessItem()->isEnabled());
-
-    // Roughness group property should be disabled for top, and enabled for other layers
-    EXPECT_FALSE(top->roughnessItem()->isEnabled());
-    EXPECT_TRUE(middle->roughnessItem()->isEnabled());
-    EXPECT_TRUE(bottom->roughnessItem()->isEnabled());
-
-    // Thickness value should be 0.0 for top and bottom layers
-    EXPECT_EQ(top->thickness(), 0.0);
-    EXPECT_EQ(middle->thickness(), thickness);
-    EXPECT_EQ(bottom->thickness(), 0.0);
-
-    // Moving middle layer to top
-    multilayer->moveLayer(middle, top);
-    // checking that middle is top now, and top layer is middle now
-    EXPECT_EQ(middle, multilayer->layers().at(0));
-    EXPECT_EQ(top, multilayer->layers().at(1));
-
-    // Thickness and roughness of middle layer should be disabled now
-    EXPECT_FALSE(middle->thicknessItem()->isEnabled());
-    EXPECT_FALSE(middle->roughnessItem()->isEnabled());
-    // And, thickness of middle should become 0 to stress the fact that it become top
-    EXPECT_EQ(middle->thickness(), 0.0);
-
-    // Thickness and roughness of former top layer should be enabled now, since it is in the middle
-    EXPECT_TRUE(top->thicknessItem()->isEnabled());
-    EXPECT_TRUE(top->roughnessItem()->isEnabled());
-}
-
-//! Testing layer appearance when it is moved from a MultiLayer to canvas.
-//!
-//! If top layer was moved to canvas, its thickness and roughness should be reenabled.
-
-TEST_F(TestMultiLayerItem, movingLayerOnCanvas)
-{
-    SampleModel model;
-
-    auto* multilayer = model.addMultiLayer();
-    auto* top = model.insertItem<LayerItem>(multilayer);
-    model.insertItem<LayerItem>(multilayer);
-
-    // Moving top layer to canvas
-    model.moveItem(top, nullptr);
-
-    // checking that it was moved
-    EXPECT_EQ(top->parent(), model.rootItem());
-
-    // thickness should be reenabled
-    //    EXPECT_TRUE(top->getItem(LayerItem::P_THICKNESS)->isEnabled());
-    //    EXPECT_TRUE(top->getItem(LayerItem::P_THICKNESS)->isEnabled());
-}
-- 
GitLab