From 21fe9bc5b4e85e0f79750de191c63d906782eff9 Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Tue, 28 Jun 2016 16:26:41 +0200
Subject: [PATCH] Make BodyTree accessible from python.

---
 include/gepetto/gui/bodytreewidget.hh |  3 +++
 include/gepetto/gui/fwd.hh            |  1 +
 include/gepetto/gui/mainwindow.hh     | 12 +++++++++---
 include/gepetto/gui/tree-item.hh      |  3 ++-
 pyplugins/CMakeLists.txt              |  1 +
 src/gui/bodytreewidget.cc             | 13 ++++++++++++-
 src/gui/pythonwidget.cc               |  1 +
 7 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/include/gepetto/gui/bodytreewidget.hh b/include/gepetto/gui/bodytreewidget.hh
index c76724a..a395078 100644
--- a/include/gepetto/gui/bodytreewidget.hh
+++ b/include/gepetto/gui/bodytreewidget.hh
@@ -68,6 +68,9 @@ namespace gepetto {
       /// Reload the body tree.
       void reloadBodyTree ();
 
+      /// Get selected bodies
+      QList<BodyTreeItem*> selectedBodies() const;
+
     protected slots:
       /// Display the context menu for one item.
       /// \param pos position of the item
diff --git a/include/gepetto/gui/fwd.hh b/include/gepetto/gui/fwd.hh
index 58c8da2..c1de5dd 100644
--- a/include/gepetto/gui/fwd.hh
+++ b/include/gepetto/gui/fwd.hh
@@ -10,6 +10,7 @@ namespace gepetto {
     class OSGWidget;
     class PickHandler;
     class BodyTreeWidget;
+    class BodyTreeItem;
 
     class ViewerCorbaServer;
 
diff --git a/include/gepetto/gui/mainwindow.hh b/include/gepetto/gui/mainwindow.hh
index 0839c59..308af89 100644
--- a/include/gepetto/gui/mainwindow.hh
+++ b/include/gepetto/gui/mainwindow.hh
@@ -68,9 +68,6 @@ namespace gepetto {
         /// Get the list of windows.
         QList <OSGWidget*> osgWindows () const;
 
-        /// Get the body tree widget.
-        BodyTreeWidget* bodyTree () const;
-
         /// Get the plugin manager.
         PluginManager* pluginManager ();
 
@@ -98,6 +95,13 @@ signals:
         void selectJointFromBodyName(const QString bodyName);
 
         public slots:
+          /// \addtogroup available_in_python Python API
+          /// These slots are available for Python scripting in plugins
+          /// \{
+
+        /// Get the body tree widget.
+        BodyTreeWidget* bodyTree () const;
+
         /// Add the text to logs.
         /// \param text text to log
         void log (const QString& text);
@@ -151,6 +155,8 @@ signals:
 
         void connectSlot(const char *slot, const char *signal, QObject* obj);
 
+        /// \}
+
         private slots:
           OSGWidget* onCreateView(QString name);
           OSGWidget* onCreateView();
diff --git a/include/gepetto/gui/tree-item.hh b/include/gepetto/gui/tree-item.hh
index a61b6c0..b1457d2 100644
--- a/include/gepetto/gui/tree-item.hh
+++ b/include/gepetto/gui/tree-item.hh
@@ -41,7 +41,7 @@ namespace gepetto {
       protected:
         void init ();
 
-        protected slots:
+        public slots:
           void setViewingMode (QString mode);
         void setVisibilityMode (QString mode);
         void removeFromGroup ();
@@ -49,6 +49,7 @@ namespace gepetto {
         void removeAll ();
         void addLandmark ();
         void deleteLandmark ();
+        QString text () const { return QStandardItem::text(); }
 
       private:
         graphics::NodePtr_t node_;
diff --git a/pyplugins/CMakeLists.txt b/pyplugins/CMakeLists.txt
index d1404b0..8cabd85 100644
--- a/pyplugins/CMakeLists.txt
+++ b/pyplugins/CMakeLists.txt
@@ -18,6 +18,7 @@
 INSTALL(
   FILES
   ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/pythonwidget.py
+  ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/blenderexport.py
   ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/__init__.py
   DESTINATION ${PYTHON_SITELIB}/gepetto/gui
   )
diff --git a/src/gui/bodytreewidget.cc b/src/gui/bodytreewidget.cc
index a44b850..00208d7 100644
--- a/src/gui/bodytreewidget.cc
+++ b/src/gui/bodytreewidget.cc
@@ -70,7 +70,7 @@ namespace gepetto {
       toolBox_ = toolBox;
       model_  = new QStandardItemModel (this);
       view_->setModel(model_);
-      view_->setSelectionMode(QAbstractItemView::SingleSelection);
+      view_->setSelectionMode(QAbstractItemView::ExtendedSelection);
 
       connect (main, SIGNAL(refresh()), SLOT(reloadBodyTree()));
       connect (view_, SIGNAL (customContextMenuRequested(QPoint)), SLOT(customContextMenu(QPoint)));
@@ -132,6 +132,17 @@ namespace gepetto {
       model_->appendRow(new BodyTreeItem (this, group));
     }
 
+    QList<BodyTreeItem*> BodyTreeWidget::selectedBodies() const
+    {
+      QList<BodyTreeItem*> list;
+      foreach (const QModelIndex& index, view_->selectionModel ()->selectedIndexes ()) {
+        BodyTreeItem *item = dynamic_cast <BodyTreeItem*>
+          (model_->itemFromIndex (index));
+        if (item) list.push_back(item);
+      }
+      return list;
+    }
+
     void BodyTreeWidget::customContextMenu(const QPoint &pos)
     {
       QModelIndex index = view_->indexAt(pos);
diff --git a/src/gui/pythonwidget.cc b/src/gui/pythonwidget.cc
index bd1b5f8..8d88550 100644
--- a/src/gui/pythonwidget.cc
+++ b/src/gui/pythonwidget.cc
@@ -37,6 +37,7 @@ namespace gepetto {
             PythonQtObjectPtr sys = PythonQt::self()->importModule ("sys");
             sys.evalScript ("argv = ['gepetto-gui']");
             console_ = new PythonQtScriptingConsole(NULL, mainContext_);
+            PythonQt::self()->registerQObjectClassNames(QStringList() << "BodyTreeWidget" << "BodyTreeItem");
             mainContext_.addObject("mainWindow", MainWindow::instance());
             console_->QTextEdit::clear();
             console_->consoleMessage(
-- 
GitLab