Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtVariantWrapper.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 PythonQtVariantWrapper.cpp35// \author Florian Link36// \author Last changed by $Author: florian $37// \date 2006-0538*/39//----------------------------------------------------------------------------------4041#include "PythonQtVariantWrapper.h"42#include <QObject>43#include <QDate>44#include <QDateTime>45#include <QTime>46#include "PythonQt.h"47#include "PythonQtSlot.h"48#include "PythonQtClassInfo.h"49#include "PythonQtConversion.h"5051static void PythonQtVariantWrapper_dealloc(PythonQtVariantWrapper* self)52{53if (self->_variant) {54delete self->_variant;55self->_variant = NULL;56}57self->ob_type->tp_free((PyObject*)self);58}5960static PyObject* PythonQtVariantWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)61{62PythonQtVariantWrapper *self;6364self = (PythonQtVariantWrapper *)type->tp_alloc(type, 0);65if (self != NULL) {66self->_variant = new QVariant();67self->_info = NULL;68}69return (PyObject *)self;70}7172static int PythonQtVariantWrapper_init(PythonQtVariantWrapper *self, PyObject *args, PyObject *kwds)73{74return 0;75}7677static PyObject *PythonQtVariantWrapper_classname(PythonQtVariantWrapper* type)78{79return PyString_FromString(type->_info->className());80}8182static PyObject *PythonQtVariantWrapper_help(PythonQtVariantWrapper* type)83{84return PythonQt::self()->helpCalled(type->_info);85}868788static PyMethodDef PythonQtVariantWrapper_methods[] = {89{"className", (PyCFunction)PythonQtVariantWrapper_classname, METH_NOARGS,90"Return the classname of the object"91},92{"help", (PyCFunction)PythonQtVariantWrapper_help, METH_NOARGS,93"Shows the help of available methods for this class"94},95{NULL} /* Sentinel */96};979899static PyObject *PythonQtVariantWrapper_getattro(PyObject *obj,PyObject *name)100{101const char *attributeName;102PythonQtVariantWrapper *wt = (PythonQtVariantWrapper *)obj;103104if ((attributeName = PyString_AsString(name)) == NULL) {105return NULL;106}107108if (wt->_wrapper && wt->_info) {109PythonQtMemberInfo member = wt->_info->member(attributeName);110if (member._type == PythonQtMemberInfo::Slot) {111return PythonQtSlotFunction_New(member._slot, obj, NULL);112} else if (member._type == PythonQtMemberInfo::EnumValue) {113return PyInt_FromLong(member._enumValue);114}115}116117// look for the internal methods (className(), help())118PyObject* internalMethod = Py_FindMethod( PythonQtVariantWrapper_methods, obj, (char*)attributeName);119if (internalMethod) {120return internalMethod;121}122PyErr_Clear();123124if (qstrcmp(attributeName, "__dict__")==0) {125QStringList l = wt->_info->memberList(false);126PyObject* dict = PyDict_New();127foreach (QString name, l) {128//PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());129PyDict_SetItemString(dict, name.toLatin1().data(), Py_None);130//Py_DECREF(o);131}132return dict;133}134135QString error = QString(wt->_variant->typeName()) + " has no attribute named '" + QString(attributeName) + "'";136PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());137138return NULL;139}140141QString qVariantToString(const QVariant& v) {142QString r;143switch (v.type()) {144case QVariant::Size:145r = QString::number(v.toSize().width()) + ", " + QString::number(v.toSize().height());146break;147case QVariant::SizeF:148r = QString::number(v.toSizeF().width()) + ", " + QString::number(v.toSizeF().height());149break;150case QVariant::Point:151r = QString::number(v.toPoint().x()) + ", " + QString::number(v.toPoint().y());152break;153case QVariant::PointF:154r = QString::number(v.toPointF().x()) + ", " + QString::number(v.toPointF().y());155break;156case QVariant::Rect:157r = QString::number(v.toRect().x()) + ", " + QString::number(v.toRect().y());158r += ", " + QString::number(v.toRect().width()) + ", " + QString::number(v.toRect().height());159break;160case QVariant::RectF:161r = QString::number(v.toRectF().x()) + ", " + QString::number(v.toRectF().y());162r += ", " + QString::number(v.toRectF().width()) + ", " + QString::number(v.toRectF().height());163break;164case QVariant::Date:165r = v.toDate().toString("ddMMyyyy");166break;167case QVariant::DateTime:168r = v.toDateTime().toString("ddMMyyyy,hh:mm:ss");169break;170case QVariant::Time:171r = v.toTime().toString("hh:mm:ss");172break;173//TODO: add more printing for other variant types174default:175r = v.toString();176}177return r;178}179180static PyObject * PythonQtVariantWrapper_str(PyObject * obj)181{182PythonQtVariantWrapper* wt = (PythonQtVariantWrapper*)obj;183QString val = qVariantToString(*wt->_variant);184return PyString_FromFormat("(%s)", val.toLatin1().constData());185}186187static PyObject * PythonQtVariantWrapper_repr(PyObject * obj)188{189PythonQtVariantWrapper* wt = (PythonQtVariantWrapper*)obj;190QString val = qVariantToString(*wt->_variant);191return PyString_FromFormat("%s(%s)", wt->_variant->typeName(), val.toLatin1().constData());192}193194static int PythonQtVariantWrapper_compare(PyObject * obj1, PyObject * obj2)195{196if (obj1->ob_type == &PythonQtVariantWrapper_Type &&197obj2->ob_type == &PythonQtVariantWrapper_Type) {198199PythonQtVariantWrapper* w1 = (PythonQtVariantWrapper*)obj1;200PythonQtVariantWrapper* w2 = (PythonQtVariantWrapper*)obj2;201if (*w1->_variant == *w2->_variant) {202return 0;203} else {204return -1;205}206} else {207return -1;208}209}210211212PyTypeObject PythonQtVariantWrapper_Type = {213PyObject_HEAD_INIT(NULL)2140, /*ob_size*/215"PythonQt.PythonQtVariantWrapper", /*tp_name*/216sizeof(PythonQtVariantWrapper), /*tp_basicsize*/2170, /*tp_itemsize*/218(destructor)PythonQtVariantWrapper_dealloc, /*tp_dealloc*/2190, /*tp_print*/2200, /*tp_getattr*/2210, /*tp_setattr*/222PythonQtVariantWrapper_compare, /*tp_compare*/223PythonQtVariantWrapper_repr, /*tp_repr*/2240, /*tp_as_number*/2250, /*tp_as_sequence*/2260, /*tp_as_mapping*/2270, /*tp_hash */2280, /*tp_call*/229PythonQtVariantWrapper_str, /*tp_str*/230PythonQtVariantWrapper_getattro, /*tp_getattro*/2310, /*tp_setattro*/2320, /*tp_as_buffer*/233Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/234"PythonQtVariantWrapper object", /* tp_doc */2350, /* tp_traverse */2360, /* tp_clear */2370, /* tp_richcompare */2380, /* tp_weaklistoffset */2390, /* tp_iter */2400, /* tp_iternext */2410, /* tp_methods */2420, /* tp_members */2430, /* tp_getset */2440, /* tp_base */2450, /* tp_dict */2460, /* tp_descr_get */2470, /* tp_descr_set */2480, /* tp_dictoffset */249(initproc)PythonQtVariantWrapper_init, /* tp_init */2500, /* tp_alloc */251PythonQtVariantWrapper_new, /* tp_new */252};253254//-------------------------------------------------------255256257258