##// END OF EJS Templates
fixed attr lookup and support for invalid (unregistered) properties...
florianlink -
r27:aac16c8308db
parent child
Show More
@@ -1,549 +1,555
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 = self->classInfo()->destructor();
72 PythonQtSlotInfo* slot = self->classInfo()->destructor();
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 self->_isShellInstance = false;
133 self->_isShellInstance = false;
134 }
134 }
135 return (PyObject *)self;
135 return (PyObject *)self;
136 }
136 }
137
137
138 int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds)
138 int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds)
139 {
139 {
140 if (args == PythonQtPrivate::dummyTuple()) {
140 if (args == PythonQtPrivate::dummyTuple()) {
141 // we are called from the internal PythonQt API, so our data will be filled later on...
141 // we are called from the internal PythonQt API, so our data will be filled later on...
142 return 0;
142 return 0;
143 }
143 }
144
144
145 // we are called from python, try to construct our object
145 // we are called from python, try to construct our object
146 if (self->classInfo()->constructors()) {
146 if (self->classInfo()->constructors()) {
147 void* directCPPPointer = NULL;
147 void* directCPPPointer = NULL;
148 PythonQtSlotFunction_CallImpl(NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer);
148 PythonQtSlotFunction_CallImpl(NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer);
149 if (PyErr_Occurred()) {
149 if (PyErr_Occurred()) {
150 return -1;
150 return -1;
151 }
151 }
152 if (directCPPPointer) {
152 if (directCPPPointer) {
153 // change ownershipflag to be owned by PythonQt
153 // change ownershipflag to be owned by PythonQt
154 self->_ownedByPythonQt = true;
154 self->_ownedByPythonQt = true;
155 self->_useQMetaTypeDestroy = false;
155 self->_useQMetaTypeDestroy = false;
156 if (self->classInfo()->isCPPWrapper()) {
156 if (self->classInfo()->isCPPWrapper()) {
157 self->_wrappedPtr = directCPPPointer;
157 self->_wrappedPtr = directCPPPointer;
158 // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?!
158 // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?!
159 } else {
159 } else {
160 self->setQObject((QObject*)directCPPPointer);
160 self->setQObject((QObject*)directCPPPointer);
161 }
161 }
162 // register with PythonQt
162 // register with PythonQt
163 PythonQt::priv()->addWrapperPointer(directCPPPointer, self);
163 PythonQt::priv()->addWrapperPointer(directCPPPointer, self);
164
164
165 PythonQtShellSetInstanceWrapperCB* cb = self->classInfo()->shellSetInstanceWrapperCB();
165 PythonQtShellSetInstanceWrapperCB* cb = self->classInfo()->shellSetInstanceWrapperCB();
166 if (cb) {
166 if (cb) {
167 // if we are a derived python class, we set the wrapper
167 // if we are a derived python class, we set the wrapper
168 // to activate the shell class, otherwise we just ignore that it is a shell...
168 // to activate the shell class, otherwise we just ignore that it is a shell...
169 // we detect it be checking if the type does not have PythonQtInstanceWrapper_Type as direct base class,
169 // we detect it be checking if the type does not have PythonQtInstanceWrapper_Type as direct base class,
170 // which is the case for all non-python derived types
170 // which is the case for all non-python derived types
171 if (((PyObject*)self)->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
171 if (((PyObject*)self)->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
172 // set the wrapper and remember that we have a shell instance!
172 // set the wrapper and remember that we have a shell instance!
173 (*cb)(directCPPPointer, self);
173 (*cb)(directCPPPointer, self);
174 self->_isShellInstance = true;
174 self->_isShellInstance = true;
175 }
175 }
176 }
176 }
177 }
177 }
178 } else {
178 } else {
179 QString error = QString("No constructors available for ") + self->classInfo()->className();
179 QString error = QString("No constructors available for ") + self->classInfo()->className();
180 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
180 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
181 return -1;
181 return -1;
182 }
182 }
183 return 0;
183 return 0;
184 }
184 }
185
185
186 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* type)
186 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* type)
187 {
187 {
188 return PyString_FromString(type->classInfo()->className());
188 return PyString_FromString(type->classInfo()->className());
189 }
189 }
190
190
191 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* type)
191 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* type)
192 {
192 {
193 return PythonQt::self()->helpCalled(type->classInfo());
193 return PythonQt::self()->helpCalled(type->classInfo());
194 }
194 }
195
195
196 static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self)
196 static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self)
197 {
197 {
198 PythonQtInstanceWrapper_deleteObject(self, true);
198 PythonQtInstanceWrapper_deleteObject(self, true);
199 Py_INCREF(Py_None);
199 Py_INCREF(Py_None);
200 return Py_None;
200 return Py_None;
201 }
201 }
202
202
203
203
204 static PyMethodDef PythonQtInstanceWrapper_methods[] = {
204 static PyMethodDef PythonQtInstanceWrapper_methods[] = {
205 {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS,
205 {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS,
206 "Return the classname of the object"
206 "Return the classname of the object"
207 },
207 },
208 {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS,
208 {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS,
209 "Shows the help of available methods for this class"
209 "Shows the help of available methods for this class"
210 },
210 },
211 {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS,
211 {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS,
212 "Deletes the C++ object (at your own risk, my friend!)"
212 "Deletes the C++ object (at your own risk, my friend!)"
213 },
213 },
214 {NULL, NULL, 0, NULL} /* Sentinel */
214 {NULL, NULL, 0, NULL} /* Sentinel */
215 };
215 };
216
216
217
217
218 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
218 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
219 {
219 {
220 const char *attributeName;
220 const char *attributeName;
221 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
221 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
222
222
223 if ((attributeName = PyString_AsString(name)) == NULL) {
223 if ((attributeName = PyString_AsString(name)) == NULL) {
224 return NULL;
224 return NULL;
225 }
225 }
226
226
227 if (qstrcmp(attributeName, "__dict__")==0) {
227 if (qstrcmp(attributeName, "__dict__")==0) {
228 QStringList l = wrapper->classInfo()->memberList(false);
228 QStringList l = wrapper->classInfo()->memberList(false);
229 PyObject* dict = PyDict_New();
229 PyObject* dict = PyDict_New();
230 foreach (QString name, l) {
230 foreach (QString name, l) {
231 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
231 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
232 PyDict_SetItemString(dict, name.toLatin1().data(), o);
232 PyDict_SetItemString(dict, name.toLatin1().data(), o);
233 Py_DECREF(o);
233 Py_DECREF(o);
234 }
234 }
235 // Note: we do not put children into the dict, is would look confusing?!
235 // Note: we do not put children into the dict, is would look confusing?!
236 return dict;
236 return dict;
237 }
237 }
238
238
239 // first look in super, to return derived methods from base object first
239 // first look in super, to return derived methods from base object first
240 PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name);
240 PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name);
241 if (superAttr) {
241 if (superAttr) {
242 return superAttr;
242 return superAttr;
243 }
243 }
244 PyErr_Clear();
244
245
245 if (!wrapper->_obj && !wrapper->_wrappedPtr) {
246 if (!wrapper->_obj && !wrapper->_wrappedPtr) {
246 QString error = QString("Trying to read attribute '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
247 QString error = QString("Trying to read attribute '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
247 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
248 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
248 return NULL;
249 return NULL;
249 }
250 }
250
251
251 // mlabDebugConst("Python","get " << attributeName);
252 // mlabDebugConst("Python","get " << attributeName);
252
253
253 // TODO: dynamic properties are missing
254 // TODO: dynamic properties are missing
254
255
255 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
256 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
256 switch (member._type) {
257 switch (member._type) {
257 case PythonQtMemberInfo::Property:
258 case PythonQtMemberInfo::Property:
258 if (wrapper->_obj) {
259 if (wrapper->_obj) {
260 if (member._property.userType() != QVariant::Invalid) {
259 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
261 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
262 } else {
263 Py_INCREF(Py_None);
264 return Py_None;
265 }
260 }
266 }
261 break;
267 break;
262 case PythonQtMemberInfo::Slot:
268 case PythonQtMemberInfo::Slot:
263 return PythonQtSlotFunction_New(member._slot, obj, NULL);
269 return PythonQtSlotFunction_New(member._slot, obj, NULL);
264 break;
270 break;
265 case PythonQtMemberInfo::EnumValue:
271 case PythonQtMemberInfo::EnumValue:
266 return PyInt_FromLong(member._enumValue);
272 return PyInt_FromLong(member._enumValue);
267 break;
273 break;
268 default:
274 default:
269 // is an invalid type, go on
275 // is an invalid type, go on
270 break;
276 break;
271 }
277 }
272
278
273 // look for the interal methods (className(), help())
279 // look for the interal methods (className(), help())
274 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
280 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
275 if (internalMethod) {
281 if (internalMethod) {
276 return internalMethod;
282 return internalMethod;
277 }
283 }
278 PyErr_Clear();
284 PyErr_Clear();
279
285
280 if (wrapper->_obj) {
286 if (wrapper->_obj) {
281 // look for a child
287 // look for a child
282 QObjectList children = wrapper->_obj->children();
288 QObjectList children = wrapper->_obj->children();
283 for (int i = 0; i < children.count(); i++) {
289 for (int i = 0; i < children.count(); i++) {
284 QObject *child = children.at(i);
290 QObject *child = children.at(i);
285 if (child->objectName() == attributeName) {
291 if (child->objectName() == attributeName) {
286 return PythonQt::self()->priv()->wrapQObject(child);
292 return PythonQt::self()->priv()->wrapQObject(child);
287 }
293 }
288 }
294 }
289 }
295 }
290
296
291 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
297 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
292 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
298 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
293 return NULL;
299 return NULL;
294 }
300 }
295
301
296 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
302 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
297 {
303 {
298 QString error;
304 QString error;
299 char *attributeName;
305 char *attributeName;
300 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
306 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
301
307
302 if ((attributeName = PyString_AsString(name)) == NULL)
308 if ((attributeName = PyString_AsString(name)) == NULL)
303 return -1;
309 return -1;
304
310
305 if (!wrapper->_obj) {
311 if (!wrapper->_obj) {
306 error = QString("Trying to set attribute '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
312 error = QString("Trying to set attribute '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
307 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
313 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
308 return -1;
314 return -1;
309 }
315 }
310
316
311 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
317 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
312 if (member._type == PythonQtMemberInfo::Property) {
318 if (member._type == PythonQtMemberInfo::Property) {
313 QMetaProperty prop = member._property;
319 QMetaProperty prop = member._property;
314 if (prop.isWritable()) {
320 if (prop.isWritable()) {
315 QVariant v;
321 QVariant v;
316 if (prop.isEnumType()) {
322 if (prop.isEnumType()) {
317 // this will give us either a string or an int, everything else will probably be an error
323 // this will give us either a string or an int, everything else will probably be an error
318 v = PythonQtConv::PyObjToQVariant(value);
324 v = PythonQtConv::PyObjToQVariant(value);
319 } else {
325 } else {
320 int t = prop.userType();
326 int t = prop.userType();
321 v = PythonQtConv::PyObjToQVariant(value, t);
327 v = PythonQtConv::PyObjToQVariant(value, t);
322 }
328 }
323 bool success = false;
329 bool success = false;
324 if (v.isValid()) {
330 if (v.isValid()) {
325 success = prop.write(wrapper->_obj, v);
331 success = prop.write(wrapper->_obj, v);
326 }
332 }
327 if (success) {
333 if (success) {
328 return 0;
334 return 0;
329 } else {
335 } else {
330 error = QString("Property '") + attributeName + "' of type '" +
336 error = QString("Property '") + attributeName + "' of type '" +
331 prop.typeName() + "' does not accept an object of type "
337 prop.typeName() + "' does not accept an object of type "
332 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
338 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
333 }
339 }
334 } else {
340 } else {
335 error = QString("Property '") + attributeName + "' of " + wrapper->classInfo()->className() + " object is not writable";
341 error = QString("Property '") + attributeName + "' of " + wrapper->classInfo()->className() + " object is not writable";
336 }
342 }
337 } else {
343 } else {
338 if (member._type == PythonQtMemberInfo::Slot) {
344 if (member._type == PythonQtMemberInfo::Slot) {
339 error = QString("Slot '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
345 error = QString("Slot '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
340 } else if (member._type == PythonQtMemberInfo::EnumValue) {
346 } else if (member._type == PythonQtMemberInfo::EnumValue) {
341 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
347 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + wrapper->classInfo()->className() + " object";
342 }
348 }
343 }
349 }
344
350
345 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
351 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
346 return -1;
352 return -1;
347 }
353 }
348
354
349 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
355 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
350 {
356 {
351 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
357 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
352 const char* typeName = obj->ob_type->tp_name;
358 const char* typeName = obj->ob_type->tp_name;
353 QObject *qobj = wrapper->_obj;
359 QObject *qobj = wrapper->_obj;
354 if (wrapper->_wrappedPtr) {
360 if (wrapper->_wrappedPtr) {
355 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
361 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
356 if (!str.isEmpty()) {
362 if (!str.isEmpty()) {
357 return PyString_FromFormat("%s", str.toLatin1().constData());
363 return PyString_FromFormat("%s", str.toLatin1().constData());
358 } else
364 } else
359 if (wrapper->_obj) {
365 if (wrapper->_obj) {
360 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
366 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
361 } else {
367 } else {
362 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
368 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
363 }
369 }
364 } else {
370 } else {
365 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
371 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
366 }
372 }
367 }
373 }
368
374
369 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
375 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
370 {
376 {
371 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
377 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
372 const char* typeName = obj->ob_type->tp_name;
378 const char* typeName = obj->ob_type->tp_name;
373
379
374 QObject *qobj = wrapper->_obj;
380 QObject *qobj = wrapper->_obj;
375 if (wrapper->_wrappedPtr) {
381 if (wrapper->_wrappedPtr) {
376 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
382 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
377 if (!str.isEmpty()) {
383 if (!str.isEmpty()) {
378 return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr);
384 return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr);
379 } else
385 } else
380 if (wrapper->_obj) {
386 if (wrapper->_obj) {
381 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
387 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
382 } else {
388 } else {
383 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
389 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
384 }
390 }
385 } else {
391 } else {
386 return PyString_FromFormat("%s (QObject %p)", wrapper->classInfo()->className(), qobj);
392 return PyString_FromFormat("%s (QObject %p)", wrapper->classInfo()->className(), qobj);
387 }
393 }
388 }
394 }
389
395
390 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
396 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
391 {
397 {
392 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
398 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
393 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
399 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
394
400
395 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
401 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
396 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
402 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
397 // check pointers directly first:
403 // check pointers directly first:
398 if (w1->_wrappedPtr != NULL) {
404 if (w1->_wrappedPtr != NULL) {
399 if (w1->_wrappedPtr == w2->_wrappedPtr) {
405 if (w1->_wrappedPtr == w2->_wrappedPtr) {
400 return 0;
406 return 0;
401 }
407 }
402 } else if (w1->_obj == w2->_obj) {
408 } else if (w1->_obj == w2->_obj) {
403 return 0;
409 return 0;
404 }
410 }
405 const char* class1 = w1->classInfo()->className();
411 const char* class1 = w1->classInfo()->className();
406 const char* class2 = w2->classInfo()->className();
412 const char* class2 = w2->classInfo()->className();
407 if (strcmp(class1, class2) == 0) {
413 if (strcmp(class1, class2) == 0) {
408 // same class names, so we can try the operator_equal
414 // same class names, so we can try the operator_equal
409 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
415 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
410 if (info._type == PythonQtMemberInfo::Slot) {
416 if (info._type == PythonQtMemberInfo::Slot) {
411 bool result = false;
417 bool result = false;
412 void* obj1 = w1->_wrappedPtr;
418 void* obj1 = w1->_wrappedPtr;
413 if (!obj1) {
419 if (!obj1) {
414 obj1 = w1->_obj;
420 obj1 = w1->_obj;
415 }
421 }
416 if (!obj1) { return -1; }
422 if (!obj1) { return -1; }
417 void* obj2 = w2->_wrappedPtr;
423 void* obj2 = w2->_wrappedPtr;
418 if (!obj2) {
424 if (!obj2) {
419 obj2 = w2->_obj;
425 obj2 = w2->_obj;
420 }
426 }
421 if (!obj2) { return -1; }
427 if (!obj2) { return -1; }
422 if (info._slot->isInstanceDecorator()) {
428 if (info._slot->isInstanceDecorator()) {
423 // call on decorator QObject
429 // call on decorator QObject
424 void* args[3];
430 void* args[3];
425 args[0] = &result;
431 args[0] = &result;
426 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
432 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
427 args[2] = obj2; // this is a reference, so it needs the direct pointer
433 args[2] = obj2; // this is a reference, so it needs the direct pointer
428 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
434 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
429 return result?0:-1;
435 return result?0:-1;
430 } else {
436 } else {
431 // call directly on QObject
437 // call directly on QObject
432 if (w1->_obj && w2->_obj) {
438 if (w1->_obj && w2->_obj) {
433 void* args[2];
439 void* args[2];
434 args[0] = &result;
440 args[0] = &result;
435 args[2] = obj2; // this is a reference, so it needs the direct pointer
441 args[2] = obj2; // this is a reference, so it needs the direct pointer
436 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
442 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
437 }
443 }
438 }
444 }
439 }
445 }
440 }
446 }
441 }
447 }
442 return -1;
448 return -1;
443 }
449 }
444
450
445 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
451 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
446 {
452 {
447 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
453 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
448 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
454 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
449 }
455 }
450
456
451
457
452 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
458 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
453 {
459 {
454 if (obj->_wrappedPtr != NULL) {
460 if (obj->_wrappedPtr != NULL) {
455 return reinterpret_cast<long>(obj->_wrappedPtr);
461 return reinterpret_cast<long>(obj->_wrappedPtr);
456 } else {
462 } else {
457 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
463 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
458 return reinterpret_cast<long>(qobj);
464 return reinterpret_cast<long>(qobj);
459 }
465 }
460 }
466 }
461
467
462
468
463
469
464 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
470 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
465 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
471 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
466 0, /* nb_add */
472 0, /* nb_add */
467 0, /* nb_subtract */
473 0, /* nb_subtract */
468 0, /* nb_multiply */
474 0, /* nb_multiply */
469 0, /* nb_divide */
475 0, /* nb_divide */
470 0, /* nb_remainder */
476 0, /* nb_remainder */
471 0, /* nb_divmod */
477 0, /* nb_divmod */
472 0, /* nb_power */
478 0, /* nb_power */
473 0, /* nb_negative */
479 0, /* nb_negative */
474 0, /* nb_positive */
480 0, /* nb_positive */
475 0, /* nb_absolute */
481 0, /* nb_absolute */
476 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
482 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
477 0, /* nb_invert */
483 0, /* nb_invert */
478 0, /* nb_lshift */
484 0, /* nb_lshift */
479 0, /* nb_rshift */
485 0, /* nb_rshift */
480 0, /* nb_and */
486 0, /* nb_and */
481 0, /* nb_xor */
487 0, /* nb_xor */
482 0, /* nb_or */
488 0, /* nb_or */
483 0, /* nb_coerce */
489 0, /* nb_coerce */
484 0, /* nb_int */
490 0, /* nb_int */
485 0, /* nb_long */
491 0, /* nb_long */
486 0, /* nb_float */
492 0, /* nb_float */
487 0, /* nb_oct */
493 0, /* nb_oct */
488 0, /* nb_hex */
494 0, /* nb_hex */
489 0, /* nb_inplace_add */
495 0, /* nb_inplace_add */
490 0, /* nb_inplace_subtract */
496 0, /* nb_inplace_subtract */
491 0, /* nb_inplace_multiply */
497 0, /* nb_inplace_multiply */
492 0, /* nb_inplace_divide */
498 0, /* nb_inplace_divide */
493 0, /* nb_inplace_remainder */
499 0, /* nb_inplace_remainder */
494 0, /* nb_inplace_power */
500 0, /* nb_inplace_power */
495 0, /* nb_inplace_lshift */
501 0, /* nb_inplace_lshift */
496 0, /* nb_inplace_rshift */
502 0, /* nb_inplace_rshift */
497 0, /* nb_inplace_and */
503 0, /* nb_inplace_and */
498 0, /* nb_inplace_xor */
504 0, /* nb_inplace_xor */
499 0, /* nb_inplace_or */
505 0, /* nb_inplace_or */
500 0, /* nb_floor_divide */
506 0, /* nb_floor_divide */
501 0, /* nb_true_divide */
507 0, /* nb_true_divide */
502 0, /* nb_inplace_floor_divide */
508 0, /* nb_inplace_floor_divide */
503 0, /* nb_inplace_true_divide */
509 0, /* nb_inplace_true_divide */
504 };
510 };
505
511
506 PyTypeObject PythonQtInstanceWrapper_Type = {
512 PyTypeObject PythonQtInstanceWrapper_Type = {
507 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
513 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
508 0, /*ob_size*/
514 0, /*ob_size*/
509 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
515 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
510 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
516 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
511 0, /*tp_itemsize*/
517 0, /*tp_itemsize*/
512 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
518 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
513 0, /*tp_print*/
519 0, /*tp_print*/
514 0, /*tp_getattr*/
520 0, /*tp_getattr*/
515 0, /*tp_setattr*/
521 0, /*tp_setattr*/
516 PythonQtInstanceWrapper_compare, /*tp_compare*/
522 PythonQtInstanceWrapper_compare, /*tp_compare*/
517 PythonQtInstanceWrapper_repr, /*tp_repr*/
523 PythonQtInstanceWrapper_repr, /*tp_repr*/
518 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
524 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
519 0, /*tp_as_sequence*/
525 0, /*tp_as_sequence*/
520 0, /*tp_as_mapping*/
526 0, /*tp_as_mapping*/
521 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
527 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
522 0, /*tp_call*/
528 0, /*tp_call*/
523 PythonQtInstanceWrapper_str, /*tp_str*/
529 PythonQtInstanceWrapper_str, /*tp_str*/
524 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
530 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
525 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
531 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
526 0, /*tp_as_buffer*/
532 0, /*tp_as_buffer*/
527 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
533 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
528 "PythonQtInstanceWrapper object", /* tp_doc */
534 "PythonQtInstanceWrapper object", /* tp_doc */
529 0, /* tp_traverse */
535 0, /* tp_traverse */
530 0, /* tp_clear */
536 0, /* tp_clear */
531 0, /* tp_richcompare */
537 0, /* tp_richcompare */
532 0, /* tp_weaklistoffset */
538 0, /* tp_weaklistoffset */
533 0, /* tp_iter */
539 0, /* tp_iter */
534 0, /* tp_iternext */
540 0, /* tp_iternext */
535 0, /* tp_methods */
541 0, /* tp_methods */
536 0, /* tp_members */
542 0, /* tp_members */
537 0, /* tp_getset */
543 0, /* tp_getset */
538 0, /* tp_base */
544 0, /* tp_base */
539 0, /* tp_dict */
545 0, /* tp_dict */
540 0, /* tp_descr_get */
546 0, /* tp_descr_get */
541 0, /* tp_descr_set */
547 0, /* tp_descr_set */
542 0, /* tp_dictoffset */
548 0, /* tp_dictoffset */
543 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
549 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
544 0, /* tp_alloc */
550 0, /* tp_alloc */
545 PythonQtInstanceWrapper_new, /* tp_new */
551 PythonQtInstanceWrapper_new, /* tp_new */
546 };
552 };
547
553
548 //-------------------------------------------------------
554 //-------------------------------------------------------
549
555
General Comments 0
You need to be logged in to leave comments. Login now