2
* This file is part of the PySide project.
4
* Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
6
* Contact: PySide team <contact@pyside.org>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27
#include "qproperty.h"
30
#define QPROPERTY_CLASS_NAME "Property"
55
static int qproperty_init(PyObject*, PyObject*, PyObject*);
56
static void qproperty_free(void*);
59
static char* translate_type_name(PyObject*);
61
PyTypeObject QProperty_Type = {
64
QPROPERTY_CLASS_NAME, /*tp_name*/
65
sizeof(QPropertyData), /*tp_basicsize*/
82
Py_TPFLAGS_DEFAULT, /*tp_flags*/
86
0, /*tp_richcompare */
87
0, /*tp_weaklistoffset */
98
(initproc)qproperty_init, /*tp_init */
100
PyType_GenericNew, /*tp_new */
101
qproperty_free, /*tp_free */
106
0, /*tp_subclasses */
111
void init_qproperty(PyObject* module)
113
if (PyType_Ready(&QProperty_Type) < 0)
116
Py_INCREF(&QProperty_Type);
117
PyModule_AddObject(module, QPROPERTY_CLASS_NAME, ((PyObject*)&QProperty_Type));
122
int qproperty_init(PyObject* self, PyObject* args, PyObject* kwds)
125
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
126
data->designable = true;
127
data->scriptable = true;
130
static const char *kwlist[] = {"type", "fget", "fset", "freset", "fdel", "doc",
131
"designable", "scriptable", "stored", "user",
132
"constant", "final", 0};
133
if (!PyArg_ParseTupleAndKeywords(args, kwds,
134
"OO|OOOsbbbbbb:QtCore.QProperty", (char**) kwlist,
135
/*OO*/ &type, &(data->fget),
136
/*OOOO*/ &(data->fset), &(data->freset), &(data->fdel),
138
/*bbbbbb*/ &(data->designable), &(data->scriptable), &(data->stored), &(data->user), &(data->constant), &(data->final)))
141
if (!data->fset && data->fget)
142
data->constant = true;
144
data->typeName = translate_type_name(type);
148
void qproperty_free(void *self)
150
PyObject *pySelf = reinterpret_cast<PyObject*>(self);
151
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
153
free(data->typeName);
156
pySelf->ob_type->tp_base->tp_free(self);
159
bool isQPropertyType(PyObject* pyObj)
162
return pyObj->ob_type == &QProperty_Type;
167
int qproperty_set(PyObject* self, PyObject* source, PyObject* value)
169
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
171
Shiboken::AutoDecRef args(PyTuple_New(2));
172
PyTuple_SET_ITEM(args, 0, source);
173
PyTuple_SET_ITEM(args, 1, value);
176
Shiboken::AutoDecRef result(PyObject_CallObject(data->fset, args));
177
return (result.isNull() ? -1 : 0);
179
PyErr_SetString(PyExc_AttributeError, "Attibute read only");
184
PyObject* qproperty_get(PyObject* self, PyObject* source)
186
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
188
Shiboken::AutoDecRef args(PyTuple_New(1));
190
PyTuple_SET_ITEM(args, 0, source);
191
return PyObject_CallObject(data->fget, args);
196
int qproperty_reset(PyObject* self, PyObject* source)
198
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
200
Shiboken::AutoDecRef args(PyTuple_New(1));
202
PyTuple_SET_ITEM(args, 0, source);
203
Shiboken::AutoDecRef result(PyObject_CallObject(data->freset, args));
204
return (result.isNull() ? -1 : 0);
210
const char* qproperty_get_type(PyObject* self)
212
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
213
return data->typeName;
216
PyObject* qproperty_get_object(PyObject* source, PyObject* name)
218
PyObject* attr = PyObject_GenericGetAttr(source, name);
219
if (attr && isQPropertyType(attr))
223
PyErr_Clear(); //Clear possible error caused by PyObject_GenericGetAttr
229
char* translate_type_name(PyObject* type)
231
if (PyType_Check(type)) {
232
char *typeName = NULL;
233
if (type->ob_type == &Shiboken::SbkBaseWrapperType_Type) {
234
Shiboken::SbkBaseWrapperType *objType = reinterpret_cast<Shiboken::SbkBaseWrapperType*>(type);
235
typeName = strdup(objType->original_name);
237
//tp_name return the full name
238
Shiboken::AutoDecRef otypeName(PyObject_GetAttrString(type, "__name__"));
239
typeName = strdup(PyString_AS_STRING(otypeName.object()));
241
if (Shiboken::TypeResolver::getType(typeName) == Shiboken::TypeResolver::ObjectType) {
242
typeName = reinterpret_cast<char*>(realloc(typeName, strlen(typeName) + 1));
243
typeName = strcat(typeName, "*");
246
} else if (PyString_Check(type)) {
247
return strdup(PyString_AS_STRING(type));
252
bool qproperty_is_readble(PyObject* self)
254
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
255
return (data->fget != 0);
258
bool qproperty_is_writable(PyObject* self)
260
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
261
return (data->fset != 0);
264
bool qproperty_has_reset(PyObject* self)
266
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
267
return (data->freset != 0);
270
bool qproperty_is_designable(PyObject* self)
272
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
273
return data->designable;
276
bool qproperty_is_scriptable(PyObject* self)
278
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
279
return data->scriptable;
282
bool qproperty_is_stored(PyObject* self)
284
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
288
bool qproperty_is_user(PyObject* self)
290
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
294
bool qproperty_is_constant(PyObject* self)
296
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
297
return data->constant;
300
bool qproperty_is_final(PyObject* self)
302
QPropertyData *data = reinterpret_cast<QPropertyData*>(self);