1/*
2 * Various conversions that are not made by PyQt5 default.
3 */
4
5
6/**
7 * Convert QSet<Poppler::Document::RenderBackend>
8 * from any Python sequence and to a Python list.
9 */
10
11%MappedType QSet<Poppler::Document::RenderBackend>
12{
13%TypeHeaderCode
14#include <qset.h>
15%End
16
17%ConvertFromTypeCode
18    // Create the list.
19    PyObject *l;
20
21    if ((l = PyList_New(sipCpp->size())) == NULL)
22        return NULL;
23
24    // Set the list elements.
25    QSet<Poppler::Document::RenderBackend> set = *sipCpp;
26    int i = 0;
27    foreach (Poppler::Document::RenderBackend value, set)
28    {
29        PyObject *obj = PyLong_FromLong ((long) value);
30        if (obj == NULL || PyList_SetItem(l, i, obj) < 0)
31        {
32            Py_DECREF(l);
33
34            if (obj)
35                Py_DECREF(obj);
36
37            return NULL;
38        }
39
40        Py_DECREF(obj);
41        i++;
42    }
43
44    return l;
45%End
46
47%ConvertToTypeCode
48    // Check the type if that is all that is required.
49    if (sipIsErr == NULL)
50    {
51        if (!PySequence_Check(sipPy))
52            return 0;
53        return 1;
54    }
55
56    QSet<Poppler::Document::RenderBackend> *qs = new QSet<Poppler::Document::RenderBackend>;
57
58    for (int i = 0; i < PySequence_Size(sipPy); ++i)
59    {
60        Poppler::Document::RenderBackend t = (Poppler::Document::RenderBackend)PyLong_AsLong(PySequence_ITEM (sipPy, i));
61        *qs << t;
62    }
63
64    *sipCppPtr = qs;
65
66    return sipGetState(sipTransferObj);
67%End
68};
69
70
71/**
72 * Convert QLinkedList<TYPE> from any sequence and to a Python list.
73 */
74
75template<TYPE>
76%MappedType QLinkedList<TYPE>
77{
78%TypeHeaderCode
79#include <qlinkedlist.h>
80%End
81
82%ConvertFromTypeCode
83    // Create the list.
84    PyObject *l;
85
86    if ((l = PyList_New(sipCpp->size())) == NULL)
87        return NULL;
88
89    // Set the list elements.
90    TYPE item;
91    QLinkedList<TYPE>::iterator i;
92    int index = 0;
93    for (i = sipCpp->begin(); i != sipCpp->end(); ++i)
94    {
95        TYPE *t = new TYPE(*i);
96        PyObject *tobj;
97
98        if ((tobj = sipConvertFromNewType(t, sipType_TYPE, sipTransferObj)) == NULL)
99        {
100            Py_DECREF(l);
101            delete t;
102
103            return NULL;
104        }
105
106        PyList_SET_ITEM(l, index, tobj);
107        ++index;
108    }
109
110    return l;
111%End
112
113%ConvertToTypeCode
114    SIP_SSIZE_T len;
115
116    // Check the type if that is all that is required.
117    if (sipIsErr == NULL)
118    {
119        if (!PySequence_Check(sipPy) || (len = PySequence_Size(sipPy)) < 0)
120            return 0;
121
122        for (SIP_SSIZE_T i = 0; i < len; ++i)
123        {
124            PyObject *itm = PySequence_ITEM(sipPy, i);
125            bool ok = (itm && sipCanConvertToType(itm, sipType_TYPE, SIP_NOT_NONE));
126
127            Py_XDECREF(itm);
128
129            if (!ok)
130                return 0;
131        }
132
133        return 1;
134    }
135
136    QLinkedList<TYPE> *qll = new QLinkedList<TYPE>;
137    len = PySequence_Size(sipPy);
138
139    for (SIP_SSIZE_T i = 0; i < len; ++i)
140    {
141        PyObject *itm = PySequence_ITEM(sipPy, i);
142        int state;
143        TYPE *t = reinterpret_cast<TYPE *>(sipConvertToType(itm, sipType_TYPE, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
144
145        Py_DECREF(itm);
146
147        if (*sipIsErr)
148        {
149            sipReleaseType(t, sipType_TYPE, state);
150
151            delete qll;
152            return 0;
153        }
154
155        qll->append(*t);
156
157        sipReleaseType(t, sipType_TYPE, state);
158    }
159
160    *sipCppPtr = qll;
161
162    return sipGetState(sipTransferObj);
163%End
164};
165
166/**
167 * Convert QList<QLinkedList<TYPE>>
168 */
169
170template <TYPE>
171%MappedType QList< QLinkedList<TYPE> >
172{
173%TypeHeaderCode
174#include <qlist.h>
175#include <qlinkedlist.h>
176%End
177
178%ConvertFromTypeCode
179  // Create the list.
180  PyObject *l;
181
182  if ((l = PyList_New(sipCpp->size())) == NULL)
183    return NULL;
184
185  const sipTypeDef* qlinkedlist_type = sipFindType("QLinkedList<TYPE>");
186
187  // Set the list elements.
188  for (int i = 0; i < sipCpp->size(); ++i)
189  {
190    QLinkedList<TYPE>* t = new QLinkedList<TYPE>(sipCpp->at(i));
191    PyObject *tobj;
192
193    if ((tobj = sipConvertFromType(t, qlinkedlist_type, sipTransferObj)) == NULL)
194    {
195      Py_DECREF(l);
196      delete t;
197      return NULL;
198    }
199    PyList_SET_ITEM(l, i, tobj);
200  }
201
202  return l;
203%End
204
205%ConvertToTypeCode
206  const sipTypeDef* qlinkedlist_type = sipFindType("QLinkedList<TYPE>");
207
208  // Check the type if that is all that is required.
209  if (sipIsErr == NULL)
210  {
211    if (!PySequence_Check(sipPy))
212      return 0;
213
214    for (int i = 0; i < PySequence_Size(sipPy); ++i)
215      if (!sipCanConvertToType(PySequence_ITEM(sipPy, i), qlinkedlist_type, SIP_NOT_NONE))
216        return 0;
217
218    return 1;
219  }
220
221
222  QList< QLinkedList<TYPE> > *ql = new QList< QLinkedList<TYPE> >;
223
224  for (int i = 0; i < PySequence_Size(sipPy); ++i)
225  {
226    int state;
227    QLinkedList<TYPE> * t = reinterpret_cast< QLinkedList<TYPE> * >(sipConvertToType(PySequence_ITEM(sipPy, i), qlinkedlist_type, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
228
229    if (*sipIsErr)
230    {
231      sipReleaseType(t, qlinkedlist_type, state);
232      delete ql;
233      return 0;
234    }
235    ql->append(*t);
236    sipReleaseType(t, qlinkedlist_type, state);
237  }
238
239  *sipCppPtr = ql;
240  return sipGetState(sipTransferObj);
241%End
242
243};
244
245/**
246 * Convert time_t to and from long integer
247 */
248
249%MappedType time_t
250{
251%TypeHeaderCode
252#include <time.h>
253%End
254
255%ConvertFromTypeCode
256    PyObject* pTime;
257    pTime = PyLong_FromLong(*sipCpp);
258    return pTime;
259%End
260
261%ConvertToTypeCode
262    if (sipIsErr == NULL)
263    {
264        return PyLong_Check(sipPy);
265    }
266
267    if (sipPy == Py_None)
268    {
269        *sipCppPtr = new time_t();
270        return 1;
271    }
272
273    if (PyLong_Check(sipPy))
274    {
275        *sipCppPtr = new time_t(PyLong_AsLong(sipPy));
276        return 1;
277    }
278    return 0;
279%End
280};
281
282/**
283 * Convert to and from QList<qint64>
284 */
285
286%MappedType QList<qint64>
287{
288%TypeHeaderCode
289#include <QList>
290%End
291
292%ConvertFromTypeCode
293  // Create the list.
294  PyObject *l;
295
296  if ((l = PyList_New(sipCpp->size())) == NULL)
297    return NULL;
298
299  // Set the list elements.
300  QList<qint64>::iterator it = sipCpp->begin();
301  for (int i = 0; it != sipCpp->end(); ++it, ++i)
302  {
303    PyObject *tobj;
304
305    if ((tobj = PyLong_FromLongLong(*it)) == NULL)
306    {
307      Py_DECREF(l);
308      return NULL;
309    }
310    PyList_SET_ITEM(l, i, tobj);
311  }
312
313  return l;
314%End
315
316%ConvertToTypeCode
317  // Check the type if that is all that is required.
318  if (sipIsErr == NULL)
319    return PyList_Check(sipPy);
320
321  QList<qint64> *qlist = new QList<qint64>;
322
323  for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
324  {
325    *qlist << PyLong_AsLongLong(PyList_GET_ITEM(sipPy, i));
326  }
327
328  *sipCppPtr = qlist;
329  return sipGetState(sipTransferObj);
330%End
331};
332
333
334
335/* kate: indent-width 4; space-indent on; hl c++; indent-mode cstyle; */
336