Skip to content

Commit

Permalink
When calling setFields, automatically initalize attributes
Browse files Browse the repository at this point in the history
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++
  • Loading branch information
m-kuhn committed Jun 5, 2013
1 parent e21f160 commit dc860f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
11 changes: 9 additions & 2 deletions python/core/qgsfeature.sip
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsfeature.cpp
Expand Up @@ -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
{
Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsfeature.h
Expand Up @@ -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
Expand Down

0 comments on commit dc860f7

Please sign in to comment.