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