From bd75aeef4980eac5a9c267c6e6f25740cf0856cd Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Fri, 17 Mar 2017 13:50:16 +0100 Subject: [PATCH] Put some sip code into qgsfeature.h Just for reference --- python/core/qgsfeature.sip | 12 +++++- src/core/qgsfeature.h | 87 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/python/core/qgsfeature.sip b/python/core/qgsfeature.sip index 8a1724c5c3b..0ea34b01486 100644 --- a/python/core/qgsfeature.sip +++ b/python/core/qgsfeature.sip @@ -7,7 +7,7 @@ typedef QVector QgsAttributes; %MappedType QgsAttributes { %TypeHeaderCode -#include +#include // NO_SIPIFY %End %ConvertFromTypeCode @@ -113,6 +113,16 @@ class QgsFeature #define sipType_QVariant ((sipWrapperType *) sipTypeAsPyTypeObject (sipType_QVariant)) #endif %End +%Docstring +/** \ingroup core + The feature class encapsulates a single feature including its id, + geometry and a list of field/values attributes. + \note QgsFeature objects are implicitly shared. + @author Gary E.Sherman +/ + +%End + public: diff --git a/src/core/qgsfeature.h b/src/core/qgsfeature.h index 5522c1656e5..b9f5724c014 100644 --- a/src/core/qgsfeature.h +++ b/src/core/qgsfeature.h @@ -44,6 +44,93 @@ typedef qint64 QgsFeatureId; // key = field index, value = field value typedef QMap QgsAttributeMap; +#ifdef SIP_RUN +typedef QVector QgsAttributes; + +// QgsAttributes is implemented as a Python list of Python objects. +% MappedType QgsAttributes +{ + % TypeHeaderCode +#include // NO_SIPIFY + % End + + % ConvertFromTypeCode + // Create the list. + PyObject *l; + + if ( ( l = PyList_New( sipCpp->size() ) ) == NULL ) + return NULL; + + // Set the list elements. + for ( int i = 0; i < sipCpp->size(); ++i ) + { + QVariant *v = new QVariant( sipCpp->at( i ) ); + PyObject *tobj; + + if ( ( tobj = sipConvertFromNewType( v, sipType_QVariant, Py_None ) ) == NULL ) + { + Py_DECREF( l ); + delete v; + + return NULL; + } + + PyList_SET_ITEM( l, i, tobj ); + } + + return l; + % End + + % ConvertToTypeCode + // Check the type if that is all that is required. + if ( sipIsErr == NULL ) + { + if ( !PyList_Check( sipPy ) ) + return 0; + + for ( SIP_SSIZE_T i = 0; i < PyList_GET_SIZE( sipPy ); ++i ) + if ( !sipCanConvertToType( PyList_GET_ITEM( sipPy, i ), sipType_QVariant, SIP_NOT_NONE ) ) + return 0; + + return 1; + } + + QgsAttributes *qv = new QgsAttributes; + + for ( SIP_SSIZE_T i = 0; i < PyList_GET_SIZE( sipPy ); ++i ) + { + int state; + PyObject *obj = PyList_GET_ITEM( sipPy, i ); + QVariant *t; + if ( obj == Py_None ) + { + t = new QVariant( QVariant::Int ); + } + else + { + t = reinterpret_cast( sipConvertToType( obj, sipType_QVariant, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr ) ); + + if ( *sipIsErr ) + { + sipReleaseType( t, sipType_QVariant, state ); + + delete qv; + return 0; + } + } + + qv->append( *t ); + + sipReleaseType( t, sipType_QVariant, state ); + } + + *sipCppPtr = qv; + + return sipGetState( sipTransferObj ); + % End +}; +#endif + /*************************************************************************** * This class is considered CRITICAL and any change MUST be accompanied with * full unit tests in testqgsfeature.cpp.