Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtMetaObjectWrapper.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 PythonQtMetaObjectWrapper.cpp
36
// \author Florian Link
37
// \author Last changed by $Author: florian $
38
// \date 2006-05
39
*/
40
//----------------------------------------------------------------------------------
41
42
#include "PythonQtMetaObjectWrapper.h"
43
#include <QObject>
44
45
#include "PythonQt.h"
46
#include "PythonQtSlot.h"
47
#include "PythonQtClassInfo.h"
48
#include "PythonQtConversion.h"
49
50
static void PythonQtMetaObjectWrapper_dealloc(PythonQtMetaObjectWrapper* self)
51
{
52
self->ob_type->tp_free((PyObject*)self);
53
}
54
55
static PyObject* PythonQtMetaObjectWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
56
{
57
PythonQtMetaObjectWrapper *self;
58
59
self = (PythonQtMetaObjectWrapper *)type->tp_alloc(type, 0);
60
if (self != NULL) {
61
self->_info = NULL;
62
}
63
return (PyObject *)self;
64
}
65
66
static int PythonQtMetaObjectWrapper_init(PythonQtMetaObjectWrapper *self, PyObject *args, PyObject *kwds)
67
{
68
return 0;
69
}
70
71
PyObject *PythonQtMetaObjectWrapper_call(PyObject *func, PyObject *args, PyObject *kw) {
72
PythonQtMetaObjectWrapper* wrapper = (PythonQtMetaObjectWrapper*)func;
73
PyObject* result = NULL;
74
QString error;
75
PyObject* err = NULL;
76
if (wrapper->_info->constructors()) {
77
result = PythonQtSlotFunction_CallImpl(NULL, wrapper->_info->constructors(), args, kw);
78
err = PyErr_Occurred();
79
}
80
if (!result) {
81
QObject* v = NULL;
82
QListIterator<PythonQtConstructorHandler*> it(PythonQt::self()->constructorHandlers());
83
while (!v && it.hasNext()) {
84
v = it.next()->create(wrapper->_info->metaObject(), args, kw, error);
85
}
86
if (v) {
87
result = PythonQt::priv()->wrapQObject(v);
88
}
89
}
90
if (result) {
91
// change ownershipflag to be owned by PythonQt
92
if (result->ob_type == &PythonQtWrapper_Type) {
93
((PythonQtWrapper*)result)->_ownedByPythonQt = true;
94
}
95
} else {
96
if (!wrapper->_info->constructors()) {
97
if (!err) {
98
if (error.isEmpty()) {
99
error = QString("No constructors available for ") + wrapper->_info->className();
100
}
101
PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
102
}
103
}
104
}
105
return result;
106
}
107
108
static PyObject *PythonQtMetaObjectWrapper_classname(PythonQtMetaObjectWrapper* type)
109
{
110
return PyString_FromString((QString("Meta_") + type->_info->className()).toLatin1().data());
111
}
112
113
static PyObject *PythonQtMetaObjectWrapper_help(PythonQtMetaObjectWrapper* type)
114
{
115
return PythonQt::self()->helpCalled(type->_info);
116
}
117
118
119
static PyMethodDef PythonQtMetaObjectWrapper_methods[] = {
120
{"className", (PyCFunction)PythonQtMetaObjectWrapper_classname, METH_NOARGS,
121
"Return the classname of the object"
122
},
123
{"help", (PyCFunction)PythonQtMetaObjectWrapper_help, METH_NOARGS,
124
"Shows the help of available methods for this class"
125
},
126
{NULL} /* Sentinel */
127
};
128
129
130
static PyObject *PythonQtMetaObjectWrapper_getattro(PyObject *obj,PyObject *name)
131
{
132
const char *attributeName;
133
PythonQtMetaObjectWrapper *wt = (PythonQtMetaObjectWrapper *)obj;
134
135
if ((attributeName = PyString_AsString(name)) == NULL) {
136
return NULL;
137
}
138
139
PythonQtMemberInfo member = wt->_info->member(attributeName);
140
if (member._type == PythonQtMemberInfo::EnumValue) {
141
return PyInt_FromLong(member._enumValue);
142
}
143
if (member._type == PythonQtMemberInfo::Slot && member._slot->isClassDecorator()) {
144
return PythonQtSlotFunction_New(member._slot, obj, NULL);
145
}
146
147
// look for the internal methods (className(), help())
148
PyObject* internalMethod = Py_FindMethod( PythonQtMetaObjectWrapper_methods, obj, (char*)attributeName);
149
if (internalMethod) {
150
return internalMethod;
151
}
152
PyErr_Clear();
153
154
if (qstrcmp(attributeName, "__dict__")==0) {
155
QStringList l = wt->_info->memberList(true);
156
PyObject* dict = PyDict_New();
157
foreach (QString name, l) {
158
//PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
159
PyDict_SetItemString(dict, name.toLatin1().data(), Py_None);
160
//Py_DECREF(o);
161
}
162
return dict;
163
}
164
165
QString error = QString(wt->_info->className()) + " has no attribute named '" + QString(attributeName) + "'";
166
PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
167
return NULL;
168
}
169
170
static PyObject * PythonQtMetaObjectWrapper_repr(PyObject * obj)
171
{
172
PythonQtMetaObjectWrapper* wt = (PythonQtMetaObjectWrapper*)obj;
173
if (wt->_info->isCPPWrapper()) {
174
return PyString_FromFormat("%s Class (C++ wrapped by %s)", wt->_info->className(), wt->_info->metaObject()->className());
175
} else {
176
return PyString_FromFormat("%s Class", wt->_info->className());
177
}
178
}
179
180
static int PythonQtMetaObjectWrapper_compare(PyObject * obj1, PyObject * obj2)
181
{
182
if (obj1->ob_type == &PythonQtMetaObjectWrapper_Type &&
183
obj2->ob_type == &PythonQtMetaObjectWrapper_Type) {
184
185
PythonQtMetaObjectWrapper* w1 = (PythonQtMetaObjectWrapper*)obj1;
186
PythonQtMetaObjectWrapper* w2 = (PythonQtMetaObjectWrapper*)obj2;
187
if (w1->_info == w2->_info) {
188
return 0;
189
} else {
190
return -1;
191
}
192
} else {
193
return -1;
194
}
195
}
196
197
PyTypeObject PythonQtMetaObjectWrapper_Type = {
198
PyObject_HEAD_INIT(NULL)
199
0, /*ob_size*/
200
"PythonQt.PythonQtMetaObjectWrapper", /*tp_name*/
201
sizeof(PythonQtMetaObjectWrapper), /*tp_basicsize*/
202
0, /*tp_itemsize*/
203
(destructor)PythonQtMetaObjectWrapper_dealloc, /*tp_dealloc*/
204
0, /*tp_print*/
205
0, /*tp_getattr*/
206
0, /*tp_setattr*/
207
PythonQtMetaObjectWrapper_compare, /*tp_compare*/
208
PythonQtMetaObjectWrapper_repr, /*tp_repr*/
209
0, /*tp_as_number*/
210
0, /*tp_as_sequence*/
211
0, /*tp_as_mapping*/
212
0, /*tp_hash */
213
PythonQtMetaObjectWrapper_call, /*tp_call*/
214
0, /*tp_str*/
215
PythonQtMetaObjectWrapper_getattro, /*tp_getattro*/
216
0, /*tp_setattro*/
217
0, /*tp_as_buffer*/
218
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
219
"PythonQtMetaObjectWrapper object", /* tp_doc */
220
0, /* tp_traverse */
221
0, /* tp_clear */
222
0, /* tp_richcompare */
223
0, /* tp_weaklistoffset */
224
0, /* tp_iter */
225
0, /* tp_iternext */
226
0, /* tp_methods */
227
0, /* tp_members */
228
0, /* tp_getset */
229
0, /* tp_base */
230
0, /* tp_dict */
231
0, /* tp_descr_get */
232
0, /* tp_descr_set */
233
0, /* tp_dictoffset */
234
(initproc)PythonQtMetaObjectWrapper_init, /* tp_init */
235
0, /* tp_alloc */
236
PythonQtMetaObjectWrapper_new, /* tp_new */
237
};
238
239
//-------------------------------------------------------
240
241
242