From 499ac8c2f96528f4c5b20909556df5add6ce4433 Mon Sep 17 00:00:00 2001
From: Peter Albrecht <usnlf@student.kit.edu>
Date: Thu, 19 Dec 2024 19:09:42 +0100
Subject: [PATCH] Fix widget not updating

---
 .../libraries/skills_gui/SkillMemoryGui.cpp   |  3 ++
 .../skill_details/SkillDetailsTreeWidget.cpp  | 42 ++++++++-----------
 .../skill_details/SkillDetailsTreeWidget.h    |  1 +
 .../skills_gui/skills/SkillGroupBox.cpp       |  2 +-
 4 files changed, 22 insertions(+), 26 deletions(-)

diff --git a/source/RobotAPI/libraries/skills_gui/SkillMemoryGui.cpp b/source/RobotAPI/libraries/skills_gui/SkillMemoryGui.cpp
index fb6f6e166..7d6b336fe 100644
--- a/source/RobotAPI/libraries/skills_gui/SkillMemoryGui.cpp
+++ b/source/RobotAPI/libraries/skills_gui/SkillMemoryGui.cpp
@@ -1,4 +1,5 @@
 #include "SkillMemoryGui.h"
+#include "RobotAPI/libraries/skills_gui/skill_details/SkillDetailsGroupBox.h"
 
 #include "gui_utils.h"
 
@@ -124,6 +125,8 @@ namespace armarx::skills::gui
                 this->skillExecutionTreeWidget,
                 &SkillExecutionTreeWidget::updateGui);
 
+        connect(this, &SkillMemoryGUI::updateGui, this->skillDetailGroupBox, &SkillDetailGroupBox::updateGui);
+
 
         // stop all
         connect(stopAllButton,
diff --git a/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.cpp b/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.cpp
index d3318e66e..862024ab5 100644
--- a/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.cpp
+++ b/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.cpp
@@ -1,4 +1,5 @@
 #include "SkillDetailsTreeWidget.h"
+#include <optional>
 
 #include <QApplication>
 #include <QClipboard>
@@ -84,7 +85,9 @@ namespace armarx::skills::gui
     SkillDetailsTreeWidget::updateGui(SkillManagerWrapper::Snapshot update)
     {
         if (not shownSkill.has_value())
+        {
             return;
+        }
         skills::SkillID sid = shownSkill.value().skillId;
 
         // we assume the id to be fully specified, as it is checked while constructing
@@ -92,7 +95,7 @@ namespace armarx::skills::gui
         ARMARX_CHECK(sid.isFullySpecified());
 
         // maybe the search is empty?
-        auto skillsMap = memory->getSkills();
+        auto skillsMap = update.skills;
         if (skillsMap.count(sid.providerId.value()) == 0 ||
             skillsMap.at(sid.providerId.value()).count(sid) == 0)
         {
@@ -100,9 +103,16 @@ namespace armarx::skills::gui
             return;
         }
 
-        auto descr = memory->getSkills().at(sid.providerId.value()).at(sid);
+        auto descr = update.skills.at(sid.providerId.value()).at(sid);
 
-        this->updateContents(sid, descr);
+        // only triggers if the description does not match
+        if (descr != shownSkill->descr)
+        {
+            if (askUserToConfirmWidgetReset("The currently shown skill in the details widget has not been found in the latest manager update. Did the skill provider die?"))
+            {
+                this->updateContents(sid, descr);
+            }
+        }
     }
 
     void
@@ -122,29 +132,10 @@ namespace armarx::skills::gui
         setColumnHidden(3, true);
     }
     
-    void SkillDetailsTreeWidget::checkIfShownSkillIsAvailable(SkillManagerWrapper::Snapshot& update) 
+    bool SkillDetailsTreeWidget::askUserToConfirmWidgetReset(std::string reason) 
     {
-        if (this->shownSkill.has_value())
-        {
-            // iterate over all available skill ids
-            for (auto& availableProviderMap : update.skills)
-            {
-                for (auto& availableSkill : availableProviderMap.second)
-                {
-                    // if we find a matching skill id, we return
-                    if (availableSkill.first == this->shownSkill->skillId)
-                    {
-                        return;
-                    }
-                }
-            }
-
-            // in this case, no matching skill id has been found
-            // -> reset this view, as the shown skill is invalid
-            ARMARX_INFO << deactivateSpam(10) 
-                        << "The currently shown skill in the details widget has not been found in the latest manager update. Resetting the widget...";
-            this->resetWidget();
-        }
+        ARMARX_WARNING << "The skill details widget will be reset. Reason: " << reason;
+        return true;
     }
 
     /**
@@ -238,6 +229,7 @@ namespace armarx::skills::gui
     SkillDetailsTreeWidget::resetWidget()
     {
         this->clear();
+        this->shownSkill = std::nullopt;
         aronTreeWidgetController = nullptr;
     }
 
diff --git a/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.h b/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.h
index ae8b35215..5d8417ea4 100644
--- a/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.h
+++ b/source/RobotAPI/libraries/skills_gui/skill_details/SkillDetailsTreeWidget.h
@@ -49,6 +49,7 @@ namespace armarx::skills::gui
         AronTreeWidgetControllerPtr aronTreeWidgetController = nullptr;
         void setupUi();
         void checkIfShownSkillIsAvailable(SkillManagerWrapper::Snapshot& update);
+        bool askUserToConfirmWidgetReset(std::string reason);
     };
 } // namespace armarx::skills::gui
 
diff --git a/source/RobotAPI/libraries/skills_gui/skills/SkillGroupBox.cpp b/source/RobotAPI/libraries/skills_gui/skills/SkillGroupBox.cpp
index d57753f5c..ab949d0f3 100644
--- a/source/RobotAPI/libraries/skills_gui/skills/SkillGroupBox.cpp
+++ b/source/RobotAPI/libraries/skills_gui/skills/SkillGroupBox.cpp
@@ -34,7 +34,7 @@ namespace armarx::skills::gui
     void
     SkillGroupBox::searchCallback()
     {
-        this->skillTreeWidget->updateGui(memory->getSkills());
+        this->skillTreeWidget->updateGui(memory->getSkillsFiltered());
     }
 
     void
-- 
GitLab