From 5933e5f9e97d677b333f69fe48ad63da7eee05f0 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Tue, 23 Jul 2024 15:17:51 +0200
Subject: [PATCH] static fcts out of PolyItem

---
 GUI/Model/Descriptor/PolyItem.h         | 24 +-----------------------
 GUI/Model/Mask/MasksSet.cpp             |  2 +-
 GUI/Model/Sample/CompoundItem.cpp       |  2 +-
 GUI/Model/Sample/ParticleLayoutItem.cpp |  2 +-
 GUI/Model/Sim/InstrumentsSet.cpp        |  2 +-
 GUI/Model/Util/UtilXML.h                | 18 ++++++++++++++++++
 6 files changed, 23 insertions(+), 27 deletions(-)

diff --git a/GUI/Model/Descriptor/PolyItem.h b/GUI/Model/Descriptor/PolyItem.h
index 91de5b6533e..b1314957112 100644
--- a/GUI/Model/Descriptor/PolyItem.h
+++ b/GUI/Model/Descriptor/PolyItem.h
@@ -37,11 +37,9 @@ public:
 
     template <typename S> S* createCertainItem();
 
-    static void writeChosen(const BaseItem* t, QXmlStreamWriter* w, const QString& tag);
-
     //! Serializes the catalog index of the currently selected type and calls
     //! main serialization method of the selected class.
-    void writeTo(QXmlStreamWriter* w) const { writeItemTo(m_item.get(), w); }
+    void writeTo(QXmlStreamWriter* w) const { XML::writeItemTo<Catalog>(m_item.get(), w); }
 
     template <typename... Args> BaseItem* readItemFrom(QXmlStreamReader* r, Args... args);
 
@@ -60,8 +58,6 @@ public:
     int currentIndex() const { return m_types.indexOf(Catalog::type(m_item.get())); }
 
 private:
-    static void writeItemTo(const BaseItem* t, QXmlStreamWriter* w);
-
     template <typename... Args>
     void initFieldsAndSetter(const QString& label, const QString& tooltip, Args... args);
 
@@ -129,24 +125,6 @@ template <typename C> template <typename S> S* PolyItem<C>::createCertainItem()
     return t;
 }
 
-template <typename C> void PolyItem<C>::writeItemTo(const BaseItem* t, QXmlStreamWriter* w)
-{
-    const uint typeIndex = static_cast<uint>(C::type(t));
-    XML::writeAttribute(w, XML::Attrib::type, typeIndex);
-    // The next line allows to see the name of item type in XML. May be skipped while reading.
-    XML::writeAttribute(w, XML::Attrib::kind, C::uiInfo(C::type(t)).menuEntry);
-    if (t)
-        t->writeTo(w);
-}
-
-template <typename C>
-void PolyItem<C>::writeChosen(const BaseItem* t, QXmlStreamWriter* w, const QString& tag)
-{
-    w->writeStartElement(tag);
-    writeItemTo(t, w);
-    w->writeEndElement();
-}
-
 template <typename C> template <typename... Args>
 C::BaseItem* PolyItem<C>::readItemFrom(QXmlStreamReader* r, Args... args)
 {
diff --git a/GUI/Model/Mask/MasksSet.cpp b/GUI/Model/Mask/MasksSet.cpp
index 6fa0c14e531..d747f476fad 100644
--- a/GUI/Model/Mask/MasksSet.cpp
+++ b/GUI/Model/Mask/MasksSet.cpp
@@ -41,7 +41,7 @@ RegionOfInterestItem* MasksSet::regionOfInterestItem() const
 void MasksSet::writeTo(QXmlStreamWriter* w) const
 {
     for (const MaskItem* t : *this)
-        PolyItem<MaskCatalog>::writeChosen(t, w, Tag::Mask);
+        XML::writeChosen<MaskCatalog>(t, w, Tag::Mask);
 }
 
 void MasksSet::readFrom(QXmlStreamReader* r)
diff --git a/GUI/Model/Sample/CompoundItem.cpp b/GUI/Model/Sample/CompoundItem.cpp
index efe3e853a72..cefdc9b8149 100644
--- a/GUI/Model/Sample/CompoundItem.cpp
+++ b/GUI/Model/Sample/CompoundItem.cpp
@@ -66,7 +66,7 @@ void CompoundItem::writeTo(QXmlStreamWriter* w) const
 
     // particles
     for (auto* t : m_particles)
-        PolyItem<ParticleCatalog>::writeChosen(t, w, Tag::Particle);
+        XML::writeChosen<ParticleCatalog>(t, w, Tag::Particle);
     XML::writeTaggedValue(w, Tag::ExpandCompoundGroupbox, expandCompound);
 }
 
diff --git a/GUI/Model/Sample/ParticleLayoutItem.cpp b/GUI/Model/Sample/ParticleLayoutItem.cpp
index 8c546466e30..d7826c0468a 100644
--- a/GUI/Model/Sample/ParticleLayoutItem.cpp
+++ b/GUI/Model/Sample/ParticleLayoutItem.cpp
@@ -109,7 +109,7 @@ void ParticleLayoutItem::writeTo(QXmlStreamWriter* w) const
 
     // particles
     for (auto* t : m_particles)
-        PolyItem<ParticleCatalog>::writeChosen(t, w, Tag::Particle);
+        XML::writeChosen<ParticleCatalog>(t, w, Tag::Particle);
     XML::writeTaggedValue(w, Tag::ExpandLayoutGroupbox, expandParticleLayout);
     XML::writeTaggedValue(w, Tag::ExpandInterferenceGroupbox, expandInterference);
 }
diff --git a/GUI/Model/Sim/InstrumentsSet.cpp b/GUI/Model/Sim/InstrumentsSet.cpp
index 2a28b1ef25b..9e4eeaa01cd 100644
--- a/GUI/Model/Sim/InstrumentsSet.cpp
+++ b/GUI/Model/Sim/InstrumentsSet.cpp
@@ -31,7 +31,7 @@ void InstrumentsSet::writeTo(QXmlStreamWriter* w) const
 {
     // instruments
     for (const InstrumentItem* t : *this)
-        PolyItem<InstrumentCatalog>::writeChosen(t, w, Tag::Instrument);
+        XML::writeChosen<InstrumentCatalog>(t, w, Tag::Instrument);
     XML::writeTaggedValue(w, Tag::CurrentIndex, (int)currentIndex());
 }
 
diff --git a/GUI/Model/Util/UtilXML.h b/GUI/Model/Util/UtilXML.h
index 091267d249a..dc6d6328338 100644
--- a/GUI/Model/Util/UtilXML.h
+++ b/GUI/Model/Util/UtilXML.h
@@ -103,6 +103,24 @@ template <typename T> void readBaseElement(QXmlStreamReader* r, const QString& t
     gotoEndElementOfTag(r, tag);
 }
 
+template <typename C> void writeItemTo(const class C::BaseItem* t, QXmlStreamWriter* w)
+{
+    const uint typeIndex = static_cast<uint>(C::type(t));
+    XML::writeAttribute(w, XML::Attrib::type, typeIndex);
+    // The next line allows to see the name of item type in XML. May be skipped while reading.
+    XML::writeAttribute(w, XML::Attrib::kind, C::uiInfo(C::type(t)).menuEntry);
+    if (t)
+        t->writeTo(w);
+}
+
+template <typename C>
+void writeChosen(const class C::BaseItem* t, QXmlStreamWriter* w, const QString& tag)
+{
+    w->writeStartElement(tag);
+    XML::writeItemTo<C>(t, w);
+    w->writeEndElement();
+}
+
 } // namespace XML
 
 #endif // BORNAGAIN_GUI_MODEL_UTIL_UTILXML_H
-- 
GitLab