##// END OF EJS Templates
moved check if a property can be set which was done to early...
florianlink -
r48:44ecc235fd76
parent child
Show More
@@ -1,571 +1,572
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(self->classInfo(), NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer);
148 PythonQtSlotFunction_CallImpl(self->classInfo(), 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* obj)
186 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* obj)
187 {
187 {
188 return PyString_FromString(obj->ob_type->tp_name);
188 return PyString_FromString(obj->ob_type->tp_name);
189 }
189 }
190
190
191 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* obj)
191 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* obj)
192 {
192 {
193 return PythonQt::self()->helpCalled(obj->classInfo());
193 return PythonQt::self()->helpCalled(obj->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 PyObject* dict = PyBaseObject_Type.tp_getattro(obj, name);
228 PyObject* dict = PyBaseObject_Type.tp_getattro(obj, name);
229 dict = PyDict_Copy(dict);
229 dict = PyDict_Copy(dict);
230
230
231 // only the properties are missing, the rest is already available from
231 // only the properties are missing, the rest is already available from
232 // PythonQtClassWrapper...
232 // PythonQtClassWrapper...
233 QStringList l = wrapper->classInfo()->propertyList();
233 QStringList l = wrapper->classInfo()->propertyList();
234 foreach (QString name, l) {
234 foreach (QString name, l) {
235 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
235 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
236 if (o) {
236 if (o) {
237 PyDict_SetItemString(dict, name.toLatin1().data(), o);
237 PyDict_SetItemString(dict, name.toLatin1().data(), o);
238 Py_DECREF(o);
238 Py_DECREF(o);
239 } else {
239 } else {
240 std::cerr << "PythonQtInstanceWrapper: something is wrong, could not get attribute " << name.toLatin1().data();
240 std::cerr << "PythonQtInstanceWrapper: something is wrong, could not get attribute " << name.toLatin1().data();
241 }
241 }
242 }
242 }
243 // Note: we do not put children into the dict, is would look confusing?!
243 // Note: we do not put children into the dict, is would look confusing?!
244 return dict;
244 return dict;
245 }
245 }
246
246
247 // first look in super, to return derived methods from base object first
247 // first look in super, to return derived methods from base object first
248 PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name);
248 PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name);
249 if (superAttr) {
249 if (superAttr) {
250 return superAttr;
250 return superAttr;
251 }
251 }
252 PyErr_Clear();
252 PyErr_Clear();
253
253
254 if (!wrapper->_obj && !wrapper->_wrappedPtr) {
254 if (!wrapper->_obj && !wrapper->_wrappedPtr) {
255 QString error = QString("Trying to read attribute '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
255 QString error = QString("Trying to read attribute '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
256 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
256 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
257 return NULL;
257 return NULL;
258 }
258 }
259
259
260 // mlabDebugConst("Python","get " << attributeName);
260 // mlabDebugConst("Python","get " << attributeName);
261
261
262 // TODO: dynamic properties are missing
262 // TODO: dynamic properties are missing
263
263
264 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
264 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
265 switch (member._type) {
265 switch (member._type) {
266 case PythonQtMemberInfo::Property:
266 case PythonQtMemberInfo::Property:
267 if (wrapper->_obj) {
267 if (wrapper->_obj) {
268 if (member._property.userType() != QVariant::Invalid) {
268 if (member._property.userType() != QVariant::Invalid) {
269 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
269 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
270 } else {
270 } else {
271 Py_INCREF(Py_None);
271 Py_INCREF(Py_None);
272 return Py_None;
272 return Py_None;
273 }
273 }
274 }
274 }
275 break;
275 break;
276 case PythonQtMemberInfo::Slot:
276 case PythonQtMemberInfo::Slot:
277 return PythonQtSlotFunction_New(member._slot, obj, NULL);
277 return PythonQtSlotFunction_New(member._slot, obj, NULL);
278 break;
278 break;
279 case PythonQtMemberInfo::EnumValue:
279 case PythonQtMemberInfo::EnumValue:
280 return PyInt_FromLong(member._enumValue);
280 return PyInt_FromLong(member._enumValue);
281 break;
281 break;
282 default:
282 default:
283 // is an invalid type, go on
283 // is an invalid type, go on
284 break;
284 break;
285 }
285 }
286
286
287 // look for the interal methods (className(), help())
287 // look for the interal methods (className(), help())
288 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
288 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
289 if (internalMethod) {
289 if (internalMethod) {
290 return internalMethod;
290 return internalMethod;
291 }
291 }
292 PyErr_Clear();
292 PyErr_Clear();
293
293
294 if (wrapper->_obj) {
294 if (wrapper->_obj) {
295 // look for a child
295 // look for a child
296 QObjectList children = wrapper->_obj->children();
296 QObjectList children = wrapper->_obj->children();
297 for (int i = 0; i < children.count(); i++) {
297 for (int i = 0; i < children.count(); i++) {
298 QObject *child = children.at(i);
298 QObject *child = children.at(i);
299 if (child->objectName() == attributeName) {
299 if (child->objectName() == attributeName) {
300 return PythonQt::self()->priv()->wrapQObject(child);
300 return PythonQt::self()->priv()->wrapQObject(child);
301 }
301 }
302 }
302 }
303 }
303 }
304
304
305 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
305 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
306 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
306 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
307 return NULL;
307 return NULL;
308 }
308 }
309
309
310 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
310 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
311 {
311 {
312 QString error;
312 QString error;
313 char *attributeName;
313 char *attributeName;
314 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
314 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
315
315
316 if ((attributeName = PyString_AsString(name)) == NULL)
316 if ((attributeName = PyString_AsString(name)) == NULL)
317 return -1;
317 return -1;
318
318
319 if (!wrapper->_obj) {
320 error = QString("Trying to set attribute '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
321 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
322 return -1;
323 }
324
325 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
319 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
326 if (member._type == PythonQtMemberInfo::Property) {
320 if (member._type == PythonQtMemberInfo::Property) {
321
322 if (!wrapper->_obj) {
323 error = QString("Trying to set property '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
324 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
325 return -1;
326 }
327
327 QMetaProperty prop = member._property;
328 QMetaProperty prop = member._property;
328 if (prop.isWritable()) {
329 if (prop.isWritable()) {
329 QVariant v;
330 QVariant v;
330 if (prop.isEnumType()) {
331 if (prop.isEnumType()) {
331 // this will give us either a string or an int, everything else will probably be an error
332 // this will give us either a string or an int, everything else will probably be an error
332 v = PythonQtConv::PyObjToQVariant(value);
333 v = PythonQtConv::PyObjToQVariant(value);
333 } else {
334 } else {
334 int t = prop.userType();
335 int t = prop.userType();
335 v = PythonQtConv::PyObjToQVariant(value, t);
336 v = PythonQtConv::PyObjToQVariant(value, t);
336 }
337 }
337 bool success = false;
338 bool success = false;
338 if (v.isValid()) {
339 if (v.isValid()) {
339 success = prop.write(wrapper->_obj, v);
340 success = prop.write(wrapper->_obj, v);
340 }
341 }
341 if (success) {
342 if (success) {
342 return 0;
343 return 0;
343 } else {
344 } else {
344 error = QString("Property '") + attributeName + "' of type '" +
345 error = QString("Property '") + attributeName + "' of type '" +
345 prop.typeName() + "' does not accept an object of type "
346 prop.typeName() + "' does not accept an object of type "
346 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
347 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
347 }
348 }
348 } else {
349 } else {
349 error = QString("Property '") + attributeName + "' of " + obj->ob_type->tp_name + " object is not writable";
350 error = QString("Property '") + attributeName + "' of " + obj->ob_type->tp_name + " object is not writable";
350 }
351 }
351 } else if (member._type == PythonQtMemberInfo::Slot) {
352 } else if (member._type == PythonQtMemberInfo::Slot) {
352 error = QString("Slot '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
353 error = QString("Slot '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
353 } else if (member._type == PythonQtMemberInfo::EnumValue) {
354 } else if (member._type == PythonQtMemberInfo::EnumValue) {
354 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
355 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
355 } else if (member._type == PythonQtMemberInfo::NotFound) {
356 } else if (member._type == PythonQtMemberInfo::NotFound) {
356 // if we are a derived python class, we allow setting attributes.
357 // if we are a derived python class, we allow setting attributes.
357 // if we are a direct CPP wrapper, we do NOT allow it, since
358 // if we are a direct CPP wrapper, we do NOT allow it, since
358 // it would be confusing to allow it because a wrapper will go away when it is not seen by python anymore
359 // it would be confusing to allow it because a wrapper will go away when it is not seen by python anymore
359 // and when it is recreated from a CPP pointer the attributes are gone...
360 // and when it is recreated from a CPP pointer the attributes are gone...
360 if (obj->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
361 if (obj->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
361 return PyBaseObject_Type.tp_setattro(obj,name,value);
362 return PyBaseObject_Type.tp_setattro(obj,name,value);
362 } else {
363 } else {
363 error = QString("'") + attributeName + "' does not exist on " + obj->ob_type->tp_name + " and creating new attributes on C++ objects is not allowed";
364 error = QString("'") + attributeName + "' does not exist on " + obj->ob_type->tp_name + " and creating new attributes on C++ objects is not allowed";
364 }
365 }
365 }
366 }
366
367
367 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
368 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
368 return -1;
369 return -1;
369 }
370 }
370
371
371 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
372 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
372 {
373 {
373 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
374 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
374 const char* typeName = obj->ob_type->tp_name;
375 const char* typeName = obj->ob_type->tp_name;
375 QObject *qobj = wrapper->_obj;
376 QObject *qobj = wrapper->_obj;
376 if (wrapper->_wrappedPtr) {
377 if (wrapper->_wrappedPtr) {
377 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
378 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
378 if (!str.isEmpty()) {
379 if (!str.isEmpty()) {
379 return PyString_FromFormat("%s", str.toLatin1().constData());
380 return PyString_FromFormat("%s", str.toLatin1().constData());
380 } else
381 } else
381 if (wrapper->_obj) {
382 if (wrapper->_obj) {
382 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
383 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
383 } else {
384 } else {
384 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
385 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
385 }
386 }
386 } else {
387 } else {
387 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
388 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
388 }
389 }
389 }
390 }
390
391
391 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
392 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
392 {
393 {
393 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
394 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
394 const char* typeName = obj->ob_type->tp_name;
395 const char* typeName = obj->ob_type->tp_name;
395
396
396 QObject *qobj = wrapper->_obj;
397 QObject *qobj = wrapper->_obj;
397 if (wrapper->_wrappedPtr) {
398 if (wrapper->_wrappedPtr) {
398 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
399 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
399 if (!str.isEmpty()) {
400 if (!str.isEmpty()) {
400 return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr);
401 return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr);
401 } else
402 } else
402 if (wrapper->_obj) {
403 if (wrapper->_obj) {
403 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
404 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
404 } else {
405 } else {
405 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
406 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
406 }
407 }
407 } else {
408 } else {
408 return PyString_FromFormat("%s (QObject %p)", typeName, wrapper->classInfo()->className(), qobj);
409 return PyString_FromFormat("%s (QObject %p)", typeName, wrapper->classInfo()->className(), qobj);
409 }
410 }
410 }
411 }
411
412
412 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
413 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
413 {
414 {
414 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
415 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
415 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
416 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
416
417
417 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
418 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
418 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
419 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
419 // check pointers directly first:
420 // check pointers directly first:
420 if (w1->_wrappedPtr != NULL) {
421 if (w1->_wrappedPtr != NULL) {
421 if (w1->_wrappedPtr == w2->_wrappedPtr) {
422 if (w1->_wrappedPtr == w2->_wrappedPtr) {
422 return 0;
423 return 0;
423 }
424 }
424 } else if (w1->_obj == w2->_obj) {
425 } else if (w1->_obj == w2->_obj) {
425 return 0;
426 return 0;
426 }
427 }
427 const char* class1 = w1->classInfo()->className();
428 const char* class1 = w1->classInfo()->className();
428 const char* class2 = w2->classInfo()->className();
429 const char* class2 = w2->classInfo()->className();
429 if (strcmp(class1, class2) == 0) {
430 if (strcmp(class1, class2) == 0) {
430 // same class names, so we can try the operator_equal
431 // same class names, so we can try the operator_equal
431 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
432 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
432 if (info._type == PythonQtMemberInfo::Slot) {
433 if (info._type == PythonQtMemberInfo::Slot) {
433 bool result = false;
434 bool result = false;
434 void* obj1 = w1->_wrappedPtr;
435 void* obj1 = w1->_wrappedPtr;
435 if (!obj1) {
436 if (!obj1) {
436 obj1 = w1->_obj;
437 obj1 = w1->_obj;
437 }
438 }
438 if (!obj1) { return -1; }
439 if (!obj1) { return -1; }
439 void* obj2 = w2->_wrappedPtr;
440 void* obj2 = w2->_wrappedPtr;
440 if (!obj2) {
441 if (!obj2) {
441 obj2 = w2->_obj;
442 obj2 = w2->_obj;
442 }
443 }
443 if (!obj2) { return -1; }
444 if (!obj2) { return -1; }
444 if (info._slot->isInstanceDecorator()) {
445 if (info._slot->isInstanceDecorator()) {
445 // call on decorator QObject
446 // call on decorator QObject
446 void* args[3];
447 void* args[3];
447 args[0] = &result;
448 args[0] = &result;
448 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
449 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
449 args[2] = obj2; // this is a reference, so it needs the direct pointer
450 args[2] = obj2; // this is a reference, so it needs the direct pointer
450 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
451 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
451 return result?0:-1;
452 return result?0:-1;
452 } else {
453 } else {
453 // call directly on QObject
454 // call directly on QObject
454 if (w1->_obj && w2->_obj) {
455 if (w1->_obj && w2->_obj) {
455 void* args[2];
456 void* args[2];
456 args[0] = &result;
457 args[0] = &result;
457 args[2] = obj2; // this is a reference, so it needs the direct pointer
458 args[2] = obj2; // this is a reference, so it needs the direct pointer
458 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
459 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
459 }
460 }
460 }
461 }
461 }
462 }
462 }
463 }
463 }
464 }
464 return -1;
465 return -1;
465 }
466 }
466
467
467 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
468 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
468 {
469 {
469 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
470 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
470 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
471 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
471 }
472 }
472
473
473
474
474 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
475 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
475 {
476 {
476 if (obj->_wrappedPtr != NULL) {
477 if (obj->_wrappedPtr != NULL) {
477 return reinterpret_cast<long>(obj->_wrappedPtr);
478 return reinterpret_cast<long>(obj->_wrappedPtr);
478 } else {
479 } else {
479 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
480 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
480 return reinterpret_cast<long>(qobj);
481 return reinterpret_cast<long>(qobj);
481 }
482 }
482 }
483 }
483
484
484
485
485
486
486 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
487 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
487 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
488 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
488 0, /* nb_add */
489 0, /* nb_add */
489 0, /* nb_subtract */
490 0, /* nb_subtract */
490 0, /* nb_multiply */
491 0, /* nb_multiply */
491 0, /* nb_divide */
492 0, /* nb_divide */
492 0, /* nb_remainder */
493 0, /* nb_remainder */
493 0, /* nb_divmod */
494 0, /* nb_divmod */
494 0, /* nb_power */
495 0, /* nb_power */
495 0, /* nb_negative */
496 0, /* nb_negative */
496 0, /* nb_positive */
497 0, /* nb_positive */
497 0, /* nb_absolute */
498 0, /* nb_absolute */
498 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
499 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
499 0, /* nb_invert */
500 0, /* nb_invert */
500 0, /* nb_lshift */
501 0, /* nb_lshift */
501 0, /* nb_rshift */
502 0, /* nb_rshift */
502 0, /* nb_and */
503 0, /* nb_and */
503 0, /* nb_xor */
504 0, /* nb_xor */
504 0, /* nb_or */
505 0, /* nb_or */
505 0, /* nb_coerce */
506 0, /* nb_coerce */
506 0, /* nb_int */
507 0, /* nb_int */
507 0, /* nb_long */
508 0, /* nb_long */
508 0, /* nb_float */
509 0, /* nb_float */
509 0, /* nb_oct */
510 0, /* nb_oct */
510 0, /* nb_hex */
511 0, /* nb_hex */
511 0, /* nb_inplace_add */
512 0, /* nb_inplace_add */
512 0, /* nb_inplace_subtract */
513 0, /* nb_inplace_subtract */
513 0, /* nb_inplace_multiply */
514 0, /* nb_inplace_multiply */
514 0, /* nb_inplace_divide */
515 0, /* nb_inplace_divide */
515 0, /* nb_inplace_remainder */
516 0, /* nb_inplace_remainder */
516 0, /* nb_inplace_power */
517 0, /* nb_inplace_power */
517 0, /* nb_inplace_lshift */
518 0, /* nb_inplace_lshift */
518 0, /* nb_inplace_rshift */
519 0, /* nb_inplace_rshift */
519 0, /* nb_inplace_and */
520 0, /* nb_inplace_and */
520 0, /* nb_inplace_xor */
521 0, /* nb_inplace_xor */
521 0, /* nb_inplace_or */
522 0, /* nb_inplace_or */
522 0, /* nb_floor_divide */
523 0, /* nb_floor_divide */
523 0, /* nb_true_divide */
524 0, /* nb_true_divide */
524 0, /* nb_inplace_floor_divide */
525 0, /* nb_inplace_floor_divide */
525 0, /* nb_inplace_true_divide */
526 0, /* nb_inplace_true_divide */
526 };
527 };
527
528
528 PyTypeObject PythonQtInstanceWrapper_Type = {
529 PyTypeObject PythonQtInstanceWrapper_Type = {
529 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
530 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
530 0, /*ob_size*/
531 0, /*ob_size*/
531 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
532 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
532 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
533 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
533 0, /*tp_itemsize*/
534 0, /*tp_itemsize*/
534 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
535 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
535 0, /*tp_print*/
536 0, /*tp_print*/
536 0, /*tp_getattr*/
537 0, /*tp_getattr*/
537 0, /*tp_setattr*/
538 0, /*tp_setattr*/
538 PythonQtInstanceWrapper_compare, /*tp_compare*/
539 PythonQtInstanceWrapper_compare, /*tp_compare*/
539 PythonQtInstanceWrapper_repr, /*tp_repr*/
540 PythonQtInstanceWrapper_repr, /*tp_repr*/
540 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
541 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
541 0, /*tp_as_sequence*/
542 0, /*tp_as_sequence*/
542 0, /*tp_as_mapping*/
543 0, /*tp_as_mapping*/
543 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
544 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
544 0, /*tp_call*/
545 0, /*tp_call*/
545 PythonQtInstanceWrapper_str, /*tp_str*/
546 PythonQtInstanceWrapper_str, /*tp_str*/
546 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
547 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
547 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
548 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
548 0, /*tp_as_buffer*/
549 0, /*tp_as_buffer*/
549 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
550 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
550 "PythonQtInstanceWrapper object", /* tp_doc */
551 "PythonQtInstanceWrapper object", /* tp_doc */
551 0, /* tp_traverse */
552 0, /* tp_traverse */
552 0, /* tp_clear */
553 0, /* tp_clear */
553 0, /* tp_richcompare */
554 0, /* tp_richcompare */
554 0, /* tp_weaklistoffset */
555 0, /* tp_weaklistoffset */
555 0, /* tp_iter */
556 0, /* tp_iter */
556 0, /* tp_iternext */
557 0, /* tp_iternext */
557 0, /* tp_methods */
558 0, /* tp_methods */
558 0, /* tp_members */
559 0, /* tp_members */
559 0, /* tp_getset */
560 0, /* tp_getset */
560 0, /* tp_base */
561 0, /* tp_base */
561 0, /* tp_dict */
562 0, /* tp_dict */
562 0, /* tp_descr_get */
563 0, /* tp_descr_get */
563 0, /* tp_descr_set */
564 0, /* tp_descr_set */
564 0, /* tp_dictoffset */
565 0, /* tp_dictoffset */
565 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
566 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
566 0, /* tp_alloc */
567 0, /* tp_alloc */
567 PythonQtInstanceWrapper_new, /* tp_new */
568 PythonQtInstanceWrapper_new, /* tp_new */
568 };
569 };
569
570
570 //-------------------------------------------------------
571 //-------------------------------------------------------
571
572
General Comments 0
You need to be logged in to leave comments. Login now