From bf4b2c7660adc99b6a8b83748787f0ea078ad13b Mon Sep 17 00:00:00 2001 From: renatofilho Date: Wed, 22 Sep 2010 16:59:46 -0300 Subject: Port old boost code used in QtUiTools. fixes bug #376. Reviewer: Luciano Wolf Marcelo Lira --- PySide/QtUiTools/glue/uitools_loadui.h | 68 +++++++++++++++++++++++++++++++++ PySide/QtUiTools/typesystem_uitools.xml | 23 ++++++++--- 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 PySide/QtUiTools/glue/uitools_loadui.h (limited to 'PySide/QtUiTools') diff --git a/PySide/QtUiTools/glue/uitools_loadui.h b/PySide/QtUiTools/glue/uitools_loadui.h new file mode 100644 index 000000000..da851c14c --- /dev/null +++ b/PySide/QtUiTools/glue/uitools_loadui.h @@ -0,0 +1,68 @@ +/* + * Based on code provided by: + * Antonio Valentino + * Frédéric + */ + +#include + +static void +_populate_parent(PyObject* pyParent, QWidget *parent) +{ + if (parent->children().isEmpty()) + return; + + foreach(QObject *child, parent->children()) { + QString name(child->objectName()); + if (!name.isEmpty() && !name.startsWith("_") && !name.startsWith("qt_")) { + bool has_attr = PyObject_HasAttrString(pyParent, qPrintable(name)); + Shiboken::AutoDecRef pyChild(Shiboken::Converter::toPython(child)); + if (!has_attr) + PyObject_SetAttrString(pyParent, qPrintable(name), pyChild); + + Shiboken::setParent(pyParent, pyChild); + _populate_parent(pyChild, qobject_cast(child)); + } + } +} + +static PyObject* +quiloader_load_ui_from_device(QUiLoader* self, QIODevice* dev, QWidget *parent) +{ + QWidget *w = self->load(dev, parent); + if (w) { + if (parent && parent->layout()) + parent->layout()->deleteLater(); + + PyObject* pyParent = Shiboken::Converter::toPython(w); + _populate_parent(pyParent, w); + + return pyParent; + } + + PyErr_SetString(PyExc_RuntimeError, "Unable to open ui file"); + return 0; +} + +static PyObject* +quiloader_load_ui(QUiLoader* self, const QString &ui_file, QWidget *parent) +{ + QFile fd(ui_file); + + if (fd.exists(ui_file) && fd.open(QFile::ReadOnly)) { + QWidget* w = self->load(&fd, parent); + fd.close(); + if (w != 0) { + PyObject* pyParent = Shiboken::Converter::toPython(w); + + if (parent && parent->layout()) + parent->layout()->deleteLater(); + + _populate_parent(pyParent, w); + + return pyParent; + } + } + PyErr_SetString(PyExc_RuntimeError, "Unable to open ui file"); + return 0; +} diff --git a/PySide/QtUiTools/typesystem_uitools.xml b/PySide/QtUiTools/typesystem_uitools.xml index 7ea988b3d..a188aa34b 100644 --- a/PySide/QtUiTools/typesystem_uitools.xml +++ b/PySide/QtUiTools/typesystem_uitools.xml @@ -48,13 +48,27 @@ + + + + + + + - + + //Avoid calling the original function: %CPPSELF.load + %PYARG_0 = quiloader_load_ui_from_device(%CPPSELF, %1, %2); + + + + + @@ -63,11 +77,8 @@ - QFile f(%1); - if (f.open(QIODevice::ReadOnly | QIODevice::Text)) - %PYARG_0 = %CONVERTTOPYTHON[QWidget*](%CPPSELF.load(&f, %2)); - else - PyErr_SetString(PyExc_RuntimeError, "Unable to open ui file"); + //Avoid calling the original function: %CPPSELF.load + %PYARG_0 = quiloader_load_ui(%CPPSELF, %1, %2); -- cgit v1.2.3