From d0550e9472a156b1aff146fd5af24a0168fddf02 Mon Sep 17 00:00:00 2001
From: Dmitry Yurov <d.yurov@fz-juelich.de>
Date: Wed, 31 Jan 2018 12:03:36 +0100
Subject: [PATCH] Moved beam and background to the InstrumentItem

Redmine: #1936
---
 GUI/coregui/Models/InstrumentItems.cpp | 96 ++++++++++++++------------
 GUI/coregui/Models/InstrumentItems.h   | 16 +++--
 2 files changed, 60 insertions(+), 52 deletions(-)

diff --git a/GUI/coregui/Models/InstrumentItems.cpp b/GUI/coregui/Models/InstrumentItems.cpp
index d30c582b298..62ea8855df8 100644
--- a/GUI/coregui/Models/InstrumentItems.cpp
+++ b/GUI/coregui/Models/InstrumentItems.cpp
@@ -31,39 +31,76 @@ const QString background_group_label = "Type";
 }
 
 const QString InstrumentItem::P_IDENTIFIER = "Identifier";
+const QString InstrumentItem::P_BEAM = "Beam";
+const QString InstrumentItem::P_BACKGROUND = "Background";
+
+QStringList InstrumentItem::translateList(const QStringList& list) const
+{
+    QStringList result;
+    // Add constant background directly to simulation
+    if (list.back().startsWith(P_BACKGROUND) && list.size()==2) {
+        result << list[0] << QString::fromStdString(BornAgain::ConstantBackgroundType);
+    } else {
+        // TODO Consider usage of ModelTypeTranslator in IntrusmentItem's constructor
+        // after the refactoring of SessionItem::translateList
+        result = SessionItem::translateList(list);
+        if (result.back() == Constants::GISASInstrumentType) {
+            result.removeLast();
+            result << QStringLiteral("Instrument");
+        }
+    }
+    return result;
+}
+
+BeamItem* InstrumentItem::beamItem() const
+{
+    return &item<BeamItem>(P_BEAM);
+}
+
+BackgroundItem* InstrumentItem::backgroundItem() const
+{
+    return &groupItem<BackgroundItem>(P_BACKGROUND);
+}
+
+GroupItem* InstrumentItem::backgroundGroup()
+{
+    return &item<GroupItem>(P_BACKGROUND);
+}
+
+std::unique_ptr<Instrument> InstrumentItem::createInstrument() const
+{
+    std::unique_ptr<Instrument> result(new Instrument);
+
+    auto beam = beamItem()->createBeam();
+    result->setBeam(*beam);
+
+    return result;
+}
 
 InstrumentItem::InstrumentItem(const QString& modelType) : SessionItem(modelType)
 {
     setItemName(modelType);
     addProperty(P_IDENTIFIER, GUIHelpers::createUuid())->setVisible(false);
 
+    addGroupProperty(P_BEAM, Constants::BeamType);
+
+    auto item = addGroupProperty(P_BACKGROUND, Constants::BackgroundGroup);
+    item->setDisplayName(background_group_label);
+    item->setToolTip("Background type");
 }
 
-const QString Instrument2DItem::P_BEAM = "Beam";
 const QString Instrument2DItem::P_DETECTOR = "Detector";
-const QString Instrument2DItem::P_BACKGROUND = "Background";
 
 Instrument2DItem::Instrument2DItem(const QString& modelType)
     : InstrumentItem(modelType)
 {
-    addGroupProperty(P_BEAM, Constants::BeamType);
-
     addGroupProperty(P_DETECTOR, Constants::DetectorGroup);
 
     setDefaultTag(P_DETECTOR);
-
-    auto item = addGroupProperty(P_BACKGROUND, Constants::BackgroundGroup);
-    item->setDisplayName(background_group_label);
-    item->setToolTip("Background type");
 }
 
 Instrument2DItem::~Instrument2DItem() = default;
 
