00001 #ifndef _PYTHONQT_H
00002 #define _PYTHONQT_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00043
00044
00045 #include "PythonQtSystem.h"
00046 #include "PythonQtInstanceWrapper.h"
00047 #include "PythonQtClassWrapper.h"
00048 #include "PythonQtSlot.h"
00049 #include "PythonQtObjectPtr.h"
00050 #include <QObject>
00051 #include <QVariant>
00052 #include <QList>
00053 #include <QHash>
00054 #include <QByteArray>
00055 #include <QStringList>
00056 #include <QtDebug>
00057 #include <iostream>
00058
00059
00060 class PythonQtClassInfo;
00061 class PythonQtPrivate;
00062 class PythonQtMethodInfo;
00063 class PythonQtSignalReceiver;
00064 class PythonQtImportFileInterface;
00065 class PythonQtCppWrapperFactory;
00066 class PythonQtQFileImporter;
00067
00068 typedef void PythonQtQObjectWrappedCB(QObject* object);
00069 typedef void PythonQtQObjectNoLongerWrappedCB(QObject* object);
00070 typedef void* PythonQtPolymorphicHandlerCB(const void *ptr, char **class_name);
00071
00072 typedef void PythonQtShellSetInstanceWrapperCB(void* object, PythonQtInstanceWrapper* wrapper);
00073
00074 template<class T> void PythonQtSetInstanceWrapperOnShell(void* object, PythonQtInstanceWrapper* wrapper) { ((T*)object)->_wrapper = wrapper; };
00075
00077 template<class T1, class T2> int PythonQtUpcastingOffset() {
00078 return (((char*)(static_cast<T2*>(reinterpret_cast<T1*>(0x100)))) - ((char*)reinterpret_cast<T1*>(0x100)));
00079 }
00080
00082 typedef QObject* PythonQtQObjectCreatorFunctionCB();
00083
00085 template<class T> QObject* PythonQtCreateObject() { return new T(); };
00086
00088
00096 class PYTHONQT_EXPORT PythonQt : public QObject {
00097
00098 Q_OBJECT
00099
00100 public:
00101
00103 enum InitFlags {
00104 RedirectStdOut = 1,
00105 IgnoreSiteModule = 2,
00106 ExternalHelp = 4,
00107 PythonAlreadyInitialized = 8
00108 };
00109
00111 enum TypeSlots {
00112 Type_Add = 1,
00113 Type_Subtract = 1 << 1,
00114 Type_Multiply = 1 << 2,
00115 Type_Divide = 1 << 3,
00116 Type_Mod = 1 << 4,
00117 Type_And = 1 << 5,
00118 Type_Or = 1 << 6,
00119 Type_Xor = 1 << 7,
00120 Type_LShift = 1 << 8,
00121 Type_RShift = 1 << 9,
00122
00123 Type_InplaceAdd = 1 << 10,
00124 Type_InplaceSubtract = 1 << 11,
00125 Type_InplaceMultiply = 1 << 12,
00126 Type_InplaceDivide = 1 << 13,
00127 Type_InplaceMod = 1 << 14,
00128 Type_InplaceAnd = 1 << 15,
00129 Type_InplaceOr = 1 << 16,
00130 Type_InplaceXor = 1 << 17,
00131 Type_InplaceLShift = 1 << 18,
00132 Type_InplaceRShift = 1 << 19,
00133
00134
00135
00136
00137
00138
00139
00140 Type_Invert = 1 << 29,
00141 Type_RichCompare = 1 << 30,
00142 Type_NonZero = 1 << 31,
00143
00144 };
00145
00146
00148
00149
00153 static void init(int flags = IgnoreSiteModule | RedirectStdOut, const QByteArray& pythonQtModuleName = QByteArray());
00154
00156 static void cleanup();
00157
00159 static PythonQt* self() { return _self; }
00160
00162
00164 enum ObjectType {
00165 Class,
00166 Function,
00167 Variable,
00168 Module,
00169 Anything,
00170 CallOverloads
00171 };
00172
00173
00175
00176
00178 PythonQtObjectPtr getMainModule();
00179
00182 PythonQtObjectPtr importModule(const QString& name);
00183
00188 PythonQtObjectPtr createModuleFromFile(const QString& name, const QString& filename);
00189
00194 PythonQtObjectPtr createModuleFromScript(const QString& name, const QString& script = QString());
00195
00198 PythonQtObjectPtr createUniqueModule();
00199
00201
00202
00204
00205
00207 void overwriteSysPath(const QStringList& paths);
00208
00210 void addSysPath(const QString& path);
00211
00213 void setModuleImportPath(PyObject* module, const QStringList& paths);
00214
00216
00217
00219
00220
00222
00223
00224 void registerClass(const QMetaObject* metaobject, const char* package = NULL, PythonQtQObjectCreatorFunctionCB* wrapperCreator = NULL, PythonQtShellSetInstanceWrapperCB* shell = NULL);
00225
00228
00234 void registerCPPClass(const char* typeName, const char* parentTypeName = NULL, const char* package = NULL, PythonQtQObjectCreatorFunctionCB* wrapperCreator = NULL, PythonQtShellSetInstanceWrapperCB* shell = NULL);
00235
00238 void registerQObjectClassNames(const QStringList& names);
00239
00244 bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset=0);
00245
00247 void addPolymorphicHandler(const char* typeName, PythonQtPolymorphicHandlerCB* cb);
00248
00250
00251
00253
00254
00256 PythonQtObjectPtr parseFile(const QString& filename);
00257
00260 QVariant evalCode(PyObject* object, PyObject* pycode);
00261
00263 QVariant evalScript(PyObject* object, const QString& script, int start = Py_file_input);
00264
00266 void evalFile(PyObject* object, const QString& filename);
00267
00269
00270
00272
00273
00275 bool addSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
00276
00278 bool removeSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname);
00279
00281 bool addSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
00282
00284 bool removeSignalHandler(QObject* obj, const char* signal, PyObject* receiver);
00285
00287
00288
00290
00291
00293 void addObject(PyObject* object, const QString& name, QObject* qObject);
00294
00296 void addVariable(PyObject* object, const QString& name, const QVariant& v);
00297
00299 void removeVariable(PyObject* module, const QString& name);
00300
00302 QVariant getVariable(PyObject* object, const QString& name);
00303
00305 QStringList introspection(PyObject* object, const QString& objectname, ObjectType type);
00306
00309 PythonQtObjectPtr lookupCallable(PyObject* object, const QString& name);
00310
00312
00313
00315
00316
00318 QVariant call(PyObject* object, const QString& callable, const QVariantList& args = QVariantList());
00319
00321 QVariant call(PyObject* callable, const QVariantList& args = QVariantList());
00322
00324 PyObject* callAndReturnPyObject(PyObject* callable, const QVariantList& args = QVariantList());
00325
00327
00328
00330
00331
00336
00357 void addInstanceDecorators(QObject* o);
00358
00361
00375 void addClassDecorators(QObject* o);
00376
00378 void addDecorators(QObject* o);
00379
00381 void addWrapperFactory(PythonQtCppWrapperFactory* factory);
00382
00384
00385
00387
00388
00398 void setImporter(PythonQtImportFileInterface* importInterface);
00399
00406 void installDefaultImporter() { setImporter(NULL); }
00407
00409 void setImporterIgnorePaths(const QStringList& paths);
00410
00412 const QStringList& getImporterIgnorePaths();
00413
00415 static PythonQtImportFileInterface* importInterface();
00416
00418
00419
00421
00422
00424 static PythonQtPrivate* priv() { return _self->_p; }
00425
00428 bool handleError();
00429
00431 void setQObjectWrappedCallback(PythonQtQObjectWrappedCB* cb);
00433 void setQObjectNoLongerWrappedCallback(PythonQtQObjectNoLongerWrappedCB* cb);
00434
00436 static void qObjectNoLongerWrappedCB(QObject* o);
00437
00439 PyObject* helpCalled(PythonQtClassInfo* info);
00440
00443 PythonQtObjectPtr lookupObject(PyObject* module, const QString& name);
00444
00446
00447 signals:
00449 void pythonStdOut(const QString& str);
00451 void pythonStdErr(const QString& str);
00452
00454 void pythonHelpRequest(const QByteArray& cppClassName);
00455
00456 private:
00457 void initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName);
00458
00460 static void stdOutRedirectCB(const QString& str);
00462 static void stdErrRedirectCB(const QString& str);
00463
00465 PythonQtSignalReceiver* getSignalReceiver(QObject* obj);
00466
00467 PythonQt(int flags, const QByteArray& pythonQtModuleName);
00468 ~PythonQt();
00469
00470 static PythonQt* _self;
00471 static int _uniqueModuleCount;
00472
00473 PythonQtPrivate* _p;
00474
00475 };
00476
00478 class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
00479
00480 Q_OBJECT
00481
00482 public:
00483 PythonQtPrivate();
00484 ~PythonQtPrivate();
00485
00486 enum DecoratorTypes {
00487 StaticDecorator = 1,
00488 ConstructorDecorator = 2,
00489 DestructorDecorator = 4,
00490 InstanceDecorator = 8,
00491 AllDecorators = 0xffff
00492 };
00493
00495 const QStringList& sharedLibrarySuffixes() { return _sharedLibrarySuffixes; }
00496
00498 bool isPythonQtObjectPtrMetaId(int id) { return _PythonQtObjectPtr_metaId == id; }
00499
00501 void addWrapperPointer(void* obj, PythonQtInstanceWrapper* wrapper);
00503 void removeWrapperPointer(void* obj);
00504
00506 bool addParentClass(const char* typeName, const char* parentTypeName, int upcastingOffset);
00507
00509 void addPolymorphicHandler(const char* typeName, PythonQtPolymorphicHandlerCB* cb);
00510
00512 PythonQtClassInfo* lookupClassInfoAndCreateIfNotPresent(const char* typeName);
00513
00515 void removeSignalEmitter(QObject* obj);
00516
00518 PyObject* wrapQObject(QObject* obj);
00519
00521 PyObject* wrapPtr(void* ptr, const QByteArray& name);
00522
00524
00525
00526 void registerClass(const QMetaObject* metaobject, const char* package = NULL, PythonQtQObjectCreatorFunctionCB* wrapperCreator = NULL, PythonQtShellSetInstanceWrapperCB* shell = NULL, PyObject* module = NULL, int typeSlots = 0);
00527
00530
00536 void registerCPPClass(const char* typeName, const char* parentTypeName = NULL, const char* package = NULL, PythonQtQObjectCreatorFunctionCB* wrapperCreator = NULL, PythonQtShellSetInstanceWrapperCB* shell = NULL, PyObject* module = NULL, int typeSlots = 0);
00537
00540 void registerQObjectClassNames(const QStringList& names);
00541
00543 void addDecorators(QObject* o, int decoTypes);
00544
00546 PythonQtClassWrapper* createNewPythonQtClassWrapper(PythonQtClassInfo* info, PyObject* module);
00547
00549 static PyObject* createEnumValueInstance(PyObject* enumType, unsigned int enumValue);
00550
00552 static PyObject* createNewPythonQtEnumWrapper(const char* enumName, PyObject* parentObject);
00553
00555 PythonQtInstanceWrapper* createNewPythonQtInstanceWrapper(QObject* obj, PythonQtClassInfo* info, void* wrappedPtr = NULL);
00556
00558 PythonQtClassInfo* getClassInfo(const QMetaObject* meta) { return _knownClassInfos.value(meta->className()); }
00559
00561 PythonQtClassInfo* getClassInfo(const QByteArray& className) { return _knownClassInfos.value(className); }
00562
00564 PythonQtObjectPtr createModule(const QString& name, PyObject* pycode);
00565
00567 PythonQtClassInfo* currentClassInfoForClassWrapperCreation();
00568
00570 static PyObject* dummyTuple();
00571
00573 void handleVirtualOverloadReturnError(const char* signature, const PythonQtMethodInfo* methodInfo, PyObject* result);
00574
00576 PythonQtObjectPtr pythonQtModule() const { return _pythonQtModule; }
00577
00578 private:
00580 void setupSharedLibrarySuffixes();
00581
00583 void createPythonQtClassWrapper(PythonQtClassInfo* info, const char* package, PyObject* module = NULL);
00584
00586 PyObject* packageByName(const char* name);
00587
00589 PythonQtInstanceWrapper* findWrapperAndRemoveUnused(void* obj);
00590
00592 QHash<void* , PythonQtInstanceWrapper *> _wrappedObjects;
00593
00595 QHash<QByteArray, PythonQtClassInfo *> _knownClassInfos;
00596
00598 QHash<QByteArray, bool> _knownQObjectClassNames;
00599
00601 QHash<QObject* , PythonQtSignalReceiver *> _signalReceivers;
00602
00604 PythonQtObjectPtr _pythonQtModule;
00605
00607 QByteArray _pythonQtModuleName;
00608
00610 PythonQtImportFileInterface* _importInterface;
00611
00613 PythonQtQFileImporter* _defaultImporter;
00614
00615 PythonQtQObjectNoLongerWrappedCB* _noLongerWrappedCB;
00616 PythonQtQObjectWrappedCB* _wrappedCB;
00617
00618 QStringList _importIgnorePaths;
00619 QStringList _sharedLibrarySuffixes;
00620
00622 QList<PythonQtCppWrapperFactory*> _cppWrapperFactories;
00623
00624 QHash<QByteArray, PyObject*> _packages;
00625
00626 PythonQtClassInfo* _currentClassInfoForClassWrapperCreation;
00627
00628 int _initFlags;
00629 int _PythonQtObjectPtr_metaId;
00630
00631 friend class PythonQt;
00632 };
00633
00634 #endif