##// END OF EJS Templates
changed implementation to allow deriving python classes from PythonQt classes...
florianlink -
r22:29d38290403f
parent child
Show More
@@ -1,229 +1,283
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
50
50 static PyObject* PythonQtClassWrapper_alloc(PyTypeObject *self, Py_ssize_t nitems)
51 static PyObject* PythonQtClassWrapper_alloc(PyTypeObject *self, Py_ssize_t nitems)
51 {
52 {
52 // call the default type alloc
53 // call the default type alloc
53 PyObject* obj = PyType_Type.tp_alloc(self, nitems);
54 PyObject* obj = PyType_Type.tp_alloc(self, nitems);
54
55
55 // take current class type, if we are called via newPythonQtClassWrapper()
56 // take current class type, if we are called via newPythonQtClassWrapper()
56 PythonQtClassWrapper* wrap = (PythonQtClassWrapper*)obj;
57 PythonQtClassWrapper* wrap = (PythonQtClassWrapper*)obj;
57 wrap->_classInfo = PythonQt::priv()->currentClassInfoForClassWrapperCreation();
58 wrap->_classInfo = PythonQt::priv()->currentClassInfoForClassWrapperCreation();
58
59
59 return obj;
60 return obj;
60 }
61 }
61
62
62
63
63 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args, PyObject* kwds)
64 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args, PyObject* kwds)
64 {
65 {
65 // call the default type init
66 // call the default type init
66 if (PyType_Type.tp_init((PyObject *)self, args, kwds) < 0) {
67 if (PyType_Type.tp_init((PyObject *)self, args, kwds) < 0) {
67 return -1;
68 return -1;
68 }
69 }
69
70
70 // if we have no CPP class information, try our base class
71 // if we have no CPP class information, try our base class
71 if (!self->classInfo()) {
72 if (!self->classInfo()) {
72 PyTypeObject* superType = ((PyTypeObject *)self)->tp_base;
73 PyTypeObject* superType = ((PyTypeObject *)self)->tp_base;
73
74
74 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
75 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
75 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
76 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
76 return -1;
77 return -1;
77 }
78 }
78
79
79 // take the class info from the superType
80 // take the class info from the superType
80 self->_classInfo = ((PythonQtClassWrapper*)superType)->classInfo();
81 self->_classInfo = ((PythonQtClassWrapper*)superType)->classInfo();
81 }
82 }
82
83
83 return 0;
84 return 0;
84 }
85 }
85
86
86 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
87 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
87 {
88 {
88 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
89 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
89 }
90 }
90
91
91 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
92 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
92 {
93 {
93 return PythonQt::self()->helpCalled(type->classInfo());
94 return PythonQt::self()->helpCalled(type->classInfo());
94 }
95 }
95
96
97 PyObject *PythonQtClassWrapper__init__(PythonQtClassWrapper *type, PyObject *args)
98 {
99 Py_ssize_t argc = PyTuple_Size(args);
100 if (argc>0) {
101 // we need to call __init__ of the instance
102 PyObject* self = PyTuple_GET_ITEM(args, 0);
103 if (PyObject_TypeCheck(self, (PyTypeObject*)type->classInfo()->pythonQtClassWrapper())) {
104 PyObject* newargs = PyTuple_New(argc-1);
105 for (int i = 0;i<argc-1; i++) {
106 PyTuple_SET_ITEM(newargs, i,PyTuple_GET_ITEM(args, i+1));
107 }
108 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
109 int result = PythonQtInstanceWrapper_init(wrapper, newargs, NULL);
110 Py_DECREF(newargs);
111 if (result==0) {
112 Py_INCREF(Py_None);
113 return Py_None;
114 } else {
115 // init failed!
116 }
117 } else {
118 // self not of correct type!
119 }
120 } else {
121 // wrong number of args
122 }
123 return NULL;
124 }
96
125
97 static PyMethodDef PythonQtClassWrapper_methods[] = {
126 static PyMethodDef PythonQtClassWrapper_methods[] = {
127 {"__init__", (PyCFunction)PythonQtClassWrapper__init__, METH_VARARGS,
128 "Return the classname of the object"
129 },
98 {"className", (PyCFunction)PythonQtClassWrapper_classname, METH_NOARGS,
130 {"className", (PyCFunction)PythonQtClassWrapper_classname, METH_NOARGS,
99 "Return the classname of the object"
131 "Return the classname of the object"
100 },
132 },
101 {"help", (PyCFunction)PythonQtClassWrapper_help, METH_NOARGS,
133 {"help", (PyCFunction)PythonQtClassWrapper_help, METH_NOARGS,
102 "Shows the help of available methods for this class"
134 "Shows the help of available methods for this class"
103 },
135 },
104 {NULL, NULL, 0 , NULL} /* Sentinel */
136 {NULL, NULL, 0 , NULL} /* Sentinel */
105 };
137 };
106
138
107
139
108 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
140 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
109 {
141 {
110 const char *attributeName;
142 const char *attributeName;
111 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
143 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
112
144
113 if ((attributeName = PyString_AsString(name)) == NULL) {
145 if ((attributeName = PyString_AsString(name)) == NULL) {
114 return NULL;
146 return NULL;
115 }
147 }
148 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
149 return NULL;
150 }
116
151
117 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
152 if (qstrcmp(attributeName, "__dict__")==0) {
118 if (member._type == PythonQtMemberInfo::EnumValue) {
153 PyObject* dict = ((PyTypeObject *)wrapper)->tp_dict;
119 return PyInt_FromLong(member._enumValue);
154 if (!wrapper->classInfo()) {
155 Py_INCREF(dict);
156 return dict;
157 }
158 dict = PyDict_Copy(dict);
159
160 QStringList l = wrapper->classInfo()->memberList(true);
161 foreach (QString name, l) {
162 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
163 PyDict_SetItemString(dict, name.toLatin1().data(), o);
164 Py_DECREF(o);
165 }
166 if (wrapper->classInfo()->constructors()) {
167 PyDict_SetItemString(dict, "__init__", PyCFunction_New(&PythonQtClassWrapper_methods[0], obj));
168 }
169 PyDict_SetItemString(dict, PythonQtClassWrapper_methods[1].ml_name, PyCFunction_New(&PythonQtClassWrapper_methods[1], obj));
170 PyDict_SetItemString(dict, PythonQtClassWrapper_methods[2].ml_name, PyCFunction_New(&PythonQtClassWrapper_methods[2], obj));
171 return dict;
120 }
172 }
121 if (member._type == PythonQtMemberInfo::Slot && member._slot->isClassDecorator()) {
173
122 return PythonQtSlotFunction_New(member._slot, obj, NULL);
174 if (wrapper->classInfo()) {
175 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
176 if (member._type == PythonQtMemberInfo::EnumValue) {
177 return PyInt_FromLong(member._enumValue);
178 } else
179 if (member._type == PythonQtMemberInfo::Slot && member._slot->isClassDecorator()) {
180 return PythonQtSlotFunction_New(member._slot, obj, NULL);
181 }
123 }
182 }
124
183
125 // look for the interal methods (className(), help())
184 // look for the interal methods (className(), help())
126 PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
185 PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
127 if (internalMethod) {
186 if (internalMethod) {
128 return internalMethod;
187 return internalMethod;
129 }
188 }
130 PyErr_Clear();
189 PyErr_Clear();
131
190
132 if (qstrcmp(attributeName, "__dict__")==0) {
191 // look in super
133 QStringList l = wrapper->classInfo()->memberList(true);
192 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
134 PyObject* dict = PyDict_New();
193 if (superAttr) {
135 foreach (QString name, l) {
194 return superAttr;
136 //PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
137 PyDict_SetItemString(dict, name.toLatin1().data(), Py_None);
138 //Py_DECREF(o);
139 }
140 return dict;
141 }
195 }
142
196
143 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
197 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
144 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
198 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
145 return NULL;
199 return NULL;
146 }
200 }
147
201
148 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject * /*value*/)
202 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject * /*value*/)
149 {
203 {
150 QString error;
204 QString error;
151 char *attributeName;
205 char *attributeName;
152 if ((attributeName = PyString_AsString(name)) == NULL) {
206 if ((attributeName = PyString_AsString(name)) == NULL) {
153 return -1;
207 return -1;
154 }
208 }
155 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
209 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
156
210
157 // TODO
211 // TODO
158 return -1;
212 return -1;
159 }
213 }
160
214
161 /*
215 /*
162 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
216 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
163 {
217 {
164 PythonQtClassWrapper* wrapper = (PythonQtClassWrapper*)obj;
218 PythonQtClassWrapper* wrapper = (PythonQtClassWrapper*)obj;
165 if (wrapper->classInfo()->isCPPWrapper()) {
219 if (wrapper->classInfo()->isCPPWrapper()) {
166 const QMetaObject* meta = wrapper->classInfo()->metaObject();
220 const QMetaObject* meta = wrapper->classInfo()->metaObject();
167 if (!meta) {
221 if (!meta) {
168 QObject* decorator = wrapper->classInfo()->decorator();
222 QObject* decorator = wrapper->classInfo()->decorator();
169 if (decorator) {
223 if (decorator) {
170 meta = decorator->metaObject();
224 meta = decorator->metaObject();
171 }
225 }
172 }
226 }
173 if (meta) {
227 if (meta) {
174 return PyString_FromFormat("%s Class (C++ wrapped by %s)", wrapper->classInfo()->className(), meta->className());
228 return PyString_FromFormat("%s Class (C++ wrapped by %s)", wrapper->classInfo()->className(), meta->className());
175 } else {
229 } else {
176 return PyString_FromFormat("%s Class (C++ unwrapped)", wrapper->classInfo()->className());
230 return PyString_FromFormat("%s Class (C++ unwrapped)", wrapper->classInfo()->className());
177 }
231 }
178 } else {
232 } else {
179 return PyString_FromFormat("%s Class", wrapper->classInfo()->className());
233 return PyString_FromFormat("%s Class", wrapper->classInfo()->className());
180 }
234 }
181 }
235 }
182
236
183 */
237 */
184
238
185 PyTypeObject PythonQtClassWrapper_Type = {
239 PyTypeObject PythonQtClassWrapper_Type = {
186 PyObject_HEAD_INIT(NULL)
240 PyObject_HEAD_INIT(NULL)
187 0, /*ob_size*/
241 0, /*ob_size*/
188 "PythonQt.PythonQtClassWrapper", /*tp_name*/
242 "PythonQt.PythonQtClassWrapper", /*tp_name*/
189 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
243 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
190 0, /*tp_itemsize*/
244 0, /*tp_itemsize*/
191 0, /*tp_dealloc*/
245 0, /*tp_dealloc*/
192 0, /*tp_print*/
246 0, /*tp_print*/
193 0, /*tp_getattr*/
247 0, /*tp_getattr*/
194 0, /*tp_setattr*/
248 0, /*tp_setattr*/
195 0, /*tp_compare*/
249 0, /*tp_compare*/
196 0, //PythonQtClassWrapper_repr, /*tp_repr*/
250 0, //PythonQtClassWrapper_repr, /*tp_repr*/
197 0, /*tp_as_number*/
251 0, /*tp_as_number*/
198 0, /*tp_as_sequence*/
252 0, /*tp_as_sequence*/
199 0, /*tp_as_mapping*/
253 0, /*tp_as_mapping*/
200 0, /*tp_hash */
254 0, /*tp_hash */
201 0, /*tp_call*/
255 0, /*tp_call*/
202 0, /*tp_str*/
256 0, /*tp_str*/
203 PythonQtClassWrapper_getattro, /*tp_getattro*/
257 PythonQtClassWrapper_getattro, /*tp_getattro*/
204 PythonQtClassWrapper_setattro, /*tp_setattro*/
258 PythonQtClassWrapper_setattro, /*tp_setattro*/
205 0, /*tp_as_buffer*/
259 0, /*tp_as_buffer*/
206 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
260 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
207 0, /* tp_doc */
261 0, /* tp_doc */
208 0, /* tp_traverse */
262 0, /* tp_traverse */
209 0, /* tp_clear */
263 0, /* tp_clear */
210 0, /* tp_richcompare */
264 0, /* tp_richcompare */
211 0, /* tp_weaklistoffset */
265 0, /* tp_weaklistoffset */
212 0, /* tp_iter */
266 0, /* tp_iter */
213 0, /* tp_iternext */
267 0, /* tp_iternext */
214 0, /* tp_methods */
268 0, /* tp_methods */
215 0, /* tp_members */
269 0, /* tp_members */
216 0, /* tp_getset */
270 0, /* tp_getset */
217 0, /* tp_base */
271 0, /* tp_base */
218 0, /* tp_dict */
272 0, /* tp_dict */
219 0, /* tp_descr_get */
273 0, /* tp_descr_get */
220 0, /* tp_descr_set */
274 0, /* tp_descr_set */
221 0, /* tp_dictoffset */
275 0, /* tp_dictoffset */
222 (initproc)PythonQtClassWrapper_init, /* tp_init */
276 (initproc)PythonQtClassWrapper_init, /* tp_init */
223 PythonQtClassWrapper_alloc, /* tp_alloc */
277 PythonQtClassWrapper_alloc, /* tp_alloc */
224 0, /* tp_new */
278 0, /* tp_new */
225 0, /* tp_free */
279 0, /* tp_free */
226 };
280 };
227
281
228 //-------------------------------------------------------
282 //-------------------------------------------------------
229
283
@@ -1,529 +1,537
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 PythonQtInstanceWrapper.cpp
35 // \file PythonQtInstanceWrapper.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 "PythonQtInstanceWrapper.h"
42 #include "PythonQtInstanceWrapper.h"
43 #include <QObject>
43 #include <QObject>
44 #include "PythonQt.h"
44 #include "PythonQt.h"
45 #include "PythonQtSlot.h"
45 #include "PythonQtSlot.h"
46 #include "PythonQtClassInfo.h"
46 #include "PythonQtClassInfo.h"
47 #include "PythonQtConversion.h"
47 #include "PythonQtConversion.h"
48 #include "PythonQtClassWrapper.h"
48 #include "PythonQtClassWrapper.h"
49
49
50 PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo()
50 PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo()
51 {
51 {
52 // take the class info from our type object
52 // take the class info from our type object
53 return ((PythonQtClassWrapper*)ob_type)->_classInfo;
53 return ((PythonQtClassWrapper*)ob_type)->_classInfo;
54 }
54 }
55
55
56 static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) {
56 static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) {
57
57
58 // is this a C++ wrapper?
58 // is this a C++ wrapper?
59 if (self->_wrappedPtr) {
59 if (self->_wrappedPtr) {
60 //mlabDebugConst("Python","c++ wrapper removed " << self->_wrappedPtr << " " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
60 //mlabDebugConst("Python","c++ wrapper removed " << self->_wrappedPtr << " " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
61
61
62 PythonQt::priv()->removeWrapperPointer(self->_wrappedPtr);
62 PythonQt::priv()->removeWrapperPointer(self->_wrappedPtr);
63 // we own our qobject, so we delete it now:
63 // we own our qobject, so we delete it now:
64 delete self->_obj;
64 delete self->_obj;
65 self->_obj = NULL;
65 self->_obj = NULL;
66 if (force || self->classInfo()->hasOwnerMethodButNoOwner(self->_wrappedPtr) || self->_ownedByPythonQt) {
66 if (force || self->classInfo()->hasOwnerMethodButNoOwner(self->_wrappedPtr) || self->_ownedByPythonQt) {
67 int type = self->classInfo()->metaTypeId();
67 int type = self->classInfo()->metaTypeId();
68 if (self->_useQMetaTypeDestroy && type>=0) {
68 if (self->_useQMetaTypeDestroy && type>=0) {
69 // use QMetaType to destroy the object
69 // use QMetaType to destroy the object
70 QMetaType::destroy(type, self->_wrappedPtr);
70 QMetaType::destroy(type, self->_wrappedPtr);
71 } else {
71 } else {
72 PythonQtSlotInfo* slot = PythonQt::priv()->getDestructorSlot(self->classInfo()->className());
72 PythonQtSlotInfo* slot = PythonQt::priv()->getDestructorSlot(self->classInfo()->className());
73 if (slot) {
73 if (slot) {
74 void* args[2];
74 void* args[2];
75 args[0] = NULL;
75 args[0] = NULL;
76 args[1] = &self->_wrappedPtr;
76 args[1] = &self->_wrappedPtr;
77 slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, slot->slotIndex(), args);
77 slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, slot->slotIndex(), args);
78 self->_wrappedPtr = NULL;
78 self->_wrappedPtr = NULL;
79 } else {
79 } else {
80 if (type>=0) {
80 if (type>=0) {
81 // use QMetaType to destroy the object
81 // use QMetaType to destroy the object
82 QMetaType::destroy(type, self->_wrappedPtr);
82 QMetaType::destroy(type, self->_wrappedPtr);
83 } else {
83 } else {
84 // TODO: warn about not being able to destroy the object?
84 // TODO: warn about not being able to destroy the object?
85 }
85 }
86 }
86 }
87 }
87 }
88 }
88 }
89 } else {
89 } else {
90 //mlabDebugConst("Python","qobject wrapper removed " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
90 //mlabDebugConst("Python","qobject wrapper removed " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
91 if (self->_objPointerCopy) {
91 if (self->_objPointerCopy) {
92 PythonQt::priv()->removeWrapperPointer(self->_objPointerCopy);
92 PythonQt::priv()->removeWrapperPointer(self->_objPointerCopy);
93 }
93 }
94 if (self->_obj) {
94 if (self->_obj) {
95 if (force || self->_ownedByPythonQt) {
95 if (force || self->_ownedByPythonQt) {
96 if (force || !self->_obj->parent()) {
96 if (force || !self->_obj->parent()) {
97 delete self->_obj;
97 delete self->_obj;
98 }
98 }
99 } else {
99 } else {
100 if (self->_obj->parent()==NULL) {
100 if (self->_obj->parent()==NULL) {
101 // tell someone who is interested that the qobject is no longer wrapped, if it has no parent
101 // tell someone who is interested that the qobject is no longer wrapped, if it has no parent
102 PythonQt::qObjectNoLongerWrappedCB(self->_obj);
102 PythonQt::qObjectNoLongerWrappedCB(self->_obj);
103 }
103 }
104 }
104 }
105 }
105 }
106 }
106 }
107 self->_obj = NULL;
107 self->_obj = NULL;
108 }
108 }
109
109
110 static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self)
110 static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self)
111 {
111 {
112 PythonQtInstanceWrapper_deleteObject(self);
112 PythonQtInstanceWrapper_deleteObject(self);
113 self->_obj.~QPointer<QObject>();
113 self->_obj.~QPointer<QObject>();
114 self->ob_type->tp_free((PyObject*)self);
114 self->ob_type->tp_free((PyObject*)self);
115 }
115 }
116
116
117 static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * args, PyObject * /*kwds*/)
117 static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * args, PyObject * /*kwds*/)
118 {
118 {
119 PythonQtClassWrapper *classType = (PythonQtClassWrapper*)type;
119 PythonQtClassWrapper *classType = (PythonQtClassWrapper*)type;
120 PythonQtInstanceWrapper *self;
120 PythonQtInstanceWrapper *self;
121 static PyObject* emptyTuple = NULL;
121 static PyObject* emptyTuple = NULL;
122 if (emptyTuple==NULL) {
122 if (emptyTuple==NULL) {
123 emptyTuple = PyTuple_New(0);
123 emptyTuple = PyTuple_New(0);
124 }
124 }
125
125
126 self = (PythonQtInstanceWrapper*)PyBaseObject_Type.tp_new(type, emptyTuple, NULL);
126 self = (PythonQtInstanceWrapper*)PyBaseObject_Type.tp_new(type, emptyTuple, NULL);
127
127
128 if (self != NULL) {
128 if (self != NULL) {
129 new (&self->_obj) QPointer<QObject>();
129 new (&self->_obj) QPointer<QObject>();
130 self->_wrappedPtr = NULL;
130 self->_wrappedPtr = NULL;
131 self->_ownedByPythonQt = false;
131 self->_ownedByPythonQt = false;
132 self->_useQMetaTypeDestroy = false;
132 self->_useQMetaTypeDestroy = false;
133 }
133 }
134 return (PyObject *)self;
134 return (PyObject *)self;
135 }
135 }
136
136
137 static int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds)
137 int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds)
138 {
138 {
139 PyObject* result = NULL;
139 PyObject* result = NULL;
140
140
141 if (args == PythonQtPrivate::dummyTuple()) {
141 if (args == PythonQtPrivate::dummyTuple()) {
142 // we are called from the internal PythonQt API, so our data will be filled later on...
142 // we are called from the internal PythonQt API, so our data will be filled later on...
143 return 0;
143 return 0;
144 }
144 }
145
145
146 // we are called from python, try to construct our object
146 // we are called from python, try to construct our object
147 if (self->classInfo()->constructors()) {
147 if (self->classInfo()->constructors()) {
148 void* directCPPPointer = NULL;
148 void* directCPPPointer = NULL;
149 PythonQtSlotFunction_CallImpl(NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer);
149 PythonQtSlotFunction_CallImpl(NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer);
150 if (PyErr_Occurred()) {
150 if (PyErr_Occurred()) {
151 return -1;
151 return -1;
152 }
152 }
153 if (directCPPPointer) {
153 if (directCPPPointer) {
154 // change ownershipflag to be owned by PythonQt
154 // change ownershipflag to be owned by PythonQt
155 self->_ownedByPythonQt = true;
155 self->_ownedByPythonQt = true;
156 self->_useQMetaTypeDestroy = false;
156 self->_useQMetaTypeDestroy = false;
157 if (self->classInfo()->isCPPWrapper()) {
157 if (self->classInfo()->isCPPWrapper()) {
158 self->_wrappedPtr = directCPPPointer;
158 self->_wrappedPtr = directCPPPointer;
159 // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?!
159 // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?!
160 } else {
160 } else {
161 self->setQObject((QObject*)directCPPPointer);
161 self->setQObject((QObject*)directCPPPointer);
162 }
162 }
163 // register with PythonQt
163 // register with PythonQt
164 PythonQt::priv()->addWrapperPointer(directCPPPointer, self);
164 PythonQt::priv()->addWrapperPointer(directCPPPointer, self);
165 }
165 }
166 } else {
166 } else {
167 QString error = QString("No constructors available for ") + self->classInfo()->className();
167 QString error = QString("No constructors available for ") + self->classInfo()->className();
168 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
168 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
169 return -1;
169 return -1;
170 }
170 }
171 return 0;
171 return 0;
172 }
172 }
173
173
174 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* type)
174 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* type)
175 {
175 {
176 return PyString_FromString(type->classInfo()->className());
176 return PyString_FromString(type->classInfo()->className());
177 }
177 }
178
178
179 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* type)
179 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* type)
180 {
180 {
181 return PythonQt::self()->helpCalled(type->classInfo());
181 return PythonQt::self()->helpCalled(type->classInfo());
182 }
182 }
183
183
184 static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self)
184 static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self)
185 {
185 {
186 PythonQtInstanceWrapper_deleteObject(self, true);
186 PythonQtInstanceWrapper_deleteObject(self, true);
187 Py_INCREF(Py_None);
187 Py_INCREF(Py_None);
188 return Py_None;
188 return Py_None;
189 }
189 }
190
190
191
191
192 static PyMethodDef PythonQtInstanceWrapper_methods[] = {
192 static PyMethodDef PythonQtInstanceWrapper_methods[] = {
193 {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS,
193 {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS,
194 "Return the classname of the object"
194 "Return the classname of the object"
195 },
195 },
196 {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS,
196 {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS,
197 "Shows the help of available methods for this class"
197 "Shows the help of available methods for this class"
198 },
198 },
199 {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS,
199 {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS,
200 "Deletes the C++ object (at your own risk, my friend!)"
200 "Deletes the C++ object (at your own risk, my friend!)"
201 },
201 },
202 {NULL, NULL, 0, NULL} /* Sentinel */
202 {NULL, NULL, 0, NULL} /* Sentinel */
203 };
203 };
204
204
205
205
206 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
206 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
207 {
207 {
208 const char *attributeName;
208 const char *attributeName;
209 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
209 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
210
210
211 if ((attributeName = PyString_AsString(name)) == NULL) {
211 if ((attributeName = PyString_AsString(name)) == NULL) {
212 return NULL;
212 return NULL;
213 }
213 }
214
214
215 if (!wrapper->_obj && !wrapper->_wrappedPtr) {
215 if (!wrapper->_obj && !wrapper->_wrappedPtr) {
216 QString error = QString("Trying to read attribute '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
216 QString error = QString("Trying to read attribute '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
217 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
217 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
218 return NULL;
218 return NULL;
219 }
219 }
220
220
221 // mlabDebugConst("Python","get " << attributeName);
221 // mlabDebugConst("Python","get " << attributeName);
222
222
223 // TODO: dynamic properties are missing
223 // TODO: dynamic properties are missing
224
224
225 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
225 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
226 switch (member._type) {
226 switch (member._type) {
227 case PythonQtMemberInfo::Property:
227 case PythonQtMemberInfo::Property:
228 if (wrapper->_obj) {
228 if (wrapper->_obj) {
229 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
229 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
230 }
230 }
231 break;
231 break;
232 case PythonQtMemberInfo::Slot:
232 case PythonQtMemberInfo::Slot:
233 return PythonQtSlotFunction_New(member._slot, obj, NULL);
233 return PythonQtSlotFunction_New(member._slot, obj, NULL);
234 break;
234 break;
235 case PythonQtMemberInfo::EnumValue:
235 case PythonQtMemberInfo::EnumValue:
236 return PyInt_FromLong(member._enumValue);
236 return PyInt_FromLong(member._enumValue);
237 break;
237 break;
238 default:
238 default:
239 // is an invalid type, go on
239 // is an invalid type, go on
240 break;
240 break;
241 }
241 }
242
242
243 // look for the interal methods (className(), help())
243 // look for the interal methods (className(), help())
244 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
244 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
245 if (internalMethod) {
245 if (internalMethod) {
246 return internalMethod;
246 return internalMethod;
247 }
247 }
248 PyErr_Clear();
248 PyErr_Clear();
249
249
250 if (wrapper->_obj) {
251 // look for a child
252 QObjectList children = wrapper->_obj->children();
253 for (int i = 0; i < children.count(); i++) {
254 QObject *child = children.at(i);
255 if (child->objectName() == attributeName) {
256 return PythonQt::self()->priv()->wrapQObject(child);
257 }
258 }
259 }
260
261 if (qstrcmp(attributeName, "__dict__")==0) {
250 if (qstrcmp(attributeName, "__dict__")==0) {
262 QStringList l = wrapper->classInfo()->memberList(false);
251 QStringList l = wrapper->classInfo()->memberList(false);
263 PyObject* dict = PyDict_New();
252 PyObject* dict = PyDict_New();
264 foreach (QString name, l) {
253 foreach (QString name, l) {
265 //PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
254 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
266 PyDict_SetItemString(dict, name.toLatin1().data(), Py_None);
255 PyDict_SetItemString(dict, name.toLatin1().data(), Py_None);
267 //Py_DECREF(o);
256 Py_DECREF(o);
268 }
257 }
269 // Note: we do not put children into the dict, is would look confusing?!
258 // Note: we do not put children into the dict, is would look confusing?!
270 return dict;
259 return dict;
271 }
260 }
272
261
262 // look in super
263 PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name);
264 if (superAttr) {
265 return superAttr;
266 }
267
268 if (wrapper->_obj) {
269 // look for a child
270 QObjectList children = wrapper->_obj->children();
271 for (int i = 0; i < children.count(); i++) {
272 QObject *child = children.at(i);
273 if (child->objectName() == attributeName) {
274 return PythonQt::self()->priv()->wrapQObject(child);
275 }
276 }
277 }
273
278
274 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
279 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
275 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
280 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
276 return NULL;
281 return NULL;
277 }
282 }
278
283
279 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
284 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
280 {
285 {
281 QString error;
286 QString error;
282 char *attributeName;
287 char *attributeName;
283 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
288 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
284
289
285 if ((attributeName = PyString_AsString(name)) == NULL)
290 if ((attributeName = PyString_AsString(name)) == NULL)
286 return -1;
291 return -1;
287
292
288 if (!wrapper->_obj) {
293 if (!wrapper->_obj) {
289 error = QString("Trying to set attribute '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
294 error = QString("Trying to set attribute '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
290 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
295 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
291 return -1;
296 return -1;
292 }
297 }
293
298
294 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
299 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
295 if (member._type == PythonQtMemberInfo::Property) {
300 if (member._type == PythonQtMemberInfo::Property) {
296 QMetaProperty prop = member._property;
301 QMetaProperty prop = member._property;
297 if (prop.isWritable()) {
302 if (prop.isWritable()) {
298 QVariant v;
303 QVariant v;
299 if (prop.isEnumType()) {
304 if (prop.isEnumType()) {
300 // this will give us either a string or an int, everything else will probably be an error
305 // this will give us either a string or an int, everything else will probably be an error
301 v = PythonQtConv::PyObjToQVariant(value);
306 v = PythonQtConv::PyObjToQVariant(value);
302 } else {
307 } else {
303 int t = prop.userType();
308 int t = prop.userType();
304 v = PythonQtConv::PyObjToQVariant(value, t);
309 v = PythonQtConv::PyObjToQVariant(value, t);
305 }
310 }
306 bool success = false;
311 bool success = false;
307 if (v.isValid()) {
312 if (v.isValid()) {
308 success = prop.write(wrapper->_obj, v);
313 success = prop.write(wrapper->_obj, v);
309 }
314 }
310 if (success) {
315 if (success) {
311 return 0;
316 return 0;
312 } else {
317 } else {
313 error = QString("Property '") + attributeName + "' of type '" +
318 error = QString("Property '") + attributeName + "' of type '" +
314 prop.typeName() + "' does not accept an object of type "
319 prop.typeName() + "' does not accept an object of type "
315 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
320 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
316 }
321 }
317 } else {
322 } else {
318 error = QString("Property '") + attributeName + "' of " + wrapper->classInfo()->className() + " object is not writable";
323 error = QString("Property '") + attributeName + "' of " + wrapper->classInfo()->className() + " object is not writable";
319 }
324 }
320 } else {
325 } else {
321 if (member._type == PythonQtMemberInfo::Slot) {
326 if (member._type == PythonQtMemberInfo::Slot) {
322 error = QString("Slot '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
327 error = QString("Slot '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
323 } else if (member._type == PythonQtMemberInfo::EnumValue) {
328 } else if (member._type == PythonQtMemberInfo::EnumValue) {
324 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
329 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
325 }
330 }
326 }
331 }
327
332
328 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
333 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
329 return -1;
334 return -1;
330 }
335 }
331
336
332 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
337 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
333 {
338 {
334 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
339 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
340 const char* typeName = obj->ob_type->tp_name;
335 QObject *qobj = wrapper->_obj;
341 QObject *qobj = wrapper->_obj;
336 if (wrapper->_wrappedPtr) {
342 if (wrapper->_wrappedPtr) {
337 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
343 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
338 if (!str.isEmpty()) {
344 if (!str.isEmpty()) {
339 return PyString_FromFormat("%s", str.toLatin1().constData());
345 return PyString_FromFormat("%s", str.toLatin1().constData());
340 } else
346 } else
341 if (wrapper->_obj) {
347 if (wrapper->_obj) {
342 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", wrapper->classInfo()->className(), wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
348 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
343 } else {
349 } else {
344 return PyString_FromFormat("%s (C++ Object %p)", wrapper->classInfo()->className(), wrapper->_wrappedPtr);
350 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
345 }
351 }
346 } else {
352 } else {
347 return PyString_FromFormat("%s (QObject %p)", wrapper->classInfo()->className(), qobj);
353 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
348 }
354 }
349 }
355 }
350
356
351 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
357 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
352 {
358 {
353 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
359 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
360 const char* typeName = obj->ob_type->tp_name;
361
354 QObject *qobj = wrapper->_obj;
362 QObject *qobj = wrapper->_obj;
355 if (wrapper->_wrappedPtr) {
363 if (wrapper->_wrappedPtr) {
356 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
364 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
357 if (!str.isEmpty()) {
365 if (!str.isEmpty()) {
358 return PyString_FromFormat("%s(%s, %p)", QMetaType::typeName(wrapper->classInfo()->metaTypeId()), str.toLatin1().constData(), wrapper->_wrappedPtr);
366 return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr);
359 } else
367 } else
360 if (wrapper->_obj) {
368 if (wrapper->_obj) {
361 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", wrapper->classInfo()->className(), wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
369 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
362 } else {
370 } else {
363 return PyString_FromFormat("%s (C++ Object %p)", wrapper->classInfo()->className(), wrapper->_wrappedPtr);
371 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
364 }
372 }
365 } else {
373 } else {
366 return PyString_FromFormat("%s (QObject %p)", wrapper->classInfo()->className(), qobj);
374 return PyString_FromFormat("%s (QObject %p)", wrapper->classInfo()->className(), qobj);
367 }
375 }
368 }
376 }
369
377
370 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
378 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
371 {
379 {
372 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
380 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
373 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
381 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
374
382
375 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
383 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
376 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
384 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
377 // check pointers directly first:
385 // check pointers directly first:
378 if (w1->_wrappedPtr != NULL) {
386 if (w1->_wrappedPtr != NULL) {
379 if (w1->_wrappedPtr == w2->_wrappedPtr) {
387 if (w1->_wrappedPtr == w2->_wrappedPtr) {
380 return 0;
388 return 0;
381 }
389 }
382 } else if (w1->_obj == w2->_obj) {
390 } else if (w1->_obj == w2->_obj) {
383 return 0;
391 return 0;
384 }
392 }
385 const char* class1 = w1->classInfo()->className();
393 const char* class1 = w1->classInfo()->className();
386 const char* class2 = w2->classInfo()->className();
394 const char* class2 = w2->classInfo()->className();
387 if (strcmp(class1, class2) == 0) {
395 if (strcmp(class1, class2) == 0) {
388 // same class names, so we can try the operator_equal
396 // same class names, so we can try the operator_equal
389 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
397 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
390 if (info._type == PythonQtMemberInfo::Slot) {
398 if (info._type == PythonQtMemberInfo::Slot) {
391 bool result = false;
399 bool result = false;
392 void* obj1 = w1->_wrappedPtr;
400 void* obj1 = w1->_wrappedPtr;
393 if (!obj1) {
401 if (!obj1) {
394 obj1 = w1->_obj;
402 obj1 = w1->_obj;
395 }
403 }
396 if (!obj1) { return -1; }
404 if (!obj1) { return -1; }
397 void* obj2 = w2->_wrappedPtr;
405 void* obj2 = w2->_wrappedPtr;
398 if (!obj2) {
406 if (!obj2) {
399 obj2 = w2->_obj;
407 obj2 = w2->_obj;
400 }
408 }
401 if (!obj2) { return -1; }
409 if (!obj2) { return -1; }
402 if (info._slot->isInstanceDecorator()) {
410 if (info._slot->isInstanceDecorator()) {
403 // call on decorator QObject
411 // call on decorator QObject
404 void* args[3];
412 void* args[3];
405 args[0] = &result;
413 args[0] = &result;
406 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
414 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
407 args[2] = obj2; // this is a reference, so it needs the direct pointer
415 args[2] = obj2; // this is a reference, so it needs the direct pointer
408 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
416 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
409 return result?0:-1;
417 return result?0:-1;
410 } else {
418 } else {
411 // call directly on QObject
419 // call directly on QObject
412 if (w1->_obj && w2->_obj) {
420 if (w1->_obj && w2->_obj) {
413 void* args[2];
421 void* args[2];
414 args[0] = &result;
422 args[0] = &result;
415 args[2] = obj2; // this is a reference, so it needs the direct pointer
423 args[2] = obj2; // this is a reference, so it needs the direct pointer
416 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
424 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
417 }
425 }
418 }
426 }
419 }
427 }
420 }
428 }
421 }
429 }
422 return -1;
430 return -1;
423 }
431 }
424
432
425 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
433 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
426 {
434 {
427 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
435 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
428 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
436 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
429 }
437 }
430
438
431
439
432 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
440 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
433 {
441 {
434 if (obj->_wrappedPtr != NULL) {
442 if (obj->_wrappedPtr != NULL) {
435 return reinterpret_cast<long>(obj->_wrappedPtr);
443 return reinterpret_cast<long>(obj->_wrappedPtr);
436 } else {
444 } else {
437 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
445 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
438 return reinterpret_cast<long>(qobj);
446 return reinterpret_cast<long>(qobj);
439 }
447 }
440 }
448 }
441
449
442
450
443
451
444 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
452 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
445 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
453 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
446 0, /* nb_add */
454 0, /* nb_add */
447 0, /* nb_subtract */
455 0, /* nb_subtract */
448 0, /* nb_multiply */
456 0, /* nb_multiply */
449 0, /* nb_divide */
457 0, /* nb_divide */
450 0, /* nb_remainder */
458 0, /* nb_remainder */
451 0, /* nb_divmod */
459 0, /* nb_divmod */
452 0, /* nb_power */
460 0, /* nb_power */
453 0, /* nb_negative */
461 0, /* nb_negative */
454 0, /* nb_positive */
462 0, /* nb_positive */
455 0, /* nb_absolute */
463 0, /* nb_absolute */
456 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
464 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
457 0, /* nb_invert */
465 0, /* nb_invert */
458 0, /* nb_lshift */
466 0, /* nb_lshift */
459 0, /* nb_rshift */
467 0, /* nb_rshift */
460 0, /* nb_and */
468 0, /* nb_and */
461 0, /* nb_xor */
469 0, /* nb_xor */
462 0, /* nb_or */
470 0, /* nb_or */
463 0, /* nb_coerce */
471 0, /* nb_coerce */
464 0, /* nb_int */
472 0, /* nb_int */
465 0, /* nb_long */
473 0, /* nb_long */
466 0, /* nb_float */
474 0, /* nb_float */
467 0, /* nb_oct */
475 0, /* nb_oct */
468 0, /* nb_hex */
476 0, /* nb_hex */
469 0, /* nb_inplace_add */
477 0, /* nb_inplace_add */
470 0, /* nb_inplace_subtract */
478 0, /* nb_inplace_subtract */
471 0, /* nb_inplace_multiply */
479 0, /* nb_inplace_multiply */
472 0, /* nb_inplace_divide */
480 0, /* nb_inplace_divide */
473 0, /* nb_inplace_remainder */
481 0, /* nb_inplace_remainder */
474 0, /* nb_inplace_power */
482 0, /* nb_inplace_power */
475 0, /* nb_inplace_lshift */
483 0, /* nb_inplace_lshift */
476 0, /* nb_inplace_rshift */
484 0, /* nb_inplace_rshift */
477 0, /* nb_inplace_and */
485 0, /* nb_inplace_and */
478 0, /* nb_inplace_xor */
486 0, /* nb_inplace_xor */
479 0, /* nb_inplace_or */
487 0, /* nb_inplace_or */
480 0, /* nb_floor_divide */
488 0, /* nb_floor_divide */
481 0, /* nb_true_divide */
489 0, /* nb_true_divide */
482 0, /* nb_inplace_floor_divide */
490 0, /* nb_inplace_floor_divide */
483 0, /* nb_inplace_true_divide */
491 0, /* nb_inplace_true_divide */
484 };
492 };
485
493
486 PyTypeObject PythonQtInstanceWrapper_Type = {
494 PyTypeObject PythonQtInstanceWrapper_Type = {
487 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
495 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
488 0, /*ob_size*/
496 0, /*ob_size*/
489 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
497 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
490 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
498 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
491 0, /*tp_itemsize*/
499 0, /*tp_itemsize*/
492 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
500 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
493 0, /*tp_print*/
501 0, /*tp_print*/
494 0, /*tp_getattr*/
502 0, /*tp_getattr*/
495 0, /*tp_setattr*/
503 0, /*tp_setattr*/
496 PythonQtInstanceWrapper_compare, /*tp_compare*/
504 PythonQtInstanceWrapper_compare, /*tp_compare*/
497 PythonQtInstanceWrapper_repr, /*tp_repr*/
505 PythonQtInstanceWrapper_repr, /*tp_repr*/
498 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
506 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
499 0, /*tp_as_sequence*/
507 0, /*tp_as_sequence*/
500 0, /*tp_as_mapping*/
508 0, /*tp_as_mapping*/
501 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
509 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
502 0, /*tp_call*/
510 0, /*tp_call*/
503 PythonQtInstanceWrapper_str, /*tp_str*/
511 PythonQtInstanceWrapper_str, /*tp_str*/
504 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
512 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
505 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
513 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
506 0, /*tp_as_buffer*/
514 0, /*tp_as_buffer*/
507 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
515 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
508 "PythonQtInstanceWrapper object", /* tp_doc */
516 "PythonQtInstanceWrapper object", /* tp_doc */
509 0, /* tp_traverse */
517 0, /* tp_traverse */
510 0, /* tp_clear */
518 0, /* tp_clear */
511 0, /* tp_richcompare */
519 0, /* tp_richcompare */
512 0, /* tp_weaklistoffset */
520 0, /* tp_weaklistoffset */
513 0, /* tp_iter */
521 0, /* tp_iter */
514 0, /* tp_iternext */
522 0, /* tp_iternext */
515 0, /* tp_methods */
523 0, /* tp_methods */
516 0, /* tp_members */
524 0, /* tp_members */
517 0, /* tp_getset */
525 0, /* tp_getset */
518 0, /* tp_base */
526 0, /* tp_base */
519 0, /* tp_dict */
527 0, /* tp_dict */
520 0, /* tp_descr_get */
528 0, /* tp_descr_get */
521 0, /* tp_descr_set */
529 0, /* tp_descr_set */
522 0, /* tp_dictoffset */
530 0, /* tp_dictoffset */
523 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
531 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
524 0, /* tp_alloc */
532 0, /* tp_alloc */
525 PythonQtInstanceWrapper_new, /* tp_new */
533 PythonQtInstanceWrapper_new, /* tp_new */
526 };
534 };
527
535
528 //-------------------------------------------------------
536 //-------------------------------------------------------
529
537
@@ -1,92 +1,93
1 #ifndef _PYTHONQTINSTANCEWRAPPER_H
1 #ifndef _PYTHONQTINSTANCEWRAPPER_H
2 #define _PYTHONQTINSTANCEWRAPPER_H
2 #define _PYTHONQTINSTANCEWRAPPER_H
3
3
4 /*
4 /*
5 *
5 *
6 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
6 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
7 *
7 *
8 * This library is free software; you can redistribute it and/or
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
11 * version 2.1 of the License, or (at your option) any later version.
12 *
12 *
13 * This library is distributed in the hope that it will be useful,
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
16 * Lesser General Public License for more details.
17 *
17 *
18 * Further, this software is distributed without any warranty that it is
18 * Further, this software is distributed without any warranty that it is
19 * free of the rightful claim of any third person regarding infringement
19 * free of the rightful claim of any third person regarding infringement
20 * or the like. Any license provided herein, whether implied or
20 * or the like. Any license provided herein, whether implied or
21 * otherwise, applies only to this software file. Patent licenses, if
21 * otherwise, applies only to this software file. Patent licenses, if
22 * any, provided herein do not apply to combinations of this program with
22 * any, provided herein do not apply to combinations of this program with
23 * other software, or any other product whatsoever.
23 * other software, or any other product whatsoever.
24 *
24 *
25 * You should have received a copy of the GNU Lesser General Public
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
28 *
29 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
29 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
30 * 28359 Bremen, Germany or:
30 * 28359 Bremen, Germany or:
31 *
31 *
32 * http://www.mevis.de
32 * http://www.mevis.de
33 *
33 *
34 */
34 */
35
35
36 //----------------------------------------------------------------------------------
36 //----------------------------------------------------------------------------------
37 /*!
37 /*!
38 // \file PythonQtInstanceWrapper.h
38 // \file PythonQtInstanceWrapper.h
39 // \author Florian Link
39 // \author Florian Link
40 // \author Last changed by $Author: florian $
40 // \author Last changed by $Author: florian $
41 // \date 2006-05
41 // \date 2006-05
42 */
42 */
43 //----------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------
44
44
45 #include <Python.h>
45 #include <Python.h>
46
46
47 #include "PythonQtSystem.h"
47 #include "PythonQtSystem.h"
48 #include <QPointer>
48 #include <QPointer>
49
49
50 #include "structmember.h"
50 #include "structmember.h"
51 #include "methodobject.h"
51 #include "methodobject.h"
52 #include "compile.h"
52 #include "compile.h"
53 #include "eval.h"
53 #include "eval.h"
54
54
55 class PythonQtClassInfo;
55 class PythonQtClassInfo;
56 class QObject;
56 class QObject;
57
57
58 extern PYTHONQT_EXPORT PyTypeObject PythonQtInstanceWrapper_Type;
58 extern PYTHONQT_EXPORT PyTypeObject PythonQtInstanceWrapper_Type;
59
59
60 //---------------------------------------------------------------
60 //---------------------------------------------------------------
61 //! a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjects)
61 //! a Python wrapper object for Qt objects and C++ objects (that are themselves wrapped by wrapper QObjects)
62 typedef struct PythonQtInstanceWrapperStruct {
62 typedef struct PythonQtInstanceWrapperStruct {
63 PyObject_HEAD
63 PyObject_HEAD
64
64
65 //! the class information, this is set even if the _obj or _wrappedPtr is NULL to support typed NULL pointers
65 //! the class information, this is set even if the _obj or _wrappedPtr is NULL to support typed NULL pointers
66 PythonQtClassInfo* classInfo();
66 PythonQtClassInfo* classInfo();
67
67
68 //! set the QObject pointer
68 //! set the QObject pointer
69 void setQObject(QObject* object) {
69 void setQObject(QObject* object) {
70 _obj = object;
70 _obj = object;
71 _objPointerCopy = object;
71 _objPointerCopy = object;
72 }
72 }
73
73
74 //! pointer to the wrapped Qt object or if _wrappedPtr is set, the Qt object that wraps the C++ Ptr
74 //! pointer to the wrapped Qt object or if _wrappedPtr is set, the Qt object that wraps the C++ Ptr
75 QPointer<QObject> _obj;
75 QPointer<QObject> _obj;
76 //! a copy of the _obj pointer, which is required because the wrapper needs to
76 //! a copy of the _obj pointer, which is required because the wrapper needs to
77 //! deregister itself via the _obj pointer, even when the QPointer<QObject> object was destroyed
77 //! deregister itself via the _obj pointer, even when the QPointer<QObject> object was destroyed
78 void* _objPointerCopy;
78 void* _objPointerCopy;
79
79
80 //! optional C++ object Ptr that is wrapped by the above _obj
80 //! optional C++ object Ptr that is wrapped by the above _obj
81 void* _wrappedPtr;
81 void* _wrappedPtr;
82
82
83 //! flag that stores if the object is owned by pythonQt
83 //! flag that stores if the object is owned by pythonQt
84 bool _ownedByPythonQt;
84 bool _ownedByPythonQt;
85
85
86 //! stores that the owned object should be destroyed using QMetaType::destroy()
86 //! stores that the owned object should be destroyed using QMetaType::destroy()
87 bool _useQMetaTypeDestroy;
87 bool _useQMetaTypeDestroy;
88
88
89 } PythonQtInstanceWrapper;
89 } PythonQtInstanceWrapper;
90
90
91 int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds);
91
92
92 #endif No newline at end of file
93 #endif
General Comments 0
You need to be logged in to leave comments. Login now