-BeamItem *Instrument2DItem::beamItem() const
-{
-    return &item<BeamItem>(P_BEAM);
-}
-
 DetectorItem* Instrument2DItem::detectorItem() const
 {
     return &groupItem<DetectorItem>(P_DETECTOR);
@@ -74,16 +111,6 @@ GroupItem* Instrument2DItem::detectorGroup()
     return &item<GroupItem>(P_DETECTOR);
 }
 
-BackgroundItem* Instrument2DItem::backgroundItem() const
-{
-    return &groupItem<BackgroundItem>(P_BACKGROUND);
-}
-
-GroupItem* Instrument2DItem::backgroundGroup()
-{
-    return &item<GroupItem>(P_BACKGROUND);
-}
-
 void Instrument2DItem::setDetectorGroup(const QString& modelType)
 {
     setGroupProperty(P_DETECTOR, modelType);
@@ -99,30 +126,9 @@ void Instrument2DItem::importMasks(MaskContainerItem* maskContainer)
     detectorItem()->importMasks(maskContainer);
 }
 
-QStringList Instrument2DItem::translateList(const QStringList& list) const
-{
-    QStringList result;
-    // Add constant background directly to simulation
-    if (list.back().startsWith(P_BACKGROUND) && list.size()==2) {
-        result << list[0] << QString::fromStdString(BornAgain::ConstantBackgroundType);
-    } else {
-        // TODO Consider usage of ModelTypeTranslator in IntrusmentItem's constructor
-        // after the refactoring of SessionItem::translateList
-        result = SessionItem::translateList(list);
-        if (result.back() == Constants::GISASInstrumentType) {
-            result.removeLast();
-            result << QStringLiteral("Instrument");
-        }
-    }
-    return result;
-}
-
 std::unique_ptr<Instrument> Instrument2DItem::createInstrument() const
 {
-    std::unique_ptr<Instrument> result(new Instrument);
-
-    auto beam = beamItem()->createBeam();
-    result->setBeam(*beam);
+    auto result = InstrumentItem::createInstrument();
 
     auto detector = detectorItem()->createDetector();
     result->setDetector(*detector);
diff --git a/GUI/coregui/Models/InstrumentItems.h b/GUI/coregui/Models/InstrumentItems.h
index db29bac05cb..c97956e3400 100644
--- a/GUI/coregui/Models/InstrumentItems.h
+++ b/GUI/coregui/Models/InstrumentItems.h
@@ -28,8 +28,17 @@ class BA_CORE_API_ InstrumentItem : public SessionItem
 {
 public:
     static const QString P_IDENTIFIER;
+    static const QString P_BEAM;
+    static const QString P_BACKGROUND;
+
+    QStringList translateList(const QStringList& list) const override;
+
+    BeamItem* beamItem() const;
+    BackgroundItem* backgroundItem() const;
+    GroupItem* backgroundGroup();
 
     virtual std::unique_ptr<Instrument> createInstrument() const = 0;
+
 protected:
     explicit InstrumentItem(const QString& modelType);
 };
@@ -39,15 +48,10 @@ class BA_CORE_API_ Instrument2DItem : public InstrumentItem
 public:
     virtual ~Instrument2DItem();
 
-    static const QString P_BEAM;
     static const QString P_DETECTOR;
-    static const QString P_BACKGROUND;
 
-    BeamItem* beamItem() const;
     DetectorItem* detectorItem() const;
     GroupItem* detectorGroup();
-    BackgroundItem* backgroundItem() const;
-    GroupItem* backgroundGroup();
 
     void setDetectorGroup(const QString& modelType);
 
@@ -55,8 +59,6 @@ public:
 
     void importMasks(MaskContainerItem* maskContainer);
 
-    QStringList translateList(const QStringList& list) const override;
-
     std::unique_ptr<Instrument> createInstrument() const override;
 
 protected:
-- 
GitLab