From 82164dd440041239981e2b8a1d9025a9bdda6bc8 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Wed, 24 Jan 2018 16:46:37 +0100
Subject: [PATCH] ParticleLayoutItem::updatePropertyAppearance simplified

---
 GUI/coregui/Models/ModelMapper.cpp           |  4 +++-
 GUI/coregui/Models/ParticleLayoutItem.cpp    | 19 +++++++------------
 GUI/coregui/Models/ParticleLayoutItem.h      |  2 +-
 Tests/UnitTests/GUI/TestParticleLayoutItem.h |  5 ++++-
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/GUI/coregui/Models/ModelMapper.cpp b/GUI/coregui/Models/ModelMapper.cpp
index 6f8bd79a81f..ec410812aec 100644
--- a/GUI/coregui/Models/ModelMapper.cpp
+++ b/GUI/coregui/Models/ModelMapper.cpp
@@ -311,11 +311,13 @@ void ModelMapper::onRowRemoved(const QModelIndex& parent, int first, int /*last*
     int nestling = nestlingDepth(m_model->itemForIndex(parent));
 
     if (nestling >= 0 || m_model->itemForIndex(parent) == m_item->parent()) {
-        callOnChildrenChange(nullptr);
         callOnAnyChildChange(nullptr);
         callOnSiblingsChange();
     }
 
+    if (nestling == 0 )
+        callOnChildrenChange(nullptr);
+
     if (m_aboutToDelete.isValid() && m_aboutToDelete == parent.child(first, 0))
         clearMapper();
 }
diff --git a/GUI/coregui/Models/ParticleLayoutItem.cpp b/GUI/coregui/Models/ParticleLayoutItem.cpp
index 1a6b52dc2a5..b244b270767 100644
--- a/GUI/coregui/Models/ParticleLayoutItem.cpp
+++ b/GUI/coregui/Models/ParticleLayoutItem.cpp
@@ -60,8 +60,8 @@ ParticleLayoutItem::ParticleLayoutItem() : SessionGraphicsItem(Constants::Partic
                                           << Constants::InterferenceFunction1DLatticeType
                                           << Constants::InterferenceFunction2DLatticeType);
 
-    mapper()->setOnChildrenChange([this](SessionItem* item) {
-        updateDensityAppearance(item);
+    mapper()->setOnChildrenChange([this](SessionItem*) {
+        updateDensityAppearance();
     });
 }
 
@@ -69,15 +69,10 @@ ParticleLayoutItem::ParticleLayoutItem() : SessionGraphicsItem(Constants::Partic
 //! Two dimensional interference calculates density automatically, so property should
 //! be disabled.
 
-void ParticleLayoutItem::updateDensityAppearance(SessionItem* item)
+void ParticleLayoutItem::updateDensityAppearance()
 {
-    int count = 0;
-    for (auto child_item : children())
-        if (isInterference2D(child_item->modelType()))
-            count++;
-
-    if ((item && count > 0) || (!item && count > 1))
-        getItem(P_TOTAL_DENSITY)->setEnabled(false);
-    else
-        getItem(P_TOTAL_DENSITY)->setEnabled(true);
+    getItem(P_TOTAL_DENSITY)->setEnabled(true);
+    if(auto interferenceItem = getItem(T_INTERFERENCE))
+        if (isInterference2D(interferenceItem->modelType()))
+            getItem(P_TOTAL_DENSITY)->setEnabled(false);
 }
diff --git a/GUI/coregui/Models/ParticleLayoutItem.h b/GUI/coregui/Models/ParticleLayoutItem.h
index cd51292378c..f49870e6399 100644
--- a/GUI/coregui/Models/ParticleLayoutItem.h
+++ b/GUI/coregui/Models/ParticleLayoutItem.h
@@ -27,7 +27,7 @@ public:
     ParticleLayoutItem();
 
 private:
-    void updateDensityAppearance(SessionItem* item);
+    void updateDensityAppearance();
 };
 
 #endif // PARTICLELAYOUTITEM_H
diff --git a/Tests/UnitTests/GUI/TestParticleLayoutItem.h b/Tests/UnitTests/GUI/TestParticleLayoutItem.h
index 5093c3584d2..574cec8fa9f 100644
--- a/Tests/UnitTests/GUI/TestParticleLayoutItem.h
+++ b/Tests/UnitTests/GUI/TestParticleLayoutItem.h
@@ -29,10 +29,12 @@ TEST_F(TestParticleLayoutItem, densityAppearance)
     auto interference
         = model.insertNewItem(Constants::InterferenceFunctionRadialParaCrystalType,
                               model.indexOfItem(layout), -1, ParticleLayoutItem::T_INTERFERENCE);
+    EXPECT_TRUE(layout->getItem(ParticleLayoutItem::T_INTERFERENCE) == interference);
     EXPECT_TRUE(layout->getItem(ParticleLayoutItem::P_TOTAL_DENSITY)->isEnabled());
 
     // removing paracrystal, TotalDensity still enabled
     layout->takeRow(ParentRow(*interference));
+    EXPECT_TRUE(layout->getItem(ParticleLayoutItem::T_INTERFERENCE) == nullptr);
     EXPECT_TRUE(layout->getItem(ParticleLayoutItem::P_TOTAL_DENSITY)->isEnabled());
     delete interference;
 
@@ -44,6 +46,7 @@ TEST_F(TestParticleLayoutItem, densityAppearance)
 
     // removing 2D interference, TotalIntensity should be reenabled
     layout->takeRow(ParentRow(*interference));
-    EXPECT_TRUE(layout->getItem(ParticleLayoutItem::P_TOTAL_DENSITY)->isEnabled());
     delete interference;
+    EXPECT_TRUE(layout->getItem(ParticleLayoutItem::T_INTERFERENCE) == nullptr);
+    EXPECT_TRUE(layout->getItem(ParticleLayoutItem::P_TOTAL_DENSITY)->isEnabled());
 }
-- 
GitLab