Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtVariantWrapper.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 PythonQtVariantWrapper.cpp
36
// \author Florian Link
37
// \author Last changed by $Author: florian $
38
// \date 2006-05
39
*/
40
//----------------------------------------------------------------------------------
41
42
#include "PythonQtVariantWrapper.h"
43
#include <QObject>
44
#include <QDate>
45
#include <QDateTime>
46
#include <QTime>
47
#include "PythonQt.h"
48
#include "PythonQtSlot.h"
49
#include "PythonQtClassInfo.h"
50
#include "PythonQtConversion.h"
51
52
static void PythonQtVariantWrapper_dealloc(PythonQtVariantWrapper* self)
53
{
54
if (self->_variant) {
55
delete self->_variant;
56
self->_variant = NULL;
57
}
58
self->ob_type->tp_free((PyObject*)self);
59
}
60
61
static PyObject* PythonQtVariantWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
62
{
63
PythonQtVariantWrapper *self;
64
65
self = (PythonQtVariantWrapper *)type->tp_alloc(type, 0);
66
if (self != NULL) {
67
self->_variant = new QVariant();
68
self->_info = NULL;
69
}
70
return (PyObject *)self;
71
}
72
73
static int PythonQtVariantWrapper_init(PythonQtVariantWrapper *self, PyObject *args, PyObject *kwds)
74
{
75
return 0;
76
}
77
78
static PyObject *PythonQtVariantWrapper_classname(PythonQtVariantWrapper* type)
79
{
80
return PyString_FromString(type->_info->className());
81
}
82
83
static PyObject *PythonQtVariantWrapper_help(PythonQtVariantWrapper* type)
84
{
85
return PythonQt::self()->helpCalled(type->_info);
86
}
87
88
89
static PyMethodDef PythonQtVariantWrapper_methods[] = {
90
{"className", (PyCFunction)PythonQtVariantWrapper_classname, METH_NOARGS,
91
"Return the classname of the object"
92
},
93
{"help", (PyCFunction)PythonQtVariantWrapper_help, METH_NOARGS,
94
"Shows the help of available methods for this class"
95
},
96
{NULL} /* Sentinel */
97
};
98
99
100
static PyObject *PythonQtVariantWrapper_getattro(PyObject *obj,PyObject *name)
101
{
102
const char *attributeName;
103
PythonQtVariantWrapper *wt = (PythonQtVariantWrapper *)obj;
104
105
if ((attributeName = PyString_AsString(name)) == NULL) {
106
return NULL;
107
}
108
109
if (wt->_wrapper && wt->_info) {
110
PythonQtMemberInfo member = wt->_info->member(attributeName);
111
if (member._type == PythonQtMemberInfo::Slot) {
112
return PythonQtSlotFunction_New(member._slot, obj, NULL);
113
} else if (member._type == PythonQtMemberInfo::EnumValue) {
114
return PyInt_FromLong(member._enumValue);
115
}
116
}
117
118
// look for the internal methods (className(), help())
119
PyObject* internalMethod = Py_FindMethod( PythonQtVariantWrapper_methods, obj, (char*)attributeName);
120
if (internalMethod) {
121
return internalMethod;
122
}
123
PyErr_Clear();
124
125
if (qstrcmp(attributeName, "__dict__")==0) {
126
QStringList l = wt->_info->memberList(false);
127
PyObject* dict = PyDict_New();
128
foreach (QString name, l) {
129
//PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
130
PyDict_SetItemString(dict, name.toLatin1().data(), Py_None);
131
//Py_DECREF(o);
132
}
133
return dict;
134
}
135
136
QString error = QString(wt->_variant->typeName()) + " has no attribute named '" + QString(attributeName) + "'";
137
PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
138
139
return NULL;
140
}
141
142
QString qVariantToString(const QVariant& v) {
143
QString r;
144
switch (v.type()) {
145
case QVariant::Size:
146
r = QString::number(v.toSize().width()) + ", " + QString::number(v.toSize().height());
147
break;
148
case QVariant::SizeF:
149
r = QString::number(v.toSizeF().width()) + ", " + QString::number(v.toSizeF().height());
150
break;
151
case QVariant::Point:
152
r = QString::number(v.toPoint().x()) + ", " + QString::number(v.toPoint().y());
153
break;
154
case QVariant::PointF:
155
r = QString::number(v.toPointF().x()) + ", " + QString::number(v.toPointF().y());
156
break;
157
case QVariant::Rect:
158
r = QString::number(v.toRect().x()) + ", " + QString::number(v.toRect().y());
159
r += ", " + QString::number(v.toRect().width()) + ", " + QString::number(v.toRect().height());
160
break;
161
case QVariant::RectF:
162
r = QString::number(v.toRectF().x()) + ", " + QString::number(v.toRectF().y());
163
r += ", " + QString::number(v.toRectF().width()) + ", " + QString::number(v.toRectF().height());
164
break;
165
case QVariant::Date:
166
r = v.toDate().toString("ddMMyyyy");
167
break;
168
case QVariant::DateTime:
169
r = v.toDateTime().toString("ddMMyyyy,hh:mm:ss");
170
break;
171
case QVariant::Time:
172
r = v.toTime().toString("hh:mm:ss");
173
break;
174
//TODO: add more printing for other variant types
175
default:
176
r = v.toString();
177
}
178
return r;
179
}
180
181
static PyObject * PythonQtVariantWrapper_str(PyObject * obj)
182
{
183
PythonQtVariantWrapper* wt = (PythonQtVariantWrapper*)obj;
184
QString val = qVariantToString(*wt->_variant);
185
return PyString_FromFormat("(%s)", val.toLatin1().constData());
186
}
187
188
static PyObject * PythonQtVariantWrapper_repr(PyObject * obj)
189
{
190
PythonQtVariantWrapper* wt = (PythonQtVariantWrapper*)obj;
191
QString val = qVariantToString(*wt->_variant);
192
return PyString_FromFormat("%s(%s)", wt->_variant->typeName(), val.toLatin1().constData());
193
}
194
195
static int PythonQtVariantWrapper_compare(PyObject * obj1, PyObject * obj2)
196
{
197
if (obj1->ob_type == &PythonQtVariantWrapper_Type &&
198
obj2->ob_type == &PythonQtVariantWrapper_Type) {
199
200
PythonQtVariantWrapper* w1 = (PythonQtVariantWrapper*)obj1;
201
PythonQtVariantWrapper* w2 = (PythonQtVariantWrapper*)obj2;
202
if (*w1->_variant == *w2->_variant) {
203
return 0;
204
} else {
205
return -1;
206
}
207
} else {
208
return -1;
209
}
210
}
211
212
213
PyTypeObject PythonQtVariantWrapper_Type = {
214
PyObject_HEAD_INIT(NULL)
215
0, /*ob_size*/
216
"PythonQt.PythonQtVariantWrapper", /*tp_name*/
217
sizeof(PythonQtVariantWrapper), /*tp_basicsize*/
218
0, /*tp_itemsize*/
219
(destructor)PythonQtVariantWrapper_dealloc, /*tp_dealloc*/
220
0, /*tp_print*/
221
0, /*tp_getattr*/
222
0, /*tp_setattr*/
223
PythonQtVariantWrapper_compare, /*tp_compare*/
224
PythonQtVariantWrapper_repr, /*tp_repr*/
225
0, /*tp_as_number*/
226
0, /*tp_as_sequence*/
227
0, /*tp_as_mapping*/
228
0, /*tp_hash */
229
0, /*tp_call*/
230
PythonQtVariantWrapper_str, /*tp_str*/
231
PythonQtVariantWrapper_getattro, /*tp_getattro*/
232
0, /*tp_setattro*/
233
0, /*tp_as_buffer*/
234
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
235
"PythonQtVariantWrapper object", /* tp_doc */
236
0, /* tp_traverse */
237
0, /* tp_clear */
238
0, /* tp_richcompare */
239
0, /* tp_weaklistoffset */
240
0, /* tp_iter */
241
0, /* tp_iternext */
242
0, /* tp_methods */
243
0, /* tp_members */
244
0, /* tp_getset */
245
0, /* tp_base */
246
0, /* tp_dict */
247
0, /* tp_descr_get */
248
0, /* tp_descr_set */
249
0, /* tp_dictoffset */
250
(initproc)PythonQtVariantWrapper_init, /* tp_init */
251
0, /* tp_alloc */
252
PythonQtVariantWrapper_new, /* tp_new */
253
};
254
255
//-------------------------------------------------------
256
257
258