From c431159d026b5b79278c9a98ee6233478a186d7d Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Fri, 20 May 2016 15:01:50 +0200
Subject: [PATCH] Add signal handler

---
 include/gepetto/gui/mainwindow.hh   |  1 +
 include/gepetto/gui/pythonwidget.hh |  3 +++
 pyplugins/gepetto/gui/plugin.py     |  7 ++++++
 src/gui/mainwindow.cc               |  1 +
 src/gui/pythonwidget.cc             | 35 ++++++++++++++++++++++++++---
 5 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/include/gepetto/gui/mainwindow.hh b/include/gepetto/gui/mainwindow.hh
index 0a0bc52..3ea06c6 100644
--- a/include/gepetto/gui/mainwindow.hh
+++ b/include/gepetto/gui/mainwindow.hh
@@ -65,6 +65,7 @@ namespace gepetto {
 signals:
         void sendToBackground (WorkItem* item);
         void createView (QString name);
+        void viewCreated (OSGWidget* widget);
         void refresh ();
         void applyCurrentConfiguration();
         void configurationValidation();
diff --git a/include/gepetto/gui/pythonwidget.hh b/include/gepetto/gui/pythonwidget.hh
index c8198b1..03e4de5 100644
--- a/include/gepetto/gui/pythonwidget.hh
+++ b/include/gepetto/gui/pythonwidget.hh
@@ -1,6 +1,7 @@
 #ifndef GEPETTO_GUI_PYTHONWIDGET_HH
 #define GEPETTO_GUI_PYTHONWIDGET_HH
 
+#include <gepetto/gui/fwd.hh>
 #include <gepetto/gui/config-dep.hh>
 
 #if ! GEPETTO_GUI_HAS_PYTHONQT
@@ -44,6 +45,8 @@ namespace gepetto {
     private:
       void unloadModulePlugin(PythonQtObjectPtr module);
 
+      void addSignalHandlersToPlugin(PythonQtObjectPtr plugin);
+
       PythonQtObjectPtr mainContext_;
       PythonQtScriptingConsole* console_;
       QPushButton* button_;
diff --git a/pyplugins/gepetto/gui/plugin.py b/pyplugins/gepetto/gui/plugin.py
index e823e38..c49f042 100644
--- a/pyplugins/gepetto/gui/plugin.py
+++ b/pyplugins/gepetto/gui/plugin.py
@@ -72,5 +72,12 @@ class Plugin(QtGui.QDockWidget):
         self.tabWidget.addTab (self.nodeCreator, "Node Creator")
         mainWindow.connect('refresh()', self.refresh)
 
+    ### If present, this function is called when a new OSG Widget is created.
+    def osgWidget(self, osgWindow):
+        osgWindow.connect('selected(QString)', self.selected)
+
     def refresh(self):
         self.nodeCreator.update()
+
+    def selected(self, name):
+        QtGui.QMessageBox.information(self, "Selected object", name)
diff --git a/src/gui/mainwindow.cc b/src/gui/mainwindow.cc
index 1169a86..e5d1ce7 100644
--- a/src/gui/mainwindow.cc
+++ b/src/gui/mainwindow.cc
@@ -215,6 +215,7 @@ namespace gepetto {
         connect(ui_->actionAdd_floor, SIGNAL (triggered()), centralWidget_, SLOT (addFloor()));
       }
       osgWindows_.append(osgWidget);
+      emit viewCreated(osgWidget);
       delayedCreateView_.unlock();
       return osgWidget;
     }
diff --git a/src/gui/pythonwidget.cc b/src/gui/pythonwidget.cc
index cd5997e..bfb49ee 100644
--- a/src/gui/pythonwidget.cc
+++ b/src/gui/pythonwidget.cc
@@ -8,6 +8,26 @@
 
 namespace gepetto {
     namespace gui {
+      namespace {
+        void addSignalHandler (PythonQtObjectPtr obj, const QString& callable,
+            QObject* sender, const char* signal) {
+          PythonQt* pqt = PythonQt::self();
+          PythonQtObjectPtr call = pqt->lookupCallable(obj, callable);
+          if (call.isNull()) {
+            qDebug() << "Callable" << callable << "not found.";
+            return;
+          }
+          if (!pqt->addSignalHandler(sender, signal, call)) {
+            qDebug() << "Signal" << signal << "not found in object"
+              << sender->objectName();
+          } else {
+            qDebug() << "Connected"
+              << signal << "of" << sender->objectName()
+              << "to" << callable;
+          }
+        }
+      }
+
         PythonWidget::PythonWidget(QWidget *parent) :
             QDockWidget("PythonQt console", parent)
         {
@@ -56,7 +76,8 @@ namespace gepetto {
         }
 
         void PythonWidget::loadModulePlugin(QString moduleName) {
-          PythonQtObjectPtr module = PythonQt::self()->importModule (moduleName);
+          PythonQt* pqt = PythonQt::self();
+          PythonQtObjectPtr module = pqt->importModule (moduleName);
           if (module.isNull()) {
             qDebug() << "Enable to load module" << moduleName;
             return;
@@ -64,7 +85,7 @@ namespace gepetto {
           module.addObject("mainWindow", MainWindow::instance());
           QString var = "pluginInstance";
           module.evalScript (var + " = Plugin(mainWindow)");
-          PythonQtObjectPtr dockPyObj = PythonQt::self()->lookupObject(module,var);
+          PythonQtObjectPtr dockPyObj = pqt->lookupObject(module,var);
           PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*) dockPyObj.object();
           if (wrap->classInfo()->className() == "QDockWidget") {
 //            This solution would be better, but when deleting this dock widget,
@@ -75,6 +96,7 @@ namespace gepetto {
             module.evalScript (var + ".visible = False");
             module.evalScript (var + ".toggleViewAction().setIcon(QtGui.QIcon.fromTheme('window-new'))");
           }
+          addSignalHandlersToPlugin(dockPyObj);
           modules_[moduleName] = module;
         }
 
@@ -87,8 +109,9 @@ namespace gepetto {
         }
 
         void PythonWidget::unloadModulePlugin(PythonQtObjectPtr module ) {
+          PythonQt* pqt = PythonQt::self();
           QString var = "pluginInstance";
-          PythonQtObjectPtr dockPyObj = PythonQt::self()->lookupObject(module,var);
+          PythonQtObjectPtr dockPyObj = pqt->lookupObject(module,var);
           PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*) dockPyObj.object();
           if (wrap->classInfo()->className() == "QDockWidget") {
 //            this generates SEGV
@@ -102,5 +125,11 @@ namespace gepetto {
         void PythonWidget::addToContext(QString const& name, QObject* obj) {
             mainContext_.addObject(name, obj);
         }
+
+      void PythonWidget::addSignalHandlersToPlugin(PythonQtObjectPtr plugin)
+      {
+        addSignalHandler(plugin, "osgWidget",
+            MainWindow::instance(), SIGNAL(viewCreated(OSGWidget*)));
+      }
     }
 }
-- 
GitLab