aboutsummaryrefslogtreecommitdiffstats
path: root/PySide/QtUiTools/glue/uitools_loadui.h
blob: da851c14cf48abee01e872ee66fc70d88025ee14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;   
}