[QGIS Commit] r9077 - trunk/qgis/python/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Aug 20 02:55:54 EDT 2008


Author: jef
Date: 2008-08-20 02:55:54 -0400 (Wed, 20 Aug 2008)
New Revision: 9077

Modified:
   trunk/qgis/python/core/conversions.sip
Log:
add conversion for QMap<QString, QVariant::Type>

Modified: trunk/qgis/python/core/conversions.sip
===================================================================
--- trunk/qgis/python/core/conversions.sip	2008-08-20 06:55:00 UTC (rev 9076)
+++ trunk/qgis/python/core/conversions.sip	2008-08-20 06:55:54 UTC (rev 9077)
@@ -6,6 +6,7 @@
 - QSet<int>
 - QSet<TYPE>
 - QMap<int, QMap<int, TYPE> >
+- QMap<QString, QVariant::Type>
 - QMap<TYPE1, TYPE2*>
 - QMultiMap<double, TYPE2>
 */
@@ -172,9 +173,6 @@
 };
 
 
-
-
-
 %MappedType QSet<int>
 {
 %TypeHeaderCode
@@ -191,16 +189,16 @@
   // 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
@@ -213,9 +211,9 @@
   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);
@@ -224,7 +222,6 @@
 };
 
 
-
 template <TYPE>
 %MappedType QSet<TYPE>
 {
@@ -415,7 +412,106 @@
 
 };
 
+%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*>
 {
@@ -523,8 +619,6 @@
 %End
 };
 
-
-
 template<double, TYPE2>
 %MappedType QMultiMap<double, TYPE2>
 {



More information about the QGIS-commit mailing list