From d666deb7c48bd0b44f15cfe758254f4d88ca1c85 Mon Sep 17 00:00:00 2001
From: Joachim Wuttke <j.wuttke@fz-juelich.de>
Date: Fri, 26 Jul 2024 12:05:56 +0200
Subject: [PATCH] rm 'using BaseItem ... from Catalog => requires relocation of
 writeTo/readFrom from UtilXML to PolyPtr

---
 GUI/Model/Descriptor/PolyPtr.h      | 46 +++++++++++++++++++++--------
 GUI/Model/Sample/RoughnessCatalog.h |  2 --
 GUI/View/Sample/HeinzFormLayout.h   |  2 +-
 GUI/View/Sample/PolyForm.h          |  2 +-
 4 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/GUI/Model/Descriptor/PolyPtr.h b/GUI/Model/Descriptor/PolyPtr.h
index a98744aa051..7c9c09653af 100644
--- a/GUI/Model/Descriptor/PolyPtr.h
+++ b/GUI/Model/Descriptor/PolyPtr.h
@@ -45,16 +45,8 @@ public:
 
     template <typename S> S* createCertainItem();
 
-    //! Serializes the catalog index of the currently selected type and calls
-    //! main serialization method of the selected class.
-    void writeTo(QXmlStreamWriter* w) const { XML::writeItemTo<Catalog>(m_item.get(), w); }
-
-    //! Deserializes the catalog index of the currently selected type, creates a new
-    //! object of this type and calls main deserialization method of the selected class.
-    template <typename... Args> void readFrom(QXmlStreamReader* r, Args... args)
-    {
-        m_item.reset(XML::readItemFrom<Catalog>(r, args...));
-    }
+    void writeTo(QXmlStreamWriter* w) const;
+    template <typename... Args> void readFrom(QXmlStreamReader* r, Args... args);
 
     void setCurrentIndex(int index) override { m_item.reset(Catalog::create(m_types[index])); }
     int currentIndex() const override { return m_types.indexOf(Catalog::type(m_item.get())); }
@@ -71,8 +63,8 @@ private:
 //! The current selection will be initialized with the first type in the catalog types. The optional
 //! arguments are the arguments which may be necessary for the creation method in the catalog.
 template <typename B, typename C>
-void PolyPtr<B,C>::simpleInit(const QString& label, const QString& tooltip,
-                             typename C::Type currentType)
+void PolyPtr<B, C>::simpleInit(const QString& label, const QString& tooltip,
+                               typename C::Type currentType)
 {
     m_label = label;
     m_tooltip = tooltip;
@@ -86,11 +78,39 @@ void PolyPtr<B,C>::simpleInit(const QString& label, const QString& tooltip,
 }
 
 //! Directly set the new item.
-template <typename B, typename C> template <typename S> S* PolyPtr<B,C>::createCertainItem()
+template <typename B, typename C> template <typename S> S* PolyPtr<B, C>::createCertainItem()
 {
     auto* t = new S;
     m_item.reset(t);
     return t;
 }
 
+//! Serializes the catalog index of the currently selected type and calls
+//! main serialization method of the selected class.
+template <typename B, typename C> void PolyPtr<B, C>::writeTo(QXmlStreamWriter* w) const
+{
+    const B* t = m_item.get();
+    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);
+}
+
+//! Deserializes the catalog index of the currently selected type, creates a new
+//! object of this type and calls main deserialization method of the selected class.
+template <typename B, typename C>
+template <typename... Args>
+void PolyPtr<B, C>::readFrom(QXmlStreamReader* r, Args... args)
+{
+    const uint typeIndex = XML::readUInt(r, XML::Attrib::type);
+    const QString kind = XML::readString(r, XML::Attrib::kind);
+    const auto type = static_cast<typename C::Type>(typeIndex);
+    B* t = C::create(type, args...);
+    if (t)
+        t->readFrom(r);
+    m_item.reset(t);
+}
+
 #endif // BORNAGAIN_GUI_MODEL_DESCRIPTOR_POLYPTR_H
diff --git a/GUI/Model/Sample/RoughnessCatalog.h b/GUI/Model/Sample/RoughnessCatalog.h
index 0319aef78ad..51bc20fe920 100644
--- a/GUI/Model/Sample/RoughnessCatalog.h
+++ b/GUI/Model/Sample/RoughnessCatalog.h
@@ -22,8 +22,6 @@ class RoughnessItem;
 
 class RoughnessCatalog {
 public:
-    using BaseItem = RoughnessItem;
-
     // Do not change the numbering! It is serialized!
     enum class Type : uint8_t { None = 0, Basic = 1 };
 
diff --git a/GUI/View/Sample/HeinzFormLayout.h b/GUI/View/Sample/HeinzFormLayout.h
index 5017f43d992..c51c834d0db 100644
--- a/GUI/View/Sample/HeinzFormLayout.h
+++ b/GUI/View/Sample/HeinzFormLayout.h
@@ -61,7 +61,7 @@ public:
         addBoldRow(d.piLabel(), new PolyForm(QFormLayout::parentWidget(), d, m_ec));
     }
 
-    template <typename B, typename C> void addSelection(PolyPtr<B,C>& d)
+    template <typename B, typename C> void addSelection(PolyPtr<B, C>& d)
     {
         addBoldRow(d.piLabel(), new PolyForm(QFormLayout::parentWidget(), d, m_ec));
     }
diff --git a/GUI/View/Sample/PolyForm.h b/GUI/View/Sample/PolyForm.h
index 3449988b397..895d51585ab 100644
--- a/GUI/View/Sample/PolyForm.h
+++ b/GUI/View/Sample/PolyForm.h
@@ -49,7 +49,7 @@ public:
         initUI(d);
     }
     template <typename B, typename C>
-    PolyForm(QWidget* parent, PolyPtr<B,C>& d, SampleEditorController* ec)
+    PolyForm(QWidget* parent, PolyPtr<B, C>& d, SampleEditorController* ec)
         : ISelectionForm(parent, ec)
     {
         currentValues = [&d] { return doublePropertiesOfItem(d.certainItem()); };
-- 
GitLab