From c5c9a18a408c01046e6a7550adb852b960b66edf Mon Sep 17 00:00:00 2001
From: Peter Albrecht <albrecpe@gmail.com>
Date: Thu, 14 Dec 2023 15:56:28 +0100
Subject: [PATCH] Implemented re-running with similar params

---
 .../executions/SkillExecutionTreeWidget.cpp   | 34 ++++++++++++++++---
 .../executions/SkillExecutionTreeWidget.h     |  3 +-
 .../executions/SkillExecutionTreeWidgetItem.h |  4 +--
 3 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.cpp b/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.cpp
index f1be93266..dd1f601f1 100644
--- a/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.cpp
+++ b/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.cpp
@@ -27,13 +27,13 @@ namespace armarx::skills::gui
         menu->addAction(stopSkillAction);
         menu->addAction(rerunSkillAction);
         connect(stopSkillAction,
-                SIGNAL(stopSkillAction.triggered()),
+                &QAction::triggered,
                 this,
-                SIGNAL(stopSelectedExecution()));
+                &SkillExecutionTreeWidget::stopSelectedExecution);
         connect(rerunSkillAction,
-                SIGNAL(rerunSkillAction.triggered()),
+                &QAction::triggered,
                 this,
-                SIGNAL(rerunSkillWithSimilarParams()));
+                &SkillExecutionTreeWidget::rerunSkillWithSimilarParams);
 
         // Temporarily disable rerun-skill-Action
         rerunSkillAction->setDisabled(true);
@@ -48,6 +48,25 @@ namespace armarx::skills::gui
         memory->stopExecution(this->selectedExecution.skillExecutionId);
     }
 
+    void
+    SkillExecutionTreeWidget::rerunSkillWithSimilarParams()
+    {
+        // we don't want to hold state in the gui, so we need to get the parameters from memory:
+        skills::SkillExecutionID currentExecutionId = this->selectedExecution.skillExecutionId;
+        auto update = memory->getLatestUpdate();
+        if (!update.statuses.contains(currentExecutionId))
+        {
+            // we didn't find an entry for the execution id
+            ARMARX_IMPORTANT << "The selected execution was not found in memory. The GUI is unable "
+                                "to determine the parametrization for this execution.";
+            return;
+        }
+        auto params = update.statuses[currentExecutionId].parameters;
+
+        // give all information to manager
+        this->memory->startExecutionWithParams(currentExecutionId.skillId, params);
+    }
+
     void
     SkillExecutionTreeWidget::refresh()
     {
@@ -61,7 +80,12 @@ namespace armarx::skills::gui
             SkillExecutionTreeWidgetItem* found = nullptr;
             for (int i = 0; i < this->topLevelItemCount(); ++i)
             {
-                auto c = static_cast<SkillExecutionTreeWidgetItem*>(topLevelItem(i));
+                auto c = dynamic_cast<SkillExecutionTreeWidgetItem*>(topLevelItem(i));
+                if (!c)
+                {
+                    // the item is probably not the correct type, skip...
+                    continue;
+                }
 
                 found = SkillExecutionTreeWidgetItem::SearchRecursiveForMatch(c, executionId);
 
diff --git a/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.h b/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.h
index 211538c64..797e60d41 100644
--- a/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.h
+++ b/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidget.h
@@ -47,14 +47,13 @@ namespace armarx::skills::gui
 
         SelectedExecution& getSelectedExecution();
 
-    signals:
-        void rerunSkillWithSimilarParams();
     private slots:
         void stopAllExecutions();
         void executionSelectionChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous);
         void refresh();
         void runContextMenu(const QPoint& pos);
         void stopSelectedExecution();
+        void rerunSkillWithSimilarParams();
 
     private:
         void setupUi();
diff --git a/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidgetItem.h b/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidgetItem.h
index 18afc6356..270ef0321 100644
--- a/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidgetItem.h
+++ b/source/RobotAPI/libraries/skills_gui/executions/SkillExecutionTreeWidgetItem.h
@@ -27,12 +27,10 @@ namespace armarx::skills::gui
         SearchRecursiveForMatch(SkillExecutionTreeWidgetItem* haystack,
                                 const skills::SkillExecutionID& needle);
 
-        skills::SkillExecutionID getExecutionID();
-        void updateItem(skills::SkillStatus status);
-
     protected:
         skills::SkillExecutionID executionId;
 
+
     private:
         void setupUi();
     };
-- 
GitLab