From bf6fd34ebadd114a990c231288390c29745fc958 Mon Sep 17 00:00:00 2001
From: Peter Albrecht <albrecpe@gmail.com>
Date: Sun, 26 Nov 2023 19:15:26 +0100
Subject: [PATCH] Added memory communicator base class

---
 .../libraries/skills_gui/CMakeLists.txt       |  6 +++++
 .../memory/MemoryCommunicatorBase.cpp         |  1 +
 .../memory/MemoryCommunicatorBase.h           | 22 +++++++++++++++++++
 .../skills_gui/memory/SkillMemoryProxy.h      |  5 +++--
 .../SkillExecutionTreeWidget.h                |  6 +++--
 .../SkillExecutionTreeWidgetItem.h            | 16 +++++---------
 .../skills_gui/skills/SkillTreeWidget.cpp     |  1 +
 .../skills_gui/skills/SkillTreeWidget.h       | 21 ++++++++++++++++++
 8 files changed, 64 insertions(+), 14 deletions(-)
 create mode 100644 source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.cpp
 create mode 100644 source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.h
 create mode 100644 source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.cpp
 create mode 100644 source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.h

diff --git a/source/RobotAPI/libraries/skills_gui/CMakeLists.txt b/source/RobotAPI/libraries/skills_gui/CMakeLists.txt
index 6d4e90f17..e446267a7 100644
--- a/source/RobotAPI/libraries/skills_gui/CMakeLists.txt
+++ b/source/RobotAPI/libraries/skills_gui/CMakeLists.txt
@@ -48,6 +48,9 @@ set(SOURCES
     skill_executions/SkillExecutionTreeWidgetItem.cpp
 
     memory/SkillMemoryProxy.cpp
+    memory/MemoryCommunicatorBase.cpp
+
+    skills/SkillTreeWidget.cpp
 )
 set(HEADERS
     aron_tree_widget/visitors/AronTreeWidgetCreator.h
@@ -77,6 +80,9 @@ set(HEADERS
     skill_executions/SkillExecutionTreeWidgetItem.h
 
     memory/SkillMemoryProxy.h
+    memory/MemoryCommunicatorBase.h
+
+    skills/SkillTreeWidget.h
 )
 
 armarx_gui_library("${LIB_NAME}" "${SOURCES}" "${GUI_MOC_HDRS}" "${GUI_UIS}" "" "${LIBRARIES}")
diff --git a/source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.cpp b/source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.cpp
new file mode 100644
index 000000000..5950600af
--- /dev/null
+++ b/source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.cpp
@@ -0,0 +1 @@
+#include "MemoryCommunicatorBase.h"
diff --git a/source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.h b/source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.h
new file mode 100644
index 000000000..2fce59401
--- /dev/null
+++ b/source/RobotAPI/libraries/skills_gui/memory/MemoryCommunicatorBase.h
@@ -0,0 +1,22 @@
+#ifndef MEMORYCOMMUNICATOR_H
+#define MEMORYCOMMUNICATOR_H
+
+#include "SkillMemoryProxy.h"
+
+namespace armarx::skills::gui
+{
+    /*
+     * This base class holds a skill memory proxy.
+     */
+    class MemoryCommunicatorBase
+    {
+    protected:
+        // We don't want to instantiate the base class
+        MemoryCommunicatorBase(std::shared_ptr<SkillMemoryProxy> _memory) : memory(_memory)
+        {
+        }
+        std::shared_ptr<SkillMemoryProxy> memory;
+    };
+} // namespace armarx::skills::gui
+
+#endif // MEMORYCOMMUNICATOR_H
diff --git a/source/RobotAPI/libraries/skills_gui/memory/SkillMemoryProxy.h b/source/RobotAPI/libraries/skills_gui/memory/SkillMemoryProxy.h
index 3bb6ff4c0..d5ba4676b 100644
--- a/source/RobotAPI/libraries/skills_gui/memory/SkillMemoryProxy.h
+++ b/source/RobotAPI/libraries/skills_gui/memory/SkillMemoryProxy.h
@@ -51,7 +51,7 @@ namespace armarx::skills::gui
 
         /*
          * Returns a snapshot, which contains the most current update from memory.
-         * Calling this function will not use an Ice call!
+         * Calling this function will *not* use an Ice call!
          * Use this instead of holding the state in a widget.
          */
         Snapshot& getLatestUpdate();
@@ -65,7 +65,8 @@ namespace armarx::skills::gui
         void fetchUpdates();
 
     private:
-        skills::dti::SkillMemoryInterfacePrx* memory = nullptr;
+        mutable std::mutex updateMutex;
+        skills::dti::SkillMemoryInterfacePrx* memory;
         Snapshot snapshot;
     };
 } // namespace armarx::skills::gui
diff --git a/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidget.h b/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidget.h
index dac8bb039..d8a23d344 100644
--- a/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidget.h
+++ b/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidget.h
@@ -7,14 +7,16 @@
 
 #include "RobotAPI/libraries/skills/core/SkillExecutionID.h"
 
+#include "../memory/MemoryCommunicatorBase.h"
 #include "SkillExecutionTreeWidgetItem.h"
 
 namespace armarx::skills::gui
 {
-    class SkillExecutionTreeWidget : public QTreeWidget
+    class SkillExecutionTreeWidget : public QTreeWidget, public MemoryCommunicatorBase
     {
     public:
-        SkillExecutionTreeWidget(QWidget* parent) : QTreeWidget(parent)
+        SkillExecutionTreeWidget(QWidget* parent, std::shared_ptr<SkillMemoryProxy> _memory) :
+            QTreeWidget(parent), MemoryCommunicatorBase(_memory)
         {
         }
 
diff --git a/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidgetItem.h b/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidgetItem.h
index 035a26ada..ef160643c 100644
--- a/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidgetItem.h
+++ b/source/RobotAPI/libraries/skills_gui/skill_executions/SkillExecutionTreeWidgetItem.h
@@ -6,23 +6,20 @@
 #include <RobotAPI/libraries/skills/core/SkillExecutionID.h>
 #include <RobotAPI/libraries/skills/core/SkillStatusUpdate.h>
 
+#include "../memory/MemoryCommunicatorBase.h"
 #include "SkillExecutionTreeWidgetItem.h"
 
 namespace armarx::skills::gui
 {
-    class SkillExecutionTreeWidgetItem : QTreeWidgetItem
+    class SkillExecutionTreeWidgetItem : QTreeWidgetItem, public MemoryCommunicatorBase
     {
     public:
         SkillExecutionTreeWidgetItem() = delete;
 
         // After constructing an item, it must be manually inserted into the tree!
-        SkillExecutionTreeWidgetItem(const skills::SkillExecutionID& id) : executionId(id)
-        {
-        }
-
-        // When using this constructor, the new item will be appended to the bottom of the tree
-        SkillExecutionTreeWidgetItem(const skills::SkillExecutionID& id, QTreeWidget* parent) :
-            QTreeWidgetItem(parent), executionId(id)
+        SkillExecutionTreeWidgetItem(const skills::SkillExecutionID& id,
+                                     std::shared_ptr<SkillMemoryProxy> _memory) :
+            MemoryCommunicatorBase(_memory), executionId(id)
         {
         }
 
@@ -36,8 +33,7 @@ namespace armarx::skills::gui
     private slots:
         void runContextMenu(const QPoint& pos);
 
-    private:
-        // private since we don't want to overwrite the executionId
+    protected:
         skills::SkillExecutionID executionId;
     };
 } // namespace armarx::skills::gui
diff --git a/source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.cpp b/source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.cpp
new file mode 100644
index 000000000..d848b4d62
--- /dev/null
+++ b/source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.cpp
@@ -0,0 +1 @@
+#include "SkillTreeWidget.h"
diff --git a/source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.h b/source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.h
new file mode 100644
index 000000000..ae47f1a36
--- /dev/null
+++ b/source/RobotAPI/libraries/skills_gui/skills/SkillTreeWidget.h
@@ -0,0 +1,21 @@
+#ifndef ARMARX_SKILLS_GUI_SKILLTREEWIDGET_H
+#define ARMARX_SKILLS_GUI_SKILLTREEWIDGET_H
+
+#include <QTreeWidget>
+
+#include "../memory/MemoryCommunicatorBase.h"
+
+namespace armarx::skills::gui
+{
+    class SkillTreeWidget : public QTreeWidget, public MemoryCommunicatorBase
+    {
+    public:
+        SkillTreeWidget(QWidget* parent, std::shared_ptr<SkillMemoryProxy> _memory) :
+            QTreeWidget(parent), MemoryCommunicatorBase(_memory)
+        {
+        }
+    };
+
+} // namespace armarx::skills::gui
+
+#endif // ARMARX_SKILLS_GUI_SKILLTREEWIDGET_H
-- 
GitLab