Skip to content

Commit

Permalink
add conversion for QMap<QString, QVariant::Type>
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9077 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Aug 20, 2008
1 parent ee48273 commit 9c45270
Showing 1 changed file with 106 additions and 12 deletions.
118 changes: 106 additions & 12 deletions python/core/conversions.sip
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ which are not wrapped by PyQt:
- QSet<int>
- QSet<TYPE>
- QMap<int, QMap<int, TYPE> >
- QMap<QString, QVariant::Type>
- QMap<TYPE1, TYPE2*>
- QMultiMap<double, TYPE2>
*/
Expand Down Expand Up @@ -172,9 +173,6 @@ template <TYPE>
};





%MappedType QSet<int>
{
%TypeHeaderCode
Expand All @@ -191,16 +189,16 @@ template <TYPE>
// Set the list elements.
QSet<int>::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
Expand All @@ -213,9 +211,9 @@ template <TYPE>
QSet<int> *qset = new QSet<int>;

for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
{
{
qset->insert(PyInt_AsLong(PyList_GET_ITEM(sipPy, i)));
}
}

*sipCppPtr = qset;
return sipGetState(sipTransferObj);
Expand All @@ -224,7 +222,6 @@ template <TYPE>
};



template <TYPE>
%MappedType QSet<TYPE>
{
Expand Down Expand Up @@ -415,6 +412,105 @@ template<TYPE>

};

%MappedType QMap<QString, QVariant::Type>
{
%TypeHeaderCode
#include <QMap>
%End

%ConvertFromTypeCode
// Create the dictionary.
PyObject *d = PyDict_New();

if (!d)
return NULL;

// Set the dictionary elements.
QMap<QString, QVariant::Type>::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<QString, QVariant::Type> *qm = new QMap<QString, QVariant::Type>;

while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
int state;

QString *t1 = reinterpret_cast<QString *>(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<TYPE1, TYPE2>
%MappedType QMap<TYPE1, TYPE2*>
Expand Down Expand Up @@ -523,8 +619,6 @@ template<TYPE1, TYPE2>
%End
};



template<double, TYPE2>
%MappedType QMultiMap<double, TYPE2>
{
Expand Down

0 comments on commit 9c45270

Please sign in to comment.