Skip to content

Commit

Permalink
Put some sip code into qgsfeature.h
Browse files Browse the repository at this point in the history
Just for reference
  • Loading branch information
m-kuhn authored and 3nids committed Mar 30, 2017
1 parent ca008d7 commit bd75aee
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
12 changes: 11 additions & 1 deletion python/core/qgsfeature.sip
Expand Up @@ -7,7 +7,7 @@ typedef QVector<QVariant> QgsAttributes;
%MappedType QgsAttributes
{
%TypeHeaderCode
#include <qgsfeature.h>
#include <qgsfeature.h> // NO_SIPIFY
%End

%ConvertFromTypeCode
Expand Down Expand Up @@ -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:

Expand Down
87 changes: 87 additions & 0 deletions src/core/qgsfeature.h
Expand Up @@ -44,6 +44,93 @@ typedef qint64 QgsFeatureId;
// key = field index, value = field value
typedef QMap<int, QVariant> QgsAttributeMap;

#ifdef SIP_RUN
typedef QVector<QVariant> QgsAttributes;

// QgsAttributes is implemented as a Python list of Python objects.
% MappedType QgsAttributes
{
% TypeHeaderCode
#include <qgsfeature.h> // 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<QVariant *>( 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.
Expand Down

0 comments on commit bd75aee

Please sign in to comment.