@@ -1,243 +1,243 | |||||
1 | #ifndef _PYTHONQTCLASSINFO_H |
|
1 | #ifndef _PYTHONQTCLASSINFO_H | |
2 | #define _PYTHONQTCLASSINFO_H |
|
2 | #define _PYTHONQTCLASSINFO_H | |
3 |
|
3 | |||
4 | /* |
|
4 | /* | |
5 | * |
|
5 | * | |
6 | * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. |
|
6 | * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. | |
7 | * |
|
7 | * | |
8 | * This library is free software; you can redistribute it and/or |
|
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public |
|
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either |
|
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. |
|
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * |
|
12 | * | |
13 | * This library is distributed in the hope that it will be useful, |
|
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. |
|
16 | * Lesser General Public License for more details. | |
17 | * |
|
17 | * | |
18 | * Further, this software is distributed without any warranty that it is |
|
18 | * Further, this software is distributed without any warranty that it is | |
19 | * free of the rightful claim of any third person regarding infringement |
|
19 | * free of the rightful claim of any third person regarding infringement | |
20 | * or the like. Any license provided herein, whether implied or |
|
20 | * or the like. Any license provided herein, whether implied or | |
21 | * otherwise, applies only to this software file. Patent licenses, if |
|
21 | * otherwise, applies only to this software file. Patent licenses, if | |
22 | * any, provided herein do not apply to combinations of this program with |
|
22 | * any, provided herein do not apply to combinations of this program with | |
23 | * other software, or any other product whatsoever. |
|
23 | * other software, or any other product whatsoever. | |
24 | * |
|
24 | * | |
25 | * You should have received a copy of the GNU Lesser General Public |
|
25 | * You should have received a copy of the GNU Lesser General Public | |
26 | * License along with this library; if not, write to the Free Software |
|
26 | * License along with this library; if not, write to the Free Software | |
27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
28 | * |
|
28 | * | |
29 | * Contact information: MeVis Research GmbH, Universitaetsallee 29, |
|
29 | * Contact information: MeVis Research GmbH, Universitaetsallee 29, | |
30 | * 28359 Bremen, Germany or: |
|
30 | * 28359 Bremen, Germany or: | |
31 | * |
|
31 | * | |
32 | * http://www.mevis.de |
|
32 | * http://www.mevis.de | |
33 | * |
|
33 | * | |
34 | */ |
|
34 | */ | |
35 |
|
35 | |||
36 | #include <QMetaObject> |
|
36 | #include <QMetaObject> | |
37 | #include <QMetaMethod> |
|
37 | #include <QMetaMethod> | |
38 | #include <QHash> |
|
38 | #include <QHash> | |
39 | #include <QByteArray> |
|
39 | #include <QByteArray> | |
40 | #include <QList> |
|
40 | #include <QList> | |
41 | #include "PythonQt.h" |
|
41 | #include "PythonQt.h" | |
42 |
|
42 | |||
43 | class PythonQtSlotInfo; |
|
43 | class PythonQtSlotInfo; | |
44 |
|
44 | |||
45 | struct PythonQtMemberInfo { |
|
45 | struct PythonQtMemberInfo { | |
46 | enum Type { |
|
46 | enum Type { | |
47 | Invalid, Slot, EnumValue, Property, NotFound |
|
47 | Invalid, Slot, EnumValue, Property, NotFound | |
48 | }; |
|
48 | }; | |
49 |
|
49 | |||
50 |
PythonQtMemberInfo():_slot(NULL),_enumValue(0) |
|
50 | PythonQtMemberInfo():_type(Invalid),_slot(NULL),_enumValue(0) { } | |
51 |
|
51 | |||
52 | PythonQtMemberInfo(PythonQtSlotInfo* info) { |
|
52 | PythonQtMemberInfo(PythonQtSlotInfo* info) { | |
53 | _type = Slot; |
|
53 | _type = Slot; | |
54 | _slot = info; |
|
54 | _slot = info; | |
55 | _enumValue = 0; |
|
55 | _enumValue = 0; | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | PythonQtMemberInfo(unsigned int enumValue) { |
|
58 | PythonQtMemberInfo(unsigned int enumValue) { | |
59 | _type = EnumValue; |
|
59 | _type = EnumValue; | |
60 | _slot = NULL; |
|
60 | _slot = NULL; | |
61 | _enumValue = enumValue; |
|
61 | _enumValue = enumValue; | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | PythonQtMemberInfo(const QMetaProperty& prop) { |
|
64 | PythonQtMemberInfo(const QMetaProperty& prop) { | |
65 | _type = Property; |
|
65 | _type = Property; | |
66 | _slot = NULL; |
|
66 | _slot = NULL; | |
67 | _enumValue = 0; |
|
67 | _enumValue = 0; | |
68 | _property = prop; |
|
68 | _property = prop; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | Type _type; |
|
71 | Type _type; | |
72 |
|
72 | |||
73 | // TODO: this could be a union... |
|
73 | // TODO: this could be a union... | |
74 | PythonQtSlotInfo* _slot; |
|
74 | PythonQtSlotInfo* _slot; | |
75 | unsigned int _enumValue; |
|
75 | unsigned int _enumValue; | |
76 | QMetaProperty _property; |
|
76 | QMetaProperty _property; | |
77 | }; |
|
77 | }; | |
78 |
|
78 | |||
79 | //! a class that stores all required information about a Qt object (and an optional associated C++ class name) |
|
79 | //! a class that stores all required information about a Qt object (and an optional associated C++ class name) | |
80 | /*! for fast lookup of slots when calling the object from Python |
|
80 | /*! for fast lookup of slots when calling the object from Python | |
81 | */ |
|
81 | */ | |
82 | class PythonQtClassInfo { |
|
82 | class PythonQtClassInfo { | |
83 |
|
83 | |||
84 | public: |
|
84 | public: | |
85 | PythonQtClassInfo(); |
|
85 | PythonQtClassInfo(); | |
86 | ~PythonQtClassInfo(); |
|
86 | ~PythonQtClassInfo(); | |
87 |
|
87 | |||
88 | //! store information about parent classes |
|
88 | //! store information about parent classes | |
89 | struct ParentClassInfo { |
|
89 | struct ParentClassInfo { | |
90 | ParentClassInfo(PythonQtClassInfo* parent, int upcastingOffset=0):_parent(parent),_upcastingOffset(upcastingOffset) |
|
90 | ParentClassInfo(PythonQtClassInfo* parent, int upcastingOffset=0):_parent(parent),_upcastingOffset(upcastingOffset) | |
91 | {}; |
|
91 | {}; | |
92 |
|
92 | |||
93 | PythonQtClassInfo* _parent; |
|
93 | PythonQtClassInfo* _parent; | |
94 | int _upcastingOffset; |
|
94 | int _upcastingOffset; | |
95 | }; |
|
95 | }; | |
96 |
|
96 | |||
97 |
|
97 | |||
98 | //! setup as a QObject, taking the meta object as meta information about the QObject |
|
98 | //! setup as a QObject, taking the meta object as meta information about the QObject | |
99 | void setupQObject(const QMetaObject* meta); |
|
99 | void setupQObject(const QMetaObject* meta); | |
100 |
|
100 | |||
101 | //! setup as a CPP (non-QObject), taking the classname |
|
101 | //! setup as a CPP (non-QObject), taking the classname | |
102 | void setupCPPObject(const QByteArray& classname); |
|
102 | void setupCPPObject(const QByteArray& classname); | |
103 |
|
103 | |||
104 | //! get the Python method definition for a given slot name (without return type and signature) |
|
104 | //! get the Python method definition for a given slot name (without return type and signature) | |
105 | PythonQtMemberInfo member(const char* member); |
|
105 | PythonQtMemberInfo member(const char* member); | |
106 |
|
106 | |||
107 | //! get access to the constructor slot (which may be overloaded if there are multiple constructors) |
|
107 | //! get access to the constructor slot (which may be overloaded if there are multiple constructors) | |
108 | PythonQtSlotInfo* constructors(); |
|
108 | PythonQtSlotInfo* constructors(); | |
109 |
|
109 | |||
110 | //! get access to the destructor slot |
|
110 | //! get access to the destructor slot | |
111 | PythonQtSlotInfo* destructor(); |
|
111 | PythonQtSlotInfo* destructor(); | |
112 |
|
112 | |||
113 | //! add a constructor, ownership is passed to classinfo |
|
113 | //! add a constructor, ownership is passed to classinfo | |
114 | void addConstructor(PythonQtSlotInfo* info); |
|
114 | void addConstructor(PythonQtSlotInfo* info); | |
115 |
|
115 | |||
116 | //! set a destructor, ownership is passed to classinfo |
|
116 | //! set a destructor, ownership is passed to classinfo | |
117 | void setDestructor(PythonQtSlotInfo* info); |
|
117 | void setDestructor(PythonQtSlotInfo* info); | |
118 |
|
118 | |||
119 | //! add a decorator slot, ownership is passed to classinfo |
|
119 | //! add a decorator slot, ownership is passed to classinfo | |
120 | void addDecoratorSlot(PythonQtSlotInfo* info); |
|
120 | void addDecoratorSlot(PythonQtSlotInfo* info); | |
121 |
|
121 | |||
122 | //! get the classname (either of the QObject or of the wrapped CPP object) |
|
122 | //! get the classname (either of the QObject or of the wrapped CPP object) | |
123 | const char* className(); |
|
123 | const char* className(); | |
124 |
|
124 | |||
125 | //! returns if the QObject |
|
125 | //! returns if the QObject | |
126 | bool isQObject() { return _isQObject; } |
|
126 | bool isQObject() { return _isQObject; } | |
127 |
|
127 | |||
128 | //! returns if the class is a CPP wrapper |
|
128 | //! returns if the class is a CPP wrapper | |
129 | bool isCPPWrapper() { return !_isQObject; } |
|
129 | bool isCPPWrapper() { return !_isQObject; } | |
130 |
|
130 | |||
131 | //! get the meta object |
|
131 | //! get the meta object | |
132 | const QMetaObject* metaObject() { return _meta; } |
|
132 | const QMetaObject* metaObject() { return _meta; } | |
133 |
|
133 | |||
134 | //! set the meta object, this will reset the caching |
|
134 | //! set the meta object, this will reset the caching | |
135 | void setMetaObject(const QMetaObject* meta); |
|
135 | void setMetaObject(const QMetaObject* meta); | |
136 |
|
136 | |||
137 | //! returns if this class inherits from the given classname |
|
137 | //! returns if this class inherits from the given classname | |
138 | bool inherits(const char* classname); |
|
138 | bool inherits(const char* classname); | |
139 |
|
139 | |||
140 | //! returns if this class inherits from the given classinfo |
|
140 | //! returns if this class inherits from the given classinfo | |
141 | bool inherits(PythonQtClassInfo* info); |
|
141 | bool inherits(PythonQtClassInfo* info); | |
142 |
|
142 | |||
143 | //! casts the given \c ptr to an object of type \c classname, returns the new pointer |
|
143 | //! casts the given \c ptr to an object of type \c classname, returns the new pointer | |
144 | //! which might be different to \c ptr due to C++ multiple inheritance |
|
144 | //! which might be different to \c ptr due to C++ multiple inheritance | |
145 | //! (if the cast is not possible or if ptr is NULL, NULL is returned) |
|
145 | //! (if the cast is not possible or if ptr is NULL, NULL is returned) | |
146 | void* castTo(void* ptr, const char* classname); |
|
146 | void* castTo(void* ptr, const char* classname); | |
147 |
|
147 | |||
148 | //! get help string for the metaobject |
|
148 | //! get help string for the metaobject | |
149 | QString help(); |
|
149 | QString help(); | |
150 |
|
150 | |||
151 | //! get list of all properties (on QObjects only, otherwise the list is empty) |
|
151 | //! get list of all properties (on QObjects only, otherwise the list is empty) | |
152 | QStringList propertyList(); |
|
152 | QStringList propertyList(); | |
153 |
|
153 | |||
154 | //! get list of all members |
|
154 | //! get list of all members | |
155 | QStringList memberList(bool metaOnly = false); |
|
155 | QStringList memberList(bool metaOnly = false); | |
156 |
|
156 | |||
157 | //! get the meta type id of this class (only valid for isCPPWrapper() == true) |
|
157 | //! get the meta type id of this class (only valid for isCPPWrapper() == true) | |
158 | int metaTypeId() { return _metaTypeId; } |
|
158 | int metaTypeId() { return _metaTypeId; } | |
159 |
|
159 | |||
160 | //! set an additional decorator provider that offers additional decorator slots for this class |
|
160 | //! set an additional decorator provider that offers additional decorator slots for this class | |
161 | void setDecoratorProvider(PythonQtQObjectCreatorFunctionCB* cb) { _decoratorProviderCB = cb; _decoratorProvider = NULL; } |
|
161 | void setDecoratorProvider(PythonQtQObjectCreatorFunctionCB* cb) { _decoratorProviderCB = cb; _decoratorProvider = NULL; } | |
162 |
|
162 | |||
163 | //! get the decorator qobject instance |
|
163 | //! get the decorator qobject instance | |
164 | QObject* decorator(); |
|
164 | QObject* decorator(); | |
165 |
|
165 | |||
166 | //! add the parent class info of a CPP object |
|
166 | //! add the parent class info of a CPP object | |
167 | void addParentClass(const ParentClassInfo& info) { _parentClasses.append(info); } |
|
167 | void addParentClass(const ParentClassInfo& info) { _parentClasses.append(info); } | |
168 |
|
168 | |||
169 | //! check if the special method "hasOwner" is implemented and if it returns false, which means that the object may be destroyed |
|
169 | //! check if the special method "hasOwner" is implemented and if it returns false, which means that the object may be destroyed | |
170 | bool hasOwnerMethodButNoOwner(void* object); |
|
170 | bool hasOwnerMethodButNoOwner(void* object); | |
171 |
|
171 | |||
172 | //! set the associated PythonQtClassWrapper (which handles instance creation of this type) |
|
172 | //! set the associated PythonQtClassWrapper (which handles instance creation of this type) | |
173 | void setPythonQtClassWrapper(PyObject* obj) { _pythonQtClassWrapper = obj; } |
|
173 | void setPythonQtClassWrapper(PyObject* obj) { _pythonQtClassWrapper = obj; } | |
174 |
|
174 | |||
175 | //! get the associated PythonQtClassWrapper (which handles instance creation of this type) |
|
175 | //! get the associated PythonQtClassWrapper (which handles instance creation of this type) | |
176 | PyObject* pythonQtClassWrapper() { return _pythonQtClassWrapper; } |
|
176 | PyObject* pythonQtClassWrapper() { return _pythonQtClassWrapper; } | |
177 |
|
177 | |||
178 | //! set the shell set instance wrapper cb |
|
178 | //! set the shell set instance wrapper cb | |
179 | void setShellSetInstanceWrapperCB(PythonQtShellSetInstanceWrapperCB* cb) { |
|
179 | void setShellSetInstanceWrapperCB(PythonQtShellSetInstanceWrapperCB* cb) { | |
180 | _shellSetInstanceWrapperCB = cb; |
|
180 | _shellSetInstanceWrapperCB = cb; | |
181 | } |
|
181 | } | |
182 |
|
182 | |||
183 | //! get the shell set instance wrapper cb |
|
183 | //! get the shell set instance wrapper cb | |
184 | PythonQtShellSetInstanceWrapperCB* shellSetInstanceWrapperCB() { |
|
184 | PythonQtShellSetInstanceWrapperCB* shellSetInstanceWrapperCB() { | |
185 | return _shellSetInstanceWrapperCB; |
|
185 | return _shellSetInstanceWrapperCB; | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | //! add a handler for polymorphic downcasting |
|
188 | //! add a handler for polymorphic downcasting | |
189 | void addPolymorphicHandler(PythonQtPolymorphicHandlerCB* cb) { _polymorphicHandlers.append(cb); } |
|
189 | void addPolymorphicHandler(PythonQtPolymorphicHandlerCB* cb) { _polymorphicHandlers.append(cb); } | |
190 |
|
190 | |||
191 | //! cast the pointer down in the class hierarchy if a polymorphic handler allows to do that |
|
191 | //! cast the pointer down in the class hierarchy if a polymorphic handler allows to do that | |
192 | void* castDownIfPossible(void* ptr, PythonQtClassInfo** resultClassInfo); |
|
192 | void* castDownIfPossible(void* ptr, PythonQtClassInfo** resultClassInfo); | |
193 |
|
193 | |||
194 | private: |
|
194 | private: | |
195 | //! clear all cached members |
|
195 | //! clear all cached members | |
196 | void clearCachedMembers(); |
|
196 | void clearCachedMembers(); | |
197 |
|
197 | |||
198 | void* recursiveCastDownIfPossible(void* ptr, char** resultClassName); |
|
198 | void* recursiveCastDownIfPossible(void* ptr, char** resultClassName); | |
199 |
|
199 | |||
200 | PythonQtSlotInfo* findDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset); |
|
200 | PythonQtSlotInfo* findDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset); | |
201 | void listDecoratorSlotsFromDecoratorProvider(QStringList& list, bool metaOnly); |
|
201 | void listDecoratorSlotsFromDecoratorProvider(QStringList& list, bool metaOnly); | |
202 | PythonQtSlotInfo* recursiveFindDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset); |
|
202 | PythonQtSlotInfo* recursiveFindDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset); | |
203 |
|
203 | |||
204 | void recursiveCollectClassInfos(QList<PythonQtClassInfo*>& classInfoObjects); |
|
204 | void recursiveCollectClassInfos(QList<PythonQtClassInfo*>& classInfoObjects); | |
205 | void recursiveCollectDecoratorObjects(QList<QObject*>& decoratorObjects); |
|
205 | void recursiveCollectDecoratorObjects(QList<QObject*>& decoratorObjects); | |
206 |
|
206 | |||
207 | bool lookForPropertyAndCache(const char* memberName); |
|
207 | bool lookForPropertyAndCache(const char* memberName); | |
208 | bool lookForMethodAndCache(const char* memberName); |
|
208 | bool lookForMethodAndCache(const char* memberName); | |
209 | bool lookForEnumAndCache(const QMetaObject* m, const char* memberName); |
|
209 | bool lookForEnumAndCache(const QMetaObject* m, const char* memberName); | |
210 |
|
210 | |||
211 | PythonQtSlotInfo* findDecoratorSlots(const char* memberName, int memberNameLen, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset); |
|
211 | PythonQtSlotInfo* findDecoratorSlots(const char* memberName, int memberNameLen, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset); | |
212 | int findCharOffset(const char* sigStart, char someChar); |
|
212 | int findCharOffset(const char* sigStart, char someChar); | |
213 |
|
213 | |||
214 | QHash<QByteArray, PythonQtMemberInfo> _cachedMembers; |
|
214 | QHash<QByteArray, PythonQtMemberInfo> _cachedMembers; | |
215 |
|
215 | |||
216 | PythonQtSlotInfo* _constructors; |
|
216 | PythonQtSlotInfo* _constructors; | |
217 | PythonQtSlotInfo* _destructor; |
|
217 | PythonQtSlotInfo* _destructor; | |
218 | QList<PythonQtSlotInfo*> _decoratorSlots; |
|
218 | QList<PythonQtSlotInfo*> _decoratorSlots; | |
219 |
|
219 | |||
220 | const QMetaObject* _meta; |
|
220 | const QMetaObject* _meta; | |
221 |
|
221 | |||
222 | QByteArray _wrappedClassName; |
|
222 | QByteArray _wrappedClassName; | |
223 | QList<ParentClassInfo> _parentClasses; |
|
223 | QList<ParentClassInfo> _parentClasses; | |
224 |
|
224 | |||
225 | QList<PythonQtPolymorphicHandlerCB*> _polymorphicHandlers; |
|
225 | QList<PythonQtPolymorphicHandlerCB*> _polymorphicHandlers; | |
226 |
|
226 | |||
227 | QObject* _decoratorProvider; |
|
227 | QObject* _decoratorProvider; | |
228 | PythonQtQObjectCreatorFunctionCB* _decoratorProviderCB; |
|
228 | PythonQtQObjectCreatorFunctionCB* _decoratorProviderCB; | |
229 |
|
229 | |||
230 | PyObject* _pythonQtClassWrapper; |
|
230 | PyObject* _pythonQtClassWrapper; | |
231 |
|
231 | |||
232 | PythonQtShellSetInstanceWrapperCB* _shellSetInstanceWrapperCB; |
|
232 | PythonQtShellSetInstanceWrapperCB* _shellSetInstanceWrapperCB; | |
233 |
|
233 | |||
234 | int _metaTypeId; |
|
234 | int _metaTypeId; | |
235 |
|
235 | |||
236 | bool _isQObject; |
|
236 | bool _isQObject; | |
237 |
|
237 | |||
238 | }; |
|
238 | }; | |
239 |
|
239 | |||
240 | //--------------------------------------------------------------- |
|
240 | //--------------------------------------------------------------- | |
241 |
|
241 | |||
242 |
|
242 | |||
243 | #endif |
|
243 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now