Blame 40e71ad88173d02648bceb2438bc0567e60dacd5.patch

cc5bb24
From 40e71ad88173d02648bceb2438bc0567e60dacd5 Mon Sep 17 00:00:00 2001
cc5bb24
From: Ben Greiner <code@bnavigator.de>
cc5bb24
Date: Tue, 23 Feb 2021 17:59:33 +0100
cc5bb24
Subject: [PATCH] map type QVector< QPair<TYPE, TYPE> > for
cc5bb24
 FormFieldChoice::choicesWithExportValues()
cc5bb24
cc5bb24
---
cc5bb24
 types.sip | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc5bb24
 1 file changed, 93 insertions(+)
cc5bb24
cc5bb24
diff --git a/types.sip b/types.sip
cc5bb24
index 239b8c9..81cb283 100644
cc5bb24
--- a/types.sip
cc5bb24
+++ b/types.sip
cc5bb24
@@ -331,5 +331,98 @@ template <TYPE>
cc5bb24
 };
cc5bb24
 
cc5bb24
 
cc5bb24
+/**
cc5bb24
+ * Convert QVector< QPair<TYPE, TYPE> >
cc5bb24
+ * from and to a Python list of a 2-item tuple
cc5bb24
+ */
cc5bb24
+
cc5bb24
+template<TYPE>
cc5bb24
+%MappedType QVector< QPair<TYPE, TYPE> >
cc5bb24
+{
cc5bb24
+%TypeHeaderCode
cc5bb24
+#include <qvector.h>
cc5bb24
+#include <qpair.h>
cc5bb24
+%End
cc5bb24
+
cc5bb24
+%ConvertFromTypeCode
cc5bb24
+  // Create the list.
cc5bb24
+  PyObject *l;
cc5bb24
+
cc5bb24
+  if ((l = PyList_New(sipCpp->size())) == NULL)
cc5bb24
+      return NULL;
cc5bb24
+
cc5bb24
+  // Set the list elements.
cc5bb24
+  for (int i = 0; i < sipCpp->size(); ++i)
cc5bb24
+  {
cc5bb24
+    QPair<TYPE, TYPE>* p = new QPair<TYPE, TYPE>(sipCpp->at(i));
cc5bb24
+    PyObject *ptuple = PyTuple_New(2);
cc5bb24
+    PyObject *pfirst;
cc5bb24
+    PyObject *psecond;
cc5bb24
+
cc5bb24
+    TYPE *sfirst = new TYPE(p->first);
cc5bb24
+    if ((pfirst = sipConvertFromType(sfirst, sipType_TYPE, sipTransferObj)) == NULL)
cc5bb24
+    {
cc5bb24
+      Py_DECREF(l);
cc5bb24
+      Py_DECREF(ptuple);
cc5bb24
+      return NULL;
cc5bb24
+    }
cc5bb24
+    PyTuple_SET_ITEM(ptuple, 0, pfirst);
cc5bb24
+
cc5bb24
+    TYPE *ssecond = new TYPE(p->second);
cc5bb24
+    if ((psecond = sipConvertFromType(ssecond, sipType_TYPE, sipTransferObj)) == NULL)
cc5bb24
+    {
cc5bb24
+      Py_DECREF(l);
cc5bb24
+      Py_DECREF(ptuple);
cc5bb24
+      Py_DECREF(pfirst);
cc5bb24
+      return NULL;
cc5bb24
+    }
cc5bb24
+    PyTuple_SET_ITEM(ptuple, 1, psecond);
cc5bb24
+
cc5bb24
+    PyList_SET_ITEM(l, i, ptuple);
cc5bb24
+  }
cc5bb24
+
cc5bb24
+  return l;
cc5bb24
+%End
cc5bb24
+
cc5bb24
+%ConvertToTypeCode
cc5bb24
+  const sipTypeDef* qpair_type = sipFindType("QPair<TYPE, TYPE>");
cc5bb24
+
cc5bb24
+  // Check the type if that is all that is required.
cc5bb24
+  if (sipIsErr == NULL)
cc5bb24
+  {
cc5bb24
+    if (!PySequence_Check(sipPy))
cc5bb24
+      return 0;
cc5bb24
+
cc5bb24
+    for (int i = 0; i < PySequence_Size(sipPy); ++i)
cc5bb24
+      if (!sipCanConvertToType(PySequence_ITEM(sipPy, i), qpair_type, SIP_NOT_NONE))
cc5bb24
+        return 0;
cc5bb24
+
cc5bb24
+    return 1;
cc5bb24
+  }
cc5bb24
+
cc5bb24
+
cc5bb24
+  QVector< QPair<TYPE, TYPE> > *qv = new QVector< QPair<TYPE, TYPE> >;
cc5bb24
+
cc5bb24
+  for (int i = 0; i < PySequence_Size(sipPy); ++i)
cc5bb24
+  {
cc5bb24
+    int state;
cc5bb24
+    QPair<TYPE, TYPE> * p = reinterpret_cast< QPair<TYPE, TYPE> * >(sipConvertToType(PySequence_ITEM(sipPy, i), qpair_type, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
cc5bb24
+
cc5bb24
+    if (*sipIsErr)
cc5bb24
+    {
cc5bb24
+      sipReleaseType(p, qpair_type, state);
cc5bb24
+      delete qv;
cc5bb24
+      return 0;
cc5bb24
+    }
cc5bb24
+    qv->append(*p);
cc5bb24
+    sipReleaseType(p, qpair_type, state);
cc5bb24
+  }
cc5bb24
+
cc5bb24
+  *sipCppPtr = qv;
cc5bb24
+  return sipGetState(sipTransferObj);
cc5bb24
+%End
cc5bb24
+
cc5bb24
+};
cc5bb24
+
cc5bb24
 
cc5bb24
 /* kate: indent-width 4; space-indent on; hl c++; indent-mode cstyle; */