From a0c3e845ac01ceb5bdc311f95fe7a1d0271c3c25 Mon Sep 17 00:00:00 2001 From: lorenz Date: Fri, 28 Apr 2017 21:27:42 +0200 Subject: [PATCH] Soqt port (#17) https://github.com/looooo/pivy/issues/16 --- Inventor/Qt/SoQt.i | 2 +- Inventor/Qt/SoQtRenderArea.i | 29 ++-- examples/Mentor/10.2.setEventCB.py | 8 +- examples/QtDesigner/python/main.py | 8 +- examples/QtDesigner/python/mainwindow.py | 10 +- examples/QtDesigner/python/ui_test.py | 4 +- examples/Quarter/mdi.py | 4 +- examples/examiner_embed4.py | 4 +- examples/soqt/examinar_viewer.py | 24 +++ examples/soqt/minimal.py | 19 +++ interfaces/soqt.i | 189 ++++++++++------------- interfaces/soqt2.i | 181 ++++++++++------------ 12 files changed, 238 insertions(+), 244 deletions(-) create mode 100644 examples/soqt/examinar_viewer.py create mode 100644 examples/soqt/minimal.py diff --git a/Inventor/Qt/SoQt.i b/Inventor/Qt/SoQt.i index b27f31de..7f9186d6 100644 --- a/Inventor/Qt/SoQt.i +++ b/Inventor/Qt/SoQt.i @@ -11,7 +11,7 @@ #ifdef PY_2 if (!strcmp(PyString_AsString(result), "")) #else - if (!strcmp(PyBytes_AsString(result), "")) + if (!strcmp(PyUnicode_AsUTF8(result), "")) #endif { cc_thread *py_thread = cc_thread_construct(Pivy_PythonInteractiveLoop, NULL); diff --git a/Inventor/Qt/SoQtRenderArea.i b/Inventor/Qt/SoQtRenderArea.i index 820b0cb6..b0dd676b 100644 --- a/Inventor/Qt/SoQtRenderArea.i +++ b/Inventor/Qt/SoQtRenderArea.i @@ -3,36 +3,33 @@ static SbBool SoQtRenderAreaEventPythonCB(void * closure, QEvent * event) { PyObject *func, *arglist; - PyObject *result, *sip, *qt, *qev = NULL; + PyObject *result, *shiboken, *qt, *qev = NULL; int ret = 0; - /* try to create a QEvent PyQt instance over sip */ + /* try to create a QEvent PySide instance over shiboken */ - initialize_pyqt_module_import_name(); - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } + /* check if the shiboken module is available and import it */ + shiboken = getShiboken(); - if (sip && PyModule_Check(sip)) { + if (shiboken && PyModule_Check(shiboken)) { /* check if the qt module is available and import it */ - if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYQT_MODULE_IMPORT_NAME))) { - qt = PyImport_ImportModule(PYQT_MODULE_IMPORT_NAME); + if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYSIDE_QTCORE))) { + qt = PyImport_ImportModule(PYSIDE_QTCORE); } if (qt && PyModule_Check(qt)) { /* grab the wrapinstance(addr, type) function */ - PyObject *sip_wrapinst_func; - sip_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "wrapinstance"); + PyObject *shiboken_wrapinst_func; + shiboken_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "wrapInstance"); - if (PyCallable_Check(sip_wrapinst_func)) { + if (PyCallable_Check(shiboken_wrapinst_func)) { PyObject *qevent_type; qevent_type = PyDict_GetItemString(PyModule_GetDict(qt), "QEvent"); arglist = Py_BuildValue("(lO)", event, qevent_type); - if (!(qev = PyEval_CallObject(sip_wrapinst_func, arglist))) { + if (!(qev = PyEval_CallObject(shiboken_wrapinst_func, arglist))) { PyErr_Print(); } @@ -41,7 +38,7 @@ SoQtRenderAreaEventPythonCB(void * closure, QEvent * event) } } - /* if no QEvent could be created through sip return a swig QEvent type */ + /* if no QEvent could be created through shiboken return a swig QEvent type */ if (!qev) { qev = SWIG_NewPointerObj((void *)event, SWIGTYPE_p_QEvent, 0); } @@ -57,7 +54,7 @@ SoQtRenderAreaEventPythonCB(void * closure, QEvent * event) ret = PyInt_AsLong(result); } - if (sip) {Py_DECREF(sip); } + if (shiboken) {Py_DECREF(shiboken); } Py_DECREF(arglist); Py_DECREF(qev); Py_XDECREF(result); diff --git a/examples/Mentor/10.2.setEventCB.py b/examples/Mentor/10.2.setEventCB.py index 357b508e..10a9ba19 100755 --- a/examples/Mentor/10.2.setEventCB.py +++ b/examples/Mentor/10.2.setEventCB.py @@ -37,11 +37,9 @@ from pivy.coin import * from pivy.gui.soqt import * -# PyQt module has to be imported as last one if used in the same namespace -if SoQt.getVersionToolkitString().startswith('4'): - from PyQt4.Qt import * -else: - from qt import * +# PySide module has to be imported as last one if used in the same namespace +from PySide.QtCore import * +from PySide.QtGui import * # Timer sensor # Rotate 90 degrees every second, update 30 times a second diff --git a/examples/QtDesigner/python/main.py b/examples/QtDesigner/python/main.py index 011f2f3f..c900ff96 100644 --- a/examples/QtDesigner/python/main.py +++ b/examples/QtDesigner/python/main.py @@ -2,10 +2,10 @@ import sys from pivy.gui.soqt import SoQt -from PyQt4.QtGui import QApplication -from PyQt4.QtCore import QObject -from PyQt4.QtCore import SIGNAL -from PyQt4.QtCore import SLOT +from PySide.QtGui import QApplication +from PySide.QtCore import QObject +from PySide.QtCore import SIGNAL +from PySide.QtCore import SLOT from mainwindow import MainWindow if __name__ == "__main__": diff --git a/examples/QtDesigner/python/mainwindow.py b/examples/QtDesigner/python/mainwindow.py index b08620e2..90a0c7bb 100644 --- a/examples/QtDesigner/python/mainwindow.py +++ b/examples/QtDesigner/python/mainwindow.py @@ -4,11 +4,11 @@ from pivy.coin import * from pivy.gui.soqt import SoQtExaminerViewer from ui_test import Ui_MainWindow -from PyQt4.QtGui import QWidget -from PyQt4.QtGui import QMainWindow -from PyQt4.QtGui import QButtonGroup -from PyQt4.QtCore import QObject -from PyQt4.QtCore import SIGNAL +from PySide.QtGui import QWidget +from PySide.QtGui import QMainWindow +from PySide.QtGui import QButtonGroup +from PySide.QtCore import QObject +from PySide.QtCore import SIGNAL class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent = None): diff --git a/examples/QtDesigner/python/ui_test.py b/examples/QtDesigner/python/ui_test.py index a856fa7c..17e969e4 100644 --- a/examples/QtDesigner/python/ui_test.py +++ b/examples/QtDesigner/python/ui_test.py @@ -3,11 +3,11 @@ # Form implementation generated from reading ui file '../test.ui' # # Created: Tue Feb 12 18:37:15 2008 -# by: PyQt4 UI code generator 4.3.3 +# by: PySide UI code generator 4.3.3 # # WARNING! All changes made in this file will be lost! -from PyQt4 import QtCore, QtGui +from PySide import QtCore, QtGui class Ui_MainWindow(object): def setupUi(self, MainWindow): diff --git a/examples/Quarter/mdi.py b/examples/Quarter/mdi.py index d9e4c822..e174b118 100755 --- a/examples/Quarter/mdi.py +++ b/examples/Quarter/mdi.py @@ -19,8 +19,8 @@ import os import sys -from PyQt4 import QtCore, QtGui -from PyQt4.QtGui import QMainWindow, QWorkspace, QAction, QFileDialog, QApplication +from PySide import QtCore, QtGui +from PySide.QtGui import QMainWindow, QWorkspace, QAction, QFileDialog, QApplication from pivy.coin import SoInput, SoDB from pivy.quarter import QuarterWidget diff --git a/examples/examiner_embed4.py b/examples/examiner_embed4.py index d16c888b..8a4bcad2 100755 --- a/examples/examiner_embed4.py +++ b/examples/examiner_embed4.py @@ -26,8 +26,8 @@ from pivy.coin import * from pivy.gui.soqt import * -from PyQt4.QtGui import * -from PyQt4.QtCore import * +from PySide.QtGui import * +from PySide.QtCore import * class EmbeddedWindow(QMainWindow): def __init__(self, *args): diff --git a/examples/soqt/examinar_viewer.py b/examples/soqt/examinar_viewer.py new file mode 100644 index 00000000..ef2016d5 --- /dev/null +++ b/examples/soqt/examinar_viewer.py @@ -0,0 +1,24 @@ +import sys +from pivy import coin +from pivy.gui import soqt +# import shiboken if you want to use the widget within qt + +# Initialize Coin (returns main window to use) +# If unsuccessful, exit. +myWindow = soqt.SoQt.init(sys.argv[0]) +print(myWindow) +# Make a scene containing a red cone. +myMaterial = coin.SoMaterial() +myMaterial.diffuseColor = (1.0, 0.0, 0.0) +scene = coin.SoSeparator() +scene.ref() +scene.addChild(myMaterial) +scene.addChild(coin.SoCone()) +# Create a viewer to visualize our scene. +viewer = soqt.SoQtExaminerViewer(myWindow) +# Put our scene into viewer, change the title. +viewer.setSceneGraph(scene) +viewer.setTitle("Examiner Viewer") +viewer.show() +soqt.SoQt.show(myWindow) +soqt.SoQt.mainLoop() \ No newline at end of file diff --git a/examples/soqt/minimal.py b/examples/soqt/minimal.py new file mode 100644 index 00000000..038b811b --- /dev/null +++ b/examples/soqt/minimal.py @@ -0,0 +1,19 @@ +import sys +from pivy import coin +from pivy.gui import soqt +# import shiboken if you want to use the widget within qt + +myWindow = soqt.SoQt.init(sys.argv[0]) +print(myWindow) +scene = coin.SoSeparator() +cam = coin.SoPerspectiveCamera() +cam.position = (0, 0, 4) +light = coin.SoLightModel() +light.model = coin.SoLightModel.BASE_COLOR +scene += light, cam, coin.SoCone() +viewer = soqt.SoQtRenderArea(myWindow) +viewer.setSceneGraph(scene) +viewer.setTitle("Examiner Viewer") +viewer.show() +soqt.SoQt.show(myWindow) +soqt.SoQt.mainLoop() \ No newline at end of file diff --git a/interfaces/soqt.i b/interfaces/soqt.i index 520d31cc..2f978dff 100644 --- a/interfaces/soqt.i +++ b/interfaces/soqt.i @@ -16,8 +16,8 @@ %define SOQT_MODULE_DOCSTRING "The soqt module is a wrapper for the SoQt library. The module will try -to import the sip module which is used for PyQt. If found the involved -wrapped Qt structures are converted to ones suitable for PyQt, +to import the shiboken module which is used for PySide. If found the involved +wrapped Qt structures are converted to ones suitable for PySide, otherwise it will fall back to regular SWIG structures." %enddef @@ -98,22 +98,18 @@ Pivy_PythonInteractiveLoop(void *data) { return NULL; } -static char * PYQT_MODULE_IMPORT_NAME = NULL; +static const char * PYSIDE_QTGUI = "PySide.QtGui"; +static const char * PYSIDE_QTCORE = "PySide.QtCore"; -static void -initialize_pyqt_module_import_name() +static PyObject* getShiboken() { - /* determine the Qt version SoQt has been compiled with for correct - * PyQt module import */ - if (!PYQT_MODULE_IMPORT_NAME) { - PYQT_MODULE_IMPORT_NAME = "qt"; - - if (strlen(SoQt::getVersionToolkitString()) >= 1 && - SoQt::getVersionToolkitString()[0] == '4') { - PYQT_MODULE_IMPORT_NAME = "PyQt4.Qt"; - } - } + // simplified version + // to get a qt representation in python + // simple import shiboken from python. + // from Shiboken import shiboken + return PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "shiboken"); } + %} /* include the typemaps common to all pivy modules */ @@ -122,39 +118,34 @@ initialize_pyqt_module_import_name() /* import the pivy main interface file */ %import coin.i -/* typemaps to bridge against PyQt */ +/* typemaps to bridge against PySide */ %typemap(out) QEvent * { $result = NULL; { - PyObject *sip, *qt; + PyObject *qt; - /* try to create a PyQt QEvent instance through sip */ + /* try to create a PySide QEvent instance through shiboken */ - initialize_pyqt_module_import_name(); - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - - if (sip && PyModule_Check(sip)) { + PyObject *shiboken = getShiboken(); + + if (shiboken && PyModule_Check(shiboken)) { /* check if the qt module is available and import it */ - if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYQT_MODULE_IMPORT_NAME))) { - qt = PyImport_ImportModule(PYQT_MODULE_IMPORT_NAME); + if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYSIDE_QTCORE))) { + qt = PyImport_ImportModule(PYSIDE_QTCORE); } if (qt && PyModule_Check(qt)) { - /* grab the wrapinstance(addr, type) function */ - PyObject *sip_wrapinst_func; - sip_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "wrapinstance"); + /* grab the wrapInstance(addr, type) function */ + PyObject *shiboken_wrapinst_func; + shiboken_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "wrapInstance"); - if (PyCallable_Check(sip_wrapinst_func)) { + if (PyCallable_Check(shiboken_wrapinst_func)) { PyObject *qevent_type, *arglist; qevent_type = PyDict_GetItemString(PyModule_GetDict(qt), "QEvent"); arglist = Py_BuildValue("(lO)", $1, qevent_type); - if (!($result = PyEval_CallObject(sip_wrapinst_func, arglist))) { + if (!($result = PyEval_CallObject(shiboken_wrapinst_func, arglist))) { PyErr_Print(); } @@ -163,7 +154,7 @@ initialize_pyqt_module_import_name() } } - /* if no QEvent could be created through sip return a swig QEvent type */ + /* if no QEvent could be created through shiboken return a swig QEvent type */ if (PyErr_ExceptionMatches(PyExc_ImportError) || !$result) { PyErr_Clear(); $result = SWIG_NewPointerObj((void *)($1), SWIGTYPE_p_QEvent, 0); @@ -174,35 +165,30 @@ initialize_pyqt_module_import_name() %typemap(out) QWidget * { $result = NULL; { - PyObject *sip, *qt; + PyObject *qt; - /* try to create a PyQt QWidget instance through sip */ + /* try to create a PySide QWidget instance through shiboken */ - initialize_pyqt_module_import_name(); - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - - if (sip && PyModule_Check(sip)) { + PyObject *shiboken = getShiboken(); + + if (shiboken && PyModule_Check(shiboken)) { /* check if the qt module is available and import it */ - if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYQT_MODULE_IMPORT_NAME))) { - qt = PyImport_ImportModule(PYQT_MODULE_IMPORT_NAME); + if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYSIDE_QTCORE))) { + qt = PyImport_ImportModule(PYSIDE_QTCORE); } if (qt && PyModule_Check(qt)) { - /* grab the wrapinstance(addr, type) function */ - PyObject *sip_wrapinst_func; - sip_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "wrapinstance"); + /* grab the wrapInstance(addr, type) function */ + PyObject *shiboken_wrapinst_func; + shiboken_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "wrapInstance"); - if (PyCallable_Check(sip_wrapinst_func)) { + if (PyCallable_Check(shiboken_wrapinst_func)) { PyObject *qwidget_type, *arglist; qwidget_type = PyDict_GetItemString(PyModule_GetDict(qt), "QWidget"); arglist = Py_BuildValue("(lO)", $1, qwidget_type); - if (!($result = PyEval_CallObject(sip_wrapinst_func, arglist))) { + if (!($result = PyEval_CallObject(shiboken_wrapinst_func, arglist))) { PyErr_Print(); } @@ -211,7 +197,7 @@ initialize_pyqt_module_import_name() } } - /* if no QWidget could be created through sip return a swig QWidget type */ + /* if no QWidget could be created through shiboken return a swig QWidget type */ if (PyErr_ExceptionMatches(PyExc_ImportError) || !$result) { PyErr_Clear(); $result = SWIG_NewPointerObj((void *)($1), SWIGTYPE_p_QWidget, 0); @@ -221,25 +207,22 @@ initialize_pyqt_module_import_name() %typemap(in) QEvent * { { - PyObject *sip; - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + PyObject *shiboken = getShiboken(); + + if (shiboken && PyModule_Check(shiboken)) { + /* grab the unwrapInstance(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "unwrapInstance"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - $1 = (QEvent*)PyLong_AsLong(address); + $1 = (QEvent*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + $1 = (QEvent*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); } Py_DECREF(arglist); @@ -258,25 +241,23 @@ initialize_pyqt_module_import_name() if ($input == Py_None) { $1 = NULL; } else { - PyObject *sip; - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } + + PyObject *shiboken = getShiboken(); - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + if (shiboken && PyModule_Check(shiboken)) { + /* grab the unwrapInstance(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "unwrapInstance"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - $1 = (QWidget*)PyLong_AsLong(address); + $1 = (QWidget*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + $1 = (QWidget*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); } Py_DECREF(arglist); @@ -298,25 +279,22 @@ class QWidget { QWidget(QWidget* parent=0, const char* name=0, WFlags f=0); }; %typemap(typecheck) QEvent * { void *ptr = NULL; { - PyObject *sip; - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + PyObject *shiboken = getShiboken(); + + if (shiboken && PyModule_Check(shiboken)) { + /* grab the unwrapInstance(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "unwrapInstance"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - ptr = (QEvent*)PyLong_AsLong(address); + ptr = (QEvent*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + ptr = (QEvent*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); } Py_DECREF(arglist); @@ -337,25 +315,22 @@ class QWidget { QWidget(QWidget* parent=0, const char* name=0, WFlags f=0); }; %typemap(typecheck) QWidget * { void *ptr = NULL; { - PyObject *sip; - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + PyObject *shiboken = getShiboken(); + + if (shiboken && PyModule_Check(shiboken)) { + /* grab the unwrapInstance(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "unwrapInstance"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - ptr = (QWidget*)PyLong_AsLong(address); + ptr = (QWidget*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + ptr = (QWidget*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); } Py_DECREF(arglist); diff --git a/interfaces/soqt2.i b/interfaces/soqt2.i index 050664ef..3254db73 100644 --- a/interfaces/soqt2.i +++ b/interfaces/soqt2.i @@ -31,8 +31,8 @@ %define SOQT_MODULE_DOCSTRING "The soqt module is a wrapper for the SoQt library. The module will try -to import the sip module which is used for PyQt. If found the involved -wrapped Qt structures are converted to ones suitable for PyQt, +to import the shiboken module which is used for PySide. If found the involved +wrapped Qt structures are converted to ones suitable for PySide, otherwise it will fall back to regular SWIG structures." %enddef @@ -80,22 +80,19 @@ Pivy_PythonInteractiveLoop(void *data) { return NULL; } -static char * PYQT_MODULE_IMPORT_NAME = NULL; +static const char * PYSIDE_QTGUI = "PySide.QtGui"; +static const char * PYSIDE_QTCORE = "PySide.QtCore"; -static void -initialize_pyqt_module_import_name() + +static PyObject* getShiboken() { - /* determine the Qt version SoQt has been compiled with for correct - * PyQt module import */ - if (!PYQT_MODULE_IMPORT_NAME) { - PYQT_MODULE_IMPORT_NAME = "qt"; - - if (strlen(SoQt::getVersionToolkitString()) >= 1 && - SoQt::getVersionToolkitString()[0] == '4') { - PYQT_MODULE_IMPORT_NAME = "PyQt4.Qt"; - } - } + // simplified version + // to get a qt representation in python + // simple import shiboken from python. + // from Shiboken import shiboken + return PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "shiboken"); } + %} /* include the typemaps common to all pivy modules */ @@ -104,39 +101,33 @@ initialize_pyqt_module_import_name() /* import the pivy main interface file */ %import coin.i -/* typemaps to bridge against PyQt */ +/* typemaps to bridge against PySide */ %typemap(out) QEvent * { $result = NULL; { - PyObject *sip, *qt; - - /* try to create a PyQt QEvent instance through sip */ + PyObject *qt; + PyObject *shiboken = getShiboken(); + /* try to create a PySide QEvent instance through shiboken */ - initialize_pyqt_module_import_name(); - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - if (sip && PyModule_Check(sip)) { + if (shiboken && PyModule_Check(shiboken)) { /* check if the qt module is available and import it */ - if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYQT_MODULE_IMPORT_NAME))) { - qt = PyImport_ImportModule(PYQT_MODULE_IMPORT_NAME); + if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYSIDE_QTCORE))) { + qt = PyImport_ImportModule(PYSIDE_QTCORE); } if (qt && PyModule_Check(qt)) { - /* grab the wrapinstance(addr, type) function */ - PyObject *sip_wrapinst_func; - sip_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "wrapinstance"); + /* grab the wrapInstance(addr, type) function */ + PyObject *shiboken_wrapinst_func; + shiboken_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "wrapInstance"); - if (PyCallable_Check(sip_wrapinst_func)) { + if (PyCallable_Check(shiboken_wrapinst_func)) { PyObject *qevent_type, *arglist; qevent_type = PyDict_GetItemString(PyModule_GetDict(qt), "QEvent"); arglist = Py_BuildValue("(lO)", $1, qevent_type); - if (!($result = PyEval_CallObject(sip_wrapinst_func, arglist))) { + if (!($result = PyEval_CallObject(shiboken_wrapinst_func, arglist))) { PyErr_Print(); } @@ -145,7 +136,7 @@ initialize_pyqt_module_import_name() } } - /* if no QEvent could be created through sip return a swig QEvent type */ + /* if no QEvent could be created through shiboken return a swig QEvent type */ if (PyErr_ExceptionMatches(PyExc_ImportError) || !$result) { PyErr_Clear(); $result = SWIG_NewPointerObj((void *)($1), SWIGTYPE_p_QEvent, 0); @@ -156,35 +147,29 @@ initialize_pyqt_module_import_name() %typemap(out) QWidget * { $result = NULL; { - PyObject *sip, *qt; + PyObject *qt; + PyObject *shiboken = getShiboken(); + /* try to create a PySide QWidget instance through shiboken */ - /* try to create a PyQt QWidget instance through sip */ - - initialize_pyqt_module_import_name(); - - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - if (sip && PyModule_Check(sip)) { + if (shiboken && PyModule_Check(shiboken)) { /* check if the qt module is available and import it */ - if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYQT_MODULE_IMPORT_NAME))) { - qt = PyImport_ImportModule(PYQT_MODULE_IMPORT_NAME); + if (!(qt = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), PYSIDE_QTGUI))) { + qt = PyImport_ImportModule(PYSIDE_QTGUI); } if (qt && PyModule_Check(qt)) { - /* grab the wrapinstance(addr, type) function */ - PyObject *sip_wrapinst_func; - sip_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "wrapinstance"); + /* grab the wrapInstance(addr, type) function */ + PyObject *shiboken_wrapinst_func; + shiboken_wrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "wrapInstance"); - if (PyCallable_Check(sip_wrapinst_func)) { + if (PyCallable_Check(shiboken_wrapinst_func)) { PyObject *qwidget_type, *arglist; qwidget_type = PyDict_GetItemString(PyModule_GetDict(qt), "QWidget"); arglist = Py_BuildValue("(lO)", $1, qwidget_type); - if (!($result = PyEval_CallObject(sip_wrapinst_func, arglist))) { + if (!($result = PyEval_CallObject(shiboken_wrapinst_func, arglist))) { PyErr_Print(); } @@ -193,7 +178,7 @@ initialize_pyqt_module_import_name() } } - /* if no QWidget could be created through sip return a swig QWidget type */ + /* if no QWidget could be created through shiboken return a swig QWidget type */ if (PyErr_ExceptionMatches(PyExc_ImportError) || !$result) { PyErr_Clear(); $result = SWIG_NewPointerObj((void *)($1), SWIGTYPE_p_QWidget, 0); @@ -203,25 +188,23 @@ initialize_pyqt_module_import_name() %typemap(in) QEvent * { { - PyObject *sip; + PyObject *shiboken = getShiboken(); - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + if (shiboken && PyModule_Check(shiboken)) { + /* grab the getCppPointer(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "getCppPointer"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - $1 = (QEvent*)PyLong_AsLong(address); + $1 = (QEvent*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + $1 = (QEvent*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); } Py_DECREF(arglist); @@ -240,26 +223,28 @@ initialize_pyqt_module_import_name() if ($input == Py_None) { $1 = NULL; } else { - PyObject *sip; + PyObject *shiboken = getShiboken(); - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); + /* check if the shiboken module is available and import it */ + if (!(shiboken = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "shiboken"))) { + shiboken = PyImport_ImportModule("shiboken"); } - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + if (shiboken && PyModule_Check(shiboken)) { + /* grab the getCppPointer(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "getCppPointer"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - $1 = (QWidget*)PyLong_AsLong(address); - } + $1 = (QWidget*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + $1 = (QWidget*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); + } Py_DECREF(arglist); } @@ -280,25 +265,23 @@ class QWidget { QWidget(QWidget* parent=0, const char* name=0, WFlags f=0); }; %typemap(typecheck) QEvent * { void *ptr = NULL; { - PyObject *sip; + PyObject *shiboken = getShiboken() - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + if (shiboken && PyModule_Check(shiboken)) { + /* grab the getCppPointer(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "getCppPointer"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - ptr = (QEvent*)PyLong_AsLong(address); + ptr = (QEvent*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + ptr = (QEvent*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); } Py_DECREF(arglist); @@ -319,25 +302,23 @@ class QWidget { QWidget(QWidget* parent=0, const char* name=0, WFlags f=0); }; %typemap(typecheck) QWidget * { void *ptr = NULL; { - PyObject *sip; + PyObject *shiboken = getShiboken(); - /* check if the sip module is available and import it */ - if (!(sip = PyDict_GetItemString(PyModule_GetDict(PyImport_AddModule("__main__")), "sip"))) { - sip = PyImport_ImportModule("sip"); - } - if (sip && PyModule_Check(sip)) { - /* grab the unwrapinstance(obj) function */ - PyObject *sip_unwrapinst_func; - sip_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(sip), "unwrapinstance"); + if (shiboken && PyModule_Check(shiboken)) { + /* grab the getCppPointer(obj) function */ + PyObject *shiboken_unwrapinst_func; + shiboken_unwrapinst_func = PyDict_GetItemString(PyModule_GetDict(shiboken), "getCppPointer"); - if (PyCallable_Check(sip_unwrapinst_func)) { + if (PyCallable_Check(shiboken_unwrapinst_func)) { PyObject *arglist, *address; arglist = Py_BuildValue("(O)", $input); - if (!(address = PyEval_CallObject(sip_unwrapinst_func, arglist))) { + if (!(address = PyEval_CallObject(shiboken_unwrapinst_func, arglist))) { PyErr_Print(); } else if (PyNumber_Check(address)) { - ptr = (QWidget*)PyLong_AsLong(address); + ptr = (QWidget*)PyLong_AsVoidPtr(address); + } else if (PyTuple_Check(address)) { + ptr = (QWidget*)PyLong_AsVoidPtr(PyTuple_GetItem(address, 0)); } Py_DECREF(arglist);