From 6d2c2adadcf8eb6ec6916b800884ec851572372a Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Wed, 20 Aug 2025 13:46:48 +0700 Subject: [PATCH] Draft conversions methods --- .../core/auto_generated/plot/qgsplot.sip.in | 4 ++ python/PyQt6/core/conversions.sip | 71 +++++++++++++++++- .../core/auto_generated/plot/qgsplot.sip.in | 4 ++ python/core/conversions.sip | 72 ++++++++++++++++++- src/core/plot/qgsplot.h | 2 +- 5 files changed, 150 insertions(+), 3 deletions(-) diff --git a/python/PyQt6/core/auto_generated/plot/qgsplot.sip.in b/python/PyQt6/core/auto_generated/plot/qgsplot.sip.in index 6dd5fe7a6f6..9f9bdbdab10 100644 --- a/python/PyQt6/core/auto_generated/plot/qgsplot.sip.in +++ b/python/PyQt6/core/auto_generated/plot/qgsplot.sip.in @@ -124,6 +124,10 @@ Encapsulates the data for an XY plot series. QgsXyPlotSeries(); ~QgsXyPlotSeries(); + QList> data() const; +%Docstring +Returns the series' list of XY pairs of double. +%End void append( const double &x, const double &y ); %Docstring diff --git a/python/PyQt6/core/conversions.sip b/python/PyQt6/core/conversions.sip index 055a3029a0f..f318369503a 100644 --- a/python/PyQt6/core/conversions.sip +++ b/python/PyQt6/core/conversions.sip @@ -5,6 +5,7 @@ which are not wrapped by PyQt: - QVector< QVector< QVector > > - QList< QList > - QList +- QList< std::pair< double, double > > - QSet - QSet - QSet @@ -16,7 +17,7 @@ which are not wrapped by PyQt: - QMultiMap - QMultiMap - QMap -- QList< QPair< QString, QList > > +- QMap< QPair< QString, QList > > - QVector - QMap - NULL QVariant which is missing in PyQt5 with sip.enableautoconversion @@ -3136,6 +3137,74 @@ template %End }; + +%MappedType QList< std::pair< double, double > > +{ +%TypeHeaderCode +#include +#include +%End + +%ConvertFromTypeCode +//convert list of pairs to a python array of tuple + PyObject *l; + + if ((l = PyList_New(sipCpp->size())) == NULL) + return NULL; + + // Set the list elements. + QList< std::pair < double, double > >::iterator it = sipCpp->begin(); + for (int i = 0; it != sipCpp->end(); ++it, ++i) + { + PyObject *obj; + if ( ( obj = PyTuple_New( 2 ) ) == NULL ) + { + Py_DECREF( l ); + return NULL; + } + + PyObject *v1 = PyFloat_FromDouble( ( *it ).first ); + PyTuple_SetItem( obj, 0, v1 ); + PyObject *v2 = PyFloat_FromDouble( ( *it ).second ); + PyTuple_SetItem( obj, 1, v2 ); + + PyList_SET_ITEM(l, i, obj); + } + + return l; +%End + +%ConvertToTypeCode + // Check the type if that is all that is required. + if (sipIsErr == NULL) + { + if (!PyList_Check(sipPy)) + return 0; + + return 1; + } + + + QList< std::pair < double, double > > *ql = new QList< std::pair< double, double > >; + for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i) + { + int state; + + PyObject *sipFirst = PyTuple_GetItem( PyList_GET_ITEM(sipPy, i), 0 ); + double v1 = PyFloat_AsDouble(sipFirst); + + PyObject *sipSecond = PyTuple_GetItem( PyList_GET_ITEM(sipPy, i), 1 ); + double v2 = PyFloat_AsDouble(sipSecond); + + ql->append( std::make_pair( v1, v2 ) ); + } + + *sipCppPtr = ql; + return sipGetState(sipTransferObj); +%End +}; + + %If (VECTOR_MAPPED_TYPE) template %MappedType QVector< TYPE* > diff --git a/python/core/auto_generated/plot/qgsplot.sip.in b/python/core/auto_generated/plot/qgsplot.sip.in index 6dd5fe7a6f6..9f9bdbdab10 100644 --- a/python/core/auto_generated/plot/qgsplot.sip.in +++ b/python/core/auto_generated/plot/qgsplot.sip.in @@ -124,6 +124,10 @@ Encapsulates the data for an XY plot series. QgsXyPlotSeries(); ~QgsXyPlotSeries(); + QList> data() const; +%Docstring +Returns the series' list of XY pairs of double. +%End void append( const double &x, const double &y ); %Docstring diff --git a/python/core/conversions.sip b/python/core/conversions.sip index 81872ec53a2..efa44753f68 100644 --- a/python/core/conversions.sip +++ b/python/core/conversions.sip @@ -5,6 +5,7 @@ which are not wrapped by PyQt: - QVector< QVector< QVector > > - QList< QList > - QList +- QList< std::pair< double, double > > - QSet - QSet - QSet @@ -16,7 +17,7 @@ which are not wrapped by PyQt: - QMultiMap - QMultiMap - QMap -- QList< QPair< QString, QList > > +- QMap< QPair< QString, QList > > - QVector - QMap - NULL QVariant which is missing in PyQt5 with sip.enableautoconversion @@ -2634,6 +2635,7 @@ template %End }; + %MappedType QMap { %TypeHeaderCode @@ -3186,6 +3188,74 @@ template %End }; + +%MappedType QList< std::pair< double, double > > +{ +%TypeHeaderCode +#include +#include +%End + +%ConvertFromTypeCode +//convert list of pairs to a python array of tuple + PyObject *l; + + if ((l = PyList_New(sipCpp->size())) == NULL) + return NULL; + + // Set the list elements. + QList< std::pair < double, double > >::iterator it = sipCpp->begin(); + for (int i = 0; it != sipCpp->end(); ++it, ++i) + { + PyObject *obj; + if ( ( obj = PyTuple_New( 2 ) ) == NULL ) + { + Py_DECREF( l ); + return NULL; + } + + PyObject *v1 = PyFloat_FromDouble( ( *it ).first ); + PyTuple_SetItem( obj, 0, v1 ); + PyObject *v2 = PyFloat_FromDouble( ( *it ).second ); + PyTuple_SetItem( obj, 1, v2 ); + + PyList_SET_ITEM(l, i, obj); + } + + return l; +%End + +%ConvertToTypeCode + // Check the type if that is all that is required. + if (sipIsErr == NULL) + { + if (!PyList_Check(sipPy)) + return 0; + + return 1; + } + + + QList< std::pair < double, double > > *ql = new QList< std::pair< double, double > >; + for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i) + { + int state; + + PyObject *sipFirst = PyTuple_GetItem( PyList_GET_ITEM(sipPy, i), 0 ); + double v1 = PyFloat_AsDouble(sipFirst); + + PyObject *sipSecond = PyTuple_GetItem( PyList_GET_ITEM(sipPy, i), 1 ); + double v2 = PyFloat_AsDouble(sipSecond); + + ql->append( std::make_pair( v1, v2 ) ); + } + + *sipCppPtr = ql; + return sipGetState(sipTransferObj); +%End +}; + + %If (VECTOR_MAPPED_TYPE) template %MappedType QVector< TYPE* > diff --git a/src/core/plot/qgsplot.h b/src/core/plot/qgsplot.h index 86c851375f0..110cad30718 100644 --- a/src/core/plot/qgsplot.h +++ b/src/core/plot/qgsplot.h @@ -137,7 +137,7 @@ class CORE_EXPORT QgsXyPlotSeries : public QgsAbstractPlotSeries /** * Returns the series' list of XY pairs of double. */ - QList> data() const SIP_SKIP; + QList> data() const; /** * Appends a pair of X/Y double values to the series.