Skip to content

Commit

Permalink
Fix up non-const getters for attributes and geometry in QgsFeature
Browse files Browse the repository at this point in the history
(preparation for implicit sharing)
  • Loading branch information
nyalldawson committed May 20, 2015
1 parent 73ed56a commit d393d26
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 62 deletions.
22 changes: 17 additions & 5 deletions python/core/qgsfeature.sip
Expand Up @@ -241,8 +241,8 @@ class QgsFeature
*/
void setFeatureId( qint64 id );

const QgsAttributes& attributes() const;
//QgsAttributes& attributes();
QgsAttributes attributes() const;

void setAttributes( const QgsAttributes& attrs );

/**
Expand Down Expand Up @@ -312,33 +312,45 @@ class QgsFeature

/**
* Get the geometry object associated with this feature
*
* It is possible to modify the geometry in place but this will
* be removed in 3.0 and therefore @link setGeometry @endlink should be called explicitly.
*
* @note will be modified to return by value in QGIS 3.0: `QgsGeometry geometry() const;`
*/
QgsGeometry* geometry() const;
QgsGeometry* geometry();

/** Gets a const pointer to the geometry object associated with this feature
/** Gets a const pointer to the geometry object associated with this feature.
* @note this is a temporary method for 2.x release cycle. Will be removed in QGIS 3.0.
* @note added in QGIS 2.9
* @note will be removed in QGIS 3.0
*/
const QgsGeometry* constGeometry() const;

/**
* Get the geometry object associated with this feature
* The caller assumes responsibility for the QgsGeometry*'s destruction.
* @deprecated will be removed in QGIS 3.0
*/
QgsGeometry *geometryAndOwnership() /Factory/;
QgsGeometry *geometryAndOwnership() /Factory,Deprecated/;

/** Set this feature's geometry from another QgsGeometry object (deep copy)
*/
void setGeometry( const QgsGeometry& geom );

/** Set this feature's geometry (takes geometry ownership)
*
* @note not available in python bindings
* @deprecated will be removed in QGIS 3.0
*/
// void setGeometry( QgsGeometry* geom /Transfer/ );

/**
* Set this feature's geometry from WKB
*
* This feature assumes responsibility for destroying geom.
*
* @deprecated will be removed in QGIS 3.0
*/
void setGeometryAndOwnership( unsigned char * geom /Transfer/, size_t length );

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -5916,7 +5916,7 @@ void QgisApp::mergeAttributesOfSelectedFeatures()

vl->beginEditCommand( tr( "Merged feature attributes" ) );

const QgsAttributes &merged = d.mergedAttributes();
QgsAttributes merged = d.mergedAttributes();

foreach ( QgsFeatureId fid, vl->selectedFeaturesIds() )
{
Expand Down Expand Up @@ -6232,7 +6232,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
QgsFeatureList::iterator featureIt = features.begin();
while ( featureIt != features.end() )
{
const QgsAttributes &srcAttr = featureIt->attributes();
QgsAttributes srcAttr = featureIt->attributes();
QgsAttributes dstAttr( dstAttrCount );

for ( int src = 0; src < srcAttr.count(); ++src )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsclipboard.cpp
Expand Up @@ -99,7 +99,7 @@ void QgsClipboard::setSystemClipboard()
// then the field contents
for ( QgsFeatureList::iterator it = mFeatureClipboard.begin(); it != mFeatureClipboard.end(); ++it )
{
const QgsAttributes& attributes = it->attributes();
QgsAttributes attributes = it->attributes();

// TODO: Set up Paste Transformations to specify the order in which fields are added.
if ( copyWKT )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfeatureaction.cpp
Expand Up @@ -229,7 +229,7 @@ void QgsFeatureAction::onFeatureSaved( const QgsFeature& feature )
QgsFields fields = mLayer->pendingFields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
const QgsAttributes &newValues = feature.attributes();
QgsAttributes newValues = feature.attributes();
QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
if ( origValues[idx] != newValues[idx] )
{
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -455,7 +455,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
}

const QgsFields &fields = vlayer->pendingFields();
const QgsAttributes& attrs = f.attributes();
QgsAttributes attrs = f.attributes();
bool featureLabeled = false;
for ( int i = 0; i < attrs.count(); ++i )
{
Expand Down Expand Up @@ -719,7 +719,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
if ( feature.isValid() )
{
QgsDebugMsg( QString( "fields size = %1 attributes size = %2" ).arg( fields.size() ).arg( feature.attributes().size() ) );
const QgsAttributes& attrs = feature.attributes();
QgsAttributes attrs = feature.attributes();
for ( int i = 0; i < attrs.count(); ++i )
{
if ( i >= fields.count() )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgslabelpropertydialog.cpp
Expand Up @@ -69,7 +69,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
{
return;
}
const QgsAttributes& attributeValues = mCurLabelFeat.attributes();
QgsAttributes attributeValues = mCurLabelFeat.attributes();

//get layerproperties. Problem: only for pallabeling...

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoollabel.cpp
Expand Up @@ -483,7 +483,7 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, const QgsFeat
return false;
}

const QgsAttributes& attributes = f.attributes();
QgsAttributes attributes = f.attributes();
if ( !attributes[xCol].isNull() )
x = attributes[xCol].toDouble( &xSuccess );
if ( !attributes[yCol].isNull() )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -119,7 +119,7 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
{
verticalHeaderLabels << FID_TO_STRING( mFeatureList[i].id() );

const QgsAttributes &attrs = mFeatureList[i].attributes();
QgsAttributes attrs = mFeatureList[i].attributes();

for ( int j = 0; j < mTableWidget->columnCount(); j++ )
{
Expand Down
10 changes: 6 additions & 4 deletions src/core/qgsfeature.cpp
Expand Up @@ -109,7 +109,7 @@ void QgsFeature::deleteAttribute( int field )
}


QgsGeometry *QgsFeature::geometry() const
QgsGeometry *QgsFeature::geometry()
{
return mGeometry;
}
Expand Down Expand Up @@ -257,9 +257,9 @@ QDataStream& operator<<( QDataStream& out, const QgsFeature& feature )
{
out << feature.id();
out << feature.attributes();
if ( feature.geometry() )
if ( feature.constGeometry() )
{
out << *( feature.geometry() );
out << *( feature.constGeometry() );
}
else
{
Expand All @@ -275,9 +275,11 @@ QDataStream& operator>>( QDataStream& in, QgsFeature& feature )
QgsFeatureId id;
QgsGeometry* geometry = new QgsGeometry();
bool valid;
in >> id >> feature.attributes() >> *geometry >> valid;
QgsAttributes attr;
in >> id >> attr >> *geometry >> valid;
feature.setFeatureId( id );
feature.setGeometry( geometry );
feature.setAttributes( attr );
feature.setValid( valid );
return in;
}
21 changes: 16 additions & 5 deletions src/core/qgsfeature.h
Expand Up @@ -144,8 +144,7 @@ class CORE_EXPORT QgsFeature
*/
void setFeatureId( QgsFeatureId id );

const QgsAttributes& attributes() const { return mAttributes; }
QgsAttributes& attributes() { return mAttributes; }
QgsAttributes attributes() const { return mAttributes; }
void setAttributes( const QgsAttributes& attrs ) { mAttributes = attrs; }

/**
Expand Down Expand Up @@ -189,33 +188,45 @@ class CORE_EXPORT QgsFeature

/**
* Get the geometry object associated with this feature
*
* It is possible to modify the geometry in place but this will
* be removed in 3.0 and therefore @link setGeometry @endlink should be called explicitly.
*
* @note will be modified to return by value in QGIS 3.0: `QgsGeometry geometry() const;`
*/
QgsGeometry* geometry() const;
QgsGeometry* geometry();

/** Gets a const pointer to the geometry object associated with this feature
/** Gets a const pointer to the geometry object associated with this feature.
* @note this is a temporary method for 2.x release cycle. Will be removed in QGIS 3.0.
* @note added in QGIS 2.9
* @note will be removed in QGIS 3.0
*/
const QgsGeometry* constGeometry() const;

/**
* Get the geometry object associated with this feature
* The caller assumes responsibility for the QgsGeometry*'s destruction.
* @deprecated will be removed in QGIS 3.0
*/
QgsGeometry *geometryAndOwnership();
Q_DECL_DEPRECATED QgsGeometry *geometryAndOwnership();

/** Set this feature's geometry from another QgsGeometry object (deep copy)
*/
void setGeometry( const QgsGeometry& geom );

/** Set this feature's geometry (takes geometry ownership)
*
* @note not available in python bindings
* @deprecated will be removed in QGIS 3.0
*/
void setGeometry( QgsGeometry* geom );

/**
* Set this feature's geometry from WKB
*
* This feature assumes responsibility for destroying geom.
*
* @deprecated will be removed in QGIS 3.0
*/
void setGeometryAndOwnership( unsigned char * geom, size_t length );

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectordataprovider.cpp
Expand Up @@ -407,7 +407,7 @@ void QgsVectorDataProvider::fillMinMaxCache()

while ( fi.nextFeature( f ) )
{
const QgsAttributes& attrs = f.attributes();
QgsAttributes attrs = f.attributes();
for ( QgsAttributeList::const_iterator it = keys.begin(); it != keys.end(); ++it )
{
const QVariant& varValue = attrs[*it];
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -980,8 +980,8 @@ bool QgsVectorLayer::updateFeature( QgsFeature &f )
}
}

const QgsAttributes &fa = f.attributes();
const QgsAttributes &ca = current.attributes();
QgsAttributes fa = f.attributes();
QgsAttributes ca = current.attributes();

for ( int attr = 0; attr < fa.count(); ++attr )
{
Expand Down
10 changes: 7 additions & 3 deletions src/core/qgsvectorlayereditbuffer.cpp
Expand Up @@ -84,7 +84,7 @@ void QgsVectorLayerEditBuffer::updateFeatureGeometry( QgsFeature &f )

void QgsVectorLayerEditBuffer::updateChangedAttributes( QgsFeature &f )
{
QgsAttributes& attrs = f.attributes();
QgsAttributes attrs = f.attributes();

// remove all attributes that will disappear - from higher indices to lower
for ( int idx = mDeletedAttributeIds.count() - 1; idx >= 0; --idx )
Expand All @@ -102,6 +102,8 @@ void QgsVectorLayerEditBuffer::updateChangedAttributes( QgsFeature &f )
for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
attrs[it.key()] = it.value();
}

f.setAttributes( attrs );
}


Expand Down Expand Up @@ -579,8 +581,9 @@ void QgsVectorLayerEditBuffer::handleAttributeAdded( int index )
QgsFeatureMap::iterator featureIt = mAddedFeatures.begin();
for ( ; featureIt != mAddedFeatures.end(); ++featureIt )
{
QgsAttributes& attrs = featureIt->attributes();
QgsAttributes attrs = featureIt->attributes();
attrs.insert( index, QVariant() );
featureIt->setAttributes( attrs );
}
}

Expand All @@ -602,8 +605,9 @@ void QgsVectorLayerEditBuffer::handleAttributeDeleted( int index )
QgsFeatureMap::iterator featureIt = mAddedFeatures.begin();
for ( ; featureIt != mAddedFeatures.end(); ++featureIt )
{
QgsAttributes& attrs = featureIt->attributes();
QgsAttributes attrs = featureIt->attributes();
attrs.remove( index );
featureIt->setAttributes( attrs );
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -547,7 +547,9 @@ void QgsVectorLayerFeatureIterator::addJoinedAttributes( QgsFeature &f )
void QgsVectorLayerFeatureIterator::addVirtualAttributes( QgsFeature& f )
{
// make sure we have space for newly added attributes
f.attributes().resize( mSource->mFields.count() ); // Provider attrs count + joined attrs count + expression attrs count
QgsAttributes attr = f.attributes();
attr.resize( mSource->mFields.count() ); // Provider attrs count + joined attrs count + expression attrs count
f.setAttributes( attr );

if ( !mFetchJoinInfo.isEmpty() )
addJoinedAttributes( f );
Expand Down Expand Up @@ -684,7 +686,7 @@ void QgsVectorLayerFeatureIterator::FetchJoinInfo::addJoinedAttributesDirect( Qg
if ( fi.nextFeature( fet ) )
{
int index = indexOffset;
const QgsAttributes& attr = fet.attributes();
QgsAttributes attr = fet.attributes();
if ( hasSubset )
{
for ( int i = 0; i < subsetIndices.count(); ++i )
Expand Down Expand Up @@ -756,7 +758,7 @@ bool QgsVectorLayerFeatureIterator::nextFeatureFid( QgsFeature& f )

void QgsVectorLayerFeatureIterator::updateChangedAttributes( QgsFeature &f )
{
QgsAttributes& attrs = f.attributes();
QgsAttributes attrs = f.attributes();

// remove all attributes that will disappear - from higher indices to lower
for ( int idx = mSource->mDeletedAttributeIds.count() - 1; idx >= 0; --idx )
Expand All @@ -774,6 +776,7 @@ void QgsVectorLayerFeatureIterator::updateChangedAttributes( QgsFeature &f )
for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
attrs[it.key()] = it.value();
}
f.setAttributes( attrs );
}

void QgsVectorLayerFeatureIterator::updateFeatureGeometry( QgsFeature &f )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerimport.cpp
Expand Up @@ -133,7 +133,7 @@ QString QgsVectorLayerImport::errorMessage()

bool QgsVectorLayerImport::addFeature( QgsFeature& feat )
{
const QgsAttributes &attrs = feat.attributes();
QgsAttributes attrs = feat.attributes();

QgsFeature newFeat;
if ( feat.constGeometry() )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -152,7 +152,7 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorJoinInfo& joinInfo )
QgsFeature f;
while ( fit.nextFeature( f ) )
{
const QgsAttributes& attrs = f.attributes();
QgsAttributes attrs = f.attributes();
QString key = attrs[joinFieldIndex].toString();
if ( hasSubset )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp
Expand Up @@ -237,7 +237,7 @@ QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& featu

QgsSymbolV2* QgsCategorizedSymbolRendererV2::originalSymbolForFeature( QgsFeature& feature )
{
const QgsAttributes& attrs = feature.attributes();
QgsAttributes attrs = feature.attributes();
QVariant value;
if ( mAttrNum == -1 )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Expand Up @@ -350,7 +350,7 @@ QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForFeature( QgsFeature& feature

QgsSymbolV2* QgsGraduatedSymbolRendererV2::originalSymbolForFeature( QgsFeature& feature )
{
const QgsAttributes& attrs = feature.attributes();
QgsAttributes attrs = feature.attributes();
QVariant value;
if ( mAttrNum < 0 || mAttrNum >= attrs.count() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsheatmaprenderer.cpp
Expand Up @@ -125,7 +125,7 @@ bool QgsHeatmapRenderer::renderFeature( QgsFeature& feature, QgsRenderContext& c
}
else
{
const QgsAttributes& attrs = feature.attributes();
QgsAttributes attrs = feature.attributes();
value = attrs.value( mWeightAttrNum );
}
bool ok = false;
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgspointdisplacementrenderer.cpp
Expand Up @@ -435,7 +435,7 @@ void QgsPointDisplacementRenderer::printInfoDisplacementGroups()
QString QgsPointDisplacementRenderer::getLabel( const QgsFeature& f )
{
QString attribute;
const QgsAttributes& attrs = f.attributes();
QgsAttributes attrs = f.attributes();
if ( mLabelIndex >= 0 && mLabelIndex < attrs.count() )
{
attribute = attrs[mLabelIndex].toString();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsformannotationitem.cpp
Expand Up @@ -94,7 +94,7 @@ QWidget* QgsFormAnnotationItem::createDesignerWidget( const QString& filePath )
if ( mVectorLayer->getFeatures( QgsFeatureRequest().setFilterFid( mFeature ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) )
{
const QgsFields& fields = mVectorLayer->pendingFields();
const QgsAttributes& attrs = f.attributes();
QgsAttributes attrs = f.attributes();
for ( int i = 0; i < attrs.count(); ++i )
{
if ( i < fields.count() )
Expand Down

0 comments on commit d393d26

Please sign in to comment.