From f11d833871f9fe916f72baa81bc88666fe9ebaa3 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Thu, 16 Dec 2021 14:45:18 +0100
Subject: [PATCH] add top/bottom info to layer

---
 GUI/Model/Sample/LayerItem.cpp      | 20 ++++++++++++++++
 GUI/Model/Sample/LayerItem.h        | 10 ++++++++
 GUI/Model/Sample/MultiLayerItem.cpp | 36 +++++++++++++++++++++--------
 3 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/GUI/Model/Sample/LayerItem.cpp b/GUI/Model/Sample/LayerItem.cpp
index 5378972c1bf..0f2a4823ef1 100644
--- a/GUI/Model/Sample/LayerItem.cpp
+++ b/GUI/Model/Sample/LayerItem.cpp
@@ -165,3 +165,23 @@ void LayerItem::setColor(const QColor& color)
 {
     setItemValue(P_COLOR, color);
 }
+
+void LayerItem::setIsTopLayer(bool b)
+{
+    m_isTopLayer = b;
+}
+
+bool LayerItem::isTopLayer() const
+{
+    return m_isTopLayer;
+}
+
+void LayerItem::setIsBottomLayer(bool b)
+{
+    m_isBottomLayer = b;
+}
+
+bool LayerItem::isBottomLayer() const
+{
+    return m_isBottomLayer;
+}
diff --git a/GUI/Model/Sample/LayerItem.h b/GUI/Model/Sample/LayerItem.h
index 178ff083e2d..b21f28582d5 100644
--- a/GUI/Model/Sample/LayerItem.h
+++ b/GUI/Model/Sample/LayerItem.h
@@ -61,6 +61,16 @@ public:
     QColor color() const;
     void setColor(const QColor& color);
 
+    void setIsTopLayer(bool b);
+    bool isTopLayer() const;
+
+    void setIsBottomLayer(bool b);
+    bool isBottomLayer() const;
+
+private:
+    bool m_isTopLayer = false;
+    bool m_isBottomLayer = false;
+
 private:
     // -- Only for testing
     friend class TestMapperForItem_onPropertyChange_Test;
diff --git a/GUI/Model/Sample/MultiLayerItem.cpp b/GUI/Model/Sample/MultiLayerItem.cpp
index dac2e49dc2d..baa3a88b1f9 100644
--- a/GUI/Model/Sample/MultiLayerItem.cpp
+++ b/GUI/Model/Sample/MultiLayerItem.cpp
@@ -106,23 +106,35 @@ LayerItem* MultiLayerItem::addLayer(int index)
     if (index < 0)
         index = m_layers.size();
 
-    auto* l = new LayerItem();
-    m_layers.insert(index, l);
-    return l;
+    auto* layer = new LayerItem();
+    m_layers.insert(index, layer);
+    layer->setIsTopLayer(m_layers.first() == layer);
+    layer->setIsBottomLayer(m_layers.last() == layer);
+    return layer;
 }
 
-void MultiLayerItem::removeLayer(LayerItem* item)
+void MultiLayerItem::removeLayer(LayerItem* layer)
 {
-    m_layers.removeAll(item);
+    m_layers.removeAll(layer);
+    delete layer;
+
+    if (!m_layers.isEmpty()) {
+        m_layers.first()->setIsTopLayer(true);
+        m_layers.last()->setIsBottomLayer(true);
+    }
 }
 
-void MultiLayerItem::moveLayer(LayerItem* item, LayerItem* beforeThisLayer)
+void MultiLayerItem::moveLayer(LayerItem* layer, LayerItem* beforeThisLayer)
 {
-    m_layers.removeAll(item);
-    int index = -1; // move to end
+    m_layers.removeAll(layer);
+    int index = m_layers.size(); // move to end
     if (beforeThisLayer != nullptr)
         index = m_layers.indexOf(beforeThisLayer);
-    m_layers.insert(index, item);
+    m_layers.insert(index, layer);
+    for (auto l : m_layers) {
+        l->setIsTopLayer(m_layers.first() == l);
+        l->setIsBottomLayer(m_layers.last() == l);
+    }
 }
 
 void MultiLayerItem::writeContentTo(QXmlStreamWriter* writer) const
@@ -144,6 +156,12 @@ void MultiLayerItem::writeContentTo(QXmlStreamWriter* writer) const
 void MultiLayerItem::readContentFrom(QXmlStreamReader* reader)
 {
     // #baMigration ++ implement
+
+    // set non-stored infos
+    for (auto l : m_layers) {
+        l->setIsTopLayer(m_layers.first() == l);
+        l->setIsBottomLayer(m_layers.last() == l);
+    }
 }
 
 QString MultiLayerItem::uidForDescriptor(const QString& lastPart) const
-- 
GitLab