~ubuntu-branches/ubuntu/utopic/pyside/utopic

« back to all changes in this revision

Viewing changes to libpyside/qproperty.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Raboud
  • Date: 2011-02-18 18:01:00 UTC
  • mfrom: (1.2.3 upstream) (6.1.6 experimental)
  • Revision ID: james.westby@ubuntu.com-20110218180100-vaczjij7g08fzfme
Tags: 1.0.0~rc1-1
* New 1.0.0~rc1 upstream release
  - Bump the B-D chain versions:
    + apiextractor to 0.10.0-2~
    + generatorrunner to 0.6.6
    + shiboken to 1.0.0~rc1
* Update patches to ~rc1.
* debian/watch: update to handle Release Candidates too.
* Bump XS-Python-Version to >= 2.6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of the PySide project.
3
 
 *
4
 
 * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
5
 
 *
6
 
 * Contact: PySide team <contact@pyside.org>
7
 
 *
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.
12
 
 *
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.
17
 
 *
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
21
 
 */
22
 
 
23
 
#include <shiboken.h>
24
 
#include <Python.h>
25
 
#include <QDebug>
26
 
 
27
 
#include "qproperty.h"
28
 
 
29
 
 
30
 
#define QPROPERTY_CLASS_NAME "Property"
31
 
 
32
 
namespace PySide
33
 
{
34
 
 
35
 
extern "C"
36
 
{
37
 
 
38
 
typedef struct {
39
 
    PyObject_HEAD
40
 
    char* typeName;
41
 
    PyObject* type;
42
 
    PyObject* fget;
43
 
    PyObject* fset;
44
 
    PyObject* freset;
45
 
    PyObject* fdel;
46
 
    char* doc;
47
 
    bool designable;
48
 
    bool scriptable;
49
 
    bool stored;
50
 
    bool user;
51
 
    bool constant;
52
 
    bool final;
53
 
} QPropertyData;
54
 
 
55
 
static int qproperty_init(PyObject*, PyObject*, PyObject*);
56
 
static void qproperty_free(void*);
57
 
 
58
 
//aux
59
 
static char* translate_type_name(PyObject*);
60
 
 
61
 
PyTypeObject QProperty_Type = {
62
 
    PyObject_HEAD_INIT(0)
63
 
    0,                         /*ob_size*/
64
 
    QPROPERTY_CLASS_NAME,      /*tp_name*/
65
 
    sizeof(QPropertyData),     /*tp_basicsize*/
66
 
    0,                         /*tp_itemsize*/
67
 
    0,                         /*tp_dealloc*/
68
 
    0,                         /*tp_print*/
69
 
    0,                         /*tp_getattr*/
70
 
    0,                         /*tp_setattr*/
71
 
    0,                         /*tp_compare*/
72
 
    0,                         /*tp_repr*/
73
 
    0,                         /*tp_as_number*/
74
 
    0,                         /*tp_as_sequence*/
75
 
    0,                         /*tp_as_mapping*/
76
 
    0,                         /*tp_hash */
77
 
    0,                         /*tp_call*/
78
 
    0,                         /*tp_str*/
79
 
    0,                         /*tp_getattro*/
80
 
    0,                         /*tp_setattro*/
81
 
    0,                         /*tp_as_buffer*/
82
 
    Py_TPFLAGS_DEFAULT,        /*tp_flags*/
83
 
    0,                         /*tp_doc */
84
 
    0,                         /*tp_traverse */
85
 
    0,                         /*tp_clear */
86
 
    0,                         /*tp_richcompare */
87
 
    0,                         /*tp_weaklistoffset */
88
 
    0,                         /*tp_iter */
89
 
    0,                         /*tp_iternext */
90
 
    0,                         /*tp_methods */
91
 
    0,                         /*tp_members */
92
 
    0,                         /*tp_getset */
93
 
    0,                         /*tp_base */
94
 
    0,                         /*tp_dict */
95
 
    0,                         /*tp_descr_get */
96
 
    0,                         /*tp_descr_set */
97
 
    0,                         /*tp_dictoffset */
98
 
    (initproc)qproperty_init,  /*tp_init */
99
 
    0,                         /*tp_alloc */
100
 
    PyType_GenericNew,         /*tp_new */
101
 
    qproperty_free,            /*tp_free */
102
 
    0,                         /*tp_is_gc */
103
 
    0,                         /*tp_bases */
104
 
    0,                         /*tp_mro */
105
 
    0,                         /*tp_cache */
106
 
    0,                         /*tp_subclasses */
107
 
    0,                         /*tp_weaklist */
108
 
    0,                         /*tp_del */
109
 
};
110
 
 
111
 
void init_qproperty(PyObject* module)
112
 
{
113
 
    if (PyType_Ready(&QProperty_Type) < 0)
114
 
        return;
115
 
 
116
 
    Py_INCREF(&QProperty_Type);
117
 
    PyModule_AddObject(module, QPROPERTY_CLASS_NAME, ((PyObject*)&QProperty_Type));
118
 
}
119
 
 
120
 
} // extern "C"
121
 
 
122
 
int qproperty_init(PyObject* self, PyObject* args, PyObject* kwds)
123
 
{
124
 
    PyObject* type = 0;
125
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
126
 
    data->designable = true;
127
 
    data->scriptable = true;
128
 
    data->stored = true;
129
 
 
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),
137
 
                                     /*s*/      &(data->doc),
138
 
                                     /*bbbbbb*/ &(data->designable), &(data->scriptable), &(data->stored), &(data->user), &(data->constant), &(data->final)))
139
 
        return 0;
140
 
 
141
 
    if (!data->fset && data->fget)
142
 
        data->constant = true;
143
 
 
144
 
    data->typeName = translate_type_name(type);
145
 
    return 1;
146
 
}
147
 
 
148
 
void qproperty_free(void *self)
149
 
{
150
 
    PyObject *pySelf = reinterpret_cast<PyObject*>(self);
151
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
152
 
 
153
 
    free(data->typeName);
154
 
    free(data->doc);
155
 
 
156
 
    pySelf->ob_type->tp_base->tp_free(self);
157
 
}
158
 
 
159
 
bool isQPropertyType(PyObject* pyObj)
160
 
{
161
 
    if (pyObj) {
162
 
        return pyObj->ob_type == &QProperty_Type;
163
 
    }
164
 
    return false;
165
 
}
166
 
 
167
 
int qproperty_set(PyObject* self, PyObject* source, PyObject* value)
168
 
{
169
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
170
 
    if (data->fset) {
171
 
        Shiboken::AutoDecRef args(PyTuple_New(2));
172
 
        PyTuple_SET_ITEM(args, 0, source);
173
 
        PyTuple_SET_ITEM(args, 1, value);
174
 
        Py_INCREF(source);
175
 
        Py_INCREF(value);
176
 
        Shiboken::AutoDecRef result(PyObject_CallObject(data->fset, args));
177
 
        return (result.isNull() ? -1 : 0);
178
 
    } else {
179
 
        PyErr_SetString(PyExc_AttributeError, "Attibute read only");
180
 
    }
181
 
    return -1;
182
 
}
183
 
 
184
 
PyObject* qproperty_get(PyObject* self, PyObject* source)
185
 
{
186
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
187
 
    if (data->fget) {
188
 
        Shiboken::AutoDecRef args(PyTuple_New(1));
189
 
        Py_INCREF(source);
190
 
        PyTuple_SET_ITEM(args, 0, source);
191
 
        return  PyObject_CallObject(data->fget, args);
192
 
    }
193
 
    return 0;
194
 
}
195
 
 
196
 
int qproperty_reset(PyObject* self, PyObject* source)
197
 
{
198
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
199
 
    if (data->freset) {
200
 
        Shiboken::AutoDecRef args(PyTuple_New(1));
201
 
        Py_INCREF(source);
202
 
        PyTuple_SET_ITEM(args, 0, source);
203
 
        Shiboken::AutoDecRef result(PyObject_CallObject(data->freset, args));
204
 
        return (result.isNull() ? -1 : 0);
205
 
    }
206
 
    return -1;
207
 
}
208
 
 
209
 
 
210
 
const char* qproperty_get_type(PyObject* self)
211
 
{
212
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
213
 
    return data->typeName;
214
 
}
215
 
 
216
 
PyObject* qproperty_get_object(PyObject* source, PyObject* name)
217
 
{
218
 
    PyObject* attr = PyObject_GenericGetAttr(source, name);
219
 
    if (attr && isQPropertyType(attr))
220
 
        return attr;
221
 
 
222
 
    if (!attr)
223
 
        PyErr_Clear(); //Clear possible error caused by PyObject_GenericGetAttr
224
 
    else
225
 
        Py_DECREF(attr);
226
 
    return 0;
227
 
}
228
 
 
229
 
char* translate_type_name(PyObject* type)
230
 
{
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);
236
 
        } else {
237
 
            //tp_name return the full name
238
 
            Shiboken::AutoDecRef otypeName(PyObject_GetAttrString(type, "__name__"));
239
 
            typeName = strdup(PyString_AS_STRING(otypeName.object()));
240
 
        }
241
 
        if (Shiboken::TypeResolver::getType(typeName) == Shiboken::TypeResolver::ObjectType) {
242
 
            typeName = reinterpret_cast<char*>(realloc(typeName, strlen(typeName) + 1));
243
 
            typeName = strcat(typeName, "*");
244
 
        }
245
 
        return typeName;
246
 
    } else if (PyString_Check(type)) {
247
 
        return strdup(PyString_AS_STRING(type));
248
 
    }
249
 
    return 0;
250
 
}
251
 
 
252
 
bool qproperty_is_readble(PyObject* self)
253
 
{
254
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
255
 
    return (data->fget != 0);
256
 
}
257
 
 
258
 
bool qproperty_is_writable(PyObject* self)
259
 
{
260
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
261
 
    return (data->fset != 0);
262
 
}
263
 
 
264
 
bool qproperty_has_reset(PyObject* self)
265
 
{
266
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
267
 
    return (data->freset != 0);
268
 
}
269
 
 
270
 
bool qproperty_is_designable(PyObject* self)
271
 
{
272
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
273
 
    return data->designable;
274
 
}
275
 
 
276
 
bool qproperty_is_scriptable(PyObject* self)
277
 
{
278
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
279
 
    return data->scriptable;
280
 
}
281
 
 
282
 
bool qproperty_is_stored(PyObject* self)
283
 
{
284
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
285
 
    return data->stored;
286
 
}
287
 
 
288
 
bool qproperty_is_user(PyObject* self)
289
 
{
290
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
291
 
    return data->user;
292
 
}
293
 
 
294
 
bool qproperty_is_constant(PyObject* self)
295
 
{
296
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
297
 
    return data->constant;
298
 
}
299
 
 
300
 
bool qproperty_is_final(PyObject* self)
301
 
{
302
 
    QPropertyData *data = reinterpret_cast<QPropertyData*>(self);
303
 
    return data->final;
304
 
}
305
 
 
306
 
} //namespace PySide