From 6bdef3f5c892b27ffb4eca19c3541d28719f1efb Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Fri, 24 Aug 2018 14:03:34 +0200
Subject: [PATCH] Fix selection of bodies

---
 include/gepetto/gui/fwd.hh             |  3 +++
 include/gepetto/gui/windows-manager.hh |  2 --
 src/gui/bodytreewidget.cc              | 26 ++++++++++----------------
 src/gui/tree-item.cc                   |  5 +----
 src/gui/windows-manager.cc             |  2 +-
 5 files changed, 15 insertions(+), 23 deletions(-)

diff --git a/include/gepetto/gui/fwd.hh b/include/gepetto/gui/fwd.hh
index 1f08e99..4b6c73b 100644
--- a/include/gepetto/gui/fwd.hh
+++ b/include/gepetto/gui/fwd.hh
@@ -17,6 +17,8 @@
 #ifndef GEPETTO_GUI_FWD_HH
 #define GEPETTO_GUI_FWD_HH
 
+#include <vector>
+
 #include <gepetto/viewer/macros.h>
 #include <gepetto/gui/config-dep.hh>
 
@@ -27,6 +29,7 @@ namespace gepetto {
     class PickHandler;
     class BodyTreeWidget;
     class BodyTreeItem;
+    typedef std::vector<BodyTreeItem*> BodyTreeItems_t;
     class ShortcutFactory;
     class SelectionHandler;
     class SelectionEvent;
diff --git a/include/gepetto/gui/windows-manager.hh b/include/gepetto/gui/windows-manager.hh
index a119d4f..0870fb5 100644
--- a/include/gepetto/gui/windows-manager.hh
+++ b/include/gepetto/gui/windows-manager.hh
@@ -46,8 +46,6 @@ namespace gepetto {
         typedef graphics::NodePtr_t      NodePtr_t;
         typedef graphics::GroupNodePtr_t GroupNodePtr_t;
 
-        typedef std::vector<BodyTreeItem*> BodyTreeItems_t;
-
         static WindowsManagerPtr_t create (BodyTreeWidget* bodyTree);
 
         WindowID createWindow(const std::string& windowName);
diff --git a/src/gui/bodytreewidget.cc b/src/gui/bodytreewidget.cc
index 51cea21..785f4f1 100644
--- a/src/gui/bodytreewidget.cc
+++ b/src/gui/bodytreewidget.cc
@@ -59,24 +59,19 @@ namespace gepetto {
 
     void BodyTreeWidget::selectBodyByName(const QString bodyName)
     {
-      QList<QStandardItem*> matches;
-      if (!bodyName.isEmpty() && !bodyName.isNull()) {
-        matches = model_->findItems(bodyName, Qt::MatchFixedString
-            | Qt::MatchCaseSensitive
-            | Qt::MatchRecursive);
-      }
-      if (matches.empty()) {
-        qDebug () << "Body" << bodyName << "not found.";
-        view_->clearSelection();
-      } else {
-        view_->setCurrentIndex(matches.first()->index());
-      }
+      qDebug () << "Use std::string instead of QString";
+      return selectBodyByName (bodyName.toStdString());
     }
 
     void BodyTreeWidget::selectBodyByName (const std::string& bodyName)
     {
-      qDebug () << "Use QString instead of std::string";
-      return selectBodyByName (QString::fromStdString (bodyName));
+      BodyTreeItems_t bodies = osg_->bodyTreeItems (bodyName);
+      if (bodies.empty()) {
+        qDebug () << "Body" << bodyName.c_str() << "not found.";
+        view_->clearSelection();
+      } else {
+        view_->setCurrentIndex(bodies[0]->index());
+      }
     }
 
     void BodyTreeWidget::handleSelectionEvent (const SelectionEvent* event)
@@ -86,8 +81,7 @@ namespace gepetto {
           this, SLOT (currentChanged(QModelIndex,QModelIndex)));
       BodyTreeItem* item = NULL;
       if (event->node()) {
-        WindowsManager::BodyTreeItems_t matches =
-          osg_->bodyTreeItems(event->node()->getID());
+        BodyTreeItems_t matches = osg_->bodyTreeItems(event->node()->getID());
 
         if (matches.empty())
           view_->clearSelection();
diff --git a/src/gui/tree-item.cc b/src/gui/tree-item.cc
index a6997c1..e671770 100644
--- a/src/gui/tree-item.cc
+++ b/src/gui/tree-item.cc
@@ -114,13 +114,10 @@ namespace gepetto {
 
     BodyTreeItem::BodyTreeItem(QObject *parent, graphics::NodePtr_t node) :
       QObject (parent),
-      QStandardItem (QString (node->getID().c_str())),
+      QStandardItem (QString::fromStdString (node->getID().substr(node->getID().find_last_of("/") + 1))),
       node_ (node)
     {
       setEditable(false);
-
-      const std::string & name = node->getID();
-      QStandardItem::setText(name.substr(name.find_last_of("/") + 1).c_str());
     }
 
     void BodyTreeItem::initialize ()
diff --git a/src/gui/windows-manager.cc b/src/gui/windows-manager.cc
index 71bc0b1..83a2d91 100644
--- a/src/gui/windows-manager.cc
+++ b/src/gui/windows-manager.cc
@@ -149,7 +149,7 @@ namespace gepetto {
       return ret;
     }
 
-    WindowsManager::BodyTreeItems_t WindowsManager::bodyTreeItems (const std::string& name) const
+    BodyTreeItems_t WindowsManager::bodyTreeItems (const std::string& name) const
     {
       BodyTreeItemMap_t::const_iterator _btis = nodeItemMap_.find(name);
       if (_btis != nodeItemMap_.end())
-- 
GitLab