Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtMetaObjectWrapper.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 PythonQtMetaObjectWrapper.cpp35// \author Florian Link36// \author Last changed by $Author: florian $37// \date 2006-0538*/39//----------------------------------------------------------------------------------4041#include "PythonQtMetaObjectWrapper.h"42#include <QObject>4344#include "PythonQt.h"45#include "PythonQtSlot.h"46#include "PythonQtClassInfo.h"47#include "PythonQtConversion.h"4849static void PythonQtMetaObjectWrapper_dealloc(PythonQtMetaObjectWrapper* self)50{51self->ob_type->tp_free((PyObject*)self);52}5354static PyObject* PythonQtMetaObjectWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)55{56PythonQtMetaObjectWrapper *self;5758self = (PythonQtMetaObjectWrapper *)type->tp_alloc(type, 0);59if (self != NULL) {60self->_info = NULL;61}62return (PyObject *)self;63}6465static int PythonQtMetaObjectWrapper_init(PythonQtMetaObjectWrapper *self, PyObject *args, PyObject *kwds)66{67return 0;68}6970PyObject *PythonQtMetaObjectWrapper_call(PyObject *func, PyObject *args, PyObject *kw) {71PythonQtMetaObjectWrapper* wrapper = (PythonQtMetaObjectWrapper*)func;72PyObject* result = NULL;73QString error;74PyObject* err = NULL;75if (wrapper->_info->constructors()) {76result = PythonQtSlotFunction_CallImpl(NULL, wrapper->_info->constructors(), args, kw);77err = PyErr_Occurred();78}79if (!result) {80QObject* v = NULL;81QListIterator<PythonQtConstructorHandler*> it(PythonQt::self()->constructorHandlers());82while (!v && it.hasNext()) {83v = it.next()->create(wrapper->_info->metaObject(), args, kw, error);84}85if (v) {86result = PythonQt::priv()->wrapQObject(v);87}88}89if (result) {90// change ownershipflag to be owned by PythonQt91if (result->ob_type == &PythonQtWrapper_Type) {92((PythonQtWrapper*)result)->_ownedByPythonQt = true;93}94} else {95if (!wrapper->_info->constructors()) {96if (!err) {97if (error.isEmpty()) {98error = QString("No constructors available for ") + wrapper->_info->className();99}100PyErr_SetString(PyExc_ValueError, error.toLatin1().data());101}102}103}104return result;105}106107static PyObject *PythonQtMetaObjectWrapper_classname(PythonQtMetaObjectWrapper* type)108{109return PyString_FromString((QString("Meta_") + type->_info->className()).toLatin1().data());110}111112static PyObject *PythonQtMetaObjectWrapper_help(PythonQtMetaObjectWrapper* type)113{114return PythonQt::self()->helpCalled(type->_info);115}116117118static PyMethodDef PythonQtMetaObjectWrapper_methods[] = {119{"className", (PyCFunction)PythonQtMetaObjectWrapper_classname, METH_NOARGS,120"Return the classname of the object"121},122{"help", (PyCFunction)PythonQtMetaObjectWrapper_help, METH_NOARGS,123"Shows the help of available methods for this class"124},125{NULL} /* Sentinel */126};127128129static PyObject *PythonQtMetaObjectWrapper_getattro(PyObject *obj,PyObject *name)130{131const char *attributeName;132PythonQtMetaObjectWrapper *wt = (PythonQtMetaObjectWrapper *)obj;133134if ((attributeName = PyString_AsString(name)) == NULL) {135return NULL;136}137138PythonQtMemberInfo member = wt->_info->member(attributeName);139if (member._type == PythonQtMemberInfo::EnumValue) {140return PyInt_FromLong(member._enumValue);141}142if (member._type == PythonQtMemberInfo::Slot && member._slot->isClassDecorator()) {143return PythonQtSlotFunction_New(member._slot, obj, NULL);144}145146// look for the internal methods (className(), help())147PyObject* internalMethod = Py_FindMethod( PythonQtMetaObjectWrapper_methods, obj, (char*)attributeName);148if (internalMethod) {149return internalMethod;150}151PyErr_Clear();152153if (qstrcmp(attributeName, "__dict__")==0) {154QStringList l = wt->_info->memberList(true);155PyObject* dict = PyDict_New();156foreach (QString name, l) {157//PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());158PyDict_SetItemString(dict, name.toLatin1().data(), Py_None);159//Py_DECREF(o);160}161return dict;162}163164QString error = QString(wt->_info->className()) + " has no attribute named '" + QString(attributeName) + "'";165PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());166return NULL;167}168169static PyObject * PythonQtMetaObjectWrapper_repr(PyObject * obj)170{171PythonQtMetaObjectWrapper* wt = (PythonQtMetaObjectWrapper*)obj;172if (wt->_info->isCPPWrapper()) {173return PyString_FromFormat("%s Class (C++ wrapped by %s)", wt->_info->className(), wt->_info->metaObject()->className());174} else {175return PyString_FromFormat("%s Class", wt->_info->className());176}177}178179static int PythonQtMetaObjectWrapper_compare(PyObject * obj1, PyObject * obj2)180{181if (obj1->ob_type == &PythonQtMetaObjectWrapper_Type &&182obj2->ob_type == &PythonQtMetaObjectWrapper_Type) {183184PythonQtMetaObjectWrapper* w1 = (PythonQtMetaObjectWrapper*)obj1;185PythonQtMetaObjectWrapper* w2 = (PythonQtMetaObjectWrapper*)obj2;186if (w1->_info == w2->_info) {187return 0;188} else {189return -1;190}191} else {192return -1;193}194}195196PyTypeObject PythonQtMetaObjectWrapper_Type = {197PyObject_HEAD_INIT(NULL)1980, /*ob_size*/199"PythonQt.PythonQtMetaObjectWrapper", /*tp_name*/200sizeof(PythonQtMetaObjectWrapper), /*tp_basicsize*/2010, /*tp_itemsize*/202(destructor)PythonQtMetaObjectWrapper_dealloc, /*tp_dealloc*/2030, /*tp_print*/2040, /*tp_getattr*/2050, /*tp_setattr*/206PythonQtMetaObjectWrapper_compare, /*tp_compare*/207PythonQtMetaObjectWrapper_repr, /*tp_repr*/2080, /*tp_as_number*/2090, /*tp_as_sequence*/2100, /*tp_as_mapping*/2110, /*tp_hash */212PythonQtMetaObjectWrapper_call, /*tp_call*/2130, /*tp_str*/214PythonQtMetaObjectWrapper_getattro, /*tp_getattro*/2150, /*tp_setattro*/2160, /*tp_as_buffer*/217Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/218"PythonQtMetaObjectWrapper object", /* tp_doc */2190, /* tp_traverse */2200, /* tp_clear */2210, /* tp_richcompare */2220, /* tp_weaklistoffset */2230, /* tp_iter */2240, /* tp_iternext */2250, /* tp_methods */2260, /* tp_members */2270, /* tp_getset */2280, /* tp_base */2290, /* tp_dict */2300, /* tp_descr_get */2310, /* tp_descr_set */2320, /* tp_dictoffset */233(initproc)PythonQtMetaObjectWrapper_init, /* tp_init */2340, /* tp_alloc */235PythonQtMetaObjectWrapper_new, /* tp_new */236};237238//-------------------------------------------------------239240241242