Menu

[r4]: / src / PythonQt.h  Maximize  Restore  History

Download this file

304 lines (223 with data), 11.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#ifndef _PYTHONQT_H
#define _PYTHONQT_H
/*
*
* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Further, this software is distributed without any warranty that it is
* free of the rightful claim of any third person regarding infringement
* or the like. Any license provided herein, whether implied or
* otherwise, applies only to this software file. Patent licenses, if
* any, provided herein do not apply to combinations of this program with
* other software, or any other product whatsoever.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact information: MeVis Research GmbH, Universitaetsallee 29,
* 28359 Bremen, Germany or:
*
* http://www.mevis.de
*
*/
//----------------------------------------------------------------------------------
/*!
// \file PythonQt.h
// \author Florian Link
// \author Last changed by $Author: florian $
// \date 2006-05
*/
//----------------------------------------------------------------------------------
#include "PythonQtSystem.h"
#include "PythonQtWrapper.h"
#include "PythonQtSlot.h"
#include "PythonQtObjectPtr.h"
#include <QObject>
#include <QVariant>
#include <QList>
#include <QHash>
#include <QByteArray>
#include <QStringList>
#include <QtDebug>
#include <iostream>
class PythonQtClassInfo;
class PythonQtPrivate;
class PythonQtMethodInfo;
class PythonQtSignalReceiver;
class PythonQtImportFileInterface;
class PythonQtCppWrapperFactory;
//! the main interface to the Python Qt binding, realized as a singleton
class PYTHONQT_EXPORT PythonQt : public QObject {
Q_OBJECT
public:
enum InitFlags {
RedirectStdOut=1, IgnoreSiteModule=2
};
//! initialize the python qt binding (flags are a or combination of InitFlags)
static void init(int flags);
//! cleanup
static void cleanup();
//! get the singleton instance
static PythonQt* self() { return _self; }
//-----------------------------------------------------------------------------
// Public API:
//! defines the object types for introspection
enum ObjectType {
Class,
Function,
Variable,
Module
};
//! overwrite the python sys path (call this directly after PythonQt::init() if you want to change the std python sys path)
void overwriteSysPath(const QStringList& paths);
//! sets the __path__ list of a module to the given list (important for local imports)
void setModuleImportPath(PyObject* module, const QStringList& paths);
//! get the __main__ module of python
PythonQtObjectPtr getMainModule();
//! registers a QObject derived class to PythonQt (this is implicitly called by addObject as well)
/* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
void registerClass(const QMetaObject* metaobject);
//! add the given factory to PythonQt (ownership stays with caller)
void addWrapperFactory(PythonQtCppWrapperFactory* factory);
//! parses the given file and returns the python code object, this can then be used to call evalCode()
PythonQtObjectPtr parseFile(const QString& filename);
//! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string)
//! If pycode is NULL, a python error is printed.
QVariant evalCode(PyObject* module, PyObject* pycode);
//! evaluates the given script code and returns the result value
QVariant evalScript(PyObject* module, const QString& script, int start = Py_file_input);
//@{ Signal handlers
//! add a signal handler to the given \c signal of \c obj and connect it to a callable \c objectname in module
bool addSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
//! remove a signal handler from the given \c signal of \c obj
bool removeSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
//! add a signal handler to the given \c signal of \c obj and connect it to a callable \c receiver
bool addSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
//! remove a signal handler from the given \c signal of \c obj
bool removeSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
//@}
//@{ Variable access
//! add the given \c object to the \c module as a variable with \c name (it can be removed via clearVariable)
void addObject(PyObject* module, const QString& name, QObject* object);
//! add the given variable to the module
void addVariable(PyObject* module, const QString& name, const QVariant& v);
//! remove the given variable
void removeVariable(PyObject* module, const QString& name);
//! get the variable with the \c name of the \c module, returns an invalid QVariant on error
QVariant getVariable(PyObject* module, const QString& name);
//! read vars etc. in scope of a module, optional looking inside of an object \c objectname
QStringList introspection(PyObject* module, const QString& objectname, ObjectType type);
//! returns the found callable object or NULL
//! @return new reference
PythonQtObjectPtr lookupCallable(PyObject* module, const QString& name);
//@}
//@{ Calling of python callables
//! call the given python method, returns the result converted to a QVariant
QVariant call(PyObject* module, const QString& callable, const QVariantList& args);
//@}
//@{ Custom importer (to replace internal import implementation of python)
//! replace the internal import implementation and use the supplied interface to load files (both py and pyc files)
//! (this method should be called directly after initialization of init() and before calling overwriteSysPath().
//! It can only be called once, further calls will be ignored silently. (ownership stays with caller)
void setImporter(PythonQtImportFileInterface* importInterface);
//! set paths that the importer should ignore
void setImporterIgnorePaths(const QStringList& paths);
//! get paths that the importer should ignore
const QStringList& getImporterIgnorePaths();
//@}
//! get access to internal data (should not be used on the public API, but is used by some C functions)
static PythonQtPrivate* priv() { return _self->_p; }
//! get access to the file importer (if set)
static PythonQtImportFileInterface* importInterface();
//! handle a python error, call this when a python function fails. If no error occurred, it returns false.
//! The error is currently just output to the python stderr, future version might implement better trace printing
bool handleError();
signals:
//! emitted when python outputs something to stdout (and redirection is turned on)
void pythonStdOut(const QString& str);
//! emitted when python outputs something to stderr (and redirection is turned on)
void pythonStdErr(const QString& str);
private:
void initPythonQtModule(bool redirectStdOut);
//! callback for stdout redirection, emits pythonStdOut signal
static void stdOutRedirectCB(const QString& str);
//! callback for stderr redirection, emits pythonStdErr signal
static void stdErrRedirectCB(const QString& str);
//! returns the found object or NULL
//! @return new reference
PythonQtObjectPtr lookupObject(PyObject* module, const QString& name);
//! get (and create if not available) the signal receiver of that QObject, signal receiver is made child of the passed \c obj
PythonQtSignalReceiver* getSignalReceiver(QObject* obj);
PythonQt(int flags);
~PythonQt();
static PythonQt* _self;
PythonQtPrivate* _p;
};
//! stores information about a currently created (pending) wrapper object
struct PythonQtPendingObject
{
QObject* _obj;
void* _wrappedPtr;
PythonQtClassInfo* _info;
};
//! internal PythonQt details
class PythonQtPrivate : public QObject {
Q_OBJECT
public:
PythonQtPrivate();
~PythonQtPrivate();
//! get information on the pending object to be wrapped
PythonQtPendingObject* pendingObject() { return &_pendingObject; }
//! remove the wrapper ptr again
void removeWrapperPointer(void* obj) { _wrappedObjects.take(obj); }
//! returns the signal info if the signature, uses a signal cache internally to speed up
//! multiple requests for the same signal
const PythonQtMethodInfo* getSignalInfo(const QMetaMethod& signal);
//! wrap the given QObject into a Python object (or return existing wrapper!)
PyObject* wrapQObject(QObject* obj);
//! wrap the given ptr into a Python object (or return existing wrapper!) if there is a known QObject of that name or a known wrapper in the factory
PyObject* wrapPtr(void* ptr, const QByteArray& name);
//! registers a QObject derived class to PythonQt (this is implicitly called by addObject as well)
/* Since Qt4 does not offer a way to detect if a given classname is derived from QObject and thus has a QMetaObject,
you MUST register all your QObject derived classes here when you want them to be detected in signal and slot calls */
void registerClass(const QMetaObject* metaobject);
protected slots:
//! called when a wrapped QObject is destroyed
void wrappedObjectDestroyed(QObject* obj);
//! called when a signal emitting QObject is destroyed to remove the signal handler from the hash map
void destroyedSignalEmitter(QObject* obj);
private:
//! stores pointer to PyObject mapping of wrapped QObjects AND C++ objects
QHash<void* , PythonQtWrapper *> _wrappedObjects;
//! stores the meta info if known Qt classes
QHash<QByteArray, PythonQtClassInfo *> _knownQtClasses;
//! stores the meta info if known Qt C++ wrapper classes
QHash<QByteArray, PythonQtClassInfo *> _knownQtWrapperClasses;
//! stores the cached signatures of signals to speedup mapping from Qt to Python types
QHash<QByteArray, PythonQtMethodInfo*> _cachedSignalSignatures;
//! stores signal receivers for QObjects
QHash<QObject* , PythonQtSignalReceiver *> _signalReceivers;
//! stores a pending object to be injected inside of the PythonQtWrapper constructor (because marshalling would be ugly as well)
PythonQtPendingObject _pendingObject;
//! the PythonQt python module
PythonQtObjectPtr _pythonQtModule;
//! the importer interface (if set)
PythonQtImportFileInterface* _importInterface;
QStringList _importIgnorePaths;
//! the cpp object wrapper factories
QList<PythonQtCppWrapperFactory*> _cppWrapperFactories;
friend class PythonQt;
};
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.