##// END OF EJS Templates
fixed deriving C++ classes...
florianlink -
r122:fb2e671067ff
parent child
Show More
@@ -1,452 +1,454
1 /*
1 /*
2 *
2 *
3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4 *
4 *
5 * This library is free software; you can redistribute it and/or
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
8 * version 2.1 of the License, or (at your option) any later version.
9 *
9 *
10 * This library is distributed in the hope that it will be useful,
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
13 * Lesser General Public License for more details.
14 *
14 *
15 * Further, this software is distributed without any warranty that it is
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
20 * other software, or any other product whatsoever.
21 *
21 *
22 * You should have received a copy of the GNU Lesser General Public
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
25 *
26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
27 * 28359 Bremen, Germany or:
27 * 28359 Bremen, Germany or:
28 *
28 *
29 * http://www.mevis.de
29 * http://www.mevis.de
30 *
30 *
31 */
31 */
32
32
33 //----------------------------------------------------------------------------------
33 //----------------------------------------------------------------------------------
34 /*!
34 /*!
35 // \file PythonQtClassWrapper.cpp
35 // \file PythonQtClassWrapper.cpp
36 // \author Florian Link
36 // \author Florian Link
37 // \author Last changed by $Author: florian $
37 // \author Last changed by $Author: florian $
38 // \date 2006-05
38 // \date 2006-05
39 */
39 */
40 //----------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------
41
41
42 #include "PythonQtClassWrapper.h"
42 #include "PythonQtClassWrapper.h"
43 #include <QObject>
43 #include <QObject>
44
44
45 #include "PythonQt.h"
45 #include "PythonQt.h"
46 #include "PythonQtSlot.h"
46 #include "PythonQtSlot.h"
47 #include "PythonQtClassInfo.h"
47 #include "PythonQtClassInfo.h"
48 #include "PythonQtConversion.h"
48 #include "PythonQtConversion.h"
49 #include "PythonQtInstanceWrapper.h"
49 #include "PythonQtInstanceWrapper.h"
50
50
51 static PyObject* PythonQtInstanceWrapper_invert(PythonQtInstanceWrapper* wrapper)
51 static PyObject* PythonQtInstanceWrapper_invert(PythonQtInstanceWrapper* wrapper)
52 {
52 {
53 PyObject* result = NULL;
53 PyObject* result = NULL;
54 static QByteArray memberName = "__invert__";
54 static QByteArray memberName = "__invert__";
55 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
55 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
56 if (opSlot._type == PythonQtMemberInfo::Slot) {
56 if (opSlot._type == PythonQtMemberInfo::Slot) {
57 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
57 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
58 }
58 }
59 return result;
59 return result;
60 }
60 }
61
61
62 static int PythonQtInstanceWrapper_nonzero(PythonQtInstanceWrapper* wrapper)
62 static int PythonQtInstanceWrapper_nonzero(PythonQtInstanceWrapper* wrapper)
63 {
63 {
64 int result = (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
64 int result = (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
65 if (result) {
65 if (result) {
66 static QByteArray memberName = "__nonzero__";
66 static QByteArray memberName = "__nonzero__";
67 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
67 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
68 if (opSlot._type == PythonQtMemberInfo::Slot) {
68 if (opSlot._type == PythonQtMemberInfo::Slot) {
69 PyObject* resultObj = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
69 PyObject* resultObj = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
70 if (resultObj == Py_False) {
70 if (resultObj == Py_False) {
71 result = 0;
71 result = 0;
72 }
72 }
73 Py_XDECREF(resultObj);
73 Py_XDECREF(resultObj);
74 }
74 }
75 }
75 }
76 return result;
76 return result;
77 }
77 }
78
78
79
79
80 static PyObject* PythonQtInstanceWrapper_binaryfunc(PythonQtInstanceWrapper* wrapper, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
80 static PyObject* PythonQtInstanceWrapper_binaryfunc(PythonQtInstanceWrapper* wrapper, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
81 {
81 {
82 PyObject* result = NULL;
82 PyObject* result = NULL;
83 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(opName);
83 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(opName);
84 if (opSlot._type == PythonQtMemberInfo::Slot) {
84 if (opSlot._type == PythonQtMemberInfo::Slot) {
85 // TODO get rid of tuple
85 // TODO get rid of tuple
86 PyObject* args = PyTuple_New(1);
86 PyObject* args = PyTuple_New(1);
87 Py_INCREF(other);
87 Py_INCREF(other);
88 PyTuple_SET_ITEM(args, 0, other);
88 PyTuple_SET_ITEM(args, 0, other);
89 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, args, NULL, wrapper->_wrappedPtr);
89 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, args, NULL, wrapper->_wrappedPtr);
90 Py_DECREF(args);
90 Py_DECREF(args);
91 if (!result && !fallbackOpName.isEmpty()) {
91 if (!result && !fallbackOpName.isEmpty()) {
92 // try fallback if we did not get a result
92 // try fallback if we did not get a result
93 result = PythonQtInstanceWrapper_binaryfunc(wrapper, other, fallbackOpName);
93 result = PythonQtInstanceWrapper_binaryfunc(wrapper, other, fallbackOpName);
94 }
94 }
95 }
95 }
96 return result;
96 return result;
97 }
97 }
98
98
99 #define BINARY_OP(NAME) \
99 #define BINARY_OP(NAME) \
100 static PyObject* PythonQtInstanceWrapper_ ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
100 static PyObject* PythonQtInstanceWrapper_ ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
101 { \
101 { \
102 static const QByteArray opName("__" #NAME "__"); \
102 static const QByteArray opName("__" #NAME "__"); \
103 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName); \
103 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName); \
104 }
104 }
105
105
106 #define BINARY_OP_INPLACE(NAME) \
106 #define BINARY_OP_INPLACE(NAME) \
107 static PyObject* PythonQtInstanceWrapper_i ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
107 static PyObject* PythonQtInstanceWrapper_i ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
108 { \
108 { \
109 static const QByteArray opName("__i" #NAME "__"); \
109 static const QByteArray opName("__i" #NAME "__"); \
110 static const QByteArray fallbackName("__" #NAME "__"); \
110 static const QByteArray fallbackName("__" #NAME "__"); \
111 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName, fallbackName); \
111 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName, fallbackName); \
112 }
112 }
113
113
114 BINARY_OP(add)
114 BINARY_OP(add)
115 BINARY_OP(sub)
115 BINARY_OP(sub)
116 BINARY_OP(mul)
116 BINARY_OP(mul)
117 BINARY_OP(div)
117 BINARY_OP(div)
118 BINARY_OP(and)
118 BINARY_OP(and)
119 BINARY_OP(or)
119 BINARY_OP(or)
120 BINARY_OP(xor)
120 BINARY_OP(xor)
121 BINARY_OP(mod)
121 BINARY_OP(mod)
122 BINARY_OP(lshift)
122 BINARY_OP(lshift)
123 BINARY_OP(rshift)
123 BINARY_OP(rshift)
124
124
125 BINARY_OP_INPLACE(add)
125 BINARY_OP_INPLACE(add)
126 BINARY_OP_INPLACE(sub)
126 BINARY_OP_INPLACE(sub)
127 BINARY_OP_INPLACE(mul)
127 BINARY_OP_INPLACE(mul)
128 BINARY_OP_INPLACE(div)
128 BINARY_OP_INPLACE(div)
129 BINARY_OP_INPLACE(and)
129 BINARY_OP_INPLACE(and)
130 BINARY_OP_INPLACE(or)
130 BINARY_OP_INPLACE(or)
131 BINARY_OP_INPLACE(xor)
131 BINARY_OP_INPLACE(xor)
132 BINARY_OP_INPLACE(mod)
132 BINARY_OP_INPLACE(mod)
133 BINARY_OP_INPLACE(lshift)
133 BINARY_OP_INPLACE(lshift)
134 BINARY_OP_INPLACE(rshift)
134 BINARY_OP_INPLACE(rshift)
135
135
136 static void initializeSlots(PythonQtClassWrapper* wrap)
136 static void initializeSlots(PythonQtClassWrapper* wrap)
137 {
137 {
138 int typeSlots = wrap->classInfo()->typeSlots();
138 int typeSlots = wrap->classInfo()->typeSlots();
139 if (typeSlots) {
139 if (typeSlots) {
140 if (typeSlots & PythonQt::Type_Add) {
140 if (typeSlots & PythonQt::Type_Add) {
141 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_add;
141 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_add;
142 }
142 }
143 if (typeSlots & PythonQt::Type_Subtract) {
143 if (typeSlots & PythonQt::Type_Subtract) {
144 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_sub;
144 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_sub;
145 }
145 }
146 if (typeSlots & PythonQt::Type_Multiply) {
146 if (typeSlots & PythonQt::Type_Multiply) {
147 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
147 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
148 }
148 }
149 if (typeSlots & PythonQt::Type_Divide) {
149 if (typeSlots & PythonQt::Type_Divide) {
150 wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
150 wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
151 wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
151 wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
152 }
152 }
153 if (typeSlots & PythonQt::Type_And) {
153 if (typeSlots & PythonQt::Type_And) {
154 wrap->_base.as_number.nb_and = (binaryfunc)PythonQtInstanceWrapper_and;
154 wrap->_base.as_number.nb_and = (binaryfunc)PythonQtInstanceWrapper_and;
155 }
155 }
156 if (typeSlots & PythonQt::Type_Or) {
156 if (typeSlots & PythonQt::Type_Or) {
157 wrap->_base.as_number.nb_or = (binaryfunc)PythonQtInstanceWrapper_or;
157 wrap->_base.as_number.nb_or = (binaryfunc)PythonQtInstanceWrapper_or;
158 }
158 }
159 if (typeSlots & PythonQt::Type_Xor) {
159 if (typeSlots & PythonQt::Type_Xor) {
160 wrap->_base.as_number.nb_xor = (binaryfunc)PythonQtInstanceWrapper_xor;
160 wrap->_base.as_number.nb_xor = (binaryfunc)PythonQtInstanceWrapper_xor;
161 }
161 }
162 if (typeSlots & PythonQt::Type_Mod) {
162 if (typeSlots & PythonQt::Type_Mod) {
163 wrap->_base.as_number.nb_remainder = (binaryfunc)PythonQtInstanceWrapper_mod;
163 wrap->_base.as_number.nb_remainder = (binaryfunc)PythonQtInstanceWrapper_mod;
164 }
164 }
165 if (typeSlots & PythonQt::Type_LShift) {
165 if (typeSlots & PythonQt::Type_LShift) {
166 wrap->_base.as_number.nb_lshift = (binaryfunc)PythonQtInstanceWrapper_lshift;
166 wrap->_base.as_number.nb_lshift = (binaryfunc)PythonQtInstanceWrapper_lshift;
167 }
167 }
168 if (typeSlots & PythonQt::Type_RShift) {
168 if (typeSlots & PythonQt::Type_RShift) {
169 wrap->_base.as_number.nb_rshift = (binaryfunc)PythonQtInstanceWrapper_rshift;
169 wrap->_base.as_number.nb_rshift = (binaryfunc)PythonQtInstanceWrapper_rshift;
170 }
170 }
171
171
172 if (typeSlots & PythonQt::Type_InplaceAdd) {
172 if (typeSlots & PythonQt::Type_InplaceAdd) {
173 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_iadd;
173 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_iadd;
174 }
174 }
175 if (typeSlots & PythonQt::Type_InplaceSubtract) {
175 if (typeSlots & PythonQt::Type_InplaceSubtract) {
176 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_isub;
176 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_isub;
177 }
177 }
178 if (typeSlots & PythonQt::Type_InplaceMultiply) {
178 if (typeSlots & PythonQt::Type_InplaceMultiply) {
179 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
179 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
180 }
180 }
181 if (typeSlots & PythonQt::Type_InplaceDivide) {
181 if (typeSlots & PythonQt::Type_InplaceDivide) {
182 wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
182 wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
183 wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
183 wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
184 }
184 }
185 if (typeSlots & PythonQt::Type_InplaceAnd) {
185 if (typeSlots & PythonQt::Type_InplaceAnd) {
186 wrap->_base.as_number.nb_inplace_and = (binaryfunc)PythonQtInstanceWrapper_iand;
186 wrap->_base.as_number.nb_inplace_and = (binaryfunc)PythonQtInstanceWrapper_iand;
187 }
187 }
188 if (typeSlots & PythonQt::Type_InplaceOr) {
188 if (typeSlots & PythonQt::Type_InplaceOr) {
189 wrap->_base.as_number.nb_inplace_or = (binaryfunc)PythonQtInstanceWrapper_ior;
189 wrap->_base.as_number.nb_inplace_or = (binaryfunc)PythonQtInstanceWrapper_ior;
190 }
190 }
191 if (typeSlots & PythonQt::Type_InplaceXor) {
191 if (typeSlots & PythonQt::Type_InplaceXor) {
192 wrap->_base.as_number.nb_inplace_xor = (binaryfunc)PythonQtInstanceWrapper_ixor;
192 wrap->_base.as_number.nb_inplace_xor = (binaryfunc)PythonQtInstanceWrapper_ixor;
193 }
193 }
194 if (typeSlots & PythonQt::Type_InplaceMod) {
194 if (typeSlots & PythonQt::Type_InplaceMod) {
195 wrap->_base.as_number.nb_inplace_remainder = (binaryfunc)PythonQtInstanceWrapper_imod;
195 wrap->_base.as_number.nb_inplace_remainder = (binaryfunc)PythonQtInstanceWrapper_imod;
196 }
196 }
197 if (typeSlots & PythonQt::Type_InplaceLShift) {
197 if (typeSlots & PythonQt::Type_InplaceLShift) {
198 wrap->_base.as_number.nb_inplace_lshift = (binaryfunc)PythonQtInstanceWrapper_ilshift;
198 wrap->_base.as_number.nb_inplace_lshift = (binaryfunc)PythonQtInstanceWrapper_ilshift;
199 }
199 }
200 if (typeSlots & PythonQt::Type_InplaceRShift) {
200 if (typeSlots & PythonQt::Type_InplaceRShift) {
201 wrap->_base.as_number.nb_inplace_rshift = (binaryfunc)PythonQtInstanceWrapper_irshift;
201 wrap->_base.as_number.nb_inplace_rshift = (binaryfunc)PythonQtInstanceWrapper_irshift;
202 }
202 }
203 if (typeSlots & PythonQt::Type_Invert) {
203 if (typeSlots & PythonQt::Type_Invert) {
204 wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
204 wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
205 }
205 }
206 if (typeSlots & PythonQt::Type_NonZero) {
206 if (typeSlots & PythonQt::Type_NonZero) {
207 wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
207 wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
208 }
208 }
209 }
209 }
210 }
210 }
211
211
212 static PyObject* PythonQtClassWrapper_alloc(PyTypeObject *self, Py_ssize_t nitems)
212 static PyObject* PythonQtClassWrapper_alloc(PyTypeObject *self, Py_ssize_t nitems)
213 {
213 {
214 // call the default type alloc
214 // call the default type alloc
215 PyObject* obj = PyType_Type.tp_alloc(self, nitems);
215 PyObject* obj = PyType_Type.tp_alloc(self, nitems);
216
216
217 // take current class type, if we are called via newPythonQtClassWrapper()
217 // take current class type, if we are called via newPythonQtClassWrapper()
218 PythonQtClassWrapper* wrap = (PythonQtClassWrapper*)obj;
218 PythonQtClassWrapper* wrap = (PythonQtClassWrapper*)obj;
219 wrap->_classInfo = PythonQt::priv()->currentClassInfoForClassWrapperCreation();
219 wrap->_classInfo = PythonQt::priv()->currentClassInfoForClassWrapperCreation();
220 initializeSlots(wrap);
220 if (wrap->_classInfo) {
221 initializeSlots(wrap);
222 }
221
223
222 return obj;
224 return obj;
223 }
225 }
224
226
225
227
226 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args, PyObject* kwds)
228 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args, PyObject* kwds)
227 {
229 {
228 // call the default type init
230 // call the default type init
229 if (PyType_Type.tp_init((PyObject *)self, args, kwds) < 0) {
231 if (PyType_Type.tp_init((PyObject *)self, args, kwds) < 0) {
230 return -1;
232 return -1;
231 }
233 }
232
234
233 // if we have no CPP class information, try our base class
235 // if we have no CPP class information, try our base class
234 if (!self->classInfo()) {
236 if (!self->classInfo()) {
235 PyTypeObject* superType = ((PyTypeObject *)self)->tp_base;
237 PyTypeObject* superType = ((PyTypeObject *)self)->tp_base;
236
238
237 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
239 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
238 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
240 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
239 return -1;
241 return -1;
240 }
242 }
241
243
242 // take the class info from the superType
244 // take the class info from the superType
243 self->_classInfo = ((PythonQtClassWrapper*)superType)->classInfo();
245 self->_classInfo = ((PythonQtClassWrapper*)superType)->classInfo();
244 }
246 }
245
247
246 return 0;
248 return 0;
247 }
249 }
248
250
249 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
251 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
250 {
252 {
251 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
253 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
252 }
254 }
253
255
254 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
256 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
255 {
257 {
256 return PythonQt::self()->helpCalled(type->classInfo());
258 return PythonQt::self()->helpCalled(type->classInfo());
257 }
259 }
258
260
259 PyObject *PythonQtClassWrapper__init__(PythonQtClassWrapper *type, PyObject *args)
261 PyObject *PythonQtClassWrapper__init__(PythonQtClassWrapper *type, PyObject *args)
260 {
262 {
261 Py_ssize_t argc = PyTuple_Size(args);
263 Py_ssize_t argc = PyTuple_Size(args);
262 if (argc>0) {
264 if (argc>0) {
263 // we need to call __init__ of the instance
265 // we need to call __init__ of the instance
264 PyObject* self = PyTuple_GET_ITEM(args, 0);
266 PyObject* self = PyTuple_GET_ITEM(args, 0);
265 if (PyObject_TypeCheck(self, (PyTypeObject*)type->classInfo()->pythonQtClassWrapper())) {
267 if (PyObject_TypeCheck(self, (PyTypeObject*)type->classInfo()->pythonQtClassWrapper())) {
266 PyObject* newargs = PyTuple_New(argc-1);
268 PyObject* newargs = PyTuple_New(argc-1);
267 for (int i = 0;i<argc-1; i++) {
269 for (int i = 0;i<argc-1; i++) {
268 PyTuple_SET_ITEM(newargs, i,PyTuple_GET_ITEM(args, i+1));
270 PyTuple_SET_ITEM(newargs, i,PyTuple_GET_ITEM(args, i+1));
269 }
271 }
270 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
272 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
271 int result = PythonQtInstanceWrapper_init(wrapper, newargs, NULL);
273 int result = PythonQtInstanceWrapper_init(wrapper, newargs, NULL);
272 Py_DECREF(newargs);
274 Py_DECREF(newargs);
273 if (result==0) {
275 if (result==0) {
274 Py_INCREF(Py_None);
276 Py_INCREF(Py_None);
275 return Py_None;
277 return Py_None;
276 } else {
278 } else {
277 // init failed!
279 // init failed!
278 }
280 }
279 } else {
281 } else {
280 // self not of correct type!
282 // self not of correct type!
281 }
283 }
282 } else {
284 } else {
283 // wrong number of args
285 // wrong number of args
284 }
286 }
285 return NULL;
287 return NULL;
286 }
288 }
287
289
288 static PyMethodDef PythonQtClassWrapper_methods[] = {
290 static PyMethodDef PythonQtClassWrapper_methods[] = {
289 {"__init__", (PyCFunction)PythonQtClassWrapper__init__, METH_VARARGS,
291 {"__init__", (PyCFunction)PythonQtClassWrapper__init__, METH_VARARGS,
290 "Return the classname of the object"
292 "Return the classname of the object"
291 },
293 },
292 {"className", (PyCFunction)PythonQtClassWrapper_classname, METH_NOARGS,
294 {"className", (PyCFunction)PythonQtClassWrapper_classname, METH_NOARGS,
293 "Return the classname of the object"
295 "Return the classname of the object"
294 },
296 },
295 {"help", (PyCFunction)PythonQtClassWrapper_help, METH_NOARGS,
297 {"help", (PyCFunction)PythonQtClassWrapper_help, METH_NOARGS,
296 "Shows the help of available methods for this class"
298 "Shows the help of available methods for this class"
297 },
299 },
298 {NULL, NULL, 0 , NULL} /* Sentinel */
300 {NULL, NULL, 0 , NULL} /* Sentinel */
299 };
301 };
300
302
301
303
302 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
304 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
303 {
305 {
304 const char *attributeName;
306 const char *attributeName;
305 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
307 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
306
308
307 if ((attributeName = PyString_AsString(name)) == NULL) {
309 if ((attributeName = PyString_AsString(name)) == NULL) {
308 return NULL;
310 return NULL;
309 }
311 }
310 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
312 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
311 return NULL;
313 return NULL;
312 }
314 }
313
315
314 if (qstrcmp(attributeName, "__dict__")==0) {
316 if (qstrcmp(attributeName, "__dict__")==0) {
315 PyObject* dict = ((PyTypeObject *)wrapper)->tp_dict;
317 PyObject* dict = ((PyTypeObject *)wrapper)->tp_dict;
316 if (!wrapper->classInfo()) {
318 if (!wrapper->classInfo()) {
317 Py_INCREF(dict);
319 Py_INCREF(dict);
318 return dict;
320 return dict;
319 }
321 }
320 dict = PyDict_Copy(dict);
322 dict = PyDict_Copy(dict);
321
323
322 QStringList l = wrapper->classInfo()->memberList(false);
324 QStringList l = wrapper->classInfo()->memberList(false);
323 foreach (QString name, l) {
325 foreach (QString name, l) {
324 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
326 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
325 if (o) {
327 if (o) {
326 PyDict_SetItemString(dict, name.toLatin1().data(), o);
328 PyDict_SetItemString(dict, name.toLatin1().data(), o);
327 Py_DECREF(o);
329 Py_DECREF(o);
328 } else {
330 } else {
329 // it must have been a property or child, which we do not know as a class object...
331 // it must have been a property or child, which we do not know as a class object...
330 }
332 }
331 }
333 }
332 if (wrapper->classInfo()->constructors()) {
334 if (wrapper->classInfo()->constructors()) {
333 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[0], obj);
335 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[0], obj);
334 PyDict_SetItemString(dict, "__init__", func);
336 PyDict_SetItemString(dict, "__init__", func);
335 Py_DECREF(func);
337 Py_DECREF(func);
336 }
338 }
337 for (int i = 1;i<3;i++) {
339 for (int i = 1;i<3;i++) {
338 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[i], obj);
340 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[i], obj);
339 PyDict_SetItemString(dict, PythonQtClassWrapper_methods[i].ml_name, func);
341 PyDict_SetItemString(dict, PythonQtClassWrapper_methods[i].ml_name, func);
340 Py_DECREF(func);
342 Py_DECREF(func);
341 }
343 }
342 return dict;
344 return dict;
343 }
345 }
344
346
345 if (wrapper->classInfo()) {
347 if (wrapper->classInfo()) {
346 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
348 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
347 if (member._type == PythonQtMemberInfo::EnumValue) {
349 if (member._type == PythonQtMemberInfo::EnumValue) {
348 PyObject* enumValue = member._enumValue;
350 PyObject* enumValue = member._enumValue;
349 Py_INCREF(enumValue);
351 Py_INCREF(enumValue);
350 return enumValue;
352 return enumValue;
351 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
353 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
352 PyObject* enumWrapper = member._enumWrapper;
354 PyObject* enumWrapper = member._enumWrapper;
353 Py_INCREF(enumWrapper);
355 Py_INCREF(enumWrapper);
354 return enumWrapper;
356 return enumWrapper;
355 } else if (member._type == PythonQtMemberInfo::Slot) {
357 } else if (member._type == PythonQtMemberInfo::Slot) {
356 // we return all slots, even the instance slots, since they are callable as unbound slots with self argument
358 // we return all slots, even the instance slots, since they are callable as unbound slots with self argument
357 return PythonQtSlotFunction_New(member._slot, obj, NULL);
359 return PythonQtSlotFunction_New(member._slot, obj, NULL);
358 }
360 }
359 }
361 }
360
362
361 // look for the interal methods (className(), help())
363 // look for the interal methods (className(), help())
362 PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
364 PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
363 if (internalMethod) {
365 if (internalMethod) {
364 return internalMethod;
366 return internalMethod;
365 }
367 }
366 PyErr_Clear();
368 PyErr_Clear();
367
369
368 // look in super
370 // look in super
369 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
371 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
370 if (superAttr) {
372 if (superAttr) {
371 return superAttr;
373 return superAttr;
372 }
374 }
373
375
374 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
376 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
375 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
377 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
376 return NULL;
378 return NULL;
377 }
379 }
378
380
379 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
381 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
380 {
382 {
381 return PyType_Type.tp_setattro(obj,name,value);
383 return PyType_Type.tp_setattro(obj,name,value);
382 }
384 }
383
385
384 /*
386 /*
385 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
387 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
386 {
388 {
387 PythonQtClassWrapper* wrapper = (PythonQtClassWrapper*)obj;
389 PythonQtClassWrapper* wrapper = (PythonQtClassWrapper*)obj;
388 if (wrapper->classInfo()->isCPPWrapper()) {
390 if (wrapper->classInfo()->isCPPWrapper()) {
389 const QMetaObject* meta = wrapper->classInfo()->metaObject();
391 const QMetaObject* meta = wrapper->classInfo()->metaObject();
390 if (!meta) {
392 if (!meta) {
391 QObject* decorator = wrapper->classInfo()->decorator();
393 QObject* decorator = wrapper->classInfo()->decorator();
392 if (decorator) {
394 if (decorator) {
393 meta = decorator->metaObject();
395 meta = decorator->metaObject();
394 }
396 }
395 }
397 }
396 if (meta) {
398 if (meta) {
397 return PyString_FromFormat("%s Class (C++ wrapped by %s)", wrapper->classInfo()->className(), meta->className());
399 return PyString_FromFormat("%s Class (C++ wrapped by %s)", wrapper->classInfo()->className(), meta->className());
398 } else {
400 } else {
399 return PyString_FromFormat("%s Class (C++ unwrapped)", wrapper->classInfo()->className());
401 return PyString_FromFormat("%s Class (C++ unwrapped)", wrapper->classInfo()->className());
400 }
402 }
401 } else {
403 } else {
402 return PyString_FromFormat("%s Class", wrapper->classInfo()->className());
404 return PyString_FromFormat("%s Class", wrapper->classInfo()->className());
403 }
405 }
404 }
406 }
405
407
406 */
408 */
407
409
408 PyTypeObject PythonQtClassWrapper_Type = {
410 PyTypeObject PythonQtClassWrapper_Type = {
409 PyObject_HEAD_INIT(NULL)
411 PyObject_HEAD_INIT(NULL)
410 0, /*ob_size*/
412 0, /*ob_size*/
411 "PythonQt.PythonQtClassWrapper", /*tp_name*/
413 "PythonQt.PythonQtClassWrapper", /*tp_name*/
412 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
414 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
413 0, /*tp_itemsize*/
415 0, /*tp_itemsize*/
414 0, /*tp_dealloc*/
416 0, /*tp_dealloc*/
415 0, /*tp_print*/
417 0, /*tp_print*/
416 0, /*tp_getattr*/
418 0, /*tp_getattr*/
417 0, /*tp_setattr*/
419 0, /*tp_setattr*/
418 0, /*tp_compare*/
420 0, /*tp_compare*/
419 0, //PythonQtClassWrapper_repr, /*tp_repr*/
421 0, //PythonQtClassWrapper_repr, /*tp_repr*/
420 0, /*tp_as_number*/
422 0, /*tp_as_number*/
421 0, /*tp_as_sequence*/
423 0, /*tp_as_sequence*/
422 0, /*tp_as_mapping*/
424 0, /*tp_as_mapping*/
423 0, /*tp_hash */
425 0, /*tp_hash */
424 0, /*tp_call*/
426 0, /*tp_call*/
425 0, /*tp_str*/
427 0, /*tp_str*/
426 PythonQtClassWrapper_getattro, /*tp_getattro*/
428 PythonQtClassWrapper_getattro, /*tp_getattro*/
427 PythonQtClassWrapper_setattro, /*tp_setattro*/
429 PythonQtClassWrapper_setattro, /*tp_setattro*/
428 0, /*tp_as_buffer*/
430 0, /*tp_as_buffer*/
429 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
431 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
430 0, /* tp_doc */
432 0, /* tp_doc */
431 0, /* tp_traverse */
433 0, /* tp_traverse */
432 0, /* tp_clear */
434 0, /* tp_clear */
433 0, /* tp_richcompare */
435 0, /* tp_richcompare */
434 0, /* tp_weaklistoffset */
436 0, /* tp_weaklistoffset */
435 0, /* tp_iter */
437 0, /* tp_iter */
436 0, /* tp_iternext */
438 0, /* tp_iternext */
437 0, /* tp_methods */
439 0, /* tp_methods */
438 0, /* tp_members */
440 0, /* tp_members */
439 0, /* tp_getset */
441 0, /* tp_getset */
440 0, /* tp_base */
442 0, /* tp_base */
441 0, /* tp_dict */
443 0, /* tp_dict */
442 0, /* tp_descr_get */
444 0, /* tp_descr_get */
443 0, /* tp_descr_set */
445 0, /* tp_descr_set */
444 0, /* tp_dictoffset */
446 0, /* tp_dictoffset */
445 (initproc)PythonQtClassWrapper_init, /* tp_init */
447 (initproc)PythonQtClassWrapper_init, /* tp_init */
446 PythonQtClassWrapper_alloc, /* tp_alloc */
448 PythonQtClassWrapper_alloc, /* tp_alloc */
447 0, /* tp_new */
449 0, /* tp_new */
448 0, /* tp_free */
450 0, /* tp_free */
449 };
451 };
450
452
451 //-------------------------------------------------------
453 //-------------------------------------------------------
452
454
General Comments 0
You need to be logged in to leave comments. Login now