1
// This is the implementation of the QPyDeclarativeListProperty class.
3
// Copyright (c) 2010 Riverbank Computing Limited <info@riverbankcomputing.com>
5
// This file is part of PyQt.
7
// This file may be used under the terms of the GNU General Public
8
// License versions 2.0 or 3.0 as published by the Free Software
9
// Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
10
// included in the packaging of this file. Alternatively you may (at
11
// your option) use any later version of the GNU General Public
12
// License if such license has been publicly approved by Riverbank
13
// Computing Limited (or its successors, if any) and the KDE Free Qt
14
// Foundation. In addition, as a special exception, Riverbank gives you
15
// certain additional rights. These rights are described in the Riverbank
16
// GPL Exception version 1.1, which can be found in the file
17
// GPL_EXCEPTION.txt in this package.
19
// Please review the following information to ensure GNU General
20
// Public Licensing requirements will be met:
21
// http://trolltech.com/products/qt/licenses/licensing/opensource/. If
22
// you are unsure which license is appropriate for your use, please
23
// review the following information:
24
// http://trolltech.com/products/qt/licenses/licensing/licensingoverview
25
// or contact the sales department at sales@riverbankcomputing.com.
27
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32
#include <QtDeclarative>
34
#include "sipAPIQtDeclarative.h"
36
#include "qpydeclarativelistproperty.h"
37
#include "qpydeclarative_listwrapper.h"
40
// Forward declarations.
42
static PyObject *QPyDeclarativeListProperty_call(PyObject *, PyObject *args,
46
static void list_append(QDeclarativeListProperty<QObject> *p, QObject *el);
47
static QObject *list_at(QDeclarativeListProperty<QObject> *p, int idx);
48
static void list_clear(QDeclarativeListProperty<QObject> *p);
49
static int list_count(QDeclarativeListProperty<QObject> *p);
52
// The type's doc-string.
53
PyDoc_STRVAR(QPyDeclarativeListProperty_doc,
54
"QPyDeclarativeListProperty(QObject, list-of-QObject)");
57
// This implements the QPyDeclarativeListProperty Python type. It is a
58
// sub-type of the standard string type that is callable.
59
PyTypeObject qpydeclarative_QPyDeclarativeListProperty_Type = {
60
PyVarObject_HEAD_INIT(NULL, 0)
61
SIP_TPNAME_CAST("PyQt4.QtDeclarative.QPyDeclarativeListProperty"),
74
QPyDeclarativeListProperty_call,
79
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|
80
#if PY_VERSION_HEX >= 0x03000000
81
Py_TPFLAGS_UNICODE_SUBCLASS,
82
#elif PY_VERSION_HEX >= 0x02060000
83
Py_TPFLAGS_STRING_SUBCLASS,
87
QPyDeclarativeListProperty_doc,
113
#if PY_VERSION_HEX >= 0x02060000
119
// This is a factory for the wrapped QDeclarativeListProperty<PyDelegate>.
120
static PyObject *QPyDeclarativeListProperty_call(PyObject *, PyObject *args,
123
PyTypeObject *qobject_type = sipTypeAsPyTypeObject(sipType_QObject);
124
PyObject *qobj_obj, *list_obj;
126
if (!PyArg_ParseTuple(args,
127
#if PY_VERSION_HEX >= 0x02050000
128
"O!O!:QPyDeclarativeListProperty",
130
const_cast<char *>("O!O!:QPyDeclarativeListProperty"),
132
qobject_type, &qobj_obj, &PyList_Type, &list_obj, &PyType_Type))
135
// Get the C++ QObject.
137
QObject *qobj = reinterpret_cast<QObject *>(sipConvertToType(qobj_obj, sipType_QObject, 0, SIP_NOT_NONE|SIP_NO_CONVERTORS, 0, &iserr));
142
// Get a list wrapper with the C++ QObject as its parent.
143
ListWrapper *list_wrapper = ListWrapper::wrapper(list_obj, qobj);
148
// Create the C++ QDeclarativeListProperty<QObject> with the list as the
150
QDeclarativeListProperty<QObject> *prop = new QDeclarativeListProperty<QObject>(qobj, &list_wrapper->qobject_list, list_append, list_count, list_at, list_clear);
152
// Convert it to a Python object.
153
static const sipTypeDef *mapped_type = 0;
156
mapped_type = sipFindType("QDeclarativeListProperty<QObject>");
158
Q_ASSERT(mapped_type);
160
// Make sure ownership is with Python.
161
PyObject *prop_obj = sipConvertFromNewType(prop, mapped_type, qobj_obj);
173
// Append to the list.
174
static void list_append(QDeclarativeListProperty<QObject> *p, QObject *el)
176
ListWrapper::append(p->object,
177
reinterpret_cast<QList<QObject *> *>(p->data), el);
181
// Get the length of the list.
182
static int list_count(QDeclarativeListProperty<QObject> *p)
184
return reinterpret_cast<QList<QObject *> *>(p->data)->count();
188
// Get an item from the list.
189
static QObject *list_at(QDeclarativeListProperty<QObject> *p, int idx)
191
return reinterpret_cast<QList<QObject *> *>(p->data)->at(idx);
196
static void list_clear(QDeclarativeListProperty<QObject> *p)
198
ListWrapper::clear(p->object,
199
reinterpret_cast<QList<QObject *> *>(p->data));