From fd94519f7695b9c29cf35903e59b30d767f5d48c Mon Sep 17 00:00:00 2001 From: Joseph Mirabel <jmirabel@laas.fr> Date: Mon, 5 Mar 2018 14:04:31 +0100 Subject: [PATCH] Add MainWindow class decorator to PythonQt. --- src/gepetto/corbaserver/client.py | 5 +++-- src/gui/CMakeLists.txt | 8 ++++++-- src/gui/python-decorator.hh | 21 +++++++++++++++++++++ src/gui/pythonwidget.cc | 11 +++++++---- 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/gui/python-decorator.hh diff --git a/src/gepetto/corbaserver/client.py b/src/gepetto/corbaserver/client.py index 1e1c496..2df5a6e 100644 --- a/src/gepetto/corbaserver/client.py +++ b/src/gepetto/corbaserver/client.py @@ -77,9 +77,10 @@ class Client: # In the python interpreter of gepetto-gui, gui.createWindow # crashes for an obscure reason. This hack makes it work. try: - self.gui.createWindow = lambda x: mainWindow.createView(x).wid() + from PythonQt.gepetto import MainWindow # At this point, we are in the python interpreter of gepetto-gui - except: + self.gui.createWindow = lambda x: MainWindow.instance().createView(x).wid() + except ImportError: # At this point, we are NOT in the python interpreter of gepetto-gui pass diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 588b8dc..8570ad8 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -55,15 +55,19 @@ SET (${PROJECT_NAME}_HEADERS ${${PROJECT_NAME}_HEADERS_MOC} ${${PROJECT_NAME}_HEADERS_NO_MOC} ) +SET (${PROJECT_NAME}_MOC + ${${PROJECT_NAME}_HEADERS_MOC} + python-decorator.hh + ) # Compile meta-objects and executable IF(USE_QT4) - QT4_WRAP_CPP(${PROJECT_NAME}_HEADERS_MOCED ${${PROJECT_NAME}_HEADERS_MOC}) + QT4_WRAP_CPP(${PROJECT_NAME}_HEADERS_MOCED ${${PROJECT_NAME}_MOC}) QT4_WRAP_UI(${PROJECT_NAME}_FORMS_HEADERS ${${PROJECT_NAME}_FORMS}) QT4_ADD_RESOURCES(${PROJECT_NAME}_RESOURCES_RCC ${${PROJECT_NAME}_RESOURCES}) INCLUDE(${QT_USE_FILE}) ELSE(USE_QT4) - QT5_WRAP_CPP(${PROJECT_NAME}_HEADERS_MOCED ${${PROJECT_NAME}_HEADERS_MOC}) + QT5_WRAP_CPP(${PROJECT_NAME}_HEADERS_MOCED ${${PROJECT_NAME}_MOC}) QT5_WRAP_UI(${PROJECT_NAME}_FORMS_HEADERS ${${PROJECT_NAME}_FORMS}) QT5_ADD_RESOURCES(${PROJECT_NAME}_RESOURCES_RCC ${${PROJECT_NAME}_RESOURCES}) ENDIF(USE_QT4) diff --git a/src/gui/python-decorator.hh b/src/gui/python-decorator.hh new file mode 100644 index 0000000..6e0973c --- /dev/null +++ b/src/gui/python-decorator.hh @@ -0,0 +1,21 @@ +#ifndef GEPETTO_GUI_PYTHON_DECORATOR_HH +#define GEPETTO_GUI_PYTHON_DECORATOR_HH + +#include <PythonQt/PythonQt.h> + +#include <gepetto/gui/mainwindow.hh> +#include <gepetto/gui/osgwidget.hh> + +namespace gepetto { + namespace gui { + class PythonDecorator : public QObject + { + Q_OBJECT + + public slots: + MainWindow* static_MainWindow_instance () const { return MainWindow::instance (); } + }; + } // namespace gui +} // namespace gepetto + +#endif // GEPETTO_GUI_PYTHON_DECORATOR_HH diff --git a/src/gui/pythonwidget.cc b/src/gui/pythonwidget.cc index d57cbb0..8cc57ae 100644 --- a/src/gui/pythonwidget.cc +++ b/src/gui/pythonwidget.cc @@ -12,6 +12,8 @@ #include "gepetto/gui/mainwindow.hh" #include "gepetto/gui/plugin-interface.hh" +#include "../../src/gui/python-decorator.hh" + namespace gepetto { namespace gui { namespace { @@ -45,11 +47,16 @@ namespace gepetto { PythonQtObjectPtr sys = PythonQt::self()->importModule ("sys"); sys.evalScript ("argv = ['gepetto-gui']"); console_ = new PythonQtScriptingConsole(NULL, mainContext); + PythonQt::self()->registerQObjectClassNames(QStringList() << "BodyTreeWidget" << "BodyTreeItem" << "SelectionEvent" << "WindowsManager"); + PythonQt::self()->addDecorators (new PythonDecorator()); + PythonQt::self()->registerCPPClass ("MainWindow", "QMainWindow", "gepetto"); + PythonQt::self()->registerCPPClass ("OSGWidget" , "QWidget" , "gepetto"); + mainContext.addObject("mainWindow", MainWindow::instance()); mainContext.addObject("windowsManager", MainWindow::instance()->osg().get()); console_->QTextEdit::clear(); @@ -72,10 +79,6 @@ namespace gepetto { toggleViewAction()->setShortcut(gepetto::gui::DockKeyShortcutBase + Qt::Key_A); connect(button_, SIGNAL(clicked()), SLOT(browseFile())); - - // Hack to make "createWindow" function work properly - PythonQtObjectPtr client = PythonQt::self()->importModule ("gepetto.corbaserver.client"); - client.addObject ("mainWindow", MainWindow::instance()); } PythonWidget::~PythonWidget() -- GitLab