Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtMethodInfo.cpp
3206 views
1
/*
2
*
3
* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4
*
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
9
*
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
14
*
15
* Further, this software is distributed without any warranty that it is
16
* free of the rightful claim of any third person regarding infringement
17
* or the like. Any license provided herein, whether implied or
18
* otherwise, applies only to this software file. Patent licenses, if
19
* any, provided herein do not apply to combinations of this program with
20
* other software, or any other product whatsoever.
21
*
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
24
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
*
26
* Contact information: MeVis Research GmbH, Universitaetsallee 29,
27
* 28359 Bremen, Germany or:
28
*
29
* http://www.mevis.de
30
*
31
*/
32
33
//----------------------------------------------------------------------------------
34
/*!
35
// \file PythonQtMethodInfo.cpp
36
// \author Florian Link
37
// \author Last changed by $Author: florian $
38
// \date 2006-05
39
*/
40
//----------------------------------------------------------------------------------
41
42
#include "PythonQtMethodInfo.h"
43
#include <iostream>
44
45
QHash<QByteArray, PythonQtMethodInfo*> PythonQtMethodInfo::_cachedSignatures;
46
47
PythonQtMethodInfo::PythonQtMethodInfo(const QMetaMethod& meta)
48
{
49
#ifdef PYTHONQT_DEBUG
50
QByteArray sig(meta.signature());
51
sig = sig.mid(sig.indexOf('('));
52
QByteArray fullSig = QByteArray(meta.typeName()) + " " + sig;
53
std::cout << "caching " << fullSig.data() << std::endl;
54
#endif
55
56
ParameterInfo type;
57
fillParameterInfo(type, QByteArray(meta.typeName()));
58
_parameters.append(type);
59
QByteArray name;
60
QList<QByteArray> names = meta.parameterTypes();
61
foreach (name, names) {
62
fillParameterInfo(type, name);
63
_parameters.append(type);
64
}
65
}
66
67
68
const PythonQtMethodInfo* PythonQtMethodInfo::getCachedMethodInfo(const QMetaMethod& signal)
69
{
70
QByteArray sig(signal.signature());
71
sig = sig.mid(sig.indexOf('('));
72
QByteArray fullSig = QByteArray(signal.typeName()) + " " + sig;
73
PythonQtMethodInfo* result = _cachedSignatures.value(fullSig);
74
if (!result) {
75
result = new PythonQtMethodInfo(signal);
76
_cachedSignatures.insert(fullSig, result);
77
}
78
return result;
79
}
80
81
void PythonQtMethodInfo::fillParameterInfo(ParameterInfo& type, const QByteArray& orgName)
82
{
83
QByteArray name = orgName;
84
85
int len = name.length();
86
if (len>0) {
87
if (strncmp(name.constData(), "const ", 6)==0) {
88
name = name.mid(6);
89
len -= 6;
90
type.isConst = true;
91
} else {
92
type.isConst = false;
93
}
94
// EXTRA: & references are not yet supported, while const & is removed by moc
95
while (name.at(len-1) == '*') {
96
len--;
97
}
98
if (len!=name.length()) {
99
name = name.left(len);
100
type.isPointer = true;
101
} else {
102
type.isPointer = false;
103
}
104
105
type.typeId = nameToType(name);
106
if (!type.isPointer && type.typeId == Unknown) {
107
type.typeId = QMetaType::type(name.constData());
108
if (type.typeId == QMetaType::Void) {
109
type.typeId = Unknown;
110
}
111
}
112
type.name = name;
113
} else {
114
type.typeId = QMetaType::Void;
115
type.isPointer = false;
116
type.isConst = false;
117
}
118
}
119
120
int PythonQtMethodInfo::nameToType(const char* name)
121
{
122
if (_parameterTypeDict.isEmpty()) {
123
// we could also use QMetaType::nameToType, but that does a string compare search
124
// and does not support QVariant
125
126
// QMetaType names
127
_parameterTypeDict.insert("long", QMetaType::Long);
128
_parameterTypeDict.insert("int", QMetaType::Int);
129
_parameterTypeDict.insert("short", QMetaType::Short);
130
_parameterTypeDict.insert("char", QMetaType::Char);
131
_parameterTypeDict.insert("ulong", QMetaType::ULong);
132
_parameterTypeDict.insert("unsigned long", QMetaType::ULong);
133
_parameterTypeDict.insert("uint", QMetaType::UInt);
134
_parameterTypeDict.insert("unsigned int", QMetaType::UInt);
135
_parameterTypeDict.insert("ushort", QMetaType::UShort);
136
_parameterTypeDict.insert("unsigned short", QMetaType::UShort);
137
_parameterTypeDict.insert("uchar", QMetaType::UChar);
138
_parameterTypeDict.insert("unsigned char", QMetaType::UChar);
139
_parameterTypeDict.insert("bool", QMetaType::Bool);
140
_parameterTypeDict.insert("float", QMetaType::Float);
141
_parameterTypeDict.insert("double", QMetaType::Double);
142
_parameterTypeDict.insert("qreal", QMetaType::Double);
143
_parameterTypeDict.insert("QChar", QMetaType::QChar);
144
_parameterTypeDict.insert("QByteArray", QMetaType::QByteArray);
145
_parameterTypeDict.insert("Q3CString", QMetaType::QByteArray);
146
_parameterTypeDict.insert("QString", QMetaType::QString);
147
_parameterTypeDict.insert("", QMetaType::Void);
148
_parameterTypeDict.insert("void", QMetaType::Void);
149
// QVariant names
150
_parameterTypeDict.insert("Q_LLONG", QMetaType::LongLong);
151
_parameterTypeDict.insert("Q_ULLONG", QMetaType::ULongLong);
152
_parameterTypeDict.insert("qlonglong", QMetaType::LongLong);
153
_parameterTypeDict.insert("qulonglong", QMetaType::ULongLong);
154
_parameterTypeDict.insert("qint64", QMetaType::LongLong);
155
_parameterTypeDict.insert("quint64", QMetaType::ULongLong);
156
_parameterTypeDict.insert("QIconSet", QMetaType::QIcon);
157
_parameterTypeDict.insert("QVariantMap", QMetaType::QVariantMap);
158
_parameterTypeDict.insert("QVariantList", QMetaType::QVariantList);
159
_parameterTypeDict.insert("QMap<QString,QVariant>", QMetaType::QVariantMap);
160
_parameterTypeDict.insert("QList<QVariant>", QMetaType::QVariantList);
161
_parameterTypeDict.insert("QStringList", QMetaType::QStringList);
162
_parameterTypeDict.insert("QBitArray", QMetaType::QBitArray);
163
_parameterTypeDict.insert("QDate", QMetaType::QDate);
164
_parameterTypeDict.insert("QTime", QMetaType::QTime);
165
_parameterTypeDict.insert("QDateTime", QMetaType::QDateTime);
166
_parameterTypeDict.insert("QUrl", QMetaType::QUrl);
167
_parameterTypeDict.insert("QLocale", QMetaType::QLocale);
168
_parameterTypeDict.insert("QRect", QMetaType::QRect);
169
_parameterTypeDict.insert("QRectf", QMetaType::QRectF);
170
_parameterTypeDict.insert("QSize", QMetaType::QSize);
171
_parameterTypeDict.insert("QSizef", QMetaType::QSizeF);
172
_parameterTypeDict.insert("QLine", QMetaType::QLine);
173
_parameterTypeDict.insert("QLinef", QMetaType::QLineF);
174
_parameterTypeDict.insert("QPoint", QMetaType::QPoint);
175
_parameterTypeDict.insert("QPointf", QMetaType::QPointF);
176
_parameterTypeDict.insert("QRegExp", QMetaType::QRegExp);
177
// _parameterTypeDict.insert("QColorGroup", QMetaType::QColorGroup);
178
_parameterTypeDict.insert("QFont", QMetaType::QFont);
179
_parameterTypeDict.insert("QPixmap", QMetaType::QPixmap);
180
_parameterTypeDict.insert("QBrush", QMetaType::QBrush);
181
_parameterTypeDict.insert("QColor", QMetaType::QColor);
182
_parameterTypeDict.insert("QCursor", QMetaType::QCursor);
183
_parameterTypeDict.insert("QPalette", QMetaType::QPalette);
184
_parameterTypeDict.insert("QIcon", QMetaType::QIcon);
185
_parameterTypeDict.insert("QImage", QMetaType::QPolygon);
186
_parameterTypeDict.insert("QRegion", QMetaType::QRegion);
187
_parameterTypeDict.insert("QBitmap", QMetaType::QBitmap);
188
_parameterTypeDict.insert("QSizePolicy", QMetaType::QSizePolicy);
189
_parameterTypeDict.insert("QKeySequence", QMetaType::QKeySequence);
190
_parameterTypeDict.insert("QPen", QMetaType::QPen);
191
_parameterTypeDict.insert("QTextLength", QMetaType::QTextLength);
192
_parameterTypeDict.insert("QTextFormat", QMetaType::QTextFormat);
193
_parameterTypeDict.insert("QMatrix", QMetaType::QMatrix);
194
_parameterTypeDict.insert("QVariant", PythonQtMethodInfo::Variant);
195
// own special types... (none so far, could be e.g. ObjectList
196
}
197
QHash<QByteArray, int>::const_iterator it = _parameterTypeDict.find(name);
198
if (it!=_parameterTypeDict.end()) {
199
return it.value();
200
} else {
201
return PythonQtMethodInfo::Unknown;
202
}
203
}
204
205
void PythonQtMethodInfo::cleanupCachedMethodInfos()
206
{
207
QHashIterator<QByteArray, PythonQtMethodInfo *> i(_cachedSignatures);
208
while (i.hasNext()) {
209
delete i.next().value();
210
}
211
}
212
213
QString PythonQtSlotInfo::fullSignature(bool skipFirstArg)
214
{
215
QString result = _meta.typeName();
216
QByteArray sig = slotName();
217
QList<QByteArray> names = _meta.parameterNames();
218
219
bool isStatic = false;
220
bool isConstructor = false;
221
bool isDestructor = false;
222
223
if (_type == ClassDecorator) {
224
if (sig.startsWith("new_")) {
225
sig = sig.mid(strlen("new_"));
226
isConstructor = true;
227
} else if (sig.startsWith("delete_")) {
228
sig = sig.mid(strlen("delete_"));
229
isDestructor = true;
230
} else if(sig.startsWith("static_")) {
231
isStatic = true;
232
sig = sig.mid(strlen("static_"));
233
int idx = sig.indexOf("_");
234
if (idx>=0) {
235
sig = sig.mid(idx+1);
236
}
237
}
238
}
239
240
result += QByteArray(" ") + sig;
241
result += "(";
242
243
int lastEntry = _parameters.count()-1;
244
for (int i = skipFirstArg?2:1; i<_parameters.count(); i++) {
245
if (_parameters.at(i).isConst) {
246
result += "const ";
247
}
248
result += _parameters.at(i).name;
249
if (_parameters.at(i).isPointer) {
250
result += "*";
251
}
252
if (!names.at(i-1).isEmpty()) {
253
result += " ";
254
result += names.at(i-1);
255
}
256
if (i!=lastEntry) {
257
result += ", ";
258
}
259
}
260
result += ")";
261
262
if (isStatic) {
263
result = QString("static ") + result;
264
}
265
if (isConstructor) {
266
// result = QString("constructor ") + result;
267
}
268
if (isDestructor) {
269
result = QString("~") + result;
270
}
271
return result;
272
}
273
274
275
QByteArray PythonQtSlotInfo::slotName()
276
{
277
QByteArray sig(_meta.signature());
278
int idx = sig.indexOf('(');
279
sig = sig.left(idx);
280
return sig;
281
}
282
283