diff --git a/python/core/conversions.sip b/python/core/conversions.sip index d1e049e66d8..4adf07f68a9 100644 --- a/python/core/conversions.sip +++ b/python/core/conversions.sip @@ -6,6 +6,7 @@ which are not wrapped by PyQt: - QSet - QSet - QMap > +- QMap - QMap - QMultiMap */ @@ -172,9 +173,6 @@ template }; - - - %MappedType QSet { %TypeHeaderCode @@ -191,16 +189,16 @@ template // Set the list elements. QSet::iterator it = sipCpp->begin(); for (int i = 0; it != sipCpp->end(); ++it, ++i) -{ + { PyObject *tobj; if ((tobj = PyInt_FromLong(*it)) == NULL) -{ + { Py_DECREF(l); return NULL; -} + } PyList_SET_ITEM(l, i, tobj); -} + } return l; %End @@ -213,9 +211,9 @@ template QSet *qset = new QSet; for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i) -{ + { qset->insert(PyInt_AsLong(PyList_GET_ITEM(sipPy, i))); -} + } *sipCppPtr = qset; return sipGetState(sipTransferObj); @@ -224,7 +222,6 @@ template }; - template %MappedType QSet { @@ -415,6 +412,105 @@ template }; +%MappedType QMap +{ +%TypeHeaderCode +#include +%End + +%ConvertFromTypeCode + // Create the dictionary. + PyObject *d = PyDict_New(); + + if (!d) + return NULL; + + // Set the dictionary elements. + QMap::const_iterator i = sipCpp->constBegin(); + + while (i != sipCpp->constEnd()) + { + QString *t1 = new QString(i.key()); + + PyObject *t1obj = sipConvertFromNewInstance(t1, sipClass_QString, sipTransferObj); + PyObject *t2obj = PyInt_FromLong( (long) i.value() ); + + if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0) + { + Py_DECREF(d); + + if (t1obj) { + Py_DECREF(t1obj); + } else { + delete t1; + } + + if (t2obj) { + Py_DECREF(t2obj); + } + + return NULL; + } + + Py_DECREF(t1obj); + Py_DECREF(t2obj); + + ++i; + } + + return d; +%End + +%ConvertToTypeCode + PyObject *t1obj, *t2obj; +#if PY_VERSION_HEX >= 0x02050000 + Py_ssize_t i = 0; +#else + int i = 0; +#endif + + + // Check the type if that is all that is required. + if (sipIsErr == NULL) + { + if (!PyDict_Check(sipPy)) + return 0; + + while (PyDict_Next(sipPy, &i, &t1obj, &t2obj)) + { + if (!sipCanConvertToInstance(t1obj, sipClass_QString, SIP_NOT_NONE)) + return 0; + } + + return 1; + } + + QMap *qm = new QMap; + + while (PyDict_Next(sipPy, &i, &t1obj, &t2obj)) + { + int state; + + QString *t1 = reinterpret_cast(sipConvertToInstance(t1obj, sipClass_QString, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr)); + QVariant::Type t2 = (QVariant::Type) PyInt_AsLong(t1obj); + + if (*sipIsErr) + { + sipReleaseInstance(t1, sipClass_QString, state); + delete qm; + return 0; + } + + qm->insert(*t1, t2); + + sipReleaseInstance(t1, sipClass_QString, state); + } + + *sipCppPtr = qm; + + return sipGetState(sipTransferObj); +%End +}; template %MappedType QMap @@ -523,8 +619,6 @@ template %End }; - - template %MappedType QMultiMap {