From 16089f54f81e7bdcc6f870e40bc49a3050c124e1 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Mon, 8 Jul 2019 16:21:26 +0200
Subject: [PATCH] Move common method to SessionItemUtils namespace

---
 GUI/coregui/Models/MesoCrystalItem.cpp         | 13 +------------
 GUI/coregui/Models/MesoCrystalItem.h           |  1 -
 GUI/coregui/Models/ParticleCompositionItem.cpp |  3 ++-
 GUI/coregui/Models/ParticleCoreShellItem.cpp   | 13 ++-----------
 GUI/coregui/Models/ParticleCoreShellItem.h     |  1 -
 GUI/coregui/Models/ParticleItem.cpp            | 16 ++--------------
 GUI/coregui/Models/ParticleItem.h              |  1 -
 GUI/coregui/Models/SessionItemUtils.cpp        | 12 ++++++++++++
 GUI/coregui/Models/SessionItemUtils.h          |  2 ++
 9 files changed, 21 insertions(+), 41 deletions(-)

diff --git a/GUI/coregui/Models/MesoCrystalItem.cpp b/GUI/coregui/Models/MesoCrystalItem.cpp
index 660e58681b7..ba02c14eb2a 100644
--- a/GUI/coregui/Models/MesoCrystalItem.cpp
+++ b/GUI/coregui/Models/MesoCrystalItem.cpp
@@ -55,11 +55,6 @@ const QString density_tooltip =
 
 bool IsIParticleName(QString name);
 
-QStringList parents_with_abundance() {
-    return QStringList() << Constants::ParticleCoreShellType << Constants::ParticleCompositionType
-                         << Constants::ParticleDistributionType << Constants::MesoCrystalType;
-}
-
 }
 
 const QString MesoCrystalItem::P_FORM_FACTOR = "Outer Shape";
@@ -107,7 +102,7 @@ MesoCrystalItem::MesoCrystalItem() : SessionGraphicsItem(Constants::MesoCrystalT
 
     mapper()->setOnParentChange(
                 [this](SessionItem *parent) {
-        if (parentHasOwnAbundance(parent)) {
+        if (SessionItemUtils::HasOwnAbundance(parent)) {
             setItemValue(ParticleItem::P_ABUNDANCE, 1.0);
             getItem(ParticleItem::P_ABUNDANCE)->setEnabled(false);
         } else {
@@ -188,12 +183,6 @@ std::unique_ptr<IFormFactor> MesoCrystalItem::getOuterShape() const
     return ff_item.createFormFactor();
 }
 
-bool MesoCrystalItem::parentHasOwnAbundance(SessionItem* parent) const
-{
-    static QStringList special_parent = parents_with_abundance();
-    return parent ? special_parent.contains(parent->modelType()) : false;
-}
-
 namespace {
 bool IsIParticleName(QString name) {
     return (name.startsWith(Constants::ParticleType)
diff --git a/GUI/coregui/Models/MesoCrystalItem.h b/GUI/coregui/Models/MesoCrystalItem.h
index 0fca4e45910..bfafa510598 100644
--- a/GUI/coregui/Models/MesoCrystalItem.h
+++ b/GUI/coregui/Models/MesoCrystalItem.h
@@ -41,7 +41,6 @@ public:
     Lattice getLattice() const;
     std::unique_ptr<IParticle> getBasis() const;
     std::unique_ptr<IFormFactor> getOuterShape() const;
-    bool parentHasOwnAbundance(SessionItem* parent) const;
 };
 
 #endif // MESOCRYSTALITEM_H
diff --git a/GUI/coregui/Models/ParticleCompositionItem.cpp b/GUI/coregui/Models/ParticleCompositionItem.cpp
index 61cb50f7389..5e1ad7e4621 100644
--- a/GUI/coregui/Models/ParticleCompositionItem.cpp
+++ b/GUI/coregui/Models/ParticleCompositionItem.cpp
@@ -21,6 +21,7 @@
 #include "Particle.h"
 #include "TransformToDomain.h"
 #include "ParticleCoreShell.h"
+#include "SessionItemUtils.h"
 
 namespace {
 const QString abundance_tooltip =
@@ -59,7 +60,7 @@ ParticleCompositionItem::ParticleCompositionItem()
 
     mapper()->setOnParentChange(
                 [this](SessionItem *parent) {
-        if (parent && parent->modelType() != Constants::ParticleLayoutType) {
+        if (SessionItemUtils::HasOwnAbundance(parent)) {
             setItemValue(ParticleItem::P_ABUNDANCE, 1.0);
             getItem(ParticleItem::P_ABUNDANCE)->setEnabled(false);
         } else {
diff --git a/GUI/coregui/Models/ParticleCoreShellItem.cpp b/GUI/coregui/Models/ParticleCoreShellItem.cpp
index 2f9f643eac8..674452b446d 100644
--- a/GUI/coregui/Models/ParticleCoreShellItem.cpp
+++ b/GUI/coregui/Models/ParticleCoreShellItem.cpp
@@ -19,6 +19,7 @@
 #include "TransformToDomain.h"
 #include "Particle.h"
 #include "ParticleCoreShell.h"
+#include "SessionItemUtils.h"
 
 namespace {
 const QString abundance_tooltip =
@@ -29,11 +30,6 @@ const QString position_tooltip =
     "Relative position of the particle's reference point \n"
     "in the coordinate system of the parent (nm)";
 
-QStringList parents_with_abundance() {
-    return QStringList() << Constants::ParticleCoreShellType << Constants::ParticleCompositionType
-                         << Constants::ParticleDistributionType << Constants::MesoCrystalType;
-}
-
 }
 
 const QString ParticleCoreShellItem::T_CORE = "Core Tag";
@@ -62,7 +58,7 @@ ParticleCoreShellItem::ParticleCoreShellItem()
     mapper()->setOnParentChange(
                 [this](SessionItem* parent)
     {
-        if (parentHasOwnAbundance(parent)) {
+        if (SessionItemUtils::HasOwnAbundance(parent)) {
             setItemValue(ParticleItem::P_ABUNDANCE, 1.0);
             getItem(ParticleItem::P_ABUNDANCE)->setEnabled(false);
         } else {
@@ -103,8 +99,3 @@ QVector<SessionItem*> ParticleCoreShellItem::materialPropertyItems()
     return result;
 }
 
-bool ParticleCoreShellItem::parentHasOwnAbundance(SessionItem* parent) const
-{
-    static QStringList special_parent = parents_with_abundance();
-    return parent ? special_parent.contains(parent->modelType()) : false;
-}
diff --git a/GUI/coregui/Models/ParticleCoreShellItem.h b/GUI/coregui/Models/ParticleCoreShellItem.h
index a80f5ab063b..00cab30f884 100644
--- a/GUI/coregui/Models/ParticleCoreShellItem.h
+++ b/GUI/coregui/Models/ParticleCoreShellItem.h
@@ -27,7 +27,6 @@ public:
     ParticleCoreShellItem();
     std::unique_ptr<ParticleCoreShell> createParticleCoreShell() const;
     QVector<SessionItem*> materialPropertyItems();
-    bool parentHasOwnAbundance(SessionItem* parent) const;
 };
 
 #endif // PARTICLECORESHELLITEM_H
diff --git a/GUI/coregui/Models/ParticleItem.cpp b/GUI/coregui/Models/ParticleItem.cpp
index 1a53bfe1843..a3b1cbe32bf 100644
--- a/GUI/coregui/Models/ParticleItem.cpp
+++ b/GUI/coregui/Models/ParticleItem.cpp
@@ -23,6 +23,7 @@
 #include "TransformToDomain.h"
 #include "VectorItem.h"
 #include "SessionModel.h"
+#include "SessionItemUtils.h"
 
 using SessionItemUtils::SetVectorItem;
 
@@ -35,10 +36,6 @@ const QString position_tooltip =
     "Relative position of the particle's reference point \n"
     "in the coordinate system of the parent (nm)";
 
-QStringList parents_with_abundance() {
-    return QStringList() << Constants::ParticleCoreShellType << Constants::ParticleCompositionType
-                         << Constants::ParticleDistributionType << Constants::MesoCrystalType;
-}
 }
 
 const QString ParticleItem::P_FORM_FACTOR = "Form Factor";
@@ -98,7 +95,7 @@ QVector<SessionItem*> ParticleItem::materialPropertyItems()
 
 void ParticleItem::updatePropertiesAppearance(SessionItem* newParent)
 {
-    if (parentHasOwnAbundance(newParent)) {
+    if (SessionItemUtils::HasOwnAbundance(newParent)) {
         setItemValue(ParticleItem::P_ABUNDANCE, 1.0);
         getItem(ParticleItem::P_ABUNDANCE)->setEnabled(false);
         if (isShellParticle()) {
@@ -133,12 +130,3 @@ bool ParticleItem::parentIsParticleLayout() const
 
     return parent()->modelType() == Constants::ParticleLayoutType;
 }
-
-//! Returns true if parent has own abundance.
-//! For rootItem and for ParticleLayout will return false.
-
-bool ParticleItem::parentHasOwnAbundance(SessionItem* parent) const
-{
-    static QStringList special_parent = parents_with_abundance();
-    return parent ? special_parent.contains(parent->modelType()) : false;
-}
diff --git a/GUI/coregui/Models/ParticleItem.h b/GUI/coregui/Models/ParticleItem.h
index eb2b48d4a0b..26aaf1402a2 100644
--- a/GUI/coregui/Models/ParticleItem.h
+++ b/GUI/coregui/Models/ParticleItem.h
@@ -37,7 +37,6 @@ private:
     void updatePropertiesAppearance(SessionItem*);
     bool isShellParticle() const;
     bool parentIsParticleLayout() const;
-    bool parentHasOwnAbundance(SessionItem* parent) const;
 };
 
 #endif // PARTICLEITEM_H
diff --git a/GUI/coregui/Models/SessionItemUtils.cpp b/GUI/coregui/Models/SessionItemUtils.cpp
index b5fbc41fd45..d3fd44329c1 100644
--- a/GUI/coregui/Models/SessionItemUtils.cpp
+++ b/GUI/coregui/Models/SessionItemUtils.cpp
@@ -30,6 +30,12 @@ const GroupInfoCatalogue& groupInfoCatalogue()
     static GroupInfoCatalogue s_catalogue = GroupInfoCatalogue();
     return s_catalogue;
 }
+
+QStringList parents_with_abundance() {
+    return QStringList() << Constants::ParticleCoreShellType << Constants::ParticleCompositionType
+                         << Constants::ParticleDistributionType << Constants::MesoCrystalType;
+}
+
 }
 
 int SessionItemUtils::ParentRow(const SessionItem& item)
@@ -163,3 +169,9 @@ bool SessionItemUtils::IsPositionRelated(const SessionItem& item)
 
     return false;
 }
+
+bool SessionItemUtils::HasOwnAbundance(const SessionItem* item)
+{
+    static QStringList special_parent = parents_with_abundance();
+    return item ? special_parent.contains(item->modelType()) : false;
+}
diff --git a/GUI/coregui/Models/SessionItemUtils.h b/GUI/coregui/Models/SessionItemUtils.h
index abe78b81d39..8af8f976185 100644
--- a/GUI/coregui/Models/SessionItemUtils.h
+++ b/GUI/coregui/Models/SessionItemUtils.h
@@ -71,6 +71,8 @@ BA_CORE_API_ bool IsTheSame(const QVariant& var1, const QVariant& var2);
 //! Returns true if current item is related to the position on sample editor canvas.
 BA_CORE_API_ bool IsPositionRelated(const SessionItem& item);
 
+BA_CORE_API_ bool HasOwnAbundance(const SessionItem* item);
+
 }  // namespace SessionItemUtils
 
 #endif // SESSIONITEMUTILS_H
-- 
GitLab