~ubuntu-branches/ubuntu/natty/pyside/natty

« back to all changes in this revision

Viewing changes to libpyside/pysidemetafunction.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Raboud
  • Date: 2011-02-18 18:01:00 UTC
  • mfrom: (1.1.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110218180100-y8aqmcdbcbd6gpeh
Tags: upstream-1.0.0~rc1
ImportĀ upstreamĀ versionĀ 1.0.0~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the PySide project.
 
3
 *
 
4
 * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
 
5
 *
 
6
 * Contact: PySide team <contact@pyside.org>
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 */
 
22
#include <Python.h>
 
23
#include "pysidemetafunction.h"
 
24
 
 
25
#include <shiboken.h>
 
26
#include <QObject>
 
27
#include <QMetaMethod>
 
28
#include <QDebug>
 
29
 
 
30
extern "C"
 
31
{
 
32
 
 
33
struct PySideMetaFunctionPrivate
 
34
{
 
35
    QObject* qobject;
 
36
    QMetaMethod method;
 
37
};
 
38
 
 
39
//methods
 
40
static void         functionFree(void*);
 
41
static PyObject*    functionCall(PyObject*, PyObject*, PyObject*);
 
42
 
 
43
PyTypeObject PySideMetaFunctionType = {
 
44
    PyObject_HEAD_INIT(0)
 
45
    /*ob_size*/             0,
 
46
    /*tp_name*/             "PySide.MetaFunction",
 
47
    /*tp_basicsize*/        sizeof(PySideMetaFunction),
 
48
    /*tp_itemsize*/         0,
 
49
    /*tp_dealloc*/          0,
 
50
    /*tp_print*/            0,
 
51
    /*tp_getattr*/          0,
 
52
    /*tp_setattr*/          0,
 
53
    /*tp_compare*/          0,
 
54
    /*tp_repr*/             0,
 
55
    /*tp_as_number*/        0,
 
56
    /*tp_as_sequence*/      0,
 
57
    /*tp_as_mapping*/       0,
 
58
    /*tp_hash*/             0,
 
59
    /*tp_call*/             functionCall,
 
60
    /*tp_str*/              0,
 
61
    /*tp_getattro*/         0,
 
62
    /*tp_setattro*/         0,
 
63
    /*tp_as_buffer*/        0,
 
64
    /*tp_flags*/            Py_TPFLAGS_DEFAULT,
 
65
    /*tp_doc*/              "MetaFunction",
 
66
    /*tp_traverse*/         0,
 
67
    /*tp_clear*/            0,
 
68
    /*tp_richcompare*/      0,
 
69
    /*tp_weaklistoffset*/   0,
 
70
    /*tp_iter*/             0,
 
71
    /*tp_iternext*/         0,
 
72
    /*tp_methods*/          0,
 
73
    /*tp_members*/          0,
 
74
    /*tp_getset*/           0,
 
75
    /*tp_base*/             0,
 
76
    /*tp_dict*/             0,
 
77
    /*tp_descr_get*/        0,
 
78
    /*tp_descr_set*/        0,
 
79
    /*tp_dictoffset*/       0,
 
80
    /*tp_init*/             0,
 
81
    /*tp_alloc*/            0,
 
82
    /*tp_new*/              PyType_GenericNew,
 
83
    /*tp_free*/             functionFree,
 
84
    /*tp_is_gc*/            0,
 
85
    /*tp_bases*/            0,
 
86
    /*tp_mro*/              0,
 
87
    /*tp_cache*/            0,
 
88
    /*tp_subclasses*/       0,
 
89
    /*tp_weaklist*/         0,
 
90
    /*tp_del*/              0,
 
91
};
 
92
 
 
93
void functionFree(void *self)
 
94
{
 
95
    PySideMetaFunction* function = reinterpret_cast<PySideMetaFunction*>(self);
 
96
    delete function->d;
 
97
}
 
98
 
 
99
PyObject* functionCall(PyObject* self, PyObject* args, PyObject* kw)
 
100
{
 
101
    static Shiboken::TypeResolver* typeResolver = Shiboken::TypeResolver::get("QVariant");
 
102
    Q_ASSERT(typeResolver);
 
103
 
 
104
    QGenericArgument gArgs[10];
 
105
    QVariant vArgs[10];
 
106
    PySideMetaFunction* function = reinterpret_cast<PySideMetaFunction*>(self);
 
107
    QMetaMethod method = function->d->method;
 
108
    int argsGiven = method.parameterTypes().size();
 
109
 
 
110
    for (int i = 0; i < argsGiven; ++i) {
 
111
        Shiboken::AutoDecRef pyArg(PySequence_GetItem(args, i));
 
112
        gArgs[i] = Q_ARG(QVariant, vArgs[i]);
 
113
        void* v[1] = { &vArgs[i] };
 
114
        typeResolver->toCpp(pyArg, v);
 
115
    }
 
116
 
 
117
    QVariant retVariant;
 
118
    QGenericReturnArgument returnValue = Q_RETURN_ARG(QVariant, retVariant);
 
119
    method.invoke(function->d->qobject, returnValue, gArgs[0], gArgs[1], gArgs[2], gArgs[3], gArgs[4], gArgs[5], gArgs[6], gArgs[7], gArgs[8], gArgs[9]);
 
120
 
 
121
 
 
122
    if (retVariant.isValid())
 
123
        return typeResolver->toPython(&retVariant);
 
124
 
 
125
    Py_RETURN_NONE;
 
126
}
 
127
 
 
128
} // extern "C"
 
129
 
 
130
namespace PySide { namespace MetaFunction {
 
131
 
 
132
void init(PyObject* module)
 
133
{
 
134
    if (PyType_Ready(&PySideMetaFunctionType) < 0)
 
135
        return;
 
136
 
 
137
    PyModule_AddObject(module, "MetaFunction", ((PyObject*)&PySideMetaFunctionType));
 
138
}
 
139
 
 
140
PySideMetaFunction* newObject(QObject* source, int methodIndex)
 
141
{
 
142
    if (methodIndex >= source->metaObject()->methodCount())
 
143
        return 0;
 
144
 
 
145
    QMetaMethod method = source->metaObject()->method(methodIndex);
 
146
    if ((method.methodType() == QMetaMethod::Slot) ||
 
147
        (method.methodType() == QMetaMethod::Method)) {
 
148
        PySideMetaFunction* function = PyObject_New(PySideMetaFunction, &PySideMetaFunctionType);
 
149
        function->d = new PySideMetaFunctionPrivate();
 
150
        function->d->qobject = source;
 
151
        function->d->method = method;
 
152
        return function;
 
153
    }
 
154
    return 0;
 
155
}
 
156
 
 
157
} //namespace MetaFunction
 
158
} //namespace PySide
 
159