@@ -1,1241 +1,1212 | |||
|
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 PythonQt.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 "PythonQt.h" |
|
43 | 43 | #include "PythonQtImporter.h" |
|
44 | 44 | #include "PythonQtClassInfo.h" |
|
45 | 45 | #include "PythonQtMethodInfo.h" |
|
46 | 46 | #include "PythonQtSignalReceiver.h" |
|
47 | 47 | #include "PythonQtConversion.h" |
|
48 | 48 | #include "PythonQtStdOut.h" |
|
49 | 49 | #include "PythonQtCppWrapperFactory.h" |
|
50 | 50 | #include "PythonQtVariants.h" |
|
51 | 51 | #include "PythonQtStdDecorators.h" |
|
52 | 52 | #include "PythonQtQFileImporter.h" |
|
53 | 53 | #include <pydebug.h> |
|
54 | 54 | #include <vector> |
|
55 | 55 | |
|
56 | 56 | PythonQt* PythonQt::_self = NULL; |
|
57 | 57 | int PythonQt::_uniqueModuleCount = 0; |
|
58 | 58 | |
|
59 | void PythonQt_init_QtGuiBuiltin(); | |
|
60 | void PythonQt_init_QtCoreBuiltin(); | |
|
61 | ||
|
59 | 62 | void PythonQt::init(int flags) |
|
60 | 63 | { |
|
61 | 64 | if (!_self) { |
|
62 | 65 | _self = new PythonQt(flags); |
|
63 | 66 | } |
|
64 | 67 | |
|
65 | 68 | PythonQtMethodInfo::addParameterTypeAlias("QObjectList", "QList<QObject*>"); |
|
66 | 69 | qRegisterMetaType<QList<QObject*> >("QList<void*>"); |
|
67 | 70 | |
|
68 | 71 | PythonQtRegisterToolClassesTemplateConverter(int); |
|
69 | 72 | PythonQtRegisterToolClassesTemplateConverter(float); |
|
70 | 73 | PythonQtRegisterToolClassesTemplateConverter(double); |
|
71 | 74 | // TODO: which other POD types should be available for QList etc. |
|
72 | 75 | |
|
73 | 76 | PythonQt::self()->addDecorators(new PythonQtStdDecorators()); |
|
74 | 77 | |
|
75 | PythonQt::self()->registerCPPClass("Qt", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_Qt>); | |
|
76 | PythonQt::self()->registerCPPClass("QBitArray", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QBitArray>); | |
|
77 | PythonQt::self()->registerCPPClass("QDate", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QDate>); | |
|
78 | PythonQt::self()->registerCPPClass("QTime", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QTime>); | |
|
79 | PythonQt::self()->registerCPPClass("QDateTime", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QDateTime>); | |
|
80 | PythonQt::self()->registerCPPClass("QUrl", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QUrl>); | |
|
81 | PythonQt::self()->registerCPPClass("QLocale", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QLocale>); | |
|
82 | PythonQt::self()->registerCPPClass("QRect", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QRect>); | |
|
83 | PythonQt::self()->registerCPPClass("QRectF", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QRectF>); | |
|
84 | PythonQt::self()->registerCPPClass("QSize", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QSize>); | |
|
85 | PythonQt::self()->registerCPPClass("QSizeF", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QSizeF>); | |
|
86 | PythonQt::self()->registerCPPClass("QLine", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QLine>); | |
|
87 | PythonQt::self()->registerCPPClass("QLineF", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QLineF>); | |
|
88 | PythonQt::self()->registerCPPClass("QPoint", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QPoint>); | |
|
89 | PythonQt::self()->registerCPPClass("QPointF", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QPointF>); | |
|
90 | PythonQt::self()->registerCPPClass("QRegExp", "", "QtCore", PythonQtCreateObject<PythonQtWrapper_QRegExp>); | |
|
91 | ||
|
78 | PythonQt_init_QtCoreBuiltin(); | |
|
79 | PythonQt_init_QtGuiBuiltin(); | |
|
80 | ||
|
92 | 81 |
|
|
93 | 82 | PythonQtRegisterToolClassesTemplateConverter(QTime); |
|
94 | 83 | PythonQtRegisterToolClassesTemplateConverter(QDateTime); |
|
95 | 84 | PythonQtRegisterToolClassesTemplateConverter(QUrl); |
|
96 | 85 | PythonQtRegisterToolClassesTemplateConverter(QLocale); |
|
97 | 86 | PythonQtRegisterToolClassesTemplateConverter(QRect); |
|
98 | 87 | PythonQtRegisterToolClassesTemplateConverter(QRectF); |
|
99 | 88 | PythonQtRegisterToolClassesTemplateConverter(QSize); |
|
100 | 89 | PythonQtRegisterToolClassesTemplateConverter(QSizeF); |
|
101 | 90 | PythonQtRegisterToolClassesTemplateConverter(QLine); |
|
102 | 91 | PythonQtRegisterToolClassesTemplateConverter(QLineF); |
|
103 | 92 | PythonQtRegisterToolClassesTemplateConverter(QPoint); |
|
104 | 93 | PythonQtRegisterToolClassesTemplateConverter(QPointF); |
|
105 | 94 | PythonQtRegisterToolClassesTemplateConverter(QRegExp); |
|
106 | 95 | |
|
107 | PythonQt::self()->registerCPPClass("QFont", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QFont>); | |
|
108 | PythonQt::self()->registerCPPClass("QPixmap", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QPixmap>); | |
|
109 | PythonQt::self()->registerCPPClass("QBrush", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QBrush>); | |
|
110 | PythonQt::self()->registerCPPClass("QColor", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QColor>); | |
|
111 | PythonQt::self()->registerCPPClass("QPalette", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QPalette>); | |
|
112 | PythonQt::self()->registerCPPClass("QIcon", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QIcon>); | |
|
113 | PythonQt::self()->registerCPPClass("QImage", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QImage>); | |
|
114 | PythonQt::self()->registerCPPClass("QPolygon", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QPolygon>); | |
|
115 | PythonQt::self()->registerCPPClass("QRegion", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QRegion>); | |
|
116 | PythonQt::self()->registerCPPClass("QBitmap", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QBitmap>); | |
|
117 | PythonQt::self()->registerCPPClass("QCursor", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QCursor>); | |
|
118 | PythonQt::self()->registerCPPClass("QSizePolicy", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QSizePolicy>); | |
|
119 | PythonQt::self()->registerCPPClass("QKeySequence", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QKeySequence>); | |
|
120 | PythonQt::self()->registerCPPClass("QPen", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QPen>); | |
|
121 | PythonQt::self()->registerCPPClass("QTextLength", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QTextLength>); | |
|
122 | PythonQt::self()->registerCPPClass("QTextFormat", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QTextFormat>); | |
|
123 | PythonQt::self()->registerCPPClass("QMatrix", "", "QtGui", PythonQtCreateObject<PythonQtWrapper_QMatrix>); | |
|
124 | ||
|
125 | 96 | PythonQtRegisterToolClassesTemplateConverter(QFont); |
|
126 | 97 | PythonQtRegisterToolClassesTemplateConverter(QPixmap); |
|
127 | 98 | PythonQtRegisterToolClassesTemplateConverter(QBrush); |
|
128 | 99 | PythonQtRegisterToolClassesTemplateConverter(QColor); |
|
129 | 100 | PythonQtRegisterToolClassesTemplateConverter(QPalette); |
|
130 | 101 | PythonQtRegisterToolClassesTemplateConverter(QIcon); |
|
131 | 102 | PythonQtRegisterToolClassesTemplateConverter(QImage); |
|
132 | 103 | PythonQtRegisterToolClassesTemplateConverter(QPolygon); |
|
133 | 104 | PythonQtRegisterToolClassesTemplateConverter(QRegion); |
|
134 | 105 | PythonQtRegisterToolClassesTemplateConverter(QBitmap); |
|
135 | 106 | PythonQtRegisterToolClassesTemplateConverter(QCursor); |
|
136 | 107 | PythonQtRegisterToolClassesTemplateConverter(QSizePolicy); |
|
137 | 108 | PythonQtRegisterToolClassesTemplateConverter(QKeySequence); |
|
138 | 109 | PythonQtRegisterToolClassesTemplateConverter(QPen); |
|
139 | 110 | PythonQtRegisterToolClassesTemplateConverter(QTextLength); |
|
140 | 111 | PythonQtRegisterToolClassesTemplateConverter(QTextFormat); |
|
141 | 112 | PythonQtRegisterToolClassesTemplateConverter(QMatrix); |
|
142 | 113 | |
|
143 | 114 | |
|
144 | 115 | PyObject* pack = PythonQt::priv()->packageByName("QtCore"); |
|
145 | 116 | PyObject* pack2 = PythonQt::priv()->packageByName("Qt"); |
|
146 | 117 | PyObject* qtNamespace = PythonQt::priv()->getClassInfo("Qt")->pythonQtClassWrapper(); |
|
147 | 118 | const char* names[16] = {"SIGNAL", "SLOT", "qAbs", "qBound","qDebug","qWarning","qCritical","qFatal" |
|
148 | 119 | ,"qFuzzyCompare", "qMax","qMin","qRound","qRound64","qVersion","qrand","qsrand"}; |
|
149 | 120 | for (unsigned int i = 0;i<16; i++) { |
|
150 | 121 | PyObject* obj = PyObject_GetAttrString(qtNamespace, names[i]); |
|
151 | 122 | if (obj) { |
|
152 | 123 | PyModule_AddObject(pack, names[i], obj); |
|
153 | 124 | Py_INCREF(obj); |
|
154 | 125 | PyModule_AddObject(pack2, names[i], obj); |
|
155 | 126 | } else { |
|
156 | 127 | std::cerr << "method not found " << names[i]; |
|
157 | 128 | } |
|
158 | 129 | } |
|
159 | 130 | } |
|
160 | 131 | |
|
161 | 132 | void PythonQt::cleanup() |
|
162 | 133 | { |
|
163 | 134 | if (_self) { |
|
164 | 135 | delete _self; |
|
165 | 136 | _self = NULL; |
|
166 | 137 | } |
|
167 | 138 | } |
|
168 | 139 | |
|
169 | 140 | PythonQt::PythonQt(int flags) |
|
170 | 141 | { |
|
171 | 142 | _p = new PythonQtPrivate; |
|
172 | 143 | _p->_initFlags = flags; |
|
173 | 144 | |
|
174 | 145 | _p->_PythonQtObjectPtr_metaId = qRegisterMetaType<PythonQtObjectPtr>("PythonQtObjectPtr"); |
|
175 | 146 | |
|
176 | 147 | Py_SetProgramName("PythonQt"); |
|
177 | 148 | if (flags & IgnoreSiteModule) { |
|
178 | 149 | // this prevents the automatic importing of Python site files |
|
179 | 150 | Py_NoSiteFlag = 1; |
|
180 | 151 | } |
|
181 | 152 | Py_Initialize(); |
|
182 | 153 | |
|
183 | 154 | // add our own python object types for qt object slots |
|
184 | 155 | if (PyType_Ready(&PythonQtSlotFunction_Type) < 0) { |
|
185 | 156 | std::cerr << "could not initialize PythonQtSlotFunction_Type" << ", in " << __FILE__ << ":" << __LINE__ << std::endl; |
|
186 | 157 | } |
|
187 | 158 | Py_INCREF(&PythonQtSlotFunction_Type); |
|
188 | 159 | |
|
189 | 160 | // according to Python docs, set the type late here, since it can not safely be stored in the struct when declaring it |
|
190 | 161 | PythonQtClassWrapper_Type.tp_base = &PyType_Type; |
|
191 | 162 | // add our own python object types for classes |
|
192 | 163 | if (PyType_Ready(&PythonQtClassWrapper_Type) < 0) { |
|
193 | 164 | std::cerr << "could not initialize PythonQtClassWrapper_Type" << ", in " << __FILE__ << ":" << __LINE__ << std::endl; |
|
194 | 165 | } |
|
195 | 166 | Py_INCREF(&PythonQtClassWrapper_Type); |
|
196 | 167 | |
|
197 | 168 | // add our own python object types for CPP instances |
|
198 | 169 | if (PyType_Ready(&PythonQtInstanceWrapper_Type) < 0) { |
|
199 | 170 | PythonQt::handleError(); |
|
200 | 171 | std::cerr << "could not initialize PythonQtInstanceWrapper_Type" << ", in " << __FILE__ << ":" << __LINE__ << std::endl; |
|
201 | 172 | } |
|
202 | 173 | Py_INCREF(&PythonQtInstanceWrapper_Type); |
|
203 | 174 | |
|
204 | 175 | // add our own python object types for redirection of stdout |
|
205 | 176 | if (PyType_Ready(&PythonQtStdOutRedirectType) < 0) { |
|
206 | 177 | std::cerr << "could not initialize PythonQtStdOutRedirectType" << ", in " << __FILE__ << ":" << __LINE__ << std::endl; |
|
207 | 178 | } |
|
208 | 179 | Py_INCREF(&PythonQtStdOutRedirectType); |
|
209 | 180 | |
|
210 | 181 | initPythonQtModule(flags & RedirectStdOut); |
|
211 | 182 | |
|
212 | 183 | _p->setupSharedLibrarySuffixes(); |
|
213 | 184 | |
|
214 | 185 | } |
|
215 | 186 | |
|
216 | 187 | PythonQt::~PythonQt() { |
|
217 | 188 | delete _p; |
|
218 | 189 | _p = NULL; |
|
219 | 190 | } |
|
220 | 191 | |
|
221 | 192 | PythonQtPrivate::~PythonQtPrivate() { |
|
222 | 193 | delete _defaultImporter; |
|
223 | 194 | _defaultImporter = NULL; |
|
224 | 195 | |
|
225 | 196 | { |
|
226 | 197 | QHashIterator<QByteArray, PythonQtClassInfo *> i(_knownClassInfos); |
|
227 | 198 | while (i.hasNext()) { |
|
228 | 199 | delete i.next().value(); |
|
229 | 200 | } |
|
230 | 201 | } |
|
231 | 202 | PythonQtConv::global_valueStorage.clear(); |
|
232 | 203 | PythonQtConv::global_ptrStorage.clear(); |
|
233 | 204 | PythonQtConv::global_variantStorage.clear(); |
|
234 | 205 | |
|
235 | 206 | PythonQtMethodInfo::cleanupCachedMethodInfos(); |
|
236 | 207 | } |
|
237 | 208 | |
|
238 | 209 | PythonQtImportFileInterface* PythonQt::importInterface() |
|
239 | 210 | { |
|
240 | 211 | return _self->_p->_importInterface?_self->_p->_importInterface:_self->_p->_defaultImporter; |
|
241 | 212 | } |
|
242 | 213 | |
|
243 | 214 | void PythonQt::qObjectNoLongerWrappedCB(QObject* o) |
|
244 | 215 | { |
|
245 | 216 | if (_self->_p->_noLongerWrappedCB) { |
|
246 | 217 | (*_self->_p->_noLongerWrappedCB)(o); |
|
247 | 218 | }; |
|
248 | 219 | } |
|
249 | 220 | |
|
250 | 221 | void PythonQt::registerClass(const QMetaObject* metaobject, const char* package, PythonQtQObjectCreatorFunctionCB* wrapperCreator, PythonQtShellSetInstanceWrapperCB* shell) |
|
251 | 222 | { |
|
252 | 223 | _p->registerClass(metaobject, package, wrapperCreator, shell); |
|
253 | 224 | } |
|
254 | 225 | |
|
255 | 226 | void PythonQtPrivate::registerClass(const QMetaObject* metaobject, const char* package, PythonQtQObjectCreatorFunctionCB* wrapperCreator, PythonQtShellSetInstanceWrapperCB* shell) |
|
256 | 227 | { |
|
257 | 228 | // we register all classes in the hierarchy |
|
258 | 229 | const QMetaObject* m = metaobject; |
|
259 | 230 | bool first = true; |
|
260 | 231 | while (m) { |
|
261 | 232 | PythonQtClassInfo* info = lookupClassInfoAndCreateIfNotPresent(m->className()); |
|
262 | 233 | if (!info->pythonQtClassWrapper()) { |
|
263 | 234 | info->setupQObject(m); |
|
264 | 235 | createPythonQtClassWrapper(info, package); |
|
265 | 236 | if (m->superClass()) { |
|
266 | 237 | PythonQtClassInfo* parentInfo = lookupClassInfoAndCreateIfNotPresent(m->superClass()->className()); |
|
267 | 238 | info->addParentClass(PythonQtClassInfo::ParentClassInfo(parentInfo)); |
|
268 | 239 | } |
|
269 | 240 | } |
|
270 | 241 | if (first) { |
|
271 | 242 | first = false; |
|
272 | 243 | if (wrapperCreator) { |
|
273 | 244 | info->setDecoratorProvider(wrapperCreator); |
|
274 | 245 | } |
|
275 | 246 | if (shell) { |
|
276 | 247 | info->setShellSetInstanceWrapperCB(shell); |
|
277 | 248 | } |
|
278 | 249 | } |
|
279 | 250 | m = m->superClass(); |
|
280 | 251 | } |
|
281 | 252 | } |
|
282 | 253 | |
|
283 | 254 | void PythonQtPrivate::createPythonQtClassWrapper(PythonQtClassInfo* info, const char* package) |
|
284 | 255 | { |
|
285 | 256 | PyObject* pack = packageByName(package); |
|
286 | 257 | PyObject* pyobj = (PyObject*)createNewPythonQtClassWrapper(info, package); |
|
287 | 258 | PyModule_AddObject(pack, info->className(), pyobj); |
|
288 | 259 | if (package && strncmp(package,"Qt",2)==0) { |
|
289 | 260 | // since PyModule_AddObject steals the reference, we need a incref once more... |
|
290 | 261 | Py_INCREF(pyobj); |
|
291 | 262 | // put all qt objects into Qt as well |
|
292 | 263 | PyModule_AddObject(packageByName("Qt"), info->className(), pyobj); |
|
293 | 264 | } |
|
294 | 265 | info->setPythonQtClassWrapper(pyobj); |
|
295 | 266 | } |
|
296 | 267 | |
|
297 | 268 | PyObject* PythonQtPrivate::wrapQObject(QObject* obj) |
|
298 | 269 | { |
|
299 | 270 | if (!obj) { |
|
300 | 271 | Py_INCREF(Py_None); |
|
301 | 272 | return Py_None; |
|
302 | 273 | } |
|
303 | 274 | PythonQtInstanceWrapper* wrap = findWrapperAndRemoveUnused(obj); |
|
304 | 275 | if (!wrap) { |
|
305 | 276 | // smuggling it in... |
|
306 | 277 | PythonQtClassInfo* classInfo = _knownClassInfos.value(obj->metaObject()->className()); |
|
307 | 278 | if (!classInfo || classInfo->pythonQtClassWrapper()==NULL) { |
|
308 | 279 | registerClass(obj->metaObject()); |
|
309 | 280 | classInfo = _knownClassInfos.value(obj->metaObject()->className()); |
|
310 | 281 | } |
|
311 | 282 | wrap = createNewPythonQtInstanceWrapper(obj, classInfo); |
|
312 | 283 | // mlabDebugConst("MLABPython","new qobject wrapper added " << " " << wrap->_obj->className() << " " << wrap->classInfo()->wrappedClassName().latin1()); |
|
313 | 284 | } else { |
|
314 | 285 | Py_INCREF(wrap); |
|
315 | 286 | // mlabDebugConst("MLABPython","qobject wrapper reused " << wrap->_obj->className() << " " << wrap->classInfo()->wrappedClassName().latin1()); |
|
316 | 287 | } |
|
317 | 288 | return (PyObject*)wrap; |
|
318 | 289 | } |
|
319 | 290 | |
|
320 | 291 | PyObject* PythonQtPrivate::wrapPtr(void* ptr, const QByteArray& name) |
|
321 | 292 | { |
|
322 | 293 | if (!ptr) { |
|
323 | 294 | Py_INCREF(Py_None); |
|
324 | 295 | return Py_None; |
|
325 | 296 | } |
|
326 | 297 | |
|
327 | 298 | PythonQtInstanceWrapper* wrap = findWrapperAndRemoveUnused(ptr); |
|
328 | 299 | if (!wrap) { |
|
329 | 300 | PythonQtClassInfo* info = _knownClassInfos.value(name); |
|
330 | 301 | if (!info) { |
|
331 | 302 | // maybe it is a PyObject, which we can return directly |
|
332 | 303 | if (name == "PyObject") { |
|
333 | 304 | PyObject* p = (PyObject*)ptr; |
|
334 | 305 | Py_INCREF(p); |
|
335 | 306 | return p; |
|
336 | 307 | } |
|
337 | 308 | |
|
338 | 309 | // we do not know the metaobject yet, but we might know it by it's name: |
|
339 | 310 | if (_knownQObjectClassNames.find(name)!=_knownQObjectClassNames.end()) { |
|
340 | 311 | // yes, we know it, so we can convert to QObject |
|
341 | 312 | QObject* qptr = (QObject*)ptr; |
|
342 | 313 | registerClass(qptr->metaObject()); |
|
343 | 314 | info = _knownClassInfos.value(qptr->metaObject()->className()); |
|
344 | 315 | } |
|
345 | 316 | } |
|
346 | 317 | if (info && info->isQObject()) { |
|
347 | 318 | QObject* qptr = (QObject*)ptr; |
|
348 | 319 | // if the object is a derived object, we want to switch the class info to the one of the derived class: |
|
349 | 320 | if (name!=(qptr->metaObject()->className())) { |
|
350 | 321 | registerClass(qptr->metaObject()); |
|
351 | 322 | info = _knownClassInfos.value(qptr->metaObject()->className()); |
|
352 | 323 | } |
|
353 | 324 | wrap = createNewPythonQtInstanceWrapper(qptr, info); |
|
354 | 325 | // mlabDebugConst("MLABPython","new qobject wrapper added " << " " << wrap->_obj->className() << " " << wrap->classInfo()->wrappedClassName().latin1()); |
|
355 | 326 | return (PyObject*)wrap; |
|
356 | 327 | } |
|
357 | 328 | |
|
358 | 329 | // not a known QObject, so try our wrapper factory: |
|
359 | 330 | QObject* wrapper = NULL; |
|
360 | 331 | for (int i=0; i<_cppWrapperFactories.size(); i++) { |
|
361 | 332 | wrapper = _cppWrapperFactories.at(i)->create(name, ptr); |
|
362 | 333 | if (wrapper) { |
|
363 | 334 | break; |
|
364 | 335 | } |
|
365 | 336 | } |
|
366 | 337 | |
|
367 | 338 | if (info) { |
|
368 | 339 | // try to downcast in the class hierarchy, which will modify info and ptr if it is successfull |
|
369 | 340 | ptr = info->castDownIfPossible(ptr, &info); |
|
370 | 341 | } |
|
371 | 342 | |
|
372 | 343 | if (!info || info->pythonQtClassWrapper()==NULL) { |
|
373 | 344 | // still unknown, register as CPP class |
|
374 | 345 | registerCPPClass(name.constData()); |
|
375 | 346 | info = _knownClassInfos.value(name); |
|
376 | 347 | } |
|
377 | 348 | if (wrapper && (info->metaObject() != wrapper->metaObject())) { |
|
378 | 349 | // if we a have a QObject wrapper and the metaobjects do not match, set the metaobject again! |
|
379 | 350 | info->setMetaObject(wrapper->metaObject()); |
|
380 | 351 | } |
|
381 | 352 | wrap = createNewPythonQtInstanceWrapper(wrapper, info, ptr); |
|
382 | 353 | // mlabDebugConst("MLABPython","new c++ wrapper added " << wrap->_wrappedPtr << " " << wrap->_obj->className() << " " << wrap->classInfo()->wrappedClassName().latin1()); |
|
383 | 354 | } else { |
|
384 | 355 | Py_INCREF(wrap); |
|
385 | 356 | //mlabDebugConst("MLABPython","c++ wrapper reused " << wrap->_wrappedPtr << " " << wrap->_obj->className() << " " << wrap->classInfo()->wrappedClassName().latin1()); |
|
386 | 357 | } |
|
387 | 358 | return (PyObject*)wrap; |
|
388 | 359 | } |
|
389 | 360 | |
|
390 | 361 | PyObject* PythonQtPrivate::dummyTuple() { |
|
391 | 362 | static PyObject* dummyTuple = NULL; |
|
392 | 363 | if (dummyTuple==NULL) { |
|
393 | 364 | dummyTuple = PyTuple_New(1); |
|
394 | 365 | PyTuple_SET_ITEM(dummyTuple, 0 , PyString_FromString("dummy")); |
|
395 | 366 | } |
|
396 | 367 | return dummyTuple; |
|
397 | 368 | } |
|
398 | 369 | |
|
399 | 370 | |
|
400 | 371 | PythonQtInstanceWrapper* PythonQtPrivate::createNewPythonQtInstanceWrapper(QObject* obj, PythonQtClassInfo* info, void* wrappedPtr) { |
|
401 | 372 | // call the associated class type to create a new instance... |
|
402 | 373 | PythonQtInstanceWrapper* result = (PythonQtInstanceWrapper*)PyObject_Call(info->pythonQtClassWrapper(), dummyTuple(), NULL); |
|
403 | 374 | |
|
404 | 375 | result->setQObject(obj); |
|
405 | 376 | result->_wrappedPtr = wrappedPtr; |
|
406 | 377 | result->_ownedByPythonQt = false; |
|
407 | 378 | result->_useQMetaTypeDestroy = false; |
|
408 | 379 | |
|
409 | 380 | if (wrappedPtr) { |
|
410 | 381 | _wrappedObjects.insert(wrappedPtr, result); |
|
411 | 382 | } else { |
|
412 | 383 | _wrappedObjects.insert(obj, result); |
|
413 | 384 | if (obj->parent()== NULL && _wrappedCB) { |
|
414 | 385 | // tell someone who is interested that the qobject is wrapped the first time, if it has no parent |
|
415 | 386 | (*_wrappedCB)(obj); |
|
416 | 387 | } |
|
417 | 388 | } |
|
418 | 389 | return result; |
|
419 | 390 | } |
|
420 | 391 | |
|
421 | 392 | PythonQtClassWrapper* PythonQtPrivate::createNewPythonQtClassWrapper(PythonQtClassInfo* info, const char* package) { |
|
422 | 393 | PythonQtClassWrapper* result; |
|
423 | 394 | |
|
424 | 395 | PyObject* className = PyString_FromString(info->className()); |
|
425 | 396 | |
|
426 | 397 | PyObject* baseClasses = PyTuple_New(1); |
|
427 | 398 | PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PythonQtInstanceWrapper_Type); |
|
428 | 399 | |
|
429 | 400 | PyObject* typeDict = PyDict_New(); |
|
430 | 401 | QByteArray moduleName("PythonQt"); |
|
431 | 402 | if (package && strcmp(package, "")!=0) { |
|
432 | 403 | moduleName += "."; |
|
433 | 404 | moduleName += package; |
|
434 | 405 | } |
|
435 | 406 | PyDict_SetItemString(typeDict, "__module__", PyString_FromString(moduleName.constData())); |
|
436 | 407 | |
|
437 | 408 | PyObject* args = Py_BuildValue("OOO", className, baseClasses, typeDict); |
|
438 | 409 | |
|
439 | 410 | // set the class info so that PythonQtClassWrapper_new can read it |
|
440 | 411 | _currentClassInfoForClassWrapperCreation = info; |
|
441 | 412 | // create the new type object by calling the type |
|
442 | 413 | result = (PythonQtClassWrapper *)PyObject_Call((PyObject *)&PythonQtClassWrapper_Type, args, NULL); |
|
443 | 414 | |
|
444 | 415 | Py_DECREF(baseClasses); |
|
445 | 416 | Py_DECREF(typeDict); |
|
446 | 417 | Py_DECREF(args); |
|
447 | 418 | Py_DECREF(className); |
|
448 | 419 | |
|
449 | 420 | return result; |
|
450 | 421 | } |
|
451 | 422 | |
|
452 | 423 | PyObject* PythonQtPrivate::createEnumValueInstance(PyObject* enumType, unsigned int enumValue) |
|
453 | 424 | { |
|
454 | 425 | PyObject* args = Py_BuildValue("(i)", enumValue); |
|
455 | 426 | PyObject* result = PyObject_Call(enumType, args, NULL); |
|
456 | 427 | Py_DECREF(args); |
|
457 | 428 | return result; |
|
458 | 429 | } |
|
459 | 430 | |
|
460 | 431 | PyObject* PythonQtPrivate::createNewPythonQtEnumWrapper(const char* enumName, PyObject* parentObject) { |
|
461 | 432 | PyObject* result; |
|
462 | 433 | |
|
463 | 434 | PyObject* className = PyString_FromString(enumName); |
|
464 | 435 | |
|
465 | 436 | PyObject* baseClasses = PyTuple_New(1); |
|
466 | 437 | PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PyInt_Type); |
|
467 | 438 | |
|
468 | 439 | PyObject* module = PyObject_GetAttrString(parentObject, "__module__"); |
|
469 | 440 | PyObject* typeDict = PyDict_New(); |
|
470 | 441 | PyDict_SetItemString(typeDict, "__module__", module); |
|
471 | 442 | |
|
472 | 443 | PyObject* args = Py_BuildValue("OOO", className, baseClasses, typeDict); |
|
473 | 444 | |
|
474 | 445 | // create the new int derived type object by calling the core type |
|
475 | 446 | result = PyObject_Call((PyObject *)&PyType_Type, args, NULL); |
|
476 | 447 | |
|
477 | 448 | Py_DECREF(baseClasses); |
|
478 | 449 | Py_DECREF(typeDict); |
|
479 | 450 | Py_DECREF(args); |
|
480 | 451 | Py_DECREF(className); |
|
481 | 452 | |
|
482 | 453 | return result; |
|
483 | 454 | } |
|
484 | 455 | |
|
485 | 456 | PythonQtSignalReceiver* PythonQt::getSignalReceiver(QObject* obj) |
|
486 | 457 | { |
|
487 | 458 | PythonQtSignalReceiver* r = _p->_signalReceivers[obj]; |
|
488 | 459 | if (!r) { |
|
489 | 460 | r = new PythonQtSignalReceiver(obj); |
|
490 | 461 | _p->_signalReceivers.insert(obj, r); |
|
491 | 462 | } |
|
492 | 463 | return r; |
|
493 | 464 | } |
|
494 | 465 | |
|
495 | 466 | bool PythonQt::addSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname) |
|
496 | 467 | { |
|
497 | 468 | bool flag = false; |
|
498 | 469 | PythonQtObjectPtr callable = lookupCallable(module, objectname); |
|
499 | 470 | if (callable) { |
|
500 | 471 | PythonQtSignalReceiver* r = getSignalReceiver(obj); |
|
501 | 472 | flag = r->addSignalHandler(signal, callable); |
|
502 | 473 | if (!flag) { |
|
503 | 474 | // signal not found |
|
504 | 475 | } |
|
505 | 476 | } else { |
|
506 | 477 | // callable not found |
|
507 | 478 | } |
|
508 | 479 | return flag; |
|
509 | 480 | } |
|
510 | 481 | |
|
511 | 482 | bool PythonQt::addSignalHandler(QObject* obj, const char* signal, PyObject* receiver) |
|
512 | 483 | { |
|
513 | 484 | bool flag = false; |
|
514 | 485 | PythonQtSignalReceiver* r = getSignalReceiver(obj); |
|
515 | 486 | if (r) { |
|
516 | 487 | flag = r->addSignalHandler(signal, receiver); |
|
517 | 488 | } |
|
518 | 489 | return flag; |
|
519 | 490 | } |
|
520 | 491 | |
|
521 | 492 | bool PythonQt::removeSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname) |
|
522 | 493 | { |
|
523 | 494 | bool flag = false; |
|
524 | 495 | PythonQtObjectPtr callable = lookupCallable(module, objectname); |
|
525 | 496 | if (callable) { |
|
526 | 497 | PythonQtSignalReceiver* r = _p->_signalReceivers[obj]; |
|
527 | 498 | if (r) { |
|
528 | 499 | flag = r->removeSignalHandler(signal, callable); |
|
529 | 500 | } |
|
530 | 501 | } else { |
|
531 | 502 | // callable not found |
|
532 | 503 | } |
|
533 | 504 | return flag; |
|
534 | 505 | } |
|
535 | 506 | |
|
536 | 507 | bool PythonQt::removeSignalHandler(QObject* obj, const char* signal, PyObject* receiver) |
|
537 | 508 | { |
|
538 | 509 | bool flag = false; |
|
539 | 510 | PythonQtSignalReceiver* r = _p->_signalReceivers[obj]; |
|
540 | 511 | if (r) { |
|
541 | 512 | flag = r->removeSignalHandler(signal, receiver); |
|
542 | 513 | } |
|
543 | 514 | return flag; |
|
544 | 515 | } |
|
545 | 516 | |
|
546 | 517 | PythonQtObjectPtr PythonQt::lookupCallable(PyObject* module, const QString& name) |
|
547 | 518 | { |
|
548 | 519 | PythonQtObjectPtr p = lookupObject(module, name); |
|
549 | 520 | if (p) { |
|
550 | 521 | if (PyCallable_Check(p)) { |
|
551 | 522 | return p; |
|
552 | 523 | } |
|
553 | 524 | } |
|
554 | 525 | PyErr_Clear(); |
|
555 | 526 | return NULL; |
|
556 | 527 | } |
|
557 | 528 | |
|
558 | 529 | PythonQtObjectPtr PythonQt::lookupObject(PyObject* module, const QString& name) |
|
559 | 530 | { |
|
560 | 531 | QStringList l = name.split('.'); |
|
561 | 532 | PythonQtObjectPtr p = module; |
|
562 | 533 | PythonQtObjectPtr prev; |
|
563 | 534 | QString s; |
|
564 | 535 | QByteArray b; |
|
565 | 536 | for (QStringList::ConstIterator i = l.begin(); i!=l.end() && p; ++i) { |
|
566 | 537 | prev = p; |
|
567 | 538 | b = (*i).toLatin1(); |
|
568 | 539 | if (PyDict_Check(p)) { |
|
569 | 540 | p = PyDict_GetItemString(p, b.data()); |
|
570 | 541 | } else { |
|
571 | 542 | p.setNewRef(PyObject_GetAttrString(p, b.data())); |
|
572 | 543 | } |
|
573 | 544 | } |
|
574 | 545 | PyErr_Clear(); |
|
575 | 546 | return p; |
|
576 | 547 | } |
|
577 | 548 | |
|
578 | 549 | PythonQtObjectPtr PythonQt::getMainModule() { |
|
579 | 550 | //both borrowed |
|
580 | 551 | PythonQtObjectPtr dict = PyImport_GetModuleDict(); |
|
581 | 552 | return PyDict_GetItemString(dict, "__main__"); |
|
582 | 553 | } |
|
583 | 554 | |
|
584 | 555 | QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) { |
|
585 | 556 | QVariant result; |
|
586 | 557 | if (pycode) { |
|
587 | 558 | PyObject* dict = NULL; |
|
588 | 559 | if (PyModule_Check(object)) { |
|
589 | 560 | dict = PyModule_GetDict(object); |
|
590 | 561 | } else if (PyDict_Check(object)) { |
|
591 | 562 | dict = object; |
|
592 | 563 | } |
|
593 | 564 | PyObject* r = NULL; |
|
594 | 565 | if (dict) { |
|
595 | 566 | r = PyEval_EvalCode((PyCodeObject*)pycode, dict , dict); |
|
596 | 567 | } |
|
597 | 568 | if (r) { |
|
598 | 569 | result = PythonQtConv::PyObjToQVariant(r); |
|
599 | 570 | Py_DECREF(r); |
|
600 | 571 | } else { |
|
601 | 572 | handleError(); |
|
602 | 573 | } |
|
603 | 574 | } else { |
|
604 | 575 | handleError(); |
|
605 | 576 | } |
|
606 | 577 | return result; |
|
607 | 578 | } |
|
608 | 579 | |
|
609 | 580 | QVariant PythonQt::evalScript(PyObject* object, const QString& script, int start) |
|
610 | 581 | { |
|
611 | 582 | QVariant result; |
|
612 | 583 | PythonQtObjectPtr p; |
|
613 | 584 | PyObject* dict = NULL; |
|
614 | 585 | if (PyModule_Check(object)) { |
|
615 | 586 | dict = PyModule_GetDict(object); |
|
616 | 587 | } else if (PyDict_Check(object)) { |
|
617 | 588 | dict = object; |
|
618 | 589 | } |
|
619 | 590 | if (dict) { |
|
620 | 591 | p.setNewRef(PyRun_String(script.toLatin1().data(), start, dict, dict)); |
|
621 | 592 | } |
|
622 | 593 | if (p) { |
|
623 | 594 | result = PythonQtConv::PyObjToQVariant(p); |
|
624 | 595 | } else { |
|
625 | 596 | handleError(); |
|
626 | 597 | } |
|
627 | 598 | return result; |
|
628 | 599 | } |
|
629 | 600 | |
|
630 | 601 | void PythonQt::evalFile(PyObject* module, const QString& filename) |
|
631 | 602 | { |
|
632 | 603 | PythonQtObjectPtr code = parseFile(filename); |
|
633 | 604 | if (code) { |
|
634 | 605 | evalCode(module, code); |
|
635 | 606 | } else { |
|
636 | 607 | handleError(); |
|
637 | 608 | } |
|
638 | 609 | } |
|
639 | 610 | |
|
640 | 611 | PythonQtObjectPtr PythonQt::parseFile(const QString& filename) |
|
641 | 612 | { |
|
642 | 613 | PythonQtObjectPtr p; |
|
643 | 614 | p.setNewRef(PythonQtImport::getCodeFromPyc(filename)); |
|
644 | 615 | if (!p) { |
|
645 | 616 | handleError(); |
|
646 | 617 | } |
|
647 | 618 | return p; |
|
648 | 619 | } |
|
649 | 620 | |
|
650 | 621 | PythonQtObjectPtr PythonQt::createModuleFromFile(const QString& name, const QString& filename) |
|
651 | 622 | { |
|
652 | 623 | PythonQtObjectPtr code = parseFile(filename); |
|
653 | 624 | PythonQtObjectPtr module = _p->createModule(name, code); |
|
654 | 625 | return module; |
|
655 | 626 | } |
|
656 | 627 | |
|
657 | 628 | PythonQtObjectPtr PythonQt::createModuleFromScript(const QString& name, const QString& script) |
|
658 | 629 | { |
|
659 | 630 | PyErr_Clear(); |
|
660 | 631 | QString scriptCode = script; |
|
661 | 632 | if (scriptCode.isEmpty()) { |
|
662 | 633 | // we always need at least a linefeed |
|
663 | 634 | scriptCode = "\n"; |
|
664 | 635 | } |
|
665 | 636 | PythonQtObjectPtr pycode; |
|
666 | 637 | pycode.setNewRef(Py_CompileString((char*)scriptCode.toLatin1().data(), "", Py_file_input)); |
|
667 | 638 | PythonQtObjectPtr module = _p->createModule(name, pycode); |
|
668 | 639 | return module; |
|
669 | 640 | } |
|
670 | 641 | |
|
671 | 642 | PythonQtObjectPtr PythonQt::createUniqueModule() |
|
672 | 643 | { |
|
673 | 644 | static QString pyQtStr("PythonQt_module"); |
|
674 | 645 | QString moduleName = pyQtStr+QString::number(_uniqueModuleCount++); |
|
675 | 646 | return createModuleFromScript(moduleName); |
|
676 | 647 | } |
|
677 | 648 | |
|
678 | 649 | void PythonQt::addObject(PyObject* object, const QString& name, QObject* qObject) |
|
679 | 650 | { |
|
680 | 651 | if (PyModule_Check(object)) { |
|
681 | 652 | PyModule_AddObject(object, name.toLatin1().data(), _p->wrapQObject(qObject)); |
|
682 | 653 | } else if (PyDict_Check(object)) { |
|
683 | 654 | PyDict_SetItemString(object, name.toLatin1().data(), _p->wrapQObject(qObject)); |
|
684 | 655 | } else { |
|
685 | 656 | PyObject_SetAttrString(object, name.toLatin1().data(), _p->wrapQObject(qObject)); |
|
686 | 657 | } |
|
687 | 658 | } |
|
688 | 659 | |
|
689 | 660 | void PythonQt::addVariable(PyObject* object, const QString& name, const QVariant& v) |
|
690 | 661 | { |
|
691 | 662 | if (PyModule_Check(object)) { |
|
692 | 663 | PyModule_AddObject(object, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v)); |
|
693 | 664 | } else if (PyDict_Check(object)) { |
|
694 | 665 | PyDict_SetItemString(object, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v)); |
|
695 | 666 | } else { |
|
696 | 667 | PyObject_SetAttrString(object, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v)); |
|
697 | 668 | } |
|
698 | 669 | } |
|
699 | 670 | |
|
700 | 671 | void PythonQt::removeVariable(PyObject* object, const QString& name) |
|
701 | 672 | { |
|
702 | 673 | if (PyDict_Check(object)) { |
|
703 | 674 | PyDict_DelItemString(object, name.toLatin1().data()); |
|
704 | 675 | } else { |
|
705 | 676 | PyObject_DelAttrString(object, name.toLatin1().data()); |
|
706 | 677 | } |
|
707 | 678 | } |
|
708 | 679 | |
|
709 | 680 | QVariant PythonQt::getVariable(PyObject* object, const QString& objectname) |
|
710 | 681 | { |
|
711 | 682 | QVariant result; |
|
712 | 683 | PythonQtObjectPtr obj = lookupObject(object, objectname); |
|
713 | 684 | if (obj) { |
|
714 | 685 | result = PythonQtConv::PyObjToQVariant(obj); |
|
715 | 686 | } |
|
716 | 687 | return result; |
|
717 | 688 | } |
|
718 | 689 | |
|
719 | 690 | QStringList PythonQt::introspection(PyObject* module, const QString& objectname, PythonQt::ObjectType type) |
|
720 | 691 | { |
|
721 | 692 | QStringList results; |
|
722 | 693 | |
|
723 | 694 | PythonQtObjectPtr object; |
|
724 | 695 | if (objectname.isEmpty()) { |
|
725 | 696 | object = module; |
|
726 | 697 | } else { |
|
727 | 698 | object = lookupObject(module, objectname); |
|
728 | 699 | if (!object && type == CallOverloads) { |
|
729 | 700 | PyObject* dict = lookupObject(module, "__builtins__"); |
|
730 | 701 | if (dict) { |
|
731 | 702 | object = PyDict_GetItemString(dict, objectname.toLatin1().constData()); |
|
732 | 703 | } |
|
733 | 704 | } |
|
734 | 705 | } |
|
735 | 706 | |
|
736 | 707 | if (object) { |
|
737 | 708 | if (type == CallOverloads) { |
|
738 | 709 | if (PythonQtSlotFunction_Check(object)) { |
|
739 | 710 | PythonQtSlotFunctionObject* o = (PythonQtSlotFunctionObject*)object.object(); |
|
740 | 711 | PythonQtSlotInfo* info = o->m_ml; |
|
741 | 712 | |
|
742 | 713 | while (info) { |
|
743 | 714 | results << info->fullSignature(); |
|
744 | 715 | info = info->nextInfo(); |
|
745 | 716 | } |
|
746 | 717 | } else if (object->ob_type == &PythonQtClassWrapper_Type) { |
|
747 | 718 | PythonQtClassWrapper* o = (PythonQtClassWrapper*)object.object(); |
|
748 | 719 | PythonQtSlotInfo* info = o->classInfo()->constructors(); |
|
749 | 720 | |
|
750 | 721 | while (info) { |
|
751 | 722 | results << info->fullSignature(); |
|
752 | 723 | info = info->nextInfo(); |
|
753 | 724 | } |
|
754 | 725 | } else { |
|
755 | 726 | //TODO: use pydoc! |
|
756 | 727 | PyObject* doc = PyObject_GetAttrString(object, "__doc__"); |
|
757 | 728 | if (doc) { |
|
758 | 729 | results << PyString_AsString(doc); |
|
759 | 730 | Py_DECREF(doc); |
|
760 | 731 | } |
|
761 | 732 | } |
|
762 | 733 | } else { |
|
763 | 734 | PyObject* keys = NULL; |
|
764 | 735 | bool isDict = false; |
|
765 | 736 | if (PyDict_Check(object)) { |
|
766 | 737 | keys = PyDict_Keys(object); |
|
767 | 738 | isDict = true; |
|
768 | 739 | } else { |
|
769 | 740 | keys = PyObject_Dir(object); |
|
770 | 741 | } |
|
771 | 742 | if (keys) { |
|
772 | 743 | int count = PyList_Size(keys); |
|
773 | 744 | PyObject* key; |
|
774 | 745 | PyObject* value; |
|
775 | 746 | QString keystr; |
|
776 | 747 | for (int i = 0;i<count;i++) { |
|
777 | 748 | key = PyList_GetItem(keys,i); |
|
778 | 749 | if (isDict) { |
|
779 | 750 | value = PyDict_GetItem(object, key); |
|
780 | 751 | Py_INCREF(value); |
|
781 | 752 | } else { |
|
782 | 753 | value = PyObject_GetAttr(object, key); |
|
783 | 754 | } |
|
784 | 755 | if (!value) continue; |
|
785 | 756 | keystr = PyString_AsString(key); |
|
786 | 757 | static const QString underscoreStr("__tmp"); |
|
787 | 758 | if (!keystr.startsWith(underscoreStr)) { |
|
788 | 759 | switch (type) { |
|
789 | 760 | case Anything: |
|
790 | 761 | results << keystr; |
|
791 | 762 | break; |
|
792 | 763 | case Class: |
|
793 | 764 | if (value->ob_type == &PyClass_Type) { |
|
794 | 765 | results << keystr; |
|
795 | 766 | } |
|
796 | 767 | break; |
|
797 | 768 | case Variable: |
|
798 | 769 | if (value->ob_type != &PyClass_Type |
|
799 | 770 | && value->ob_type != &PyCFunction_Type |
|
800 | 771 | && value->ob_type != &PyFunction_Type |
|
801 | 772 | && value->ob_type != &PyModule_Type |
|
802 | 773 | ) { |
|
803 | 774 | results << keystr; |
|
804 | 775 | } |
|
805 | 776 | break; |
|
806 | 777 | case Function: |
|
807 | 778 | if (value->ob_type == &PyFunction_Type || |
|
808 | 779 | value->ob_type == &PyMethod_Type |
|
809 | 780 | ) { |
|
810 | 781 | results << keystr; |
|
811 | 782 | } |
|
812 | 783 | break; |
|
813 | 784 | case Module: |
|
814 | 785 | if (value->ob_type == &PyModule_Type) { |
|
815 | 786 | results << keystr; |
|
816 | 787 | } |
|
817 | 788 | break; |
|
818 | 789 | default: |
|
819 | 790 | std::cerr << "PythonQt: introspection: unknown case" << ", in " << __FILE__ << ":" << __LINE__ << std::endl; |
|
820 | 791 | } |
|
821 | 792 | } |
|
822 | 793 | Py_DECREF(value); |
|
823 | 794 | } |
|
824 | 795 | Py_DECREF(keys); |
|
825 | 796 | } |
|
826 | 797 | } |
|
827 | 798 | } |
|
828 | 799 | return results; |
|
829 | 800 | } |
|
830 | 801 | |
|
831 | 802 | QVariant PythonQt::call(PyObject* object, const QString& name, const QVariantList& args) |
|
832 | 803 | { |
|
833 | 804 | PythonQtObjectPtr callable = lookupCallable(object, name); |
|
834 | 805 | if (callable) { |
|
835 | 806 | return call(callable, args); |
|
836 | 807 | } else { |
|
837 | 808 | return QVariant(); |
|
838 | 809 | } |
|
839 | 810 | } |
|
840 | 811 | |
|
841 | 812 | QVariant PythonQt::call(PyObject* callable, const QVariantList& args) |
|
842 | 813 | { |
|
843 | 814 | QVariant r; |
|
844 | 815 | PythonQtObjectPtr result; |
|
845 | 816 | result.setNewRef(callAndReturnPyObject(callable, args)); |
|
846 | 817 | if (result) { |
|
847 | 818 | r = PythonQtConv::PyObjToQVariant(result); |
|
848 | 819 | } else { |
|
849 | 820 | PythonQt::self()->handleError(); |
|
850 | 821 | } |
|
851 | 822 | return r; |
|
852 | 823 | } |
|
853 | 824 | |
|
854 | 825 | PyObject* PythonQt::callAndReturnPyObject(PyObject* callable, const QVariantList& args) |
|
855 | 826 | { |
|
856 | 827 | PyObject* result = NULL; |
|
857 | 828 | if (callable) { |
|
858 | 829 | PythonQtObjectPtr pargs; |
|
859 | 830 | int count = args.size(); |
|
860 | 831 | if (count>0) { |
|
861 | 832 | pargs.setNewRef(PyTuple_New(count)); |
|
862 | 833 | } |
|
863 | 834 | bool err = false; |
|
864 | 835 | // transform QVariants to Python |
|
865 | 836 | for (int i = 0; i < count; i++) { |
|
866 | 837 | PyObject* arg = PythonQtConv::QVariantToPyObject(args.at(i)); |
|
867 | 838 | if (arg) { |
|
868 | 839 | // steals reference, no unref |
|
869 | 840 | PyTuple_SetItem(pargs, i,arg); |
|
870 | 841 | } else { |
|
871 | 842 | err = true; |
|
872 | 843 | break; |
|
873 | 844 | } |
|
874 | 845 | } |
|
875 | 846 | |
|
876 | 847 | if (!err) { |
|
877 | 848 | PyErr_Clear(); |
|
878 | 849 | result = PyObject_CallObject(callable, pargs); |
|
879 | 850 | } |
|
880 | 851 | } |
|
881 | 852 | return result; |
|
882 | 853 | } |
|
883 | 854 | |
|
884 | 855 | void PythonQt::addInstanceDecorators(QObject* o) |
|
885 | 856 | { |
|
886 | 857 | _p->addDecorators(o, PythonQtPrivate::InstanceDecorator); |
|
887 | 858 | } |
|
888 | 859 | |
|
889 | 860 | void PythonQt::addClassDecorators(QObject* o) |
|
890 | 861 | { |
|
891 | 862 | _p->addDecorators(o, PythonQtPrivate::StaticDecorator | PythonQtPrivate::ConstructorDecorator | PythonQtPrivate::DestructorDecorator); |
|
892 | 863 | } |
|
893 | 864 | |
|
894 | 865 | void PythonQt::addDecorators(QObject* o) |
|
895 | 866 | { |
|
896 | 867 | _p->addDecorators(o, PythonQtPrivate::AllDecorators); |
|
897 | 868 | } |
|
898 | 869 | |
|
899 | 870 | void PythonQt::registerQObjectClassNames(const QStringList& names) |
|
900 | 871 | { |
|
901 | 872 | _p->registerQObjectClassNames(names); |
|
902 | 873 | } |
|
903 | 874 | |
|
904 | 875 | void PythonQt::setImporter(PythonQtImportFileInterface* importInterface) |
|
905 | 876 | { |
|
906 | 877 | _p->_importInterface = importInterface; |
|
907 | 878 | PythonQtImport::init(); |
|
908 | 879 | } |
|
909 | 880 | |
|
910 | 881 | void PythonQt::setImporterIgnorePaths(const QStringList& paths) |
|
911 | 882 | { |
|
912 | 883 | _p->_importIgnorePaths = paths; |
|
913 | 884 | } |
|
914 | 885 | |
|
915 | 886 | const QStringList& PythonQt::getImporterIgnorePaths() |
|
916 | 887 | { |
|
917 | 888 | return _p->_importIgnorePaths; |
|
918 | 889 | } |
|
919 | 890 | |
|
920 | 891 | void PythonQt::addWrapperFactory(PythonQtCppWrapperFactory* factory) |
|
921 | 892 | { |
|
922 | 893 | _p->_cppWrapperFactories.append(factory); |
|
923 | 894 | } |
|
924 | 895 | |
|
925 | 896 | //--------------------------------------------------------------------------------------------------- |
|
926 | 897 | PythonQtPrivate::PythonQtPrivate() |
|
927 | 898 | { |
|
928 | 899 | _importInterface = NULL; |
|
929 | 900 | _defaultImporter = new PythonQtQFileImporter; |
|
930 | 901 | _noLongerWrappedCB = NULL; |
|
931 | 902 | _wrappedCB = NULL; |
|
932 | 903 | _currentClassInfoForClassWrapperCreation = NULL; |
|
933 | 904 | } |
|
934 | 905 | |
|
935 | 906 | void PythonQtPrivate::setupSharedLibrarySuffixes() |
|
936 | 907 | { |
|
937 | 908 | _sharedLibrarySuffixes.clear(); |
|
938 | 909 | PythonQtObjectPtr imp; |
|
939 | 910 | imp.setNewRef(PyImport_ImportModule("imp")); |
|
940 | 911 | int cExtensionCode = imp.getVariable("C_EXTENSION").toInt(); |
|
941 | 912 | QVariant result = imp.call("get_suffixes"); |
|
942 | 913 | foreach (QVariant entry, result.toList()) { |
|
943 | 914 | QVariantList suffixEntry = entry.toList(); |
|
944 | 915 | if (suffixEntry.count()==3) { |
|
945 | 916 | int code = suffixEntry.at(2).toInt(); |
|
946 | 917 | if (code == cExtensionCode) { |
|
947 | 918 | _sharedLibrarySuffixes << suffixEntry.at(0).toString(); |
|
948 | 919 | } |
|
949 | 920 | } |
|
950 | 921 | } |
|
951 | 922 | } |
|
952 | 923 | |
|
953 | 924 | PythonQtClassInfo* PythonQtPrivate::currentClassInfoForClassWrapperCreation() |
|
954 | 925 | { |
|
955 | 926 | PythonQtClassInfo* info = _currentClassInfoForClassWrapperCreation; |
|
956 | 927 | _currentClassInfoForClassWrapperCreation = NULL; |
|
957 | 928 | return info; |
|
958 | 929 | } |
|
959 | 930 | |
|
960 | 931 | void PythonQtPrivate::addDecorators(QObject* o, int decoTypes) |
|
961 | 932 | { |
|
962 | 933 | o->setParent(this); |
|
963 | 934 | int numMethods = o->metaObject()->methodCount(); |
|
964 | 935 | for (int i = 0; i < numMethods; i++) { |
|
965 | 936 | QMetaMethod m = o->metaObject()->method(i); |
|
966 | 937 | if ((m.methodType() == QMetaMethod::Method || |
|
967 | 938 | m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public) { |
|
968 | 939 | if (qstrncmp(m.signature(), "new_", 4)==0) { |
|
969 | 940 | if ((decoTypes & ConstructorDecorator) == 0) continue; |
|
970 | 941 | const PythonQtMethodInfo* info = PythonQtMethodInfo::getCachedMethodInfo(m, NULL); |
|
971 | 942 | if (info->parameters().at(0).isPointer) { |
|
972 | 943 | QByteArray signature = m.signature(); |
|
973 | 944 | QByteArray nameOfClass = signature.mid(4, signature.indexOf('(')-4); |
|
974 | 945 | PythonQtClassInfo* classInfo = lookupClassInfoAndCreateIfNotPresent(nameOfClass); |
|
975 | 946 | PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(NULL, m, i, o, PythonQtSlotInfo::ClassDecorator); |
|
976 | 947 | classInfo->addConstructor(newSlot); |
|
977 | 948 | } |
|
978 | 949 | } else if (qstrncmp(m.signature(), "delete_", 7)==0) { |
|
979 | 950 | if ((decoTypes & DestructorDecorator) == 0) continue; |
|
980 | 951 | QByteArray signature = m.signature(); |
|
981 | 952 | QByteArray nameOfClass = signature.mid(7, signature.indexOf('(')-7); |
|
982 | 953 | PythonQtClassInfo* classInfo = lookupClassInfoAndCreateIfNotPresent(nameOfClass); |
|
983 | 954 | PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(NULL, m, i, o, PythonQtSlotInfo::ClassDecorator); |
|
984 | 955 | classInfo->setDestructor(newSlot); |
|
985 | 956 | } else if (qstrncmp(m.signature(), "static_", 7)==0) { |
|
986 | 957 | if ((decoTypes & StaticDecorator) == 0) continue; |
|
987 | 958 | QByteArray signature = m.signature(); |
|
988 | 959 | QByteArray nameOfClass = signature.mid(signature.indexOf('_')+1); |
|
989 | 960 | nameOfClass = nameOfClass.mid(0, nameOfClass.indexOf('_')); |
|
990 | 961 | PythonQtClassInfo* classInfo = lookupClassInfoAndCreateIfNotPresent(nameOfClass); |
|
991 | 962 | PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(NULL, m, i, o, PythonQtSlotInfo::ClassDecorator); |
|
992 | 963 | classInfo->addDecoratorSlot(newSlot); |
|
993 | 964 | } else { |
|
994 | 965 | if ((decoTypes & InstanceDecorator) == 0) continue; |
|
995 | 966 | const PythonQtMethodInfo* info = PythonQtMethodInfo::getCachedMethodInfo(m, NULL); |
|
996 | 967 | if (info->parameters().count()>1) { |
|
997 | 968 | PythonQtMethodInfo::ParameterInfo p = info->parameters().at(1); |
|
998 | 969 | if (p.isPointer) { |
|
999 | 970 | PythonQtClassInfo* classInfo = lookupClassInfoAndCreateIfNotPresent(p.name); |
|
1000 | 971 | PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(NULL, m, i, o, PythonQtSlotInfo::InstanceDecorator); |
|
1001 | 972 | classInfo->addDecoratorSlot(newSlot); |
|
1002 | 973 | } |
|
1003 | 974 | } |
|
1004 | 975 | } |
|
1005 | 976 | } |
|
1006 | 977 | } |
|
1007 | 978 | } |
|
1008 | 979 | |
|
1009 | 980 | void PythonQtPrivate::registerQObjectClassNames(const QStringList& names) |
|
1010 | 981 | { |
|
1011 | 982 | foreach(QString name, names) { |
|
1012 | 983 | _knownQObjectClassNames.insert(name.toLatin1(), true); |
|
1013 | 984 | } |
|
1014 | 985 | } |
|
1015 | 986 | |
|
1016 | 987 | void PythonQtPrivate::removeSignalEmitter(QObject* obj) |
|
1017 | 988 | { |
|
1018 | 989 | _signalReceivers.remove(obj); |
|
1019 | 990 | } |
|
1020 | 991 | |
|
1021 | 992 | bool PythonQt::handleError() |
|
1022 | 993 | { |
|
1023 | 994 | bool flag = false; |
|
1024 | 995 | if (PyErr_Occurred()) { |
|
1025 | 996 | |
|
1026 | 997 | // currently we just print the error and the stderr handler parses the errors |
|
1027 | 998 | PyErr_Print(); |
|
1028 | 999 | |
|
1029 | 1000 | /* |
|
1030 | 1001 | // EXTRA: the format of the ptype and ptraceback is not really documented, so I use PyErr_Print() above |
|
1031 | 1002 | PyObject *ptype; |
|
1032 | 1003 | PyObject *pvalue; |
|
1033 | 1004 | PyObject *ptraceback; |
|
1034 | 1005 | PyErr_Fetch( &ptype, &pvalue, &ptraceback); |
|
1035 | 1006 | |
|
1036 | 1007 | Py_XDECREF(ptype); |
|
1037 | 1008 | Py_XDECREF(pvalue); |
|
1038 | 1009 | Py_XDECREF(ptraceback); |
|
1039 | 1010 | */ |
|
1040 | 1011 | PyErr_Clear(); |
|
1041 | 1012 | flag = true; |
|
1042 | 1013 | } |
|
1043 | 1014 | return flag; |
|
1044 | 1015 | } |
|
1045 | 1016 | |
|
1046 | 1017 | void PythonQt::addSysPath(const QString& path) |
|
1047 | 1018 | { |
|
1048 | 1019 | PythonQtObjectPtr sys; |
|
1049 | 1020 | sys.setNewRef(PyImport_ImportModule("sys")); |
|
1050 | 1021 | PythonQtObjectPtr obj = lookupObject(sys, "path"); |
|
1051 | 1022 | PyList_Insert(obj, 0, PythonQtConv::QStringToPyObject(path)); |
|
1052 | 1023 | } |
|
1053 | 1024 | |
|
1054 | 1025 | void PythonQt::overwriteSysPath(const QStringList& paths) |
|
1055 | 1026 | { |
|
1056 | 1027 | PythonQtObjectPtr sys; |
|
1057 | 1028 | sys.setNewRef(PyImport_ImportModule("sys")); |
|
1058 | 1029 | PyModule_AddObject(sys, "path", PythonQtConv::QStringListToPyList(paths)); |
|
1059 | 1030 | } |
|
1060 | 1031 | |
|
1061 | 1032 | void PythonQt::setModuleImportPath(PyObject* module, const QStringList& paths) |
|
1062 | 1033 | { |
|
1063 | 1034 | PyModule_AddObject(module, "__path__", PythonQtConv::QStringListToPyList(paths)); |
|
1064 | 1035 | } |
|
1065 | 1036 | |
|
1066 | 1037 | void PythonQt::stdOutRedirectCB(const QString& str) |
|
1067 | 1038 | { |
|
1068 | 1039 | emit PythonQt::self()->pythonStdOut(str); |
|
1069 | 1040 | } |
|
1070 | 1041 | |
|
1071 | 1042 | void PythonQt::stdErrRedirectCB(const QString& str) |
|
1072 | 1043 | { |
|
1073 | 1044 | emit PythonQt::self()->pythonStdErr(str); |
|
1074 | 1045 | } |
|
1075 | 1046 | |
|
1076 | 1047 | void PythonQt::setQObjectWrappedCallback(PythonQtQObjectWrappedCB* cb) |
|
1077 | 1048 | { |
|
1078 | 1049 | _p->_wrappedCB = cb; |
|
1079 | 1050 | } |
|
1080 | 1051 | |
|
1081 | 1052 | void PythonQt::setQObjectNoLongerWrappedCallback(PythonQtQObjectNoLongerWrappedCB* cb) |
|
1082 | 1053 | { |
|
1083 | 1054 | _p->_noLongerWrappedCB = cb; |
|
1084 | 1055 | } |
|
1085 | 1056 | |
|
1086 | 1057 | |
|
1087 | 1058 | |
|
1088 | 1059 | static PyMethodDef PythonQtMethods[] = { |
|
1089 | 1060 | {NULL, NULL, 0, NULL} |
|
1090 | 1061 | }; |
|
1091 | 1062 | |
|
1092 | 1063 | void PythonQt::initPythonQtModule(bool redirectStdOut) |
|
1093 | 1064 | { |
|
1094 | 1065 | _p->_pythonQtModule = Py_InitModule("PythonQt", PythonQtMethods); |
|
1095 | 1066 | |
|
1096 | 1067 | if (redirectStdOut) { |
|
1097 | 1068 | PythonQtObjectPtr sys; |
|
1098 | 1069 | PythonQtObjectPtr out; |
|
1099 | 1070 | PythonQtObjectPtr err; |
|
1100 | 1071 | sys.setNewRef(PyImport_ImportModule("sys")); |
|
1101 | 1072 | // create a redirection object for stdout and stderr |
|
1102 | 1073 | out = PythonQtStdOutRedirectType.tp_new(&PythonQtStdOutRedirectType,NULL, NULL); |
|
1103 | 1074 | ((PythonQtStdOutRedirect*)out.object())->_cb = stdOutRedirectCB; |
|
1104 | 1075 | err = PythonQtStdOutRedirectType.tp_new(&PythonQtStdOutRedirectType,NULL, NULL); |
|
1105 | 1076 | ((PythonQtStdOutRedirect*)err.object())->_cb = stdErrRedirectCB; |
|
1106 | 1077 | // replace the built in file objects with our own objects |
|
1107 | 1078 | PyModule_AddObject(sys, "stdout", out); |
|
1108 | 1079 | PyModule_AddObject(sys, "stderr", err); |
|
1109 | 1080 | } |
|
1110 | 1081 | } |
|
1111 | 1082 | |
|
1112 | 1083 | void PythonQt::registerCPPClass(const char* typeName, const char* parentTypeName, const char* package, PythonQtQObjectCreatorFunctionCB* wrapperCreator, PythonQtShellSetInstanceWrapperCB* shell) |
|
1113 | 1084 | { |
|
1114 | 1085 | _p->registerCPPClass(typeName, parentTypeName, package, wrapperCreator, shell); |
|
1115 | 1086 | } |
|
1116 | 1087 | |
|
1117 | 1088 | |
|
1118 | 1089 | PythonQtClassInfo* PythonQtPrivate::lookupClassInfoAndCreateIfNotPresent(const char* typeName) |
|
1119 | 1090 | { |
|
1120 | 1091 | PythonQtClassInfo* info = _knownClassInfos.value(typeName); |
|
1121 | 1092 | if (!info) { |
|
1122 | 1093 | info = new PythonQtClassInfo(); |
|
1123 | 1094 | info->setupCPPObject(typeName); |
|
1124 | 1095 | _knownClassInfos.insert(typeName, info); |
|
1125 | 1096 | } |
|
1126 | 1097 | return info; |
|
1127 | 1098 | } |
|
1128 | 1099 | |
|
1129 | 1100 | void PythonQt::addPolymorphicHandler(const char* typeName, PythonQtPolymorphicHandlerCB* cb) |
|
1130 | 1101 | { |
|
1131 | 1102 | _p->addPolymorphicHandler(typeName, cb); |
|
1132 | 1103 | } |
|
1133 | 1104 | |
|
1134 | 1105 | void PythonQtPrivate::addPolymorphicHandler(const char* typeName, PythonQtPolymorphicHandlerCB* cb) |
|
1135 | 1106 | { |
|
1136 | 1107 | PythonQtClassInfo* info = lookupClassInfoAndCreateIfNotPresent(typeName); |
|
1137 | 1108 | info->addPolymorphicHandler(cb); |
|
1138 | 1109 | } |
|
1139 | 1110 | |
|
1140 | 1111 | bool PythonQt::addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset) |
|
1141 | 1112 | { |
|
1142 | 1113 | return _p->addParentClass(typeName, parentTypeName, upcastingOffset); |
|
1143 | 1114 | } |
|
1144 | 1115 | |
|
1145 | 1116 | bool PythonQtPrivate::addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset) |
|
1146 | 1117 | { |
|
1147 | 1118 | PythonQtClassInfo* info = _knownClassInfos.value(typeName); |
|
1148 | 1119 | if (info) { |
|
1149 | 1120 | PythonQtClassInfo* parentInfo = lookupClassInfoAndCreateIfNotPresent(parentTypeName); |
|
1150 | 1121 | info->addParentClass(PythonQtClassInfo::ParentClassInfo(parentInfo, upcastingOffset)); |
|
1151 | 1122 | return true; |
|
1152 | 1123 | } else { |
|
1153 | 1124 | return false; |
|
1154 | 1125 | } |
|
1155 | 1126 | } |
|
1156 | 1127 | |
|
1157 | 1128 | void PythonQtPrivate::registerCPPClass(const char* typeName, const char* parentTypeName, const char* package, PythonQtQObjectCreatorFunctionCB* wrapperCreator, PythonQtShellSetInstanceWrapperCB* shell) |
|
1158 | 1129 | { |
|
1159 | 1130 | PythonQtClassInfo* info = lookupClassInfoAndCreateIfNotPresent(typeName); |
|
1160 | 1131 | if (!info->pythonQtClassWrapper()) { |
|
1161 | 1132 | info->setupCPPObject(typeName); |
|
1162 | 1133 | createPythonQtClassWrapper(info, package); |
|
1163 | 1134 | } |
|
1164 | 1135 | if (parentTypeName && strcmp(parentTypeName,"")!=0) { |
|
1165 | 1136 | addParentClass(typeName, parentTypeName, 0); |
|
1166 | 1137 | } |
|
1167 | 1138 | if (wrapperCreator) { |
|
1168 | 1139 | info->setDecoratorProvider(wrapperCreator); |
|
1169 | 1140 | } |
|
1170 | 1141 | if (shell) { |
|
1171 | 1142 | info->setShellSetInstanceWrapperCB(shell); |
|
1172 | 1143 | } |
|
1173 | 1144 | } |
|
1174 | 1145 | |
|
1175 | 1146 | PyObject* PythonQtPrivate::packageByName(const char* name) |
|
1176 | 1147 | { |
|
1177 | 1148 | if (name==NULL || name[0]==0) { |
|
1178 | 1149 | return _pythonQtModule; |
|
1179 | 1150 | } |
|
1180 | 1151 | PyObject* v = _packages.value(name); |
|
1181 | 1152 | if (!v) { |
|
1182 | 1153 | v = PyImport_AddModule((QByteArray("PythonQt.") + name).constData()); |
|
1183 | 1154 | _packages.insert(name, v); |
|
1184 | 1155 | // AddObject steals the reference, so increment it! |
|
1185 | 1156 | Py_INCREF(v); |
|
1186 | 1157 | PyModule_AddObject(_pythonQtModule, name, v); |
|
1187 | 1158 | } |
|
1188 | 1159 | return v; |
|
1189 | 1160 | } |
|
1190 | 1161 | |
|
1191 | 1162 | void PythonQtPrivate::handleVirtualOverloadReturnError(const char* signature, const PythonQtMethodInfo* methodInfo, PyObject* result) |
|
1192 | 1163 | { |
|
1193 | 1164 | QString error = "Return value '" + PythonQtConv::PyObjGetString(result) + "' can not be converted to expected C++ type '" + methodInfo->parameters().at(0).name + "' as return value of virtual method " + signature; |
|
1194 | 1165 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); |
|
1195 | 1166 | PythonQt::self()->handleError(); |
|
1196 | 1167 | } |
|
1197 | 1168 | |
|
1198 | 1169 | PyObject* PythonQt::helpCalled(PythonQtClassInfo* info) |
|
1199 | 1170 | { |
|
1200 | 1171 | if (_p->_initFlags & ExternalHelp) { |
|
1201 | 1172 | emit pythonHelpRequest(QByteArray(info->className())); |
|
1202 | 1173 | return Py_BuildValue(""); |
|
1203 | 1174 | } else { |
|
1204 | 1175 | return PyString_FromString(info->help().toLatin1().data()); |
|
1205 | 1176 | } |
|
1206 | 1177 | } |
|
1207 | 1178 | |
|
1208 | 1179 | void PythonQtPrivate::removeWrapperPointer(void* obj) |
|
1209 | 1180 | { |
|
1210 | 1181 | _wrappedObjects.remove(obj); |
|
1211 | 1182 | } |
|
1212 | 1183 | |
|
1213 | 1184 | void PythonQtPrivate::addWrapperPointer(void* obj, PythonQtInstanceWrapper* wrapper) |
|
1214 | 1185 | { |
|
1215 | 1186 | _wrappedObjects.insert(obj, wrapper); |
|
1216 | 1187 | } |
|
1217 | 1188 | |
|
1218 | 1189 | PythonQtInstanceWrapper* PythonQtPrivate::findWrapperAndRemoveUnused(void* obj) |
|
1219 | 1190 | { |
|
1220 | 1191 | PythonQtInstanceWrapper* wrap = _wrappedObjects.value(obj); |
|
1221 | 1192 | if (wrap && !wrap->_wrappedPtr && wrap->_obj == NULL) { |
|
1222 | 1193 | // this is a wrapper whose QObject was already removed due to destruction |
|
1223 | 1194 | // so the obj pointer has to be a new QObject with the same address... |
|
1224 | 1195 | // we remove the old one and set the copy to NULL |
|
1225 | 1196 | wrap->_objPointerCopy = NULL; |
|
1226 | 1197 | removeWrapperPointer(obj); |
|
1227 | 1198 | wrap = NULL; |
|
1228 | 1199 | } |
|
1229 | 1200 | return wrap; |
|
1230 | 1201 | } |
|
1231 | 1202 | |
|
1232 | 1203 | PythonQtObjectPtr PythonQtPrivate::createModule(const QString& name, PyObject* pycode) |
|
1233 | 1204 | { |
|
1234 | 1205 | PythonQtObjectPtr result; |
|
1235 | 1206 | if (pycode) { |
|
1236 | 1207 | result.setNewRef(PyImport_ExecCodeModule((char*)name.toLatin1().data(), pycode)); |
|
1237 | 1208 | } else { |
|
1238 | 1209 | PythonQt::self()->handleError(); |
|
1239 | 1210 | } |
|
1240 | 1211 | return result; |
|
1241 | 1212 | } |
@@ -1,81 +1,78 | |||
|
1 | 1 | #ifndef _PYTHONQTVARIANTS_ |
|
2 | 2 | #define _PYTHONQTVARIANTS_ |
|
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 PythonQtVariants.h |
|
39 | 39 | // \author Florian Link |
|
40 | 40 | // \author Last changed by $Author: stk $ |
|
41 | 41 | // \date 2006-08 |
|
42 | 42 | */ |
|
43 | 43 | //---------------------------------------------------------------------------------- |
|
44 | 44 | |
|
45 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_Qt.h" | |
|
46 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QBitArray.h" | |
|
47 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QDate.h" | |
|
48 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QTime.h" | |
|
49 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QDateTime.h" | |
|
50 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QUrl.h" | |
|
51 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLocale.h" | |
|
52 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRect.h" | |
|
53 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRectF.h" | |
|
54 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QSize.h" | |
|
55 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QSizeF.h" | |
|
56 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLine.h" | |
|
57 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLineF.h" | |
|
58 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QPoint.h" | |
|
59 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QPointF.h" | |
|
60 | #include "../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRegExp.h" | |
|
45 | #include <QBitArray> | |
|
46 | #include <QDate> | |
|
47 | #include <QTime> | |
|
48 | #include <QDateTime> | |
|
49 | #include <QUrl> | |
|
50 | #include <QLocale> | |
|
51 | #include <QRect> | |
|
52 | #include <QRectF> | |
|
53 | #include <QLine> | |
|
54 | #include <QLineF> | |
|
55 | #include <QPoint> | |
|
56 | #include <QPointF> | |
|
57 | #include <QRegExp> | |
|
61 | 58 | |
|
62 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QFont.h" | |
|
63 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPixmap.h" | |
|
64 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QBrush.h" | |
|
65 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QColor.h" | |
|
66 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPalette.h" | |
|
67 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QIcon.h" | |
|
68 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QImage.h" | |
|
69 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPolygon.h" | |
|
70 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QRegion.h" | |
|
71 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QBitmap.h" | |
|
72 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QCursor.h" | |
|
73 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QSizePolicy.h" | |
|
74 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QKeySequence.h" | |
|
75 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPen.h" | |
|
76 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QTextLength.h" | |
|
77 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QTextFormat.h" | |
|
78 | #include "../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QMatrix.h" | |
|
59 | #include <QFont> | |
|
60 | #include <QBitmap> | |
|
61 | #include <QBrush> | |
|
62 | #include <QColor> | |
|
63 | #include <QPalette> | |
|
64 | #include <QIcon> | |
|
65 | #include <QImage> | |
|
66 | #include <QPolygon> | |
|
67 | #include <QRegion> | |
|
68 | #include <QBitmap> | |
|
69 | #include <QCursor> | |
|
70 | #include <QSizePolicy> | |
|
71 | #include <QKeySequence> | |
|
72 | #include <QPen> | |
|
73 | #include <QTextLength> | |
|
74 | #include <QTextFormat> | |
|
75 | #include <QMatrix> | |
|
79 | 76 | |
|
80 | 77 | #endif |
|
81 | 78 |
@@ -1,113 +1,43 | |||
|
1 | 1 | DEFINES += PYTHONQT_EXPORTS |
|
2 | 2 | |
|
3 | 3 | HEADERS += \ |
|
4 | 4 | $$PWD/PythonQt.h \ |
|
5 | 5 | $$PWD/PythonQtStdDecorators.h \ |
|
6 | 6 | $$PWD/PythonQtClassInfo.h \ |
|
7 | 7 | $$PWD/PythonQtImporter.h \ |
|
8 | 8 | $$PWD/PythonQtObjectPtr.h \ |
|
9 | 9 | $$PWD/PythonQtSlot.h \ |
|
10 | 10 | $$PWD/PythonQtStdOut.h \ |
|
11 | 11 | $$PWD/PythonQtMisc.h \ |
|
12 | 12 | $$PWD/PythonQtMethodInfo.h \ |
|
13 | 13 | $$PWD/PythonQtImportFileInterface.h \ |
|
14 | 14 | $$PWD/PythonQtConversion.h \ |
|
15 | 15 | $$PWD/PythonQtSignalReceiver.h \ |
|
16 | 16 | $$PWD/PythonQtInstanceWrapper.h \ |
|
17 | 17 | $$PWD/PythonQtClassWrapper.h \ |
|
18 | 18 | $$PWD/PythonQtCppWrapperFactory.h \ |
|
19 | $$PWD/PythonQtVariants.h \ | |
|
20 | 19 | $$PWD/PythonQtQFileImporter.h \ |
|
21 | 20 | $$PWD/PythonQtQFileImporter.h \ |
|
21 | $$PWD/PythonQtVariants.h \ | |
|
22 | 22 | $$PWD/gui/PythonQtScriptingConsole.h \ |
|
23 |
$$PWD/PythonQtSystem.h |
|
|
24 | \ | |
|
25 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_Qt.h \ | |
|
26 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QBitArray.h \ | |
|
27 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QDate.h \ | |
|
28 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QTime.h \ | |
|
29 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QDateTime.h \ | |
|
30 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QUrl.h \ | |
|
31 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLocale.h \ | |
|
32 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRect.h \ | |
|
33 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRectF.h \ | |
|
34 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QSize.h \ | |
|
35 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QSizeF.h \ | |
|
36 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLine.h \ | |
|
37 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLineF.h \ | |
|
38 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QPoint.h \ | |
|
39 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QPointF.h \ | |
|
40 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRegExp.h \ | |
|
41 | \ | |
|
42 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QFont.h \ | |
|
43 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPixmap.h \ | |
|
44 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QBrush.h \ | |
|
45 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QColor.h \ | |
|
46 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPalette.h \ | |
|
47 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QIcon.h \ | |
|
48 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QImage.h \ | |
|
49 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPolygon.h \ | |
|
50 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QRegion.h \ | |
|
51 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QBitmap.h \ | |
|
52 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QCursor.h \ | |
|
53 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QSizePolicy.h \ | |
|
54 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QKeySequence.h \ | |
|
55 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPen.h \ | |
|
56 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QTextLength.h \ | |
|
57 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QTextFormat.h \ | |
|
58 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QMatrix.h | |
|
59 | ||
|
23 | $$PWD/PythonQtSystem.h | |
|
60 | 24 | |
|
61 | 25 | SOURCES += \ |
|
62 | 26 | $$PWD/PythonQtStdDecorators.cpp \ |
|
63 | 27 | $$PWD/PythonQt.cpp \ |
|
64 | 28 | $$PWD/PythonQtClassInfo.cpp \ |
|
65 | 29 | $$PWD/PythonQtImporter.cpp \ |
|
66 | 30 | $$PWD/PythonQtObjectPtr.cpp \ |
|
67 | 31 | $$PWD/PythonQtStdOut.cpp \ |
|
68 | 32 | $$PWD/PythonQtSlot.cpp \ |
|
69 | 33 | $$PWD/PythonQtMisc.cpp \ |
|
70 | 34 | $$PWD/PythonQtMethodInfo.cpp \ |
|
71 | 35 | $$PWD/PythonQtConversion.cpp \ |
|
72 | 36 | $$PWD/PythonQtSignalReceiver.cpp \ |
|
73 | $$PWD/PythonQtVariants.cpp \ | |
|
74 | 37 | $$PWD/PythonQtInstanceWrapper.cpp \ |
|
75 | 38 | $$PWD/PythonQtQFileImporter.cpp \ |
|
76 | 39 | $$PWD/PythonQtClassWrapper.cpp \ |
|
77 | 40 | $$PWD/gui/PythonQtScriptingConsole.cpp \ |
|
78 | \ | |
|
79 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_Qt.cpp \ | |
|
80 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QBitArray.cpp \ | |
|
81 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QDate.cpp \ | |
|
82 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QTime.cpp \ | |
|
83 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QDateTime.cpp \ | |
|
84 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QUrl.cpp \ | |
|
85 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLocale.cpp \ | |
|
86 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRect.cpp \ | |
|
87 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRectF.cpp \ | |
|
88 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QSize.cpp \ | |
|
89 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QSizeF.cpp \ | |
|
90 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLine.cpp \ | |
|
91 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QLineF.cpp \ | |
|
92 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QPoint.cpp \ | |
|
93 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QPointF.cpp \ | |
|
94 | $$PWD/../generated_cpp/com_trolltech_qt_core/PythonQtWrapper_QRegExp.cpp \ | |
|
95 | \ | |
|
96 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QFont.cpp \ | |
|
97 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPixmap.cpp \ | |
|
98 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QBrush.cpp \ | |
|
99 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QColor.cpp \ | |
|
100 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPalette.cpp \ | |
|
101 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QIcon.cpp \ | |
|
102 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QImage.cpp \ | |
|
103 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPolygon.cpp \ | |
|
104 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QRegion.cpp \ | |
|
105 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QBitmap.cpp \ | |
|
106 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QCursor.cpp \ | |
|
107 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QSizePolicy.cpp \ | |
|
108 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QKeySequence.cpp \ | |
|
109 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QPen.cpp \ | |
|
110 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QTextLength.cpp \ | |
|
111 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QTextFormat.cpp \ | |
|
112 | $$PWD/../generated_cpp/com_trolltech_qt_gui/PythonQtWrapper_QMatrix.cpp | |
|
113 | 41 | |
|
42 | include($$PWD/../generated_cpp/com_trolltech_qt_core_builtin/com_trolltech_qt_core_builtin.pri) | |
|
43 | include($$PWD/../generated_cpp/com_trolltech_qt_gui_builtin/com_trolltech_qt_gui_builtin.pri) |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now