Port old boost code used in QtUiTools.
fixes bug #376. Reviewer: Luciano Wolf <luciano.wolf@openbossa.org> Marcelo Lira <marcelo.lira@openbossa.org>
This commit is contained in:
parent
3525b77d69
commit
bf4b2c7660
2 changed files with 91 additions and 12 deletions
68
PySide/QtUiTools/glue/uitools_loadui.h
Normal file
68
PySide/QtUiTools/glue/uitools_loadui.h
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* Based on code provided by:
|
||||||
|
* Antonio Valentino <antonio.valentino at tiscali.it>
|
||||||
|
* Frédéric <frederic.mantegazza at gbiloba.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <shiboken.h>
|
||||||
|
|
||||||
|
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<QObject*>::toPython(child));
|
||||||
|
if (!has_attr)
|
||||||
|
PyObject_SetAttrString(pyParent, qPrintable(name), pyChild);
|
||||||
|
|
||||||
|
Shiboken::setParent(pyParent, pyChild);
|
||||||
|
_populate_parent(pyChild, qobject_cast<QWidget*>(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<QWidget*>::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<QWidget*>::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;
|
||||||
|
}
|
||||||
|
|
@ -48,13 +48,9 @@
|
||||||
</modify-function>
|
</modify-function>
|
||||||
|
|
||||||
<modify-function signature="load(QIODevice*, QWidget*)">
|
<modify-function signature="load(QIODevice*, QWidget*)">
|
||||||
<modify-argument index="return">
|
<extra-includes>
|
||||||
<parent index="2" action="add"/>
|
<include file-name="glue/uitools_loadui.h" location="local"/>
|
||||||
</modify-argument>
|
</extra-includes>
|
||||||
</modify-function>
|
|
||||||
|
|
||||||
<!-- Syntax sugar -->
|
|
||||||
<add-function signature="load(const char*, QWidget*)" return-type="QWidget*">
|
|
||||||
<modify-argument index="2">
|
<modify-argument index="2">
|
||||||
<replace-default-expression with="0" />
|
<replace-default-expression with="0" />
|
||||||
<rename to="parentWidget" />
|
<rename to="parentWidget" />
|
||||||
|
|
@ -63,11 +59,26 @@
|
||||||
<parent index="2" action="add"/>
|
<parent index="2" action="add"/>
|
||||||
</modify-argument>
|
</modify-argument>
|
||||||
<inject-code>
|
<inject-code>
|
||||||
QFile f(%1);
|
//Avoid calling the original function: %CPPSELF.load
|
||||||
if (f.open(QIODevice::ReadOnly | QIODevice::Text))
|
%PYARG_0 = quiloader_load_ui_from_device(%CPPSELF, %1, %2);
|
||||||
%PYARG_0 = %CONVERTTOPYTHON[QWidget*](%CPPSELF.load(&f, %2));
|
</inject-code>
|
||||||
else
|
</modify-function>
|
||||||
PyErr_SetString(PyExc_RuntimeError, "Unable to open ui file");
|
|
||||||
|
<!-- Syntax sugar -->
|
||||||
|
<add-function signature="load(const char*, QWidget*)" return-type="QWidget*">
|
||||||
|
<extra-includes>
|
||||||
|
<include file-name="glue/uitools_loadui.h" location="local"/>
|
||||||
|
</extra-includes>
|
||||||
|
<modify-argument index="2">
|
||||||
|
<replace-default-expression with="0" />
|
||||||
|
<rename to="parentWidget" />
|
||||||
|
</modify-argument>
|
||||||
|
<modify-argument index="return">
|
||||||
|
<parent index="2" action="add"/>
|
||||||
|
</modify-argument>
|
||||||
|
<inject-code>
|
||||||
|
//Avoid calling the original function: %CPPSELF.load
|
||||||
|
%PYARG_0 = quiloader_load_ui(%CPPSELF, %1, %2);
|
||||||
</inject-code>
|
</inject-code>
|
||||||
</add-function>
|
</add-function>
|
||||||
</object-type>
|
</object-type>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue