From 78c7925cb58f4b9f96160d47e1fe1f8f7420f324 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Wed, 22 Dec 2021 12:18:43 +0100
Subject: [PATCH] fix total density value on creation of widget

---
 GUI/Model/Sample/ParticleLayoutItem.cpp        | 16 ++++++++++++++--
 GUI/Model/Sample/ParticleLayoutItem.h          |  4 ++--
 GUI/View/SampleDesigner/ParticleLayoutForm.cpp |  9 ++-------
 3 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/GUI/Model/Sample/ParticleLayoutItem.cpp b/GUI/Model/Sample/ParticleLayoutItem.cpp
index a42046c9c8c..94e835697f7 100644
--- a/GUI/Model/Sample/ParticleLayoutItem.cpp
+++ b/GUI/Model/Sample/ParticleLayoutItem.cpp
@@ -41,7 +41,20 @@ DoubleDescriptor ParticleLayoutItem::ownDensity() const
     return m_ownDensity;
 }
 
-double ParticleLayoutItem::totalDensity() const
+DoubleDescriptor ParticleLayoutItem::totalDensity() const
+{
+    // create descriptor with same value as own density, then change the getter and setter
+    DoubleDescriptor d = ownDensity();
+    d.set = [=](double d) {
+        if (!totalDensityIsDefinedByInterference())
+            const_cast<DoubleProperty*>(&m_ownDensity)->set(d);
+    };
+    d.get = [=]() { return totalDensityValue(); };
+    d.path = [=]() { return m_ownDensity.uid() + "/totalDensity"; };
+    return d;
+}
+
+double ParticleLayoutItem::totalDensityValue() const
 {
     if (!totalDensityIsDefinedByInterference())
         return m_ownDensity;
@@ -63,7 +76,6 @@ double ParticleLayoutItem::totalDensity() const
     if (const auto* hd = dynamic_cast<const InterferenceHardDiskItem*>(m_interference.get()))
         return hd->density();
 
-
     ASSERT(false);
 }
 
diff --git a/GUI/Model/Sample/ParticleLayoutItem.h b/GUI/Model/Sample/ParticleLayoutItem.h
index 3f03e306017..b107adf0470 100644
--- a/GUI/Model/Sample/ParticleLayoutItem.h
+++ b/GUI/Model/Sample/ParticleLayoutItem.h
@@ -42,8 +42,8 @@ public:
     //!
     //! Returns ownDensity() if the interference is not influencing the density. If the
     //! interference is defining the density, this is the interference-calculated density.
-    double totalDensity() const;
-
+    DoubleDescriptor totalDensity() const;
+    double totalDensityValue() const;
     DoubleDescriptor weight() const;
 
     //! The particles this layout contains.
diff --git a/GUI/View/SampleDesigner/ParticleLayoutForm.cpp b/GUI/View/SampleDesigner/ParticleLayoutForm.cpp
index 797df9fa6ff..7420d22e679 100644
--- a/GUI/View/SampleDesigner/ParticleLayoutForm.cpp
+++ b/GUI/View/SampleDesigner/ParticleLayoutForm.cpp
@@ -34,7 +34,7 @@ ParticleLayoutForm::ParticleLayoutForm(LayerForm* parent, ParticleLayoutItem* la
 {
     FormLayouter layouter(this, ec);
     layouter.setContentsMargins(30, 6, 0, 0);
-    int rowOfTotalDensity = layouter.addValue(m_layoutItem->ownDensity());
+    int rowOfTotalDensity = layouter.addValue(m_layoutItem->totalDensity());
     m_totalDensitySpinBox =
         layouter.widgetAt<DoubleSpinBox*>(rowOfTotalDensity, QFormLayout::FieldRole);
     ASSERT(m_totalDensitySpinBox);
@@ -63,7 +63,6 @@ ParticleLayoutForm::ParticleLayoutForm(LayerForm* parent, ParticleLayoutItem* la
     m_layout = layouter.layout();
 
     updateDensityEnabling();
-    updateDensityValue();
     updateTitle(parent->layerItem());
 }
 
@@ -105,11 +104,7 @@ void ParticleLayoutForm::updateDensityEnabling()
 
 void ParticleLayoutForm::updateDensityValue()
 {
-    if (m_layoutItem->totalDensityIsDefinedByInterference()) {
-        QSignalBlocker b(m_totalDensitySpinBox);
-        m_totalDensitySpinBox->setBaseValue(m_layoutItem->totalDensity());
-    } else
-        m_totalDensitySpinBox->updateValue();
+    m_totalDensitySpinBox->updateValue();
 }
 
 void ParticleLayoutForm::updateTitle(const LayerItem* layerItem)
-- 
GitLab