From 20e0f863e1844a31baec1811133609b2f5eaef88 Mon Sep 17 00:00:00 2001
From: Tobias Knopff <t.knopff@fz-juelich.de>
Date: Wed, 2 Jun 2021 12:10:58 +0200
Subject: [PATCH] Make JobItem::T_INSTRUMENT private

---
 GUI/Models/JobItem.cpp                         | 5 +++++
 GUI/Models/JobItem.h                           | 3 ++-
 GUI/Models/JobModel.cpp                        | 2 +-
 GUI/Models/JobModelFunctions.cpp               | 3 +--
 GUI/Models/ModelPath.cpp                       | 3 ++-
 GUI/Models/ParameterTreeUtils.cpp              | 3 ++-
 Tests/UnitTests/GUI/TestSavingSpecularData.cpp | 4 ++--
 Tests/UnitTests/GUI/TestSessionModel.cpp       | 4 ++--
 8 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/GUI/Models/JobItem.cpp b/GUI/Models/JobItem.cpp
index 6f434f7902c..ba852bf2afc 100644
--- a/GUI/Models/JobItem.cpp
+++ b/GUI/Models/JobItem.cpp
@@ -239,6 +239,11 @@ InstrumentItem* JobItem::instrumentItem()
     return dynamic_cast<InstrumentItem*>(getItem(T_INSTRUMENT));
 }
 
+InstrumentItem* JobItem::copyInstrumentIntoJob(const InstrumentItem* instrument)
+{
+    return model()->copyItem(instrument, this, T_INSTRUMENT);
+}
+
 void JobItem::setResults(const ISimulation* simulation)
 {
     JobItemUtils::setResults(dataItem(), simulation);
diff --git a/GUI/Models/JobItem.h b/GUI/Models/JobItem.h
index 2ac7195e5b6..be60f0130aa 100644
--- a/GUI/Models/JobItem.h
+++ b/GUI/Models/JobItem.h
@@ -46,9 +46,9 @@ private:
     static const QString P_PRESENTATION_TYPE;
     static const QString T_SAMPLE;
     static const QString T_MATERIAL_CONTAINER;
+    static const QString T_INSTRUMENT;
 
 public:
-    static const QString T_INSTRUMENT;
     static const QString T_OUTPUT;
     static const QString T_REALDATA;
     static const QString T_DATAVIEW;
@@ -95,6 +95,7 @@ public:
     MultiLayerItem* copySampleIntoJob(const MultiLayerItem* sample);
 
     InstrumentItem* instrumentItem();
+    InstrumentItem* copyInstrumentIntoJob(const InstrumentItem* instrument);
 
     SimulationOptionsItem* simulationOptionsItem();
 
diff --git a/GUI/Models/JobModel.cpp b/GUI/Models/JobModel.cpp
index d597b10605e..77aaec9c058 100644
--- a/GUI/Models/JobModel.cpp
+++ b/GUI/Models/JobModel.cpp
@@ -149,7 +149,7 @@ QVector<SessionItem*> JobModel::nonXMLItems() const
         }
 
         auto instrument =
-            dynamic_cast<SpecularInstrumentItem*>(jobItem->getItem(JobItem::T_INSTRUMENT));
+            dynamic_cast<SpecularInstrumentItem*>(jobItem->instrumentItem());
         if (instrument) {
             auto axis_group = instrument->beamItem()->inclinationAxisGroup();
             result.push_back(axis_group->firstChildOfType<PointwiseAxisItem>());
diff --git a/GUI/Models/JobModelFunctions.cpp b/GUI/Models/JobModelFunctions.cpp
index 8cb6e03502d..60efe22a852 100644
--- a/GUI/Models/JobModelFunctions.cpp
+++ b/GUI/Models/JobModelFunctions.cpp
@@ -109,8 +109,7 @@ void JobModelFunctions::setupJobItemSampleData(JobItem* jobItem, const MultiLaye
 
 void JobModelFunctions::setupJobItemInstrument(JobItem* jobItem, const InstrumentItem* from)
 {
-    auto model = jobItem->model();
-    auto copiedInstrument = model->copyItem(from, jobItem, JobItem::T_INSTRUMENT);
+    auto copiedInstrument = jobItem->copyInstrumentIntoJob(from);
     copiedInstrument->setItemName(from->modelType());
     copiedInstrument->setId(GUIHelpers::createUuid());
     jobItem->setInstrumentName(from->itemName());
diff --git a/GUI/Models/ModelPath.cpp b/GUI/Models/ModelPath.cpp
index 126f7ab7308..d257975357b 100644
--- a/GUI/Models/ModelPath.cpp
+++ b/GUI/Models/ModelPath.cpp
@@ -13,6 +13,7 @@
 //  ************************************************************************************************
 
 #include "GUI/Models/ModelPath.h"
+#include "GUI/Models/InstrumentItems.h"
 #include "GUI/Models/JobItem.h"
 #include "GUI/Models/SessionModel.h"
 
@@ -40,7 +41,7 @@ QModelIndex ModelPath::getIndexFromPath(const SessionModel* model, const QString
         SessionItem* t = model->rootItem();
         for (int i = 0; i < parts.length(); i++) {
             if (t->modelType() == "JobItem" && parts[i] == "GISASInstrument") {
-                t = t->getItem(JobItem::T_INSTRUMENT);
+                t = dynamic_cast<JobItem*>(t)->instrumentItem();
                 continue;
             }
             for (int j = 0; j < t->numberOfChildren(); j++) {
diff --git a/GUI/Models/ParameterTreeUtils.cpp b/GUI/Models/ParameterTreeUtils.cpp
index 087d74eee95..f07106cc620 100644
--- a/GUI/Models/ParameterTreeUtils.cpp
+++ b/GUI/Models/ParameterTreeUtils.cpp
@@ -16,6 +16,7 @@
 #include "GUI/Models/Error.h"
 #include "GUI/Models/FitParameterHelper.h"
 #include "GUI/Models/GroupItem.h"
+#include "GUI/Models/InstrumentItems.h"
 #include "GUI/Models/JobItem.h"
 #include "GUI/Models/MaterialItemContainer.h"
 #include "GUI/Models/ModelPath.h"
@@ -94,5 +95,5 @@ void ParameterTreeUtils::createParameterTree(JobItem* jobItem)
 
     populateParameterContainer(container, jobItem->sampleItem());
 
-    populateParameterContainer(container, jobItem->getItem(JobItem::T_INSTRUMENT));
+    populateParameterContainer(container, jobItem->instrumentItem());
 }
diff --git a/Tests/UnitTests/GUI/TestSavingSpecularData.cpp b/Tests/UnitTests/GUI/TestSavingSpecularData.cpp
index 201a05db000..fdc16c8cbd3 100644
--- a/Tests/UnitTests/GUI/TestSavingSpecularData.cpp
+++ b/Tests/UnitTests/GUI/TestSavingSpecularData.cpp
@@ -106,7 +106,7 @@ TEST_F(TestSavingSpecularData, test_InstrumentInJobItem)
 
     // adding instrument
     auto instrument =
-        models.jobModel()->insertItem<SpecularInstrumentItem>(jobItem, -1, JobItem::T_INSTRUMENT);
+        models.jobModel()->insertItem<SpecularInstrumentItem>(jobItem, -1, "Instrument tag");
     // instrument contains hidden pointwise axis item
     EXPECT_EQ(models.jobModel()->nonXMLItems().size(), 2);
 
@@ -264,7 +264,7 @@ TEST_F(TestSavingSpecularData, test_CopyInstrumentToJobItem)
     auto jobItem = models.jobModel()->insertItem<JobItem>();
     JobModelFunctions::setupJobItemInstrument(jobItem, instrument);
     auto job_instrument =
-        dynamic_cast<SpecularInstrumentItem*>(jobItem->getItem(JobItem::T_INSTRUMENT));
+        dynamic_cast<SpecularInstrumentItem*>(jobItem->instrumentItem());
     PointwiseAxisItem* job_axis_item =
         getAxisGroup(job_instrument)->firstChildOfType<PointwiseAxisItem>();
 
diff --git a/Tests/UnitTests/GUI/TestSessionModel.cpp b/Tests/UnitTests/GUI/TestSessionModel.cpp
index 42b842a4a0e..fa59a997901 100644
--- a/Tests/UnitTests/GUI/TestSessionModel.cpp
+++ b/Tests/UnitTests/GUI/TestSessionModel.cpp
@@ -144,8 +144,8 @@ TEST_F(TestSessionModel, copyItem)
     jobItem->copySampleIntoJob(multilayer1);
     EXPECT_NE(jobItem->sampleItem(), nullptr);
 
-    jobModel.copyItem(instrument1, jobItem, JobItem::T_INSTRUMENT);
-    EXPECT_EQ(jobItem->sessionItemTags()->childCount(JobItem::T_INSTRUMENT), 1);
+    jobItem->copyInstrumentIntoJob(instrument1);
+    EXPECT_NE(jobItem->instrumentItem(), nullptr);
 }
 
 TEST_F(TestSessionModel, moveItemFromRoot)
-- 
GitLab