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