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