Skip to content

Commit 48dcaff

Browse files
author
jef
committed
add conversion for QMap<QString, QVariant::Type>
git-svn-id: http://svn.osgeo.org/qgis/trunk@9077 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 40446fb commit 48dcaff

File tree

1 file changed

+106
-12
lines changed

1 file changed

+106
-12
lines changed

python/core/conversions.sip

+106-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ which are not wrapped by PyQt:
66
- QSet<int>
77
- QSet<TYPE>
88
- QMap<int, QMap<int, TYPE> >
9+
- QMap<QString, QVariant::Type>
910
- QMap<TYPE1, TYPE2*>
1011
- QMultiMap<double, TYPE2>
1112
*/
@@ -172,9 +173,6 @@ template <TYPE>
172173
};
173174

174175

175-
176-
177-
178176
%MappedType QSet<int>
179177
{
180178
%TypeHeaderCode
@@ -191,16 +189,16 @@ template <TYPE>
191189
// Set the list elements.
192190
QSet<int>::iterator it = sipCpp->begin();
193191
for (int i = 0; it != sipCpp->end(); ++it, ++i)
194-
{
192+
{
195193
PyObject *tobj;
196194

197195
if ((tobj = PyInt_FromLong(*it)) == NULL)
198-
{
196+
{
199197
Py_DECREF(l);
200198
return NULL;
201-
}
199+
}
202200
PyList_SET_ITEM(l, i, tobj);
203-
}
201+
}
204202

205203
return l;
206204
%End
@@ -213,9 +211,9 @@ template <TYPE>
213211
QSet<int> *qset = new QSet<int>;
214212

215213
for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
216-
{
214+
{
217215
qset->insert(PyInt_AsLong(PyList_GET_ITEM(sipPy, i)));
218-
}
216+
}
219217

220218
*sipCppPtr = qset;
221219
return sipGetState(sipTransferObj);
@@ -224,7 +222,6 @@ template <TYPE>
224222
};
225223

226224

227-
228225
template <TYPE>
229226
%MappedType QSet<TYPE>
230227
{
@@ -415,6 +412,105 @@ template<TYPE>
415412

416413
};
417414

415+
%MappedType QMap<QString, QVariant::Type>
416+
{
417+
%TypeHeaderCode
418+
#include <QMap>
419+
%End
420+
421+
%ConvertFromTypeCode
422+
// Create the dictionary.
423+
PyObject *d = PyDict_New();
424+
425+
if (!d)
426+
return NULL;
427+
428+
// Set the dictionary elements.
429+
QMap<QString, QVariant::Type>::const_iterator i = sipCpp->constBegin();
430+
431+
while (i != sipCpp->constEnd())
432+
{
433+
QString *t1 = new QString(i.key());
434+
435+
PyObject *t1obj = sipConvertFromNewInstance(t1, sipClass_QString, sipTransferObj);
436+
PyObject *t2obj = PyInt_FromLong( (long) i.value() );
437+
438+
if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
439+
{
440+
Py_DECREF(d);
441+
442+
if (t1obj) {
443+
Py_DECREF(t1obj);
444+
} else {
445+
delete t1;
446+
}
447+
448+
if (t2obj) {
449+
Py_DECREF(t2obj);
450+
}
451+
452+
return NULL;
453+
}
454+
455+
Py_DECREF(t1obj);
456+
Py_DECREF(t2obj);
457+
458+
++i;
459+
}
460+
461+
return d;
462+
%End
463+
464+
%ConvertToTypeCode
465+
PyObject *t1obj, *t2obj;
466+
#if PY_VERSION_HEX >= 0x02050000
467+
Py_ssize_t i = 0;
468+
#else
469+
int i = 0;
470+
#endif
471+
472+
473+
// Check the type if that is all that is required.
474+
if (sipIsErr == NULL)
475+
{
476+
if (!PyDict_Check(sipPy))
477+
return 0;
478+
479+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
480+
{
481+
if (!sipCanConvertToInstance(t1obj, sipClass_QString, SIP_NOT_NONE))
482+
return 0;
483+
}
484+
485+
return 1;
486+
}
487+
488+
QMap<QString, QVariant::Type> *qm = new QMap<QString, QVariant::Type>;
489+
490+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
491+
{
492+
int state;
493+
494+
QString *t1 = reinterpret_cast<QString *>(sipConvertToInstance(t1obj, sipClass_QString, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
495+
QVariant::Type t2 = (QVariant::Type) PyInt_AsLong(t1obj);
496+
497+
if (*sipIsErr)
498+
{
499+
sipReleaseInstance(t1, sipClass_QString, state);
500+
delete qm;
501+
return 0;
502+
}
503+
504+
qm->insert(*t1, t2);
505+
506+
sipReleaseInstance(t1, sipClass_QString, state);
507+
}
508+
509+
*sipCppPtr = qm;
510+
511+
return sipGetState(sipTransferObj);
512+
%End
513+
};
418514

419515
template<TYPE1, TYPE2>
420516
%MappedType QMap<TYPE1, TYPE2*>
@@ -523,8 +619,6 @@ template<TYPE1, TYPE2>
523619
%End
524620
};
525621

526-
527-
528622
template<double, TYPE2>
529623
%MappedType QMultiMap<double, TYPE2>
530624
{

0 commit comments

Comments
 (0)