From 049a6d8c166ddf6e84980893d3dc1d136f4b040a Mon Sep 17 00:00:00 2001
From: Mikhail Svechnikov <m.svechnikov@fz-juelich.de>
Date: Thu, 6 Jul 2023 16:28:55 +0200
Subject: [PATCH] cleanup & use ActionFactory::createDuplicateAction

---
 GUI/Support/Util/ActionFactory.cpp            | 15 +++++++++++++
 GUI/Support/Util/ActionFactory.h              |  6 +++++
 GUI/View/SampleDesigner/LayerForm.cpp         |  8 ++-----
 .../SampleDesigner/ParticleLayoutForm.cpp     | 22 ++++++++++++-------
 4 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/GUI/Support/Util/ActionFactory.cpp b/GUI/Support/Util/ActionFactory.cpp
index 3fdbd28c823..ee36e5fe827 100644
--- a/GUI/Support/Util/ActionFactory.cpp
+++ b/GUI/Support/Util/ActionFactory.cpp
@@ -31,6 +31,21 @@ QAction* ActionFactory::createRemoveAction(QObject* parent, const QString& what,
     return removeAction;
 }
 
+QAction* ActionFactory::createDuplicateAction(QObject* parent, const QString& what,
+                                              std::function<void()> slot)
+{
+    auto* duplicateAction = new QAction(parent);
+    duplicateAction->setText("Duplicate");
+    duplicateAction->setIcon(QIcon(":/images/content-copy.svg"));
+    duplicateAction->setIconText("Duplicate");
+    duplicateAction->setToolTip("Duplicate " + what);
+
+    if (slot)
+        QObject::connect(duplicateAction, &QAction::triggered, slot);
+
+    return duplicateAction;
+}
+
 QAction* ActionFactory::createShowInRealspaceAction(QObject* parent, const QString& what,
                                                     std::function<void()> slot)
 {
diff --git a/GUI/Support/Util/ActionFactory.h b/GUI/Support/Util/ActionFactory.h
index b0370214863..ea3ba0d00a3 100644
--- a/GUI/Support/Util/ActionFactory.h
+++ b/GUI/Support/Util/ActionFactory.h
@@ -37,6 +37,12 @@ public:
     //! If a slot is given, it will be connected to the "triggered" signal.
     static QAction* createRemoveAction(QObject* parent, const QString& what,
                                        std::function<void()> slot = nullptr);
+    //! Create "duplicate" action.
+    //!
+    //! The "what" text will be used in the tooltip, appended to "Duplicate ".
+    //! If a slot is given, it will be connected to the "triggered" signal.
+    static QAction* createDuplicateAction(QObject* parent, const QString& what,
+                                          std::function<void()> slot = nullptr);
 
     //! Create "show in Realspace" action.
     //!
diff --git a/GUI/View/SampleDesigner/LayerForm.cpp b/GUI/View/SampleDesigner/LayerForm.cpp
index 05ea512a606..f3d15e6eb1a 100644
--- a/GUI/View/SampleDesigner/LayerForm.cpp
+++ b/GUI/View/SampleDesigner/LayerForm.cpp
@@ -83,12 +83,8 @@ LayerForm::LayerForm(QWidget* parent, LayerItem* layer, SampleEditorController*
     }
     // duplicate
     {
-        m_duplicateAction = new QAction(this);
-        m_duplicateAction->setIcon(QIcon(":/images/content-copy.svg"));
-        m_duplicateAction->setToolTip("Duplicate layer");
-        connect(m_duplicateAction, &QAction::triggered,
-                [this] { m_ec->duplicateLayerItem(m_layer); });
-
+        m_duplicateAction = ActionFactory::createDuplicateAction(
+            this, "layer", [this] { m_ec->duplicateLayerItem(m_layer); });
         m_collapser->addAction(m_duplicateAction);
     }
     // remove
diff --git a/GUI/View/SampleDesigner/ParticleLayoutForm.cpp b/GUI/View/SampleDesigner/ParticleLayoutForm.cpp
index c29ffe72bde..db040892cfd 100644
--- a/GUI/View/SampleDesigner/ParticleLayoutForm.cpp
+++ b/GUI/View/SampleDesigner/ParticleLayoutForm.cpp
@@ -53,19 +53,25 @@ ParticleLayoutForm::ParticleLayoutForm(LayerForm* parent, ParticleLayoutItem* la
     m_structureEditingWidgets << btn;
     layouter.addStructureEditingRow(btn);
 
-    m_removeAction = ActionFactory::createRemoveAction(
-        this, "particle layout", [=] { ec->removeLayoutItem(parent, layoutItem); });
-
-    auto* showInRealspaceAction = ActionFactory::createShowInRealspaceAction(
-        this, "particle layout", [=] { ec->requestViewInRealspace(layoutItem); });
-
     m_collapser = GroupBoxCollapser::installIntoGroupBox(this);
     m_collapser->setExpanded(layoutItem->isExpandLayout());
     connect(m_collapser, &GroupBoxCollapser::toggled, this,
             [layoutItem](bool b) { layoutItem->setExpandLayout(b); });
 
-    m_collapser->addAction(showInRealspaceAction);
-    m_collapser->addAction(m_removeAction);
+    // top right corner actions
+    // show in real space
+    {
+        auto* showInRealspaceAction = ActionFactory::createShowInRealspaceAction(
+            this, "particle layout", [=] { ec->requestViewInRealspace(layoutItem); });
+        m_collapser->addAction(showInRealspaceAction);
+    }
+    // remove
+    {
+        m_removeAction = ActionFactory::createRemoveAction(
+            this, "particle layout", [=] { ec->removeLayoutItem(parent, layoutItem); });
+        m_collapser->addAction(m_removeAction);
+    }
+
     m_layout = layouter.layout();
 
     updateDensityEnabling();
-- 
GitLab