Skip to content

Commit 797c4d1

Browse files
committed
started qt5 port
1 parent 9174777 commit 797c4d1

17 files changed

Lines changed: 153 additions & 69 deletions

src/PythonQt.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,28 +1204,26 @@ void PythonQtPrivate::addDecorators(QObject* o, int decoTypes)
12041204
int numMethods = o->metaObject()->methodCount();
12051205
for (int i = 0; i < numMethods; i++) {
12061206
QMetaMethod m = o->metaObject()->method(i);
1207+
QByteArray signature = PythonQtUtils::methodName(m);
12071208
if ((m.methodType() == QMetaMethod::Method ||
12081209
m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public) {
1209-
if (qstrncmp(m.signature(), "new_", 4)==0) {
1210+
if (signature.startsWith("new_")) {
12101211
if ((decoTypes & ConstructorDecorator) == 0) continue;
12111212
const PythonQtMethodInfo* info = PythonQtMethodInfo::getCachedMethodInfo(m, NULL);
12121213
if (info->parameters().at(0).pointerCount == 1) {
1213-
QByteArray signature = m.signature();
1214-
QByteArray nameOfClass = signature.mid(4, signature.indexOf('(')-4);
1214+
QByteArray nameOfClass = signature.mid(4);
12151215
PythonQtClassInfo* classInfo = lookupClassInfoAndCreateIfNotPresent(nameOfClass);
12161216
PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(NULL, m, i, o, PythonQtSlotInfo::ClassDecorator);
12171217
classInfo->addConstructor(newSlot);
12181218
}
1219-
} else if (qstrncmp(m.signature(), "delete_", 7)==0) {
1219+
} else if (signature.startsWith("delete_")) {
12201220
if ((decoTypes & DestructorDecorator) == 0) continue;
1221-
QByteArray signature = m.signature();
1222-
QByteArray nameOfClass = signature.mid(7, signature.indexOf('(')-7);
1221+
QByteArray nameOfClass = signature.mid(7);
12231222
PythonQtClassInfo* classInfo = lookupClassInfoAndCreateIfNotPresent(nameOfClass);
12241223
PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(NULL, m, i, o, PythonQtSlotInfo::ClassDecorator);
12251224
classInfo->setDestructor(newSlot);
1226-
} else if (qstrncmp(m.signature(), "static_", 7)==0) {
1225+
} else if (signature.startsWith("static_")) {
12271226
if ((decoTypes & StaticDecorator) == 0) continue;
1228-
QByteArray signature = m.signature();
12291227
QByteArray nameOfClass = signature.mid(7);
12301228
nameOfClass = nameOfClass.mid(0, nameOfClass.indexOf('_'));
12311229
PythonQtClassInfo* classInfo = lookupClassInfoAndCreateIfNotPresent(nameOfClass);

src/PythonQt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*/
4343
//----------------------------------------------------------------------------------
4444

45+
#include "PythonQtUtils.h"
4546
#include "PythonQtSystem.h"
4647
#include "PythonQtInstanceWrapper.h"
4748
#include "PythonQtClassWrapper.h"

src/PythonQtClassInfo.cpp

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ PythonQtSlotInfo* PythonQtClassInfo::recursiveFindDecoratorSlotsFromDecoratorPro
159159

160160
PythonQtSlotInfo* PythonQtClassInfo::findDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset) {
161161
QObject* decoratorProvider = decorator();
162-
int memberNameLen = static_cast<int>(strlen(memberName));
163162
if (decoratorProvider) {
164163
//qDebug()<< "looking " << decoratorProvider->metaObject()->className() << " " << memberName << " " << upcastingOffset;
165164
const QMetaObject* meta = decoratorProvider->metaObject();
@@ -170,24 +169,21 @@ PythonQtSlotInfo* PythonQtClassInfo::findDecoratorSlotsFromDecoratorProvider(con
170169
if ((m.methodType() == QMetaMethod::Method ||
171170
m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public) {
172171

173-
const char* sigStart = m.signature();
172+
QByteArray signature = PythonQtUtils::methodName(m);
174173
bool isClassDeco = false;
175-
if (qstrncmp(sigStart, "static_", 7)==0) {
174+
if (signature.startsWith("static_")) {
176175
// skip the static_classname_ part of the string
177-
sigStart += 7 + 1 + strlen(className());
176+
signature = signature.mid(7 + 1 + strlen(className()));
178177
isClassDeco = true;
179-
} else if (qstrncmp(sigStart, "new_", 4)==0) {
178+
} else if (signature.startsWith("new_")) {
180179
isClassDeco = true;
181-
} else if (qstrncmp(sigStart, "delete_", 7)==0) {
180+
} else if (signature.startsWith("delete_")) {
182181
isClassDeco = true;
183182
}
184-
// find the first '('
185-
int offset = findCharOffset(sigStart, '(');
186-
187183
// XXX no checking is currently done if the slots have correct first argument or not...
188184

189185
// check if same length and same name
190-
if (memberNameLen == offset && qstrncmp(memberName, sigStart, offset)==0) {
186+
if (signature == memberName) {
191187
found = true;
192188
PythonQtSlotInfo* info = new PythonQtSlotInfo(this, m, i, decoratorProvider, isClassDeco?PythonQtSlotInfo::ClassDecorator:PythonQtSlotInfo::InstanceDecorator);
193189
info->setUpcastingOffset(upcastingOffset);
@@ -196,15 +192,15 @@ PythonQtSlotInfo* PythonQtClassInfo::findDecoratorSlotsFromDecoratorProvider(con
196192
tail->setNextInfo(info);
197193
} else {
198194
PythonQtMemberInfo newInfo(info);
199-
memberCache.insert(memberName, newInfo);
195+
memberCache.insert(signature, newInfo);
200196
}
201197
tail = info;
202198
}
203199
}
204200
}
205201
}
206202

207-
tail = findDecoratorSlots(memberName, memberNameLen, tail, found, memberCache, upcastingOffset);
203+
tail = findDecoratorSlots(memberName, tail, found, memberCache, upcastingOffset);
208204

209205
// now look for slots/signals/methods on this level of the meta object
210206
if (_meta) {
@@ -220,19 +216,17 @@ PythonQtSlotInfo* PythonQtClassInfo::findDecoratorSlotsFromDecoratorProvider(con
220216
m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public)
221217
|| m.methodType()==QMetaMethod::Signal) {
222218

223-
const char* sigStart = m.signature();
224-
// find the first '('
225-
int offset = findCharOffset(sigStart, '(');
219+
QByteArray signature = PythonQtUtils::methodName(m);
226220

227221
// check if same length and same name
228-
if (memberNameLen == offset && qstrncmp(memberName, sigStart, offset)==0) {
222+
if (signature == memberName) {
229223
found = true;
230224
PythonQtSlotInfo* info = new PythonQtSlotInfo(this, m, i);
231225
if (tail) {
232226
tail->setNextInfo(info);
233227
} else {
234228
PythonQtMemberInfo newInfo(info);
235-
memberCache.insert(memberName, newInfo);
229+
memberCache.insert(signature, newInfo);
236230
}
237231
tail = info;
238232
}
@@ -369,20 +363,18 @@ void PythonQtClassInfo::recursiveCollectClassInfos(QList<PythonQtClassInfo*>& cl
369363
}
370364
}
371365

372-
PythonQtSlotInfo* PythonQtClassInfo::findDecoratorSlots(const char* memberName, int memberNameLen, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset)
366+
PythonQtSlotInfo* PythonQtClassInfo::findDecoratorSlots(const char* memberName, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset)
373367
{
374368
QListIterator<PythonQtSlotInfo*> it(_decoratorSlots);
375369
while (it.hasNext()) {
376370

377371
PythonQtSlotInfo* infoOrig = it.next();
378-
379-
const char* sigStart = infoOrig->metaMethod()->signature();
380-
if (qstrncmp("static_", sigStart, 7)==0) {
381-
sigStart += 7;
382-
sigStart += findCharOffset(sigStart, '_')+1;
372+
QByteArray signature = PythonQtUtils::methodName(*infoOrig->metaMethod());
373+
if (signature.startsWith("static_")) {
374+
int offset = signature.indexOf('_', 7);
375+
signature = signature.mid(offset+1);
383376
}
384-
int offset = findCharOffset(sigStart, '(');
385-
if (memberNameLen == offset && qstrncmp(memberName, sigStart, offset)==0) {
377+
if (signature == memberName) {
386378
//make a copy, otherwise we will have trouble on overloads!
387379
PythonQtSlotInfo* info = new PythonQtSlotInfo(*infoOrig);
388380
info->setUpcastingOffset(upcastingOffset);
@@ -391,7 +383,7 @@ PythonQtSlotInfo* PythonQtClassInfo::findDecoratorSlots(const char* memberName,
391383
tail->setNextInfo(info);
392384
} else {
393385
PythonQtMemberInfo newInfo(info);
394-
memberCache.insert(memberName, newInfo);
386+
memberCache.insert(signature, newInfo);
395387
}
396388
tail = info;
397389
}
@@ -410,26 +402,23 @@ void PythonQtClassInfo::listDecoratorSlotsFromDecoratorProvider(QStringList& lis
410402
if ((m.methodType() == QMetaMethod::Method ||
411403
m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public) {
412404

413-
const char* sigStart = m.signature();
405+
QByteArray signature = PythonQtUtils::methodName(m);
414406
bool isClassDeco = false;
415-
if (qstrncmp(sigStart, "static_", 7)==0) {
407+
if (signature.startsWith("static_")) {
416408
// skip the static_classname_ part of the string
417-
sigStart += 7 + 1 + strlen(className());
409+
signature = signature.mid(7 + 1 + strlen(className()));
418410
isClassDeco = true;
419-
} else if (qstrncmp(sigStart, "new_", 4)==0) {
411+
} else if (signature.startsWith("new_")) {
420412
continue;
421-
} else if (qstrncmp(sigStart, "delete_", 7)==0) {
413+
} else if (signature.startsWith("delete_")) {
422414
continue;
423-
} else if (qstrncmp(sigStart, "py_", 3)==0) {
415+
} else if (signature.startsWith("py_")) {
424416
// hide everything that starts with py_
425417
continue;
426418
}
427-
// find the first '('
428-
int offset = findCharOffset(sigStart, '(');
429-
430419
// XXX no checking is currently done if the slots have correct first argument or not...
431420
if (!metaOnly || isClassDeco) {
432-
list << QString::fromLatin1(sigStart, offset);
421+
list << QString::fromLatin1(signature.constData());
433422
}
434423
}
435424
}
@@ -484,9 +473,7 @@ QStringList PythonQtClassInfo::memberList()
484473
if (((m.methodType() == QMetaMethod::Method ||
485474
m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public)
486475
|| m.methodType()==QMetaMethod::Signal) {
487-
QByteArray signa(m.signature());
488-
signa = signa.left(signa.indexOf('('));
489-
l << signa;
476+
l << PythonQtUtils::methodName(m);
490477
}
491478
}
492479
}
@@ -641,7 +628,7 @@ QString PythonQtClassInfo::help()
641628
for (int i = 0; i < numMethods; i++) {
642629
QMetaMethod m = _meta->method(i);
643630
if (m.methodType() == QMetaMethod::Signal) {
644-
h += QString(m.signature()) + "\n";
631+
h += QString(PythonQtUtils::signature(m)) + "\n";
645632
}
646633
}
647634
}

src/PythonQtClassInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class PYTHONQT_EXPORT PythonQtClassInfo {
208208
bool lookForMethodAndCache(const char* memberName);
209209
bool lookForEnumAndCache(const QMetaObject* m, const char* memberName);
210210

211-
PythonQtSlotInfo* findDecoratorSlots(const char* memberName, int memberNameLen, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
211+
PythonQtSlotInfo* findDecoratorSlots(const char* memberName, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
212212
int findCharOffset(const char* sigStart, char someChar);
213213

214214
QHash<QByteArray, PythonQtMemberInfo> _cachedMembers;

src/PythonQtConversion.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ PyObject* PythonQtConv::ConvertQtValueToPythonInternal(int type, const void* dat
178178
case PythonQtMethodInfo::Variant:
179179
return PythonQtConv::QVariantToPyObject(*((QVariant*)data));
180180
case QMetaType::QObjectStar:
181+
#if( QT_VERSION < QT_VERSION_CHECK(5,0,0) )
181182
case QMetaType::QWidgetStar:
183+
#endif
182184
return PythonQt::priv()->wrapQObject(*((QObject**)data));
183185

184186
default:
@@ -190,7 +192,11 @@ PyObject* PythonQtConv::ConvertQtValueToPythonInternal(int type, const void* dat
190192
} else {
191193
if (type > 0) {
192194
// if the type is known, we can construct it via QMetaType::construct
195+
#if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
196+
void* newCPPObject = QMetaType::create(type, data);
197+
#else
193198
void* newCPPObject = QMetaType::construct(type, data);
199+
#endif
194200
// XXX this could be optimized by using metatypeid directly
195201
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type));
196202
wrap->_ownedByPythonQt = true;

src/PythonQtConversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ bool PythonQtConvertPythonListToListOfValueType(PyObject* obj, void* /*QList<T>*
206206
// this is quite some overhead, but it avoids having another large switch...
207207
QVariant v = PythonQtConv::PyObjToQVariant(value, innerType);
208208
if (v.isValid()) {
209-
list->push_back(qVariantValue<T>(v));
209+
list->push_back(qvariant_cast<T>(v));
210210
} else {
211211
result = false;
212212
break;

src/PythonQtMethodInfo.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ QHash<QByteArray, QByteArray> PythonQtMethodInfo::_parameterNameAliases;
4949
PythonQtMethodInfo::PythonQtMethodInfo(const QMetaMethod& meta, PythonQtClassInfo* classInfo)
5050
{
5151
#ifdef PYTHONQT_DEBUG
52-
QByteArray sig(meta.signature());
52+
QByteArray sig = PythonQtUtils::signature(meta));
5353
sig = sig.mid(sig.indexOf('('));
5454
QByteArray fullSig = QByteArray(meta.typeName()) + " " + sig;
5555
std::cout << "caching " << fullSig.data() << std::endl;
@@ -78,7 +78,7 @@ PythonQtMethodInfo::PythonQtMethodInfo(const QByteArray& typeName, const QList<Q
7878

7979
const PythonQtMethodInfo* PythonQtMethodInfo::getCachedMethodInfo(const QMetaMethod& signal, PythonQtClassInfo* classInfo)
8080
{
81-
QByteArray sig(signal.signature());
81+
QByteArray sig(PythonQtUtils::signature(signal));
8282
sig = sig.mid(sig.indexOf('('));
8383
QByteArray fullSig = QByteArray(signal.typeName()) + " " + sig;
8484
PythonQtMethodInfo* result = _cachedSignatures.value(fullSig);
@@ -151,7 +151,11 @@ void PythonQtMethodInfo::fillParameterInfo(ParameterInfo& type, const QByteArray
151151
type.typeId = nameToType(name);
152152
if ((type.pointerCount == 0) && type.typeId == Unknown) {
153153
type.typeId = QMetaType::type(name.constData());
154+
#if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
155+
if (type.typeId == QMetaType::UnknownType) {
156+
#else
154157
if (type.typeId == QMetaType::Void) {
158+
#endif
155159
type.typeId = Unknown;
156160
}
157161
}
@@ -369,11 +373,12 @@ QString PythonQtSlotInfo::fullSignature()
369373
}
370374

371375

372-
QByteArray PythonQtSlotInfo::slotName()
376+
QByteArray PythonQtSlotInfo::slotName() const
373377
{
374-
QByteArray sig(_meta.signature());
375-
int idx = sig.indexOf('(');
376-
sig = sig.left(idx);
377-
return sig;
378+
return PythonQtUtils::methodName(_meta);
378379
}
379380

381+
QByteArray PythonQtSlotInfo::signature() const
382+
{
383+
return PythonQtUtils::signature(_meta);
384+
}

src/PythonQtMethodInfo.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,11 @@ class PythonQtSlotInfo : public PythonQtMethodInfo
174174
//! get the full signature including return type
175175
QString fullSignature();
176176

177+
//! get the Qt signature of the slot
178+
QByteArray signature() const;
179+
177180
//! get the short slot name
178-
QByteArray slotName();
181+
QByteArray slotName() const;
179182

180183
private:
181184
int _slotIndex;

src/PythonQtObjectPtr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ QVariant PythonQtObjectPtr::call(const QVariantList& args, const QVariantMap& kw
109109
bool PythonQtObjectPtr::fromVariant(const QVariant& variant)
110110
{
111111
if (!variant.isNull()) {
112-
setObject(qVariantValue<PythonQtObjectPtr>(variant));
112+
setObject(qvariant_cast<PythonQtObjectPtr>(variant));
113113
return true;
114114
}
115115
else {

src/PythonQtSignal.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ meth_get__doc__(PythonQtSignalFunctionObject * /*m*/, void * /*closure*/)
109109
static PyObject *
110110
meth_get__name__(PythonQtSignalFunctionObject *m, void * /*closure*/)
111111
{
112-
return PyString_FromString(m->m_ml->metaMethod()->signature());
112+
return PyString_FromString(m->m_ml->signature());
113113
}
114114

115115
static int
@@ -187,7 +187,7 @@ static PyObject *PythonQtSignalFunction_connect(PythonQtSignalFunctionObject* ty
187187
if (argc==1) {
188188
// connect with Python callable
189189
PyObject* callable = PyTuple_GET_ITEM(args, 0);
190-
bool result = PythonQt::self()->addSignalHandler(self->_obj, QByteArray("2") + type->m_ml->metaMethod()->signature(), callable);
190+
bool result = PythonQt::self()->addSignalHandler(self->_obj, QByteArray("2") + type->m_ml->signature(), callable);
191191
return PythonQtConv::GetPyBool(result);
192192
} else {
193193
PyErr_SetString(PyExc_ValueError, "Called connect with wrong number of arguments");
@@ -203,7 +203,7 @@ static PyObject *PythonQtSignalFunction_disconnect(PythonQtSignalFunctionObject*
203203
PythonQtInstanceWrapper* self = (PythonQtInstanceWrapper*) type->m_self;
204204
if (self->_obj) {
205205
Py_ssize_t argc = PyTuple_Size(args);
206-
QByteArray signal = QByteArray("2") + type->m_ml->metaMethod()->signature();
206+
QByteArray signal = QByteArray("2") + type->m_ml->signature();
207207
if (argc==1) {
208208
// disconnect with Python callable
209209
PyObject* callable = PyTuple_GET_ITEM(args, 0);
@@ -272,7 +272,7 @@ meth_compare(PythonQtSignalFunctionObject *a, PythonQtSignalFunctionObject *b)
272272
return (a->m_self < b->m_self) ? -1 : 1;
273273
if (a->m_ml == b->m_ml)
274274
return 0;
275-
if (strcmp(a->m_ml->metaMethod()->signature(), b->m_ml->metaMethod()->signature()) < 0)
275+
if (strcmp(a->m_ml->signature().constData(), b->m_ml->signature().constData()) < 0)
276276
return -1;
277277
else
278278
return 1;

0 commit comments

Comments
 (0)