From 7dfa5a426ff51f46c95ac3806188d59b62b7cdda Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Fri, 30 Sep 2016 17:47:49 +0200
Subject: [PATCH] Make WindowsManager accessible in PythonQt plugins

* and update example plugin
---
 CMakeLists.txt                         |  2 +-
 include/gepetto/gui/windows-manager.hh |  2 ++
 pyplugins/gepetto/gui/pythonwidget.py  | 13 ++++++++++++-
 src/gui/pythonwidget.cc                |  4 +++-
 src/gui/windows-manager.cc             |  5 +++++
 5 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7baa439..3c60f4c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -85,12 +85,12 @@ IF(NOT CLIENT_ONLY)
     ${CMAKE_SOURCE_DIR}/include/gepetto/gui/pick-handler.hh
     ${CMAKE_SOURCE_DIR}/include/gepetto/gui/shortcut-factory.hh
     ${CMAKE_SOURCE_DIR}/include/gepetto/gui/selection-handler.hh
+    ${CMAKE_SOURCE_DIR}/include/gepetto/gui/windows-manager.hh
     )
   SET (${PROJECT_NAME}_HEADERS_NO_MOC
     ${CMAKE_SOURCE_DIR}/include/gepetto/gui/fwd.hh
     ${CMAKE_SOURCE_DIR}/include/gepetto/gui/meta.hh
     ${CMAKE_SOURCE_DIR}/include/gepetto/gui/settings.hh
-    ${CMAKE_SOURCE_DIR}/include/gepetto/gui/windows-manager.hh
     ${CMAKE_SOURCE_DIR}/include/gepetto/gui/color-map.hh
     ${CMAKE_BINARY_DIR}/include/gepetto/gui/config-dep.hh
     )
diff --git a/include/gepetto/gui/windows-manager.hh b/include/gepetto/gui/windows-manager.hh
index 0a6bea8..6c75aea 100644
--- a/include/gepetto/gui/windows-manager.hh
+++ b/include/gepetto/gui/windows-manager.hh
@@ -40,6 +40,8 @@ namespace gepetto {
         bool removeFromGroup (const std::string& nodeName, const std::string& groupName);
         bool deleteNode (const std::string& nodeName, bool all);
 
+        public slots:
+          int createWindow(QString windowName);
       protected:
         WindowsManager (BodyTreeWidget* bodyTree);
 
diff --git a/pyplugins/gepetto/gui/pythonwidget.py b/pyplugins/gepetto/gui/pythonwidget.py
index 6a7c543..7e39cea 100644
--- a/pyplugins/gepetto/gui/pythonwidget.py
+++ b/pyplugins/gepetto/gui/pythonwidget.py
@@ -29,6 +29,8 @@ class _NodeCreator (QtGui.QWidget):
         # Add box
         box.addWidget(self.bindFunctionToButton("Add box", self.addBox))
 
+        box.addWidget(self.bindFunctionToButton("Create window", self.createWindow))
+
         self.update()
 
     def update(self):
@@ -58,6 +60,9 @@ class _NodeCreator (QtGui.QWidget):
         self.plugin.client.gui.addBox(str(self.nodeName.text), 1, 1, 1, [1, 0, 0, 1])
         self.refreshBodyTree()
 
+    def createWindow (self):
+        self.plugin.windowsManager.createWindow(str(self.nodeName.text))
+
     def createGroup (self):
         self.plugin.client.gui.createGroup(str(self.nodeName.text))
         self.groupNodes.addItem(self.nodeName.text)
@@ -81,18 +86,24 @@ class Plugin(QtGui.QDockWidget):
             super(Plugin, self).__init__ ("Gepetto Viewer plugin", mainWindow)
         else:
             super(Plugin, self).__init__ ("Gepetto Viewer plugin", mainWindow, flags)
+        self.setObjectName("Gepetto Viewer plugin")
         self.client = Client()
         # Initialize the widget
         self.tabWidget = QtGui.QTabWidget(self)
+        # This avoids having a widget bigger than what it needs. It avoids having
+        # a big dock widget and a small osg widget when creating the main osg widget.
+        p = Qt.QSizePolicy.Maximum
+        self.tabWidget.setSizePolicy(Qt.QSizePolicy(p,p))
         self.setWidget (self.tabWidget)
         self.nodeCreator = _NodeCreator(self, self)
         self.tabWidget.addTab (self.nodeCreator, "Node Creator")
         self.main = mainWindow
+        self.windowsManager = windowsManager
         mainWindow.connect('refresh()', self.refresh)
 
     ### If present, this function is called when a new OSG Widget is created.
     def osgWidget(self, osgWindow):
-        osgWindow.connect('clicked(QString,QVector3D)', self.selected)
+        osgWindow.connect('clicked(QString,QVector3D,QKeyEvent*)', self.selected)
 
     def resetConnection(self):
         self.client = Client()
diff --git a/src/gui/pythonwidget.cc b/src/gui/pythonwidget.cc
index d441233..9c16aff 100644
--- a/src/gui/pythonwidget.cc
+++ b/src/gui/pythonwidget.cc
@@ -37,8 +37,9 @@ namespace gepetto {
             PythonQtObjectPtr sys = PythonQt::self()->importModule ("sys");
             sys.evalScript ("argv = ['gepetto-gui']");
             console_ = new PythonQtScriptingConsole(NULL, mainContext_);
-            PythonQt::self()->registerQObjectClassNames(QStringList() << "BodyTreeWidget" << "BodyTreeItem");
+            PythonQt::self()->registerQObjectClassNames(QStringList() << "BodyTreeWidget" << "BodyTreeItem" << "WindowsManager");
             mainContext_.addObject("mainWindow", MainWindow::instance());
+            mainContext_.addObject("windowsManager", MainWindow::instance()->osg().get());
             console_->QTextEdit::clear();
             console_->consoleMessage(
                     "PythonQt command prompt\n"
@@ -91,6 +92,7 @@ namespace gepetto {
           }
           module.evalScript("from PythonQt import QtGui");
           module.addObject("mainWindow", MainWindow::instance());
+          module.addObject("windowsManager", MainWindow::instance()->osg().get());
           module.addObject("_menuPlugin", MainWindow::instance()->pluginMenu());
           QString var = "pluginInstance";
           module.evalScript (var + " = Plugin(mainWindow)");
diff --git a/src/gui/windows-manager.cc b/src/gui/windows-manager.cc
index 1b5c31c..a70734a 100644
--- a/src/gui/windows-manager.cc
+++ b/src/gui/windows-manager.cc
@@ -15,6 +15,11 @@ namespace gepetto {
       return WindowsManagerPtr_t (new WindowsManager(bodyTree));
     }
 
+    int WindowsManager::createWindow(QString windowName)
+    {
+      return createWindow(windowName.toStdString());
+    }
+
     WindowsManager::WindowID WindowsManager::createWindow(const std::string& windowName)
     {
       return MainWindow::instance()->createView(windowName)->windowID();
-- 
GitLab