@@ -1,207 +1,204 | |||||
1 | #ifndef _PYTHONQTCONVERSION_H |
|
1 | #ifndef _PYTHONQTCONVERSION_H | |
2 | #define _PYTHONQTCONVERSION_H |
|
2 | #define _PYTHONQTCONVERSION_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 | //---------------------------------------------------------------------------------- |
|
36 | //---------------------------------------------------------------------------------- | |
37 | /*! |
|
37 | /*! | |
38 | // \file PythonQtConversion.h |
|
38 | // \file PythonQtConversion.h | |
39 | // \author Florian Link |
|
39 | // \author Florian Link | |
40 | // \author Last changed by $Author: florian $ |
|
40 | // \author Last changed by $Author: florian $ | |
41 | // \date 2006-05 |
|
41 | // \date 2006-05 | |
42 | */ |
|
42 | */ | |
43 | //---------------------------------------------------------------------------------- |
|
43 | //---------------------------------------------------------------------------------- | |
44 |
|
44 | |||
45 | #include "PythonQt.h" |
|
45 | #include "PythonQt.h" | |
46 | #include "PythonQtMisc.h" |
|
46 | #include "PythonQtMisc.h" | |
47 | #include "PythonQtClassInfo.h" |
|
47 | #include "PythonQtClassInfo.h" | |
48 | #include "PythonQtMethodInfo.h" |
|
48 | #include "PythonQtMethodInfo.h" | |
49 |
|
49 | |||
50 | #include <QWidget> |
|
50 | #include <QWidget> | |
51 | #include <QList> |
|
51 | #include <QList> | |
52 | #include <vector> |
|
52 | #include <vector> | |
53 |
|
53 | |||
54 | typedef PyObject* PythonQtConvertMetaTypeToPythonCB(const void* inObject, int metaTypeId); |
|
54 | typedef PyObject* PythonQtConvertMetaTypeToPythonCB(const void* inObject, int metaTypeId); | |
55 | typedef bool PythonQtConvertPythonToMetaTypeCB(PyObject* inObject, void* outObject, int metaTypeId, bool strict); |
|
55 | typedef bool PythonQtConvertPythonToMetaTypeCB(PyObject* inObject, void* outObject, int metaTypeId, bool strict); | |
56 |
|
56 | |||
57 | #define PythonQtRegisterListTemplateConverter(type, innertype) \ |
|
57 | #define PythonQtRegisterListTemplateConverter(type, innertype) \ | |
58 | { int typeId = qRegisterMetaType<type<innertype> >(#type"<"#innertype">"); \ |
|
58 | { int typeId = qRegisterMetaType<type<innertype> >(#type"<"#innertype">"); \ | |
59 | PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfValueType<type<innertype>, innertype>); \ |
|
59 | PythonQtConv::registerPythonToMetaTypeConverter(typeId, PythonQtConvertPythonListToListOfValueType<type<innertype>, innertype>); \ | |
60 | PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfValueTypeToPythonList<type<innertype>, innertype>); \ |
|
60 | PythonQtConv::registerMetaTypeToPythonConverter(typeId, PythonQtConvertListOfValueTypeToPythonList<type<innertype>, innertype>); \ | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | #define PythonQtRegisterToolClassesTemplateConverter(innertype) \ |
|
63 | #define PythonQtRegisterToolClassesTemplateConverter(innertype) \ | |
64 | PythonQtRegisterListTemplateConverter(QList, innertype); \ |
|
64 | PythonQtRegisterListTemplateConverter(QList, innertype); \ | |
65 | PythonQtRegisterListTemplateConverter(QVector, innertype); \ |
|
65 | PythonQtRegisterListTemplateConverter(QVector, innertype); \ | |
66 | PythonQtRegisterListTemplateConverter(std::vector, innertype); |
|
66 | PythonQtRegisterListTemplateConverter(std::vector, innertype); | |
67 | // TODO: add QHash etc. here! |
|
67 | // TODO: add QHash etc. here! | |
68 |
|
68 | |||
69 | //! a static class that offers methods for type conversion |
|
69 | //! a static class that offers methods for type conversion | |
70 | class PYTHONQT_EXPORT PythonQtConv { |
|
70 | class PYTHONQT_EXPORT PythonQtConv { | |
71 |
|
71 | |||
72 | public: |
|
72 | public: | |
73 |
|
73 | |||
74 | //! get a ref counted True or False Python object |
|
74 | //! get a ref counted True or False Python object | |
75 | static PyObject* GetPyBool(bool val); |
|
75 | static PyObject* GetPyBool(bool val); | |
76 |
|
76 | |||
77 | //! converts the Qt parameter given in \c data, interpreting it as a \c info parameter, into a Python object, |
|
77 | //! converts the Qt parameter given in \c data, interpreting it as a \c info parameter, into a Python object, | |
78 | static PyObject* ConvertQtValueToPython(const PythonQtMethodInfo::ParameterInfo& info, const void* data); |
|
78 | static PyObject* ConvertQtValueToPython(const PythonQtMethodInfo::ParameterInfo& info, const void* data); | |
79 |
|
79 | |||
80 | //! convert python object to Qt (according to the given parameter) and if the conversion should be strict (classInfo is currently not used anymore) |
|
80 | //! convert python object to Qt (according to the given parameter) and if the conversion should be strict (classInfo is currently not used anymore) | |
81 | static void* ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& info, PyObject* obj, bool strict, PythonQtClassInfo* classInfo, void* alreadyAllocatedCPPObject = NULL); |
|
81 | static void* ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& info, PyObject* obj, bool strict, PythonQtClassInfo* classInfo, void* alreadyAllocatedCPPObject = NULL); | |
82 |
|
82 | |||
83 | //! creates a data storage for the passed parameter type and returns a void pointer to be set as arg[0] of qt_metacall |
|
83 | //! creates a data storage for the passed parameter type and returns a void pointer to be set as arg[0] of qt_metacall | |
84 | static void* CreateQtReturnValue(const PythonQtMethodInfo::ParameterInfo& info); |
|
84 | static void* CreateQtReturnValue(const PythonQtMethodInfo::ParameterInfo& info); | |
85 |
|
85 | |||
86 | //! converts QString to Python string (unicode!) |
|
86 | //! converts QString to Python string (unicode!) | |
87 | static PyObject* QStringToPyObject(const QString& str); |
|
87 | static PyObject* QStringToPyObject(const QString& str); | |
88 |
|
88 | |||
89 | //! converts QStringList to Python tuple |
|
89 | //! converts QStringList to Python tuple | |
90 | static PyObject* QStringListToPyObject(const QStringList& list); |
|
90 | static PyObject* QStringListToPyObject(const QStringList& list); | |
91 |
|
91 | |||
92 | //! converts QStringList to Python list |
|
92 | //! converts QStringList to Python list | |
93 | static PyObject* QStringListToPyList(const QStringList& list); |
|
93 | static PyObject* QStringListToPyList(const QStringList& list); | |
94 |
|
94 | |||
95 | //! get string representation of py object |
|
95 | //! get string representation of py object | |
96 | static QString PyObjGetRepresentation(PyObject* val); |
|
96 | static QString PyObjGetRepresentation(PyObject* val); | |
97 |
|
97 | |||
98 | //! get string value from py object |
|
98 | //! get string value from py object | |
99 | static QString PyObjGetString(PyObject* val) { bool ok; QString s = PyObjGetString(val, false, ok); return s; } |
|
99 | static QString PyObjGetString(PyObject* val) { bool ok; QString s = PyObjGetString(val, false, ok); return s; } | |
100 | //! get string value from py object |
|
100 | //! get string value from py object | |
101 | static QString PyObjGetString(PyObject* val, bool strict, bool &ok); |
|
101 | static QString PyObjGetString(PyObject* val, bool strict, bool &ok); | |
102 | //! get bytes from py object |
|
102 | //! get bytes from py object | |
103 | static QByteArray PyObjGetBytes(PyObject* val, bool strict, bool &ok); |
|
103 | static QByteArray PyObjGetBytes(PyObject* val, bool strict, bool &ok); | |
104 | //! get int from py object |
|
104 | //! get int from py object | |
105 | static int PyObjGetInt(PyObject* val, bool strict, bool &ok); |
|
105 | static int PyObjGetInt(PyObject* val, bool strict, bool &ok); | |
106 | //! get int64 from py object |
|
106 | //! get int64 from py object | |
107 | static qint64 PyObjGetLongLong(PyObject* val, bool strict, bool &ok); |
|
107 | static qint64 PyObjGetLongLong(PyObject* val, bool strict, bool &ok); | |
108 | //! get int64 from py object |
|
108 | //! get int64 from py object | |
109 | static quint64 PyObjGetULongLong(PyObject* val, bool strict, bool &ok); |
|
109 | static quint64 PyObjGetULongLong(PyObject* val, bool strict, bool &ok); | |
110 | //! get double from py object |
|
110 | //! get double from py object | |
111 | static double PyObjGetDouble(PyObject* val, bool strict, bool &ok); |
|
111 | static double PyObjGetDouble(PyObject* val, bool strict, bool &ok); | |
112 | //! get bool from py object |
|
112 | //! get bool from py object | |
113 | static bool PyObjGetBool(PyObject* val, bool strict, bool &ok); |
|
113 | static bool PyObjGetBool(PyObject* val, bool strict, bool &ok); | |
114 |
|
114 | |||
115 | //! create a string list from python sequence |
|
115 | //! create a string list from python sequence | |
116 | static QStringList PyObjToStringList(PyObject* val, bool strict, bool& ok); |
|
116 | static QStringList PyObjToStringList(PyObject* val, bool strict, bool& ok); | |
117 |
|
117 | |||
118 | //! convert python object to qvariant, if type is given it will try to create a qvariant of that type, otherwise |
|
118 | //! convert python object to qvariant, if type is given it will try to create a qvariant of that type, otherwise | |
119 | //! it will guess from the python type |
|
119 | //! it will guess from the python type | |
120 | static QVariant PyObjToQVariant(PyObject* val, int type = -1); |
|
120 | static QVariant PyObjToQVariant(PyObject* val, int type = -1); | |
121 |
|
121 | |||
122 | //! convert QVariant from PyObject |
|
122 | //! convert QVariant from PyObject | |
123 | static PyObject* QVariantToPyObject(const QVariant& v); |
|
123 | static PyObject* QVariantToPyObject(const QVariant& v); | |
124 |
|
124 | |||
125 | static PyObject* QVariantMapToPyObject(const QVariantMap& m); |
|
125 | static PyObject* QVariantMapToPyObject(const QVariantMap& m); | |
126 | static PyObject* QVariantListToPyObject(const QVariantList& l); |
|
126 | static PyObject* QVariantListToPyObject(const QVariantList& l); | |
127 |
|
127 | |||
128 | //! get human readable string from qvariant |
|
|||
129 | static QString qVariantToString(const QVariant& v); |
|
|||
130 |
|
||||
131 | //! get human readable string from CPP object (when the metatype is known) |
|
128 | //! get human readable string from CPP object (when the metatype is known) | |
132 | static QString CPPObjectToString(int type, const void* data); |
|
129 | static QString CPPObjectToString(int type, const void* data); | |
133 |
|
130 | |||
134 | //! register a converter callback from python to cpp for given metatype |
|
131 | //! register a converter callback from python to cpp for given metatype | |
135 | static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB* cb) { _pythonToMetaTypeConverters.insert(metaTypeId, cb); } |
|
132 | static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB* cb) { _pythonToMetaTypeConverters.insert(metaTypeId, cb); } | |
136 |
|
133 | |||
137 | //! register a converter callback from cpp to python for given metatype |
|
134 | //! register a converter callback from cpp to python for given metatype | |
138 | static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB* cb) { _metaTypeToPythonConverters.insert(metaTypeId, cb); } |
|
135 | static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB* cb) { _metaTypeToPythonConverters.insert(metaTypeId, cb); } | |
139 |
|
136 | |||
140 | //! returns the inner type id of a simple template of the form SomeObject<InnerType> |
|
137 | //! returns the inner type id of a simple template of the form SomeObject<InnerType> | |
141 | static int getInnerTemplateMetaType(const QByteArray& typeName); |
|
138 | static int getInnerTemplateMetaType(const QByteArray& typeName); | |
142 |
|
139 | |||
143 | //! converts the Qt parameter given in \c data, interpreting it as a \c type registered qvariant/meta type, into a Python object, |
|
140 | //! converts the Qt parameter given in \c data, interpreting it as a \c type registered qvariant/meta type, into a Python object, | |
144 | static PyObject* ConvertQtValueToPythonInternal(int type, const void* data); |
|
141 | static PyObject* ConvertQtValueToPythonInternal(int type, const void* data); | |
145 |
|
142 | |||
146 | public: |
|
143 | public: | |
147 |
|
144 | |||
148 | static PythonQtValueStorage<qint64, 128> global_valueStorage; |
|
145 | static PythonQtValueStorage<qint64, 128> global_valueStorage; | |
149 | static PythonQtValueStorage<void*, 128> global_ptrStorage; |
|
146 | static PythonQtValueStorage<void*, 128> global_ptrStorage; | |
150 | static PythonQtValueStorage<QVariant, 32> global_variantStorage; |
|
147 | static PythonQtValueStorage<QVariant, 32> global_variantStorage; | |
151 |
|
148 | |||
152 | protected: |
|
149 | protected: | |
153 | static QHash<int, PythonQtConvertMetaTypeToPythonCB*> _metaTypeToPythonConverters; |
|
150 | static QHash<int, PythonQtConvertMetaTypeToPythonCB*> _metaTypeToPythonConverters; | |
154 | static QHash<int, PythonQtConvertPythonToMetaTypeCB*> _pythonToMetaTypeConverters; |
|
151 | static QHash<int, PythonQtConvertPythonToMetaTypeCB*> _pythonToMetaTypeConverters; | |
155 |
|
152 | |||
156 | //! handle automatic conversion of some special types (QColor, QBrush, ...) |
|
153 | //! handle automatic conversion of some special types (QColor, QBrush, ...) | |
157 | static void* handlePythonToQtAutoConversion(int typeId, PyObject* obj, void* alreadyAllocatedCPPObject); |
|
154 | static void* handlePythonToQtAutoConversion(int typeId, PyObject* obj, void* alreadyAllocatedCPPObject); | |
158 |
|
155 | |||
159 | //! converts the list of pointers of given type to Python |
|
156 | //! converts the list of pointers of given type to Python | |
160 | static PyObject* ConvertQListOfPointerTypeToPythonList(QList<void*>* list, const QByteArray& type); |
|
157 | static PyObject* ConvertQListOfPointerTypeToPythonList(QList<void*>* list, const QByteArray& type); | |
161 | //! tries to convert the python object to a QList of pointers to \c type objects, returns true on success |
|
158 | //! tries to convert the python object to a QList of pointers to \c type objects, returns true on success | |
162 | static bool ConvertPythonListToQListOfPointerType(PyObject* obj, QList<void*>* list, const QByteArray& type, bool strict); |
|
159 | static bool ConvertPythonListToQListOfPointerType(PyObject* obj, QList<void*>* list, const QByteArray& type, bool strict); | |
163 |
|
160 | |||
164 | //! cast wrapper to given className if possible |
|
161 | //! cast wrapper to given className if possible | |
165 | static void* castWrapperTo(PythonQtInstanceWrapper* wrapper, const QByteArray& className, bool& ok); |
|
162 | static void* castWrapperTo(PythonQtInstanceWrapper* wrapper, const QByteArray& className, bool& ok); | |
166 | }; |
|
163 | }; | |
167 |
|
164 | |||
168 | template<class ListType, class T> |
|
165 | template<class ListType, class T> | |
169 | PyObject* PythonQtConvertListOfValueTypeToPythonList(const void* /*QList<T>* */ inList, int metaTypeId) |
|
166 | PyObject* PythonQtConvertListOfValueTypeToPythonList(const void* /*QList<T>* */ inList, int metaTypeId) | |
170 | { |
|
167 | { | |
171 | ListType* list = (ListType*)inList; |
|
168 | ListType* list = (ListType*)inList; | |
172 | static const int innerType = PythonQtConv::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId))); |
|
169 | static const int innerType = PythonQtConv::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId))); | |
173 | PyObject* result = PyTuple_New(list->size()); |
|
170 | PyObject* result = PyTuple_New(list->size()); | |
174 | int i = 0; |
|
171 | int i = 0; | |
175 | foreach (const T& value, *list) { |
|
172 | foreach (const T& value, *list) { | |
176 | PyTuple_SET_ITEM(result, i, PythonQtConv::ConvertQtValueToPythonInternal(innerType, &value)); |
|
173 | PyTuple_SET_ITEM(result, i, PythonQtConv::ConvertQtValueToPythonInternal(innerType, &value)); | |
177 | i++; |
|
174 | i++; | |
178 | } |
|
175 | } | |
179 | return result; |
|
176 | return result; | |
180 | } |
|
177 | } | |
181 |
|
178 | |||
182 | template<class ListType, class T> |
|
179 | template<class ListType, class T> | |
183 | bool PythonQtConvertPythonListToListOfValueType(PyObject* obj, void* /*QList<T>* */ outList, int metaTypeId, bool /*strict*/) |
|
180 | bool PythonQtConvertPythonListToListOfValueType(PyObject* obj, void* /*QList<T>* */ outList, int metaTypeId, bool /*strict*/) | |
184 | { |
|
181 | { | |
185 | ListType* list = (ListType*)outList; |
|
182 | ListType* list = (ListType*)outList; | |
186 | static const int innerType = PythonQtConv::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId))); |
|
183 | static const int innerType = PythonQtConv::getInnerTemplateMetaType(QByteArray(QMetaType::typeName(metaTypeId))); | |
187 | bool result = false; |
|
184 | bool result = false; | |
188 | if (PySequence_Check(obj)) { |
|
185 | if (PySequence_Check(obj)) { | |
189 | result = true; |
|
186 | result = true; | |
190 | int count = PySequence_Size(obj); |
|
187 | int count = PySequence_Size(obj); | |
191 | PyObject* value; |
|
188 | PyObject* value; | |
192 | for (int i = 0;i<count;i++) { |
|
189 | for (int i = 0;i<count;i++) { | |
193 | value = PySequence_GetItem(obj,i); |
|
190 | value = PySequence_GetItem(obj,i); | |
194 | // this is quite some overhead, but it avoids having another large switch... |
|
191 | // this is quite some overhead, but it avoids having another large switch... | |
195 | QVariant v = PythonQtConv::PyObjToQVariant(value, innerType); |
|
192 | QVariant v = PythonQtConv::PyObjToQVariant(value, innerType); | |
196 | if (v.isValid()) { |
|
193 | if (v.isValid()) { | |
197 | list->push_back(qVariantValue<T>(v)); |
|
194 | list->push_back(qVariantValue<T>(v)); | |
198 | } else { |
|
195 | } else { | |
199 | result = false; |
|
196 | result = false; | |
200 | break; |
|
197 | break; | |
201 | } |
|
198 | } | |
202 | } |
|
199 | } | |
203 | } |
|
200 | } | |
204 | return result; |
|
201 | return result; | |
205 | } |
|
202 | } | |
206 |
|
203 | |||
207 | #endif |
|
204 | #endif |
@@ -1,620 +1,620 | |||||
1 | /* |
|
1 | /* | |
2 | * |
|
2 | * | |
3 | * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. |
|
3 | * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. | |
4 | * |
|
4 | * | |
5 | * This library is free software; you can redistribute it and/or |
|
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public |
|
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either |
|
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. |
|
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * |
|
9 | * | |
10 | * This library is distributed in the hope that it will be useful, |
|
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. |
|
13 | * Lesser General Public License for more details. | |
14 | * |
|
14 | * | |
15 | * Further, this software is distributed without any warranty that it is |
|
15 | * Further, this software is distributed without any warranty that it is | |
16 | * free of the rightful claim of any third person regarding infringement |
|
16 | * free of the rightful claim of any third person regarding infringement | |
17 | * or the like. Any license provided herein, whether implied or |
|
17 | * or the like. Any license provided herein, whether implied or | |
18 | * otherwise, applies only to this software file. Patent licenses, if |
|
18 | * otherwise, applies only to this software file. Patent licenses, if | |
19 | * any, provided herein do not apply to combinations of this program with |
|
19 | * any, provided herein do not apply to combinations of this program with | |
20 | * other software, or any other product whatsoever. |
|
20 | * other software, or any other product whatsoever. | |
21 | * |
|
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public |
|
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this library; if not, write to the Free Software |
|
23 | * License along with this library; if not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
25 | * |
|
25 | * | |
26 | * Contact information: MeVis Research GmbH, Universitaetsallee 29, |
|
26 | * Contact information: MeVis Research GmbH, Universitaetsallee 29, | |
27 | * 28359 Bremen, Germany or: |
|
27 | * 28359 Bremen, Germany or: | |
28 | * |
|
28 | * | |
29 | * http://www.mevis.de |
|
29 | * http://www.mevis.de | |
30 | * |
|
30 | * | |
31 | */ |
|
31 | */ | |
32 |
|
32 | |||
33 | //---------------------------------------------------------------------------------- |
|
33 | //---------------------------------------------------------------------------------- | |
34 | /*! |
|
34 | /*! | |
35 | // \file PythonQtInstanceWrapper.cpp |
|
35 | // \file PythonQtInstanceWrapper.cpp | |
36 | // \author Florian Link |
|
36 | // \author Florian Link | |
37 | // \author Last changed by $Author: florian $ |
|
37 | // \author Last changed by $Author: florian $ | |
38 | // \date 2006-05 |
|
38 | // \date 2006-05 | |
39 | */ |
|
39 | */ | |
40 | //---------------------------------------------------------------------------------- |
|
40 | //---------------------------------------------------------------------------------- | |
41 |
|
41 | |||
42 | #include "PythonQtInstanceWrapper.h" |
|
42 | #include "PythonQtInstanceWrapper.h" | |
43 | #include <QObject> |
|
43 | #include <QObject> | |
44 | #include "PythonQt.h" |
|
44 | #include "PythonQt.h" | |
45 | #include "PythonQtSlot.h" |
|
45 | #include "PythonQtSlot.h" | |
46 | #include "PythonQtClassInfo.h" |
|
46 | #include "PythonQtClassInfo.h" | |
47 | #include "PythonQtConversion.h" |
|
47 | #include "PythonQtConversion.h" | |
48 | #include "PythonQtClassWrapper.h" |
|
48 | #include "PythonQtClassWrapper.h" | |
49 |
|
49 | |||
50 | PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo() |
|
50 | PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo() | |
51 | { |
|
51 | { | |
52 | // take the class info from our type object |
|
52 | // take the class info from our type object | |
53 | return ((PythonQtClassWrapper*)ob_type)->_classInfo; |
|
53 | return ((PythonQtClassWrapper*)ob_type)->_classInfo; | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) { |
|
56 | static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) { | |
57 |
|
57 | |||
58 | // is this a C++ wrapper? |
|
58 | // is this a C++ wrapper? | |
59 | if (self->_wrappedPtr) { |
|
59 | if (self->_wrappedPtr) { | |
60 | //mlabDebugConst("Python","c++ wrapper removed " << self->_wrappedPtr << " " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1()); |
|
60 | //mlabDebugConst("Python","c++ wrapper removed " << self->_wrappedPtr << " " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1()); | |
61 |
|
61 | |||
62 | PythonQt::priv()->removeWrapperPointer(self->_wrappedPtr); |
|
62 | PythonQt::priv()->removeWrapperPointer(self->_wrappedPtr); | |
63 | // we own our qobject, so we delete it now: |
|
63 | // we own our qobject, so we delete it now: | |
64 | delete self->_obj; |
|
64 | delete self->_obj; | |
65 | self->_obj = NULL; |
|
65 | self->_obj = NULL; | |
66 | if (force || self->classInfo()->hasOwnerMethodButNoOwner(self->_wrappedPtr) || self->_ownedByPythonQt) { |
|
66 | if (force || self->classInfo()->hasOwnerMethodButNoOwner(self->_wrappedPtr) || self->_ownedByPythonQt) { | |
67 | int type = self->classInfo()->metaTypeId(); |
|
67 | int type = self->classInfo()->metaTypeId(); | |
68 | if (self->_useQMetaTypeDestroy && type>=0) { |
|
68 | if (self->_useQMetaTypeDestroy && type>=0) { | |
69 | // use QMetaType to destroy the object |
|
69 | // use QMetaType to destroy the object | |
70 | QMetaType::destroy(type, self->_wrappedPtr); |
|
70 | QMetaType::destroy(type, self->_wrappedPtr); | |
71 | } else { |
|
71 | } else { | |
72 | PythonQtSlotInfo* slot = self->classInfo()->destructor(); |
|
72 | PythonQtSlotInfo* slot = self->classInfo()->destructor(); | |
73 | if (slot) { |
|
73 | if (slot) { | |
74 | void* args[2]; |
|
74 | void* args[2]; | |
75 | args[0] = NULL; |
|
75 | args[0] = NULL; | |
76 | args[1] = &self->_wrappedPtr; |
|
76 | args[1] = &self->_wrappedPtr; | |
77 | slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, slot->slotIndex(), args); |
|
77 | slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, slot->slotIndex(), args); | |
78 | self->_wrappedPtr = NULL; |
|
78 | self->_wrappedPtr = NULL; | |
79 | } else { |
|
79 | } else { | |
80 | if (type>=0) { |
|
80 | if (type>=0) { | |
81 | // use QMetaType to destroy the object |
|
81 | // use QMetaType to destroy the object | |
82 | QMetaType::destroy(type, self->_wrappedPtr); |
|
82 | QMetaType::destroy(type, self->_wrappedPtr); | |
83 | } else { |
|
83 | } else { | |
84 | // TODO: warn about not being able to destroy the object? |
|
84 | // TODO: warn about not being able to destroy the object? | |
85 | } |
|
85 | } | |
86 | } |
|
86 | } | |
87 | } |
|
87 | } | |
88 | } |
|
88 | } | |
89 | } else { |
|
89 | } else { | |
90 | //mlabDebugConst("Python","qobject wrapper removed " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1()); |
|
90 | //mlabDebugConst("Python","qobject wrapper removed " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1()); | |
91 | if (self->_objPointerCopy) { |
|
91 | if (self->_objPointerCopy) { | |
92 | PythonQt::priv()->removeWrapperPointer(self->_objPointerCopy); |
|
92 | PythonQt::priv()->removeWrapperPointer(self->_objPointerCopy); | |
93 | } |
|
93 | } | |
94 | if (self->_obj) { |
|
94 | if (self->_obj) { | |
95 | if (force || self->_ownedByPythonQt) { |
|
95 | if (force || self->_ownedByPythonQt) { | |
96 | if (force || !self->_obj->parent()) { |
|
96 | if (force || !self->_obj->parent()) { | |
97 | delete self->_obj; |
|
97 | delete self->_obj; | |
98 | } |
|
98 | } | |
99 | } else { |
|
99 | } else { | |
100 | if (self->_obj->parent()==NULL) { |
|
100 | if (self->_obj->parent()==NULL) { | |
101 | // tell someone who is interested that the qobject is no longer wrapped, if it has no parent |
|
101 | // tell someone who is interested that the qobject is no longer wrapped, if it has no parent | |
102 | PythonQt::qObjectNoLongerWrappedCB(self->_obj); |
|
102 | PythonQt::qObjectNoLongerWrappedCB(self->_obj); | |
103 | } |
|
103 | } | |
104 | } |
|
104 | } | |
105 | } |
|
105 | } | |
106 | } |
|
106 | } | |
107 | self->_obj = NULL; |
|
107 | self->_obj = NULL; | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 | static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self) |
|
110 | static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self) | |
111 | { |
|
111 | { | |
112 | PythonQtInstanceWrapper_deleteObject(self); |
|
112 | PythonQtInstanceWrapper_deleteObject(self); | |
113 | self->_obj.~QPointer<QObject>(); |
|
113 | self->_obj.~QPointer<QObject>(); | |
114 | self->ob_type->tp_free((PyObject*)self); |
|
114 | self->ob_type->tp_free((PyObject*)self); | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/) |
|
117 | static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/) | |
118 | { |
|
118 | { | |
119 | //PythonQtClassWrapper *classType = (PythonQtClassWrapper*)type; |
|
119 | //PythonQtClassWrapper *classType = (PythonQtClassWrapper*)type; | |
120 | PythonQtInstanceWrapper *self; |
|
120 | PythonQtInstanceWrapper *self; | |
121 | static PyObject* emptyTuple = NULL; |
|
121 | static PyObject* emptyTuple = NULL; | |
122 | if (emptyTuple==NULL) { |
|
122 | if (emptyTuple==NULL) { | |
123 | emptyTuple = PyTuple_New(0); |
|
123 | emptyTuple = PyTuple_New(0); | |
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | self = (PythonQtInstanceWrapper*)PyBaseObject_Type.tp_new(type, emptyTuple, NULL); |
|
126 | self = (PythonQtInstanceWrapper*)PyBaseObject_Type.tp_new(type, emptyTuple, NULL); | |
127 |
|
127 | |||
128 | if (self != NULL) { |
|
128 | if (self != NULL) { | |
129 | new (&self->_obj) QPointer<QObject>(); |
|
129 | new (&self->_obj) QPointer<QObject>(); | |
130 | self->_wrappedPtr = NULL; |
|
130 | self->_wrappedPtr = NULL; | |
131 | self->_ownedByPythonQt = false; |
|
131 | self->_ownedByPythonQt = false; | |
132 | self->_useQMetaTypeDestroy = false; |
|
132 | self->_useQMetaTypeDestroy = false; | |
133 | self->_isShellInstance = false; |
|
133 | self->_isShellInstance = false; | |
134 | } |
|
134 | } | |
135 | return (PyObject *)self; |
|
135 | return (PyObject *)self; | |
136 | } |
|
136 | } | |
137 |
|
137 | |||
138 | int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds) |
|
138 | int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds) | |
139 | { |
|
139 | { | |
140 | if (args == PythonQtPrivate::dummyTuple()) { |
|
140 | if (args == PythonQtPrivate::dummyTuple()) { | |
141 | // we are called from the internal PythonQt API, so our data will be filled later on... |
|
141 | // we are called from the internal PythonQt API, so our data will be filled later on... | |
142 | return 0; |
|
142 | return 0; | |
143 | } |
|
143 | } | |
144 |
|
144 | |||
145 | // we are called from python, try to construct our object |
|
145 | // we are called from python, try to construct our object | |
146 | if (self->classInfo()->constructors()) { |
|
146 | if (self->classInfo()->constructors()) { | |
147 | void* directCPPPointer = NULL; |
|
147 | void* directCPPPointer = NULL; | |
148 | PythonQtSlotFunction_CallImpl(self->classInfo(), NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer); |
|
148 | PythonQtSlotFunction_CallImpl(self->classInfo(), NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer); | |
149 | if (PyErr_Occurred()) { |
|
149 | if (PyErr_Occurred()) { | |
150 | return -1; |
|
150 | return -1; | |
151 | } |
|
151 | } | |
152 | if (directCPPPointer) { |
|
152 | if (directCPPPointer) { | |
153 | // change ownershipflag to be owned by PythonQt |
|
153 | // change ownershipflag to be owned by PythonQt | |
154 | self->_ownedByPythonQt = true; |
|
154 | self->_ownedByPythonQt = true; | |
155 | self->_useQMetaTypeDestroy = false; |
|
155 | self->_useQMetaTypeDestroy = false; | |
156 | if (self->classInfo()->isCPPWrapper()) { |
|
156 | if (self->classInfo()->isCPPWrapper()) { | |
157 | self->_wrappedPtr = directCPPPointer; |
|
157 | self->_wrappedPtr = directCPPPointer; | |
158 | // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?! |
|
158 | // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?! | |
159 | } else { |
|
159 | } else { | |
160 | self->setQObject((QObject*)directCPPPointer); |
|
160 | self->setQObject((QObject*)directCPPPointer); | |
161 | } |
|
161 | } | |
162 | // register with PythonQt |
|
162 | // register with PythonQt | |
163 | PythonQt::priv()->addWrapperPointer(directCPPPointer, self); |
|
163 | PythonQt::priv()->addWrapperPointer(directCPPPointer, self); | |
164 |
|
164 | |||
165 | PythonQtShellSetInstanceWrapperCB* cb = self->classInfo()->shellSetInstanceWrapperCB(); |
|
165 | PythonQtShellSetInstanceWrapperCB* cb = self->classInfo()->shellSetInstanceWrapperCB(); | |
166 | if (cb) { |
|
166 | if (cb) { | |
167 | // if we are a derived python class, we set the wrapper |
|
167 | // if we are a derived python class, we set the wrapper | |
168 | // to activate the shell class, otherwise we just ignore that it is a shell... |
|
168 | // to activate the shell class, otherwise we just ignore that it is a shell... | |
169 | // we detect it be checking if the type does not have PythonQtInstanceWrapper_Type as direct base class, |
|
169 | // we detect it be checking if the type does not have PythonQtInstanceWrapper_Type as direct base class, | |
170 | // which is the case for all non-python derived types |
|
170 | // which is the case for all non-python derived types | |
171 | if (((PyObject*)self)->ob_type->tp_base != &PythonQtInstanceWrapper_Type) { |
|
171 | if (((PyObject*)self)->ob_type->tp_base != &PythonQtInstanceWrapper_Type) { | |
172 | // set the wrapper and remember that we have a shell instance! |
|
172 | // set the wrapper and remember that we have a shell instance! | |
173 | (*cb)(directCPPPointer, self); |
|
173 | (*cb)(directCPPPointer, self); | |
174 | self->_isShellInstance = true; |
|
174 | self->_isShellInstance = true; | |
175 | } |
|
175 | } | |
176 | } |
|
176 | } | |
177 | } |
|
177 | } | |
178 | } else { |
|
178 | } else { | |
179 | QString error = QString("No constructors available for ") + self->classInfo()->className(); |
|
179 | QString error = QString("No constructors available for ") + self->classInfo()->className(); | |
180 | PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); |
|
180 | PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); | |
181 | return -1; |
|
181 | return -1; | |
182 | } |
|
182 | } | |
183 | return 0; |
|
183 | return 0; | |
184 | } |
|
184 | } | |
185 |
|
185 | |||
186 | static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* obj) |
|
186 | static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* obj) | |
187 | { |
|
187 | { | |
188 | return PyString_FromString(obj->ob_type->tp_name); |
|
188 | return PyString_FromString(obj->ob_type->tp_name); | |
189 | } |
|
189 | } | |
190 |
|
190 | |||
191 | static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* obj) |
|
191 | static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* obj) | |
192 | { |
|
192 | { | |
193 | return PythonQt::self()->helpCalled(obj->classInfo()); |
|
193 | return PythonQt::self()->helpCalled(obj->classInfo()); | |
194 | } |
|
194 | } | |
195 |
|
195 | |||
196 | static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self) |
|
196 | static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self) | |
197 | { |
|
197 | { | |
198 | PythonQtInstanceWrapper_deleteObject(self, true); |
|
198 | PythonQtInstanceWrapper_deleteObject(self, true); | |
199 | Py_INCREF(Py_None); |
|
199 | Py_INCREF(Py_None); | |
200 | return Py_None; |
|
200 | return Py_None; | |
201 | } |
|
201 | } | |
202 |
|
202 | |||
203 |
|
203 | |||
204 | static PyMethodDef PythonQtInstanceWrapper_methods[] = { |
|
204 | static PyMethodDef PythonQtInstanceWrapper_methods[] = { | |
205 | {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS, |
|
205 | {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS, | |
206 | "Return the classname of the object" |
|
206 | "Return the classname of the object" | |
207 | }, |
|
207 | }, | |
208 | {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS, |
|
208 | {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS, | |
209 | "Shows the help of available methods for this class" |
|
209 | "Shows the help of available methods for this class" | |
210 | }, |
|
210 | }, | |
211 | {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS, |
|
211 | {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS, | |
212 | "Deletes the C++ object (at your own risk, my friend!)" |
|
212 | "Deletes the C++ object (at your own risk, my friend!)" | |
213 | }, |
|
213 | }, | |
214 | {NULL, NULL, 0, NULL} /* Sentinel */ |
|
214 | {NULL, NULL, 0, NULL} /* Sentinel */ | |
215 | }; |
|
215 | }; | |
216 |
|
216 | |||
217 |
|
217 | |||
218 | static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name) |
|
218 | static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name) | |
219 | { |
|
219 | { | |
220 | const char *attributeName; |
|
220 | const char *attributeName; | |
221 | PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj; |
|
221 | PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj; | |
222 |
|
222 | |||
223 | if ((attributeName = PyString_AsString(name)) == NULL) { |
|
223 | if ((attributeName = PyString_AsString(name)) == NULL) { | |
224 | return NULL; |
|
224 | return NULL; | |
225 | } |
|
225 | } | |
226 |
|
226 | |||
227 | if (qstrcmp(attributeName, "__dict__")==0) { |
|
227 | if (qstrcmp(attributeName, "__dict__")==0) { | |
228 | PyObject* dict = PyBaseObject_Type.tp_getattro(obj, name); |
|
228 | PyObject* dict = PyBaseObject_Type.tp_getattro(obj, name); | |
229 | dict = PyDict_Copy(dict); |
|
229 | dict = PyDict_Copy(dict); | |
230 |
|
230 | |||
231 | if (wrapper->_obj) { |
|
231 | if (wrapper->_obj) { | |
232 | // only the properties are missing, the rest is already available from |
|
232 | // only the properties are missing, the rest is already available from | |
233 | // PythonQtClassWrapper... |
|
233 | // PythonQtClassWrapper... | |
234 | QStringList l = wrapper->classInfo()->propertyList(); |
|
234 | QStringList l = wrapper->classInfo()->propertyList(); | |
235 | foreach (QString name, l) { |
|
235 | foreach (QString name, l) { | |
236 | PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data()); |
|
236 | PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data()); | |
237 | if (o) { |
|
237 | if (o) { | |
238 | PyDict_SetItemString(dict, name.toLatin1().data(), o); |
|
238 | PyDict_SetItemString(dict, name.toLatin1().data(), o); | |
239 | Py_DECREF(o); |
|
239 | Py_DECREF(o); | |
240 | } else { |
|
240 | } else { | |
241 | std::cerr << "PythonQtInstanceWrapper: something is wrong, could not get attribute " << name.toLatin1().data(); |
|
241 | std::cerr << "PythonQtInstanceWrapper: something is wrong, could not get attribute " << name.toLatin1().data(); | |
242 | } |
|
242 | } | |
243 | } |
|
243 | } | |
244 |
|
244 | |||
245 | QList<QByteArray> dynamicProps = wrapper->_obj->dynamicPropertyNames(); |
|
245 | QList<QByteArray> dynamicProps = wrapper->_obj->dynamicPropertyNames(); | |
246 | foreach (QByteArray name, dynamicProps) { |
|
246 | foreach (QByteArray name, dynamicProps) { | |
247 | PyObject* o = PyObject_GetAttrString(obj, name.data()); |
|
247 | PyObject* o = PyObject_GetAttrString(obj, name.data()); | |
248 | if (o) { |
|
248 | if (o) { | |
249 | PyDict_SetItemString(dict, name.data(), o); |
|
249 | PyDict_SetItemString(dict, name.data(), o); | |
250 | Py_DECREF(o); |
|
250 | Py_DECREF(o); | |
251 | } else { |
|
251 | } else { | |
252 | std::cerr << "PythonQtInstanceWrapper: dynamic property could not be read " << name.data(); |
|
252 | std::cerr << "PythonQtInstanceWrapper: dynamic property could not be read " << name.data(); | |
253 | } |
|
253 | } | |
254 | } |
|
254 | } | |
255 | } |
|
255 | } | |
256 | // Note: we do not put children into the dict, is would look confusing?! |
|
256 | // Note: we do not put children into the dict, is would look confusing?! | |
257 | return dict; |
|
257 | return dict; | |
258 | } |
|
258 | } | |
259 |
|
259 | |||
260 | // first look in super, to return derived methods from base object first |
|
260 | // first look in super, to return derived methods from base object first | |
261 | PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name); |
|
261 | PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name); | |
262 | if (superAttr) { |
|
262 | if (superAttr) { | |
263 | return superAttr; |
|
263 | return superAttr; | |
264 | } |
|
264 | } | |
265 | PyErr_Clear(); |
|
265 | PyErr_Clear(); | |
266 |
|
266 | |||
267 | // mlabDebugConst("Python","get " << attributeName); |
|
267 | // mlabDebugConst("Python","get " << attributeName); | |
268 |
|
268 | |||
269 | PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName); |
|
269 | PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName); | |
270 | switch (member._type) { |
|
270 | switch (member._type) { | |
271 | case PythonQtMemberInfo::Property: |
|
271 | case PythonQtMemberInfo::Property: | |
272 | if (wrapper->_obj) { |
|
272 | if (wrapper->_obj) { | |
273 | if (member._property.userType() != QVariant::Invalid) { |
|
273 | if (member._property.userType() != QVariant::Invalid) { | |
274 | return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj)); |
|
274 | return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj)); | |
275 | } else { |
|
275 | } else { | |
276 | Py_INCREF(Py_None); |
|
276 | Py_INCREF(Py_None); | |
277 | return Py_None; |
|
277 | return Py_None; | |
278 | } |
|
278 | } | |
279 | } else { |
|
279 | } else { | |
280 | QString error = QString("Trying to read property '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object"; |
|
280 | QString error = QString("Trying to read property '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object"; | |
281 | PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); |
|
281 | PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); | |
282 | return NULL; |
|
282 | return NULL; | |
283 | } |
|
283 | } | |
284 | break; |
|
284 | break; | |
285 | case PythonQtMemberInfo::Slot: |
|
285 | case PythonQtMemberInfo::Slot: | |
286 | return PythonQtSlotFunction_New(member._slot, obj, NULL); |
|
286 | return PythonQtSlotFunction_New(member._slot, obj, NULL); | |
287 | break; |
|
287 | break; | |
288 | case PythonQtMemberInfo::EnumValue: |
|
288 | case PythonQtMemberInfo::EnumValue: | |
289 | { |
|
289 | { | |
290 | PyObject* enumValue = member._enumValue; |
|
290 | PyObject* enumValue = member._enumValue; | |
291 | Py_INCREF(enumValue); |
|
291 | Py_INCREF(enumValue); | |
292 | return enumValue; |
|
292 | return enumValue; | |
293 | } |
|
293 | } | |
294 | break; |
|
294 | break; | |
295 | case PythonQtMemberInfo::EnumWrapper: |
|
295 | case PythonQtMemberInfo::EnumWrapper: | |
296 | { |
|
296 | { | |
297 | PyObject* enumWrapper = member._enumWrapper; |
|
297 | PyObject* enumWrapper = member._enumWrapper; | |
298 | Py_INCREF(enumWrapper); |
|
298 | Py_INCREF(enumWrapper); | |
299 | return enumWrapper; |
|
299 | return enumWrapper; | |
300 | } |
|
300 | } | |
301 | break; |
|
301 | break; | |
302 | case PythonQtMemberInfo::NotFound: |
|
302 | case PythonQtMemberInfo::NotFound: | |
303 | // handle dynamic properties |
|
303 | // handle dynamic properties | |
304 | if (wrapper->_obj) { |
|
304 | if (wrapper->_obj) { | |
305 | QVariant v = wrapper->_obj->property(attributeName); |
|
305 | QVariant v = wrapper->_obj->property(attributeName); | |
306 | if (v.isValid()) { |
|
306 | if (v.isValid()) { | |
307 | return PythonQtConv::QVariantToPyObject(v); |
|
307 | return PythonQtConv::QVariantToPyObject(v); | |
308 | } |
|
308 | } | |
309 | } |
|
309 | } | |
310 | break; |
|
310 | break; | |
311 | default: |
|
311 | default: | |
312 | // is an invalid type, go on |
|
312 | // is an invalid type, go on | |
313 | break; |
|
313 | break; | |
314 | } |
|
314 | } | |
315 |
|
315 | |||
316 | // look for the internal methods (className(), help()) |
|
316 | // look for the internal methods (className(), help()) | |
317 | PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName); |
|
317 | PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName); | |
318 | if (internalMethod) { |
|
318 | if (internalMethod) { | |
319 | return internalMethod; |
|
319 | return internalMethod; | |
320 | } |
|
320 | } | |
321 | PyErr_Clear(); |
|
321 | PyErr_Clear(); | |
322 |
|
322 | |||
323 | if (wrapper->_obj) { |
|
323 | if (wrapper->_obj) { | |
324 | // look for a child |
|
324 | // look for a child | |
325 | QObjectList children = wrapper->_obj->children(); |
|
325 | QObjectList children = wrapper->_obj->children(); | |
326 | for (int i = 0; i < children.count(); i++) { |
|
326 | for (int i = 0; i < children.count(); i++) { | |
327 | QObject *child = children.at(i); |
|
327 | QObject *child = children.at(i); | |
328 | if (child->objectName() == attributeName) { |
|
328 | if (child->objectName() == attributeName) { | |
329 | return PythonQt::priv()->wrapQObject(child); |
|
329 | return PythonQt::priv()->wrapQObject(child); | |
330 | } |
|
330 | } | |
331 | } |
|
331 | } | |
332 | } |
|
332 | } | |
333 |
|
333 | |||
334 | QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'"; |
|
334 | QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'"; | |
335 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); |
|
335 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); | |
336 | return NULL; |
|
336 | return NULL; | |
337 | } |
|
337 | } | |
338 |
|
338 | |||
339 | static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value) |
|
339 | static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value) | |
340 | { |
|
340 | { | |
341 | QString error; |
|
341 | QString error; | |
342 | char *attributeName; |
|
342 | char *attributeName; | |
343 | PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj; |
|
343 | PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj; | |
344 |
|
344 | |||
345 | if ((attributeName = PyString_AsString(name)) == NULL) |
|
345 | if ((attributeName = PyString_AsString(name)) == NULL) | |
346 | return -1; |
|
346 | return -1; | |
347 |
|
347 | |||
348 | PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName); |
|
348 | PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName); | |
349 | if (member._type == PythonQtMemberInfo::Property) { |
|
349 | if (member._type == PythonQtMemberInfo::Property) { | |
350 |
|
350 | |||
351 | if (!wrapper->_obj) { |
|
351 | if (!wrapper->_obj) { | |
352 | error = QString("Trying to set property '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object"; |
|
352 | error = QString("Trying to set property '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object"; | |
353 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); |
|
353 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); | |
354 | return -1; |
|
354 | return -1; | |
355 | } |
|
355 | } | |
356 |
|
356 | |||
357 | QMetaProperty prop = member._property; |
|
357 | QMetaProperty prop = member._property; | |
358 | if (prop.isWritable()) { |
|
358 | if (prop.isWritable()) { | |
359 | QVariant v; |
|
359 | QVariant v; | |
360 | if (prop.isEnumType()) { |
|
360 | if (prop.isEnumType()) { | |
361 | // this will give us either a string or an int, everything else will probably be an error |
|
361 | // this will give us either a string or an int, everything else will probably be an error | |
362 | v = PythonQtConv::PyObjToQVariant(value); |
|
362 | v = PythonQtConv::PyObjToQVariant(value); | |
363 | } else { |
|
363 | } else { | |
364 | int t = prop.userType(); |
|
364 | int t = prop.userType(); | |
365 | v = PythonQtConv::PyObjToQVariant(value, t); |
|
365 | v = PythonQtConv::PyObjToQVariant(value, t); | |
366 | } |
|
366 | } | |
367 | bool success = false; |
|
367 | bool success = false; | |
368 | if (v.isValid()) { |
|
368 | if (v.isValid()) { | |
369 | success = prop.write(wrapper->_obj, v); |
|
369 | success = prop.write(wrapper->_obj, v); | |
370 | } |
|
370 | } | |
371 | if (success) { |
|
371 | if (success) { | |
372 | return 0; |
|
372 | return 0; | |
373 | } else { |
|
373 | } else { | |
374 | error = QString("Property '") + attributeName + "' of type '" + |
|
374 | error = QString("Property '") + attributeName + "' of type '" + | |
375 | prop.typeName() + "' does not accept an object of type " |
|
375 | prop.typeName() + "' does not accept an object of type " | |
376 | + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")"; |
|
376 | + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")"; | |
377 | } |
|
377 | } | |
378 | } else { |
|
378 | } else { | |
379 | error = QString("Property '") + attributeName + "' of " + obj->ob_type->tp_name + " object is not writable"; |
|
379 | error = QString("Property '") + attributeName + "' of " + obj->ob_type->tp_name + " object is not writable"; | |
380 | } |
|
380 | } | |
381 | } else if (member._type == PythonQtMemberInfo::Slot) { |
|
381 | } else if (member._type == PythonQtMemberInfo::Slot) { | |
382 | error = QString("Slot '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object"; |
|
382 | error = QString("Slot '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object"; | |
383 | } else if (member._type == PythonQtMemberInfo::EnumValue) { |
|
383 | } else if (member._type == PythonQtMemberInfo::EnumValue) { | |
384 | error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object"; |
|
384 | error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object"; | |
385 | } else if (member._type == PythonQtMemberInfo::EnumWrapper) { |
|
385 | } else if (member._type == PythonQtMemberInfo::EnumWrapper) { | |
386 | error = QString("Enum '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object"; |
|
386 | error = QString("Enum '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object"; | |
387 | } else if (member._type == PythonQtMemberInfo::NotFound) { |
|
387 | } else if (member._type == PythonQtMemberInfo::NotFound) { | |
388 | // handle dynamic properties |
|
388 | // handle dynamic properties | |
389 | if (wrapper->_obj) { |
|
389 | if (wrapper->_obj) { | |
390 | QVariant prop = wrapper->_obj->property(attributeName); |
|
390 | QVariant prop = wrapper->_obj->property(attributeName); | |
391 | if (prop.isValid()) { |
|
391 | if (prop.isValid()) { | |
392 | QVariant v = PythonQtConv::PyObjToQVariant(value); |
|
392 | QVariant v = PythonQtConv::PyObjToQVariant(value); | |
393 | if (v.isValid()) { |
|
393 | if (v.isValid()) { | |
394 | wrapper->_obj->setProperty(attributeName, v); |
|
394 | wrapper->_obj->setProperty(attributeName, v); | |
395 | return 0; |
|
395 | return 0; | |
396 | } else { |
|
396 | } else { | |
397 | error = QString("Dynamic property '") + attributeName + "' does not accept an object of type " |
|
397 | error = QString("Dynamic property '") + attributeName + "' does not accept an object of type " | |
398 | + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")"; |
|
398 | + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")"; | |
399 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); |
|
399 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); | |
400 | return -1; |
|
400 | return -1; | |
401 | } |
|
401 | } | |
402 | } |
|
402 | } | |
403 | } |
|
403 | } | |
404 |
|
404 | |||
405 | // if we are a derived python class, we allow setting attributes. |
|
405 | // if we are a derived python class, we allow setting attributes. | |
406 | // if we are a direct CPP wrapper, we do NOT allow it, since |
|
406 | // if we are a direct CPP wrapper, we do NOT allow it, since | |
407 | // it would be confusing to allow it because a wrapper will go away when it is not seen by python anymore |
|
407 | // it would be confusing to allow it because a wrapper will go away when it is not seen by python anymore | |
408 | // and when it is recreated from a CPP pointer the attributes are gone... |
|
408 | // and when it is recreated from a CPP pointer the attributes are gone... | |
409 | if (obj->ob_type->tp_base != &PythonQtInstanceWrapper_Type) { |
|
409 | if (obj->ob_type->tp_base != &PythonQtInstanceWrapper_Type) { | |
410 | return PyBaseObject_Type.tp_setattro(obj,name,value); |
|
410 | return PyBaseObject_Type.tp_setattro(obj,name,value); | |
411 | } else { |
|
411 | } else { | |
412 | error = QString("'") + attributeName + "' does not exist on " + obj->ob_type->tp_name + " and creating new attributes on C++ objects is not allowed"; |
|
412 | error = QString("'") + attributeName + "' does not exist on " + obj->ob_type->tp_name + " and creating new attributes on C++ objects is not allowed"; | |
413 | } |
|
413 | } | |
414 | } |
|
414 | } | |
415 |
|
415 | |||
416 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); |
|
416 | PyErr_SetString(PyExc_AttributeError, error.toLatin1().data()); | |
417 | return -1; |
|
417 | return -1; | |
418 | } |
|
418 | } | |
419 |
|
419 | |||
420 | static PyObject * PythonQtInstanceWrapper_str(PyObject * obj) |
|
420 | static PyObject * PythonQtInstanceWrapper_str(PyObject * obj) | |
421 | { |
|
421 | { | |
422 | PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj; |
|
422 | PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj; | |
423 | const char* typeName = obj->ob_type->tp_name; |
|
423 | const char* typeName = obj->ob_type->tp_name; | |
424 | QObject *qobj = wrapper->_obj; |
|
424 | QObject *qobj = wrapper->_obj; | |
425 | if (wrapper->_wrappedPtr) { |
|
425 | if (wrapper->_wrappedPtr) { | |
426 | QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr); |
|
426 | QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr); | |
427 | if (!str.isEmpty()) { |
|
427 | if (!str.isEmpty()) { | |
428 | return PyString_FromFormat("%s", str.toLatin1().constData()); |
|
428 | return PyString_FromFormat("%s", str.toLatin1().constData()); | |
429 | } else |
|
429 | } else | |
430 | if (wrapper->_obj) { |
|
430 | if (wrapper->_obj) { | |
431 | return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj); |
|
431 | return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj); | |
432 | } else { |
|
432 | } else { | |
433 | return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr); |
|
433 | return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr); | |
434 | } |
|
434 | } | |
435 | } else { |
|
435 | } else { | |
436 | return PyString_FromFormat("%s (QObject %p)", typeName, qobj); |
|
436 | return PyString_FromFormat("%s (QObject %p)", typeName, qobj); | |
437 | } |
|
437 | } | |
438 | } |
|
438 | } | |
439 |
|
439 | |||
440 | static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj) |
|
440 | static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj) | |
441 | { |
|
441 | { | |
442 | PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj; |
|
442 | PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj; | |
443 | const char* typeName = obj->ob_type->tp_name; |
|
443 | const char* typeName = obj->ob_type->tp_name; | |
444 |
|
444 | |||
445 | QObject *qobj = wrapper->_obj; |
|
445 | QObject *qobj = wrapper->_obj; | |
446 | if (wrapper->_wrappedPtr) { |
|
446 | if (wrapper->_wrappedPtr) { | |
447 | QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr); |
|
447 | QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr); | |
448 | if (!str.isEmpty()) { |
|
448 | if (!str.isEmpty()) { | |
449 | return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr); |
|
449 | return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr); | |
450 | } else |
|
450 | } else | |
451 | if (wrapper->_obj) { |
|
451 | if (wrapper->_obj) { | |
452 | return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj); |
|
452 | return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj); | |
453 | } else { |
|
453 | } else { | |
454 | return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr); |
|
454 | return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr); | |
455 | } |
|
455 | } | |
456 | } else { |
|
456 | } else { | |
457 | return PyString_FromFormat("%s (%s %p)", typeName, wrapper->classInfo()->className(), qobj); |
|
457 | return PyString_FromFormat("%s (%s %p)", typeName, wrapper->classInfo()->className(), qobj); | |
458 | } |
|
458 | } | |
459 | } |
|
459 | } | |
460 |
|
460 | |||
461 | static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2) |
|
461 | static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2) | |
462 | { |
|
462 | { | |
463 | if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) && |
|
463 | if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) && | |
464 | PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) { |
|
464 | PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) { | |
465 |
|
465 | |||
466 | PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1; |
|
466 | PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1; | |
467 | PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2; |
|
467 | PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2; | |
468 | // check pointers directly first: |
|
468 | // check pointers directly first: | |
469 | if (w1->_wrappedPtr != NULL) { |
|
469 | if (w1->_wrappedPtr != NULL) { | |
470 | if (w1->_wrappedPtr == w2->_wrappedPtr) { |
|
470 | if (w1->_wrappedPtr == w2->_wrappedPtr) { | |
471 | return 0; |
|
471 | return 0; | |
472 | } |
|
472 | } | |
473 | } else if (w1->_obj == w2->_obj) { |
|
473 | } else if (w1->_obj == w2->_obj) { | |
474 | return 0; |
|
474 | return 0; | |
475 | } |
|
475 | } | |
476 | const char* class1 = w1->classInfo()->className(); |
|
476 | const char* class1 = w1->classInfo()->className(); | |
477 | const char* class2 = w2->classInfo()->className(); |
|
477 | const char* class2 = w2->classInfo()->className(); | |
478 | if (strcmp(class1, class2) == 0) { |
|
478 | if (strcmp(class1, class2) == 0) { | |
479 | // same class names, so we can try the operator_equal |
|
479 | // same class names, so we can try the operator_equal | |
480 | PythonQtMemberInfo info = w1->classInfo()->member("operator_equal"); |
|
480 | PythonQtMemberInfo info = w1->classInfo()->member("operator_equal"); | |
481 | if (info._type == PythonQtMemberInfo::Slot) { |
|
481 | if (info._type == PythonQtMemberInfo::Slot) { | |
482 | bool result = false; |
|
482 | bool result = false; | |
483 | void* obj1 = w1->_wrappedPtr; |
|
483 | void* obj1 = w1->_wrappedPtr; | |
484 | if (!obj1) { |
|
484 | if (!obj1) { | |
485 | obj1 = w1->_obj; |
|
485 | obj1 = w1->_obj; | |
486 | } |
|
486 | } | |
487 | if (!obj1) { return -1; } |
|
487 | if (!obj1) { return -1; } | |
488 | void* obj2 = w2->_wrappedPtr; |
|
488 | void* obj2 = w2->_wrappedPtr; | |
489 | if (!obj2) { |
|
489 | if (!obj2) { | |
490 | obj2 = w2->_obj; |
|
490 | obj2 = w2->_obj; | |
491 | } |
|
491 | } | |
492 | if (!obj2) { return -1; } |
|
492 | if (!obj2) { return -1; } | |
493 | if (info._slot->isInstanceDecorator()) { |
|
493 | if (info._slot->isInstanceDecorator()) { | |
494 | // call on decorator QObject |
|
494 | // call on decorator QObject | |
495 | void* args[3]; |
|
495 | void* args[3]; | |
496 | args[0] = &result; |
|
496 | args[0] = &result; | |
497 | args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer |
|
497 | args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer | |
498 | args[2] = obj2; // this is a reference, so it needs the direct pointer |
|
498 | args[2] = obj2; // this is a reference, so it needs the direct pointer | |
499 | info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args); |
|
499 | info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args); | |
500 | return result?0:-1; |
|
500 | return result?0:-1; | |
501 | } else { |
|
501 | } else { | |
502 | // call directly on QObject |
|
502 | // call directly on QObject | |
503 | if (w1->_obj && w2->_obj) { |
|
503 | if (w1->_obj && w2->_obj) { | |
504 | void* args[2]; |
|
504 | void* args[2]; | |
505 | args[0] = &result; |
|
505 | args[0] = &result; | |
506 |
args[ |
|
506 | args[1] = obj2; // this is a reference, so it needs the direct pointer | |
507 | w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args); |
|
507 | w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args); | |
508 | } |
|
508 | } | |
509 | } |
|
509 | } | |
510 | } |
|
510 | } | |
511 | } |
|
511 | } | |
512 | } |
|
512 | } | |
513 | return -1; |
|
513 | return -1; | |
514 | } |
|
514 | } | |
515 |
|
515 | |||
516 | static int PythonQtInstanceWrapper_nonzero(PyObject *obj) |
|
516 | static int PythonQtInstanceWrapper_nonzero(PyObject *obj) | |
517 | { |
|
517 | { | |
518 | PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj; |
|
518 | PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj; | |
519 | return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1; |
|
519 | return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1; | |
520 | } |
|
520 | } | |
521 |
|
521 | |||
522 |
|
522 | |||
523 | static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj) |
|
523 | static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj) | |
524 | { |
|
524 | { | |
525 | if (obj->_wrappedPtr != NULL) { |
|
525 | if (obj->_wrappedPtr != NULL) { | |
526 | return reinterpret_cast<long>(obj->_wrappedPtr); |
|
526 | return reinterpret_cast<long>(obj->_wrappedPtr); | |
527 | } else { |
|
527 | } else { | |
528 | QObject* qobj = obj->_obj; // get pointer from QPointer wrapper |
|
528 | QObject* qobj = obj->_obj; // get pointer from QPointer wrapper | |
529 | return reinterpret_cast<long>(qobj); |
|
529 | return reinterpret_cast<long>(qobj); | |
530 | } |
|
530 | } | |
531 | } |
|
531 | } | |
532 |
|
532 | |||
533 |
|
533 | |||
534 |
|
534 | |||
535 | // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr |
|
535 | // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr | |
536 | static PyNumberMethods PythonQtInstanceWrapper_as_number = { |
|
536 | static PyNumberMethods PythonQtInstanceWrapper_as_number = { | |
537 | 0, /* nb_add */ |
|
537 | 0, /* nb_add */ | |
538 | 0, /* nb_subtract */ |
|
538 | 0, /* nb_subtract */ | |
539 | 0, /* nb_multiply */ |
|
539 | 0, /* nb_multiply */ | |
540 | 0, /* nb_divide */ |
|
540 | 0, /* nb_divide */ | |
541 | 0, /* nb_remainder */ |
|
541 | 0, /* nb_remainder */ | |
542 | 0, /* nb_divmod */ |
|
542 | 0, /* nb_divmod */ | |
543 | 0, /* nb_power */ |
|
543 | 0, /* nb_power */ | |
544 | 0, /* nb_negative */ |
|
544 | 0, /* nb_negative */ | |
545 | 0, /* nb_positive */ |
|
545 | 0, /* nb_positive */ | |
546 | 0, /* nb_absolute */ |
|
546 | 0, /* nb_absolute */ | |
547 | PythonQtInstanceWrapper_nonzero, /* nb_nonzero */ |
|
547 | PythonQtInstanceWrapper_nonzero, /* nb_nonzero */ | |
548 | 0, /* nb_invert */ |
|
548 | 0, /* nb_invert */ | |
549 | 0, /* nb_lshift */ |
|
549 | 0, /* nb_lshift */ | |
550 | 0, /* nb_rshift */ |
|
550 | 0, /* nb_rshift */ | |
551 | 0, /* nb_and */ |
|
551 | 0, /* nb_and */ | |
552 | 0, /* nb_xor */ |
|
552 | 0, /* nb_xor */ | |
553 | 0, /* nb_or */ |
|
553 | 0, /* nb_or */ | |
554 | 0, /* nb_coerce */ |
|
554 | 0, /* nb_coerce */ | |
555 | 0, /* nb_int */ |
|
555 | 0, /* nb_int */ | |
556 | 0, /* nb_long */ |
|
556 | 0, /* nb_long */ | |
557 | 0, /* nb_float */ |
|
557 | 0, /* nb_float */ | |
558 | 0, /* nb_oct */ |
|
558 | 0, /* nb_oct */ | |
559 | 0, /* nb_hex */ |
|
559 | 0, /* nb_hex */ | |
560 | 0, /* nb_inplace_add */ |
|
560 | 0, /* nb_inplace_add */ | |
561 | 0, /* nb_inplace_subtract */ |
|
561 | 0, /* nb_inplace_subtract */ | |
562 | 0, /* nb_inplace_multiply */ |
|
562 | 0, /* nb_inplace_multiply */ | |
563 | 0, /* nb_inplace_divide */ |
|
563 | 0, /* nb_inplace_divide */ | |
564 | 0, /* nb_inplace_remainder */ |
|
564 | 0, /* nb_inplace_remainder */ | |
565 | 0, /* nb_inplace_power */ |
|
565 | 0, /* nb_inplace_power */ | |
566 | 0, /* nb_inplace_lshift */ |
|
566 | 0, /* nb_inplace_lshift */ | |
567 | 0, /* nb_inplace_rshift */ |
|
567 | 0, /* nb_inplace_rshift */ | |
568 | 0, /* nb_inplace_and */ |
|
568 | 0, /* nb_inplace_and */ | |
569 | 0, /* nb_inplace_xor */ |
|
569 | 0, /* nb_inplace_xor */ | |
570 | 0, /* nb_inplace_or */ |
|
570 | 0, /* nb_inplace_or */ | |
571 | 0, /* nb_floor_divide */ |
|
571 | 0, /* nb_floor_divide */ | |
572 | 0, /* nb_true_divide */ |
|
572 | 0, /* nb_true_divide */ | |
573 | 0, /* nb_inplace_floor_divide */ |
|
573 | 0, /* nb_inplace_floor_divide */ | |
574 | 0, /* nb_inplace_true_divide */ |
|
574 | 0, /* nb_inplace_true_divide */ | |
575 | }; |
|
575 | }; | |
576 |
|
576 | |||
577 | PyTypeObject PythonQtInstanceWrapper_Type = { |
|
577 | PyTypeObject PythonQtInstanceWrapper_Type = { | |
578 | PyObject_HEAD_INIT(&PythonQtClassWrapper_Type) |
|
578 | PyObject_HEAD_INIT(&PythonQtClassWrapper_Type) | |
579 | 0, /*ob_size*/ |
|
579 | 0, /*ob_size*/ | |
580 | "PythonQt.PythonQtInstanceWrapper", /*tp_name*/ |
|
580 | "PythonQt.PythonQtInstanceWrapper", /*tp_name*/ | |
581 | sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/ |
|
581 | sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/ | |
582 | 0, /*tp_itemsize*/ |
|
582 | 0, /*tp_itemsize*/ | |
583 | (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/ |
|
583 | (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/ | |
584 | 0, /*tp_print*/ |
|
584 | 0, /*tp_print*/ | |
585 | 0, /*tp_getattr*/ |
|
585 | 0, /*tp_getattr*/ | |
586 | 0, /*tp_setattr*/ |
|
586 | 0, /*tp_setattr*/ | |
587 | PythonQtInstanceWrapper_compare, /*tp_compare*/ |
|
587 | PythonQtInstanceWrapper_compare, /*tp_compare*/ | |
588 | PythonQtInstanceWrapper_repr, /*tp_repr*/ |
|
588 | PythonQtInstanceWrapper_repr, /*tp_repr*/ | |
589 | &PythonQtInstanceWrapper_as_number, /*tp_as_number*/ |
|
589 | &PythonQtInstanceWrapper_as_number, /*tp_as_number*/ | |
590 | 0, /*tp_as_sequence*/ |
|
590 | 0, /*tp_as_sequence*/ | |
591 | 0, /*tp_as_mapping*/ |
|
591 | 0, /*tp_as_mapping*/ | |
592 | (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */ |
|
592 | (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */ | |
593 | 0, /*tp_call*/ |
|
593 | 0, /*tp_call*/ | |
594 | PythonQtInstanceWrapper_str, /*tp_str*/ |
|
594 | PythonQtInstanceWrapper_str, /*tp_str*/ | |
595 | PythonQtInstanceWrapper_getattro, /*tp_getattro*/ |
|
595 | PythonQtInstanceWrapper_getattro, /*tp_getattro*/ | |
596 | PythonQtInstanceWrapper_setattro, /*tp_setattro*/ |
|
596 | PythonQtInstanceWrapper_setattro, /*tp_setattro*/ | |
597 | 0, /*tp_as_buffer*/ |
|
597 | 0, /*tp_as_buffer*/ | |
598 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
598 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
599 | "PythonQtInstanceWrapper object", /* tp_doc */ |
|
599 | "PythonQtInstanceWrapper object", /* tp_doc */ | |
600 | 0, /* tp_traverse */ |
|
600 | 0, /* tp_traverse */ | |
601 | 0, /* tp_clear */ |
|
601 | 0, /* tp_clear */ | |
602 | 0, /* tp_richcompare */ |
|
602 | 0, /* tp_richcompare */ | |
603 | 0, /* tp_weaklistoffset */ |
|
603 | 0, /* tp_weaklistoffset */ | |
604 | 0, /* tp_iter */ |
|
604 | 0, /* tp_iter */ | |
605 | 0, /* tp_iternext */ |
|
605 | 0, /* tp_iternext */ | |
606 | 0, /* tp_methods */ |
|
606 | 0, /* tp_methods */ | |
607 | 0, /* tp_members */ |
|
607 | 0, /* tp_members */ | |
608 | 0, /* tp_getset */ |
|
608 | 0, /* tp_getset */ | |
609 | 0, /* tp_base */ |
|
609 | 0, /* tp_base */ | |
610 | 0, /* tp_dict */ |
|
610 | 0, /* tp_dict */ | |
611 | 0, /* tp_descr_get */ |
|
611 | 0, /* tp_descr_get */ | |
612 | 0, /* tp_descr_set */ |
|
612 | 0, /* tp_descr_set */ | |
613 | 0, /* tp_dictoffset */ |
|
613 | 0, /* tp_dictoffset */ | |
614 | (initproc)PythonQtInstanceWrapper_init, /* tp_init */ |
|
614 | (initproc)PythonQtInstanceWrapper_init, /* tp_init */ | |
615 | 0, /* tp_alloc */ |
|
615 | 0, /* tp_alloc */ | |
616 | PythonQtInstanceWrapper_new, /* tp_new */ |
|
616 | PythonQtInstanceWrapper_new, /* tp_new */ | |
617 | }; |
|
617 | }; | |
618 |
|
618 | |||
619 | //------------------------------------------------------- |
|
619 | //------------------------------------------------------- | |
620 |
|
620 |
General Comments 0
You need to be logged in to leave comments.
Login now