From 1ad5d422eb66644be4d4768cfb11ba03df631a44 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Thu, 2 Dec 2021 15:45:10 +0100
Subject: [PATCH] add missing material initializations

---
 GUI/Model/From/GUIDomainSampleVisitor.cpp     |  4 ++--
 GUI/Model/Sample/ParticleCoreShellItem.cpp    | 15 +++++++++++----
 GUI/Model/Sample/ParticleCoreShellItem.h      |  5 +++--
 .../SampleDesigner/SampleEditorController.cpp | 19 ++++++++++++-------
 Tests/Unit/GUI/TestParticleCoreShell.cpp      |  5 ++++-
 5 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/GUI/Model/From/GUIDomainSampleVisitor.cpp b/GUI/Model/From/GUIDomainSampleVisitor.cpp
index e6060320a14..cf65051d6ea 100644
--- a/GUI/Model/From/GUIDomainSampleVisitor.cpp
+++ b/GUI/Model/From/GUIDomainSampleVisitor.cpp
@@ -600,9 +600,9 @@ ParticleItem* GUIDomainSampleVisitor::CreateIParticle(SessionItem* parent,
         ASSERT(coreshell);
         auto* parentCoreShell = polymorphic_cast<ParticleCoreShellItem*>(parent);
         if (particle == coreshell->coreParticle())
-            return parentCoreShell->createCore();
+            return parentCoreShell->createCore(m_materialModel);
         if (particle == coreshell->shellParticle())
-            return parentCoreShell->createShell();
+            return parentCoreShell->createShell(m_materialModel);
         ASSERT(0);
     }
     return m_sampleModel->insertItem<ParticleItem>(parent, -1);
diff --git a/GUI/Model/Sample/ParticleCoreShellItem.cpp b/GUI/Model/Sample/ParticleCoreShellItem.cpp
index dbe7fb69516..28d7fc5d3c3 100644
--- a/GUI/Model/Sample/ParticleCoreShellItem.cpp
+++ b/GUI/Model/Sample/ParticleCoreShellItem.cpp
@@ -13,6 +13,7 @@
 //  ************************************************************************************************
 
 #include "GUI/Model/Sample/ParticleCoreShellItem.h"
+#include "GUI/Model/Material/MaterialModel.h"
 #include "GUI/Model/Sample/ParticleItem.h"
 #include "GUI/Model/Session/SessionItemUtils.h"
 #include "GUI/Model/Session/SessionModel.h"
@@ -76,9 +77,12 @@ void ParticleCoreShellItem::setCore(ParticleItem* newCore)
     model()->moveItem(newCore, this, -1, T_CORE);
 }
 
-ParticleItem* ParticleCoreShellItem::createCore()
+ParticleItem* ParticleCoreShellItem::createCore(MaterialModel* materials)
 {
-    return model()->insertItem<ParticleItem>(this, -1, T_CORE);
+    auto* p = model()->insertItem<ParticleItem>(this, -1, T_CORE);
+    p->setMaterialModel(materials);
+    p->setMaterial(materials->defaultMaterial());
+    return p;
 }
 
 bool ParticleCoreShellItem::isCoreTagName(const QString& name)
@@ -102,9 +106,12 @@ void ParticleCoreShellItem::setShell(ParticleItem* newShell)
     model()->moveItem(newShell, this, -1, T_SHELL);
 }
 
-ParticleItem* ParticleCoreShellItem::createShell()
+ParticleItem* ParticleCoreShellItem::createShell(MaterialModel* materials)
 {
-    return model()->insertItem<ParticleItem>(this, -1, T_SHELL);
+    auto* p = model()->insertItem<ParticleItem>(this, -1, T_SHELL);
+    p->setMaterialModel(materials);
+    p->setMaterial(materials->defaultMaterial());
+    return p;
 }
 
 bool ParticleCoreShellItem::isShellTagName(const QString& name)
diff --git a/GUI/Model/Sample/ParticleCoreShellItem.h b/GUI/Model/Sample/ParticleCoreShellItem.h
index f2c9cf5f8d8..8a39ffc0d92 100644
--- a/GUI/Model/Sample/ParticleCoreShellItem.h
+++ b/GUI/Model/Sample/ParticleCoreShellItem.h
@@ -20,6 +20,7 @@
 class ParticleCoreShell;
 class ParticleItem;
 class VectorItem;
+class MaterialModel;
 
 class BA_CORE_API_ ParticleCoreShellItem : public ItemWithParticles {
 private:
@@ -35,12 +36,12 @@ public:
 
     ParticleItem* core() const;
     void setCore(ParticleItem* core);
-    ParticleItem* createCore();
+    ParticleItem* createCore(MaterialModel* materials);
     static bool isCoreTagName(const QString& name);
 
     ParticleItem* shell() const;
     void setShell(ParticleItem* newShell);
-    ParticleItem* createShell();
+    ParticleItem* createShell(MaterialModel* materials);
     static bool isShellTagName(const QString& name);
 };
 
diff --git a/GUI/View/SampleDesigner/SampleEditorController.cpp b/GUI/View/SampleDesigner/SampleEditorController.cpp
index b7a2376ecc3..2039d38c7b9 100644
--- a/GUI/View/SampleDesigner/SampleEditorController.cpp
+++ b/GUI/View/SampleDesigner/SampleEditorController.cpp
@@ -138,8 +138,8 @@ void SampleEditorController::addParticle(ParticleLayoutItem* layoutItem, const Q
         newItem = layoutItem->model()->insertNewItem(classname, layoutItem);
 
     if (auto* cs = dynamic_cast<ParticleCoreShellItem*>(newItem)) {
-        cs->createCore();
-        cs->createShell();
+        cs->createCore(materialModel());
+        cs->createShell(materialModel());
     }
 
     //  search for particle layout widget for notification
@@ -160,12 +160,17 @@ void SampleEditorController::addParticle(ParticleCompositionItem* compositionIte
         new_particle->setFormFactor(classname);
         new_particle->setMaterial(materialModel()->defaultMaterial());
         newItem = new_particle;
-    } else
+    } else {
         newItem = compositionItem->model()->insertNewItem(classname, compositionItem);
+        if (auto* p = dynamic_cast<ItemWithMaterial*>(newItem)) {
+            p->setMaterialModel(materialModel());
+            p->setMaterial(materialModel()->defaultMaterial());
+        }
+    }
 
     if (auto* cs = dynamic_cast<ParticleCoreShellItem*>(newItem)) {
-        cs->createCore();
-        cs->createShell();
+        cs->createCore(materialModel());
+        cs->createShell(materialModel());
     }
 
     //  search for composition widget for notification
@@ -187,7 +192,7 @@ void SampleEditorController::setCoreFormFactor(ParticleCoreShellForm* widget,
     }
 
     if (particleCoreShell->core() == nullptr)
-        particleCoreShell->createCore();
+        particleCoreShell->createCore(materialModel());
 
     particleCoreShell->core()->setFormFactor(formFactorModelType);
     widget->createCoreWidgets();
@@ -205,7 +210,7 @@ void SampleEditorController::setShellFormFactor(ParticleCoreShellForm* widget,
     }
 
     if (particleCoreShell->shell() == nullptr)
-        particleCoreShell->createShell();
+        particleCoreShell->createShell(materialModel());
 
     particleCoreShell->shell()->setFormFactor(formFactorModelType);
     widget->createShellWidgets();
diff --git a/Tests/Unit/GUI/TestParticleCoreShell.cpp b/Tests/Unit/GUI/TestParticleCoreShell.cpp
index 23752db3d9a..034c4c12513 100644
--- a/Tests/Unit/GUI/TestParticleCoreShell.cpp
+++ b/Tests/Unit/GUI/TestParticleCoreShell.cpp
@@ -1,3 +1,4 @@
+#include "GUI/Model/Material/MaterialModel.h"
 #include "GUI/Model/Sample/ParticleCompositionItem.h"
 #include "GUI/Model/Sample/ParticleCoreShellItem.h"
 #include "GUI/Model/Sample/ParticleItem.h"
@@ -51,6 +52,8 @@ TEST_F(TestParticleCoreShell, moveCoreAndShell)
 TEST_F(TestParticleCoreShell, propertyAppearance)
 {
     SampleModel model;
+    MaterialModel materials;
+    materials.addRefractiveMaterial("Default", 1e-3, 1e-5);
 
     // empty coreshell particle
     auto* coreshell = model.insertItem<ParticleCoreShellItem>();
@@ -63,7 +66,7 @@ TEST_F(TestParticleCoreShell, propertyAppearance)
     EXPECT_EQ(pos.z(), 0.0);
 
     // adding core, and checking that abundance is disabled
-    auto* core = coreshell->createCore();
+    auto* core = coreshell->createCore(&materials);
     EXPECT_FALSE(core->abundanceItem()->isEnabled());
     EXPECT_TRUE(core->positionItem()->isEnabled());
 
-- 
GitLab