From dc860f75ed039981db5a916dfae9f2918111be2c Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Wed, 5 Jun 2013 11:59:50 +0200 Subject: [PATCH] When calling setFields, automatically initalize attributes That what you want most of the time when creating a new feature within a plugin. Therefore defaults to true when used from python, but to false when used from C++ --- python/core/qgsfeature.sip | 11 +++++++++-- src/core/qgsfeature.cpp | 9 +++++++++ src/core/qgsfeature.h | 11 ++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/python/core/qgsfeature.sip b/python/core/qgsfeature.sip index 94755a5a2d5..14558565bd3 100644 --- a/python/core/qgsfeature.sip +++ b/python/core/qgsfeature.sip @@ -158,10 +158,17 @@ class QgsFeature */ void setGeometryAndOwnership( unsigned char * geom /Transfer/, size_t length ); - /** Assign a field map with the feature to allow attribute access by attribute name + /** Assign a field map with the feature to allow attribute access by + * attribute name + * + * @param fields The attribute fields which this feature holds + * @param initAttributes If true, attributes are initialized. Clears any + * data previously assigned. + * C++: Defaults to false + * Python: Defaults to true * @note added in 2.0 */ - void setFields( const QgsFields* fields ); + void setFields( const QgsFields* fields, bool initAttributes = true ); /** Get associated field map. may be NULL * @note added in 2.0 diff --git a/src/core/qgsfeature.cpp b/src/core/qgsfeature.cpp index bda531ed398..513c60a5a32 100644 --- a/src/core/qgsfeature.cpp +++ b/src/core/qgsfeature.cpp @@ -159,6 +159,15 @@ void QgsFeature::setGeometryAndOwnership( unsigned char *geom, size_t length ) setGeometry( g ); } +void QgsFeature::setFields( const QgsFields* fields, bool initAttributes ) +{ + mFields = fields; + if ( initAttributes ) + { + this->initAttributes( fields->count() ); + } +} + bool QgsFeature::isValid() const { diff --git a/src/core/qgsfeature.h b/src/core/qgsfeature.h index 705b0d0c4a4..6a7576a4d76 100644 --- a/src/core/qgsfeature.h +++ b/src/core/qgsfeature.h @@ -189,9 +189,18 @@ class CORE_EXPORT QgsFeature void setGeometryAndOwnership( unsigned char * geom, size_t length ); /** Assign a field map with the feature to allow attribute access by attribute name + * + * @param fields The attribute fields which this feature holds. When used from python, make sure + * a copy of the fields is held by python, as ownership stays there. + * I.e. Do not call feature.setFields( myDataProvider.fields() ) but instead call + * myFields = myDataProvider.fields() + * feature.setFields( myFields ) + * @param initAttributes If true, attributes are initialized. Clears any data previously assigned. + * C++: Defaults to false + * Python: Defaults to true * @note added in 2.0 */ - void setFields( const QgsFields* fields ) { mFields = fields; } + void setFields( const QgsFields* fields, bool initAttributes = false ); /** Get associated field map. may be NULL * @note added in 2.0