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