Skip to content

Commit d393d26

Browse files
committed
Fix up non-const getters for attributes and geometry in QgsFeature
(preparation for implicit sharing)
1 parent 73ed56a commit d393d26

33 files changed

+100
-62
lines changed

python/core/qgsfeature.sip

+17-5
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ class QgsFeature
241241
*/
242242
void setFeatureId( qint64 id );
243243

244-
const QgsAttributes& attributes() const;
245-
//QgsAttributes& attributes();
244+
QgsAttributes attributes() const;
245+
246246
void setAttributes( const QgsAttributes& attrs );
247247

248248
/**
@@ -312,33 +312,45 @@ class QgsFeature
312312

313313
/**
314314
* Get the geometry object associated with this feature
315+
*
316+
* It is possible to modify the geometry in place but this will
317+
* be removed in 3.0 and therefore @link setGeometry @endlink should be called explicitly.
318+
*
319+
* @note will be modified to return by value in QGIS 3.0: `QgsGeometry geometry() const;`
315320
*/
316-
QgsGeometry* geometry() const;
321+
QgsGeometry* geometry();
317322

318-
/** Gets a const pointer to the geometry object associated with this feature
323+
/** Gets a const pointer to the geometry object associated with this feature.
324+
* @note this is a temporary method for 2.x release cycle. Will be removed in QGIS 3.0.
319325
* @note added in QGIS 2.9
326+
* @note will be removed in QGIS 3.0
320327
*/
321328
const QgsGeometry* constGeometry() const;
322329

323330
/**
324331
* Get the geometry object associated with this feature
325332
* The caller assumes responsibility for the QgsGeometry*'s destruction.
333+
* @deprecated will be removed in QGIS 3.0
326334
*/
327-
QgsGeometry *geometryAndOwnership() /Factory/;
335+
QgsGeometry *geometryAndOwnership() /Factory,Deprecated/;
328336

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

333341
/** Set this feature's geometry (takes geometry ownership)
342+
*
334343
* @note not available in python bindings
344+
* @deprecated will be removed in QGIS 3.0
335345
*/
336346
// void setGeometry( QgsGeometry* geom /Transfer/ );
337347

338348
/**
339349
* Set this feature's geometry from WKB
340350
*
341351
* This feature assumes responsibility for destroying geom.
352+
*
353+
* @deprecated will be removed in QGIS 3.0
342354
*/
343355
void setGeometryAndOwnership( unsigned char * geom /Transfer/, size_t length );
344356

src/app/qgisapp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5916,7 +5916,7 @@ void QgisApp::mergeAttributesOfSelectedFeatures()
59165916

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

5919-
const QgsAttributes &merged = d.mergedAttributes();
5919+
QgsAttributes merged = d.mergedAttributes();
59205920

59215921
foreach ( QgsFeatureId fid, vl->selectedFeaturesIds() )
59225922
{
@@ -6232,7 +6232,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
62326232
QgsFeatureList::iterator featureIt = features.begin();
62336233
while ( featureIt != features.end() )
62346234
{
6235-
const QgsAttributes &srcAttr = featureIt->attributes();
6235+
QgsAttributes srcAttr = featureIt->attributes();
62366236
QgsAttributes dstAttr( dstAttrCount );
62376237

62386238
for ( int src = 0; src < srcAttr.count(); ++src )

src/app/qgsclipboard.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void QgsClipboard::setSystemClipboard()
9999
// then the field contents
100100
for ( QgsFeatureList::iterator it = mFeatureClipboard.begin(); it != mFeatureClipboard.end(); ++it )
101101
{
102-
const QgsAttributes& attributes = it->attributes();
102+
QgsAttributes attributes = it->attributes();
103103

104104
// TODO: Set up Paste Transformations to specify the order in which fields are added.
105105
if ( copyWKT )

src/app/qgsfeatureaction.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void QgsFeatureAction::onFeatureSaved( const QgsFeature& feature )
229229
QgsFields fields = mLayer->pendingFields();
230230
for ( int idx = 0; idx < fields.count(); ++idx )
231231
{
232-
const QgsAttributes &newValues = feature.attributes();
232+
QgsAttributes newValues = feature.attributes();
233233
QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
234234
if ( origValues[idx] != newValues[idx] )
235235
{

src/app/qgsidentifyresultsdialog.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
455455
}
456456

457457
const QgsFields &fields = vlayer->pendingFields();
458-
const QgsAttributes& attrs = f.attributes();
458+
QgsAttributes attrs = f.attributes();
459459
bool featureLabeled = false;
460460
for ( int i = 0; i < attrs.count(); ++i )
461461
{
@@ -719,7 +719,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
719719
if ( feature.isValid() )
720720
{
721721
QgsDebugMsg( QString( "fields size = %1 attributes size = %2" ).arg( fields.size() ).arg( feature.attributes().size() ) );
722-
const QgsAttributes& attrs = feature.attributes();
722+
QgsAttributes attrs = feature.attributes();
723723
for ( int i = 0; i < attrs.count(); ++i )
724724
{
725725
if ( i >= fields.count() )

src/app/qgslabelpropertydialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
6969
{
7070
return;
7171
}
72-
const QgsAttributes& attributeValues = mCurLabelFeat.attributes();
72+
QgsAttributes attributeValues = mCurLabelFeat.attributes();
7373

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

src/app/qgsmaptoollabel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, const QgsFeat
483483
return false;
484484
}
485485

486-
const QgsAttributes& attributes = f.attributes();
486+
QgsAttributes attributes = f.attributes();
487487
if ( !attributes[xCol].isNull() )
488488
x = attributes[xCol].toDouble( &xSuccess );
489489
if ( !attributes[yCol].isNull() )

src/app/qgsmergeattributesdialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
119119
{
120120
verticalHeaderLabels << FID_TO_STRING( mFeatureList[i].id() );
121121

122-
const QgsAttributes &attrs = mFeatureList[i].attributes();
122+
QgsAttributes attrs = mFeatureList[i].attributes();
123123

124124
for ( int j = 0; j < mTableWidget->columnCount(); j++ )
125125
{

src/core/qgsfeature.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void QgsFeature::deleteAttribute( int field )
109109
}
110110

111111

112-
QgsGeometry *QgsFeature::geometry() const
112+
QgsGeometry *QgsFeature::geometry()
113113
{
114114
return mGeometry;
115115
}
@@ -257,9 +257,9 @@ QDataStream& operator<<( QDataStream& out, const QgsFeature& feature )
257257
{
258258
out << feature.id();
259259
out << feature.attributes();
260-
if ( feature.geometry() )
260+
if ( feature.constGeometry() )
261261
{
262-
out << *( feature.geometry() );
262+
out << *( feature.constGeometry() );
263263
}
264264
else
265265
{
@@ -275,9 +275,11 @@ QDataStream& operator>>( QDataStream& in, QgsFeature& feature )
275275
QgsFeatureId id;
276276
QgsGeometry* geometry = new QgsGeometry();
277277
bool valid;
278-
in >> id >> feature.attributes() >> *geometry >> valid;
278+
QgsAttributes attr;
279+
in >> id >> attr >> *geometry >> valid;
279280
feature.setFeatureId( id );
280281
feature.setGeometry( geometry );
282+
feature.setAttributes( attr );
281283
feature.setValid( valid );
282284
return in;
283285
}

src/core/qgsfeature.h

+16-5
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ class CORE_EXPORT QgsFeature
144144
*/
145145
void setFeatureId( QgsFeatureId id );
146146

147-
const QgsAttributes& attributes() const { return mAttributes; }
148-
QgsAttributes& attributes() { return mAttributes; }
147+
QgsAttributes attributes() const { return mAttributes; }
149148
void setAttributes( const QgsAttributes& attrs ) { mAttributes = attrs; }
150149

151150
/**
@@ -189,33 +188,45 @@ class CORE_EXPORT QgsFeature
189188

190189
/**
191190
* Get the geometry object associated with this feature
191+
*
192+
* It is possible to modify the geometry in place but this will
193+
* be removed in 3.0 and therefore @link setGeometry @endlink should be called explicitly.
194+
*
195+
* @note will be modified to return by value in QGIS 3.0: `QgsGeometry geometry() const;`
192196
*/
193-
QgsGeometry* geometry() const;
197+
QgsGeometry* geometry();
194198

195-
/** Gets a const pointer to the geometry object associated with this feature
199+
/** Gets a const pointer to the geometry object associated with this feature.
200+
* @note this is a temporary method for 2.x release cycle. Will be removed in QGIS 3.0.
196201
* @note added in QGIS 2.9
202+
* @note will be removed in QGIS 3.0
197203
*/
198204
const QgsGeometry* constGeometry() const;
199205

200206
/**
201207
* Get the geometry object associated with this feature
202208
* The caller assumes responsibility for the QgsGeometry*'s destruction.
209+
* @deprecated will be removed in QGIS 3.0
203210
*/
204-
QgsGeometry *geometryAndOwnership();
211+
Q_DECL_DEPRECATED QgsGeometry *geometryAndOwnership();
205212

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

210217
/** Set this feature's geometry (takes geometry ownership)
218+
*
211219
* @note not available in python bindings
220+
* @deprecated will be removed in QGIS 3.0
212221
*/
213222
void setGeometry( QgsGeometry* geom );
214223

215224
/**
216225
* Set this feature's geometry from WKB
217226
*
218227
* This feature assumes responsibility for destroying geom.
228+
*
229+
* @deprecated will be removed in QGIS 3.0
219230
*/
220231
void setGeometryAndOwnership( unsigned char * geom, size_t length );
221232

src/core/qgsvectordataprovider.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ void QgsVectorDataProvider::fillMinMaxCache()
407407

408408
while ( fi.nextFeature( f ) )
409409
{
410-
const QgsAttributes& attrs = f.attributes();
410+
QgsAttributes attrs = f.attributes();
411411
for ( QgsAttributeList::const_iterator it = keys.begin(); it != keys.end(); ++it )
412412
{
413413
const QVariant& varValue = attrs[*it];

src/core/qgsvectorlayer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ bool QgsVectorLayer::updateFeature( QgsFeature &f )
980980
}
981981
}
982982

983-
const QgsAttributes &fa = f.attributes();
984-
const QgsAttributes &ca = current.attributes();
983+
QgsAttributes fa = f.attributes();
984+
QgsAttributes ca = current.attributes();
985985

986986
for ( int attr = 0; attr < fa.count(); ++attr )
987987
{

src/core/qgsvectorlayereditbuffer.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void QgsVectorLayerEditBuffer::updateFeatureGeometry( QgsFeature &f )
8484

8585
void QgsVectorLayerEditBuffer::updateChangedAttributes( QgsFeature &f )
8686
{
87-
QgsAttributes& attrs = f.attributes();
87+
QgsAttributes attrs = f.attributes();
8888

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

107109

@@ -579,8 +581,9 @@ void QgsVectorLayerEditBuffer::handleAttributeAdded( int index )
579581
QgsFeatureMap::iterator featureIt = mAddedFeatures.begin();
580582
for ( ; featureIt != mAddedFeatures.end(); ++featureIt )
581583
{
582-
QgsAttributes& attrs = featureIt->attributes();
584+
QgsAttributes attrs = featureIt->attributes();
583585
attrs.insert( index, QVariant() );
586+
featureIt->setAttributes( attrs );
584587
}
585588
}
586589

@@ -602,8 +605,9 @@ void QgsVectorLayerEditBuffer::handleAttributeDeleted( int index )
602605
QgsFeatureMap::iterator featureIt = mAddedFeatures.begin();
603606
for ( ; featureIt != mAddedFeatures.end(); ++featureIt )
604607
{
605-
QgsAttributes& attrs = featureIt->attributes();
608+
QgsAttributes attrs = featureIt->attributes();
606609
attrs.remove( index );
610+
featureIt->setAttributes( attrs );
607611
}
608612
}
609613

src/core/qgsvectorlayerfeatureiterator.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ void QgsVectorLayerFeatureIterator::addJoinedAttributes( QgsFeature &f )
547547
void QgsVectorLayerFeatureIterator::addVirtualAttributes( QgsFeature& f )
548548
{
549549
// make sure we have space for newly added attributes
550-
f.attributes().resize( mSource->mFields.count() ); // Provider attrs count + joined attrs count + expression attrs count
550+
QgsAttributes attr = f.attributes();
551+
attr.resize( mSource->mFields.count() ); // Provider attrs count + joined attrs count + expression attrs count
552+
f.setAttributes( attr );
551553

552554
if ( !mFetchJoinInfo.isEmpty() )
553555
addJoinedAttributes( f );
@@ -684,7 +686,7 @@ void QgsVectorLayerFeatureIterator::FetchJoinInfo::addJoinedAttributesDirect( Qg
684686
if ( fi.nextFeature( fet ) )
685687
{
686688
int index = indexOffset;
687-
const QgsAttributes& attr = fet.attributes();
689+
QgsAttributes attr = fet.attributes();
688690
if ( hasSubset )
689691
{
690692
for ( int i = 0; i < subsetIndices.count(); ++i )
@@ -756,7 +758,7 @@ bool QgsVectorLayerFeatureIterator::nextFeatureFid( QgsFeature& f )
756758

757759
void QgsVectorLayerFeatureIterator::updateChangedAttributes( QgsFeature &f )
758760
{
759-
QgsAttributes& attrs = f.attributes();
761+
QgsAttributes attrs = f.attributes();
760762

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

779782
void QgsVectorLayerFeatureIterator::updateFeatureGeometry( QgsFeature &f )

src/core/qgsvectorlayerimport.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ QString QgsVectorLayerImport::errorMessage()
133133

134134
bool QgsVectorLayerImport::addFeature( QgsFeature& feat )
135135
{
136-
const QgsAttributes &attrs = feat.attributes();
136+
QgsAttributes attrs = feat.attributes();
137137

138138
QgsFeature newFeat;
139139
if ( feat.constGeometry() )

src/core/qgsvectorlayerjoinbuffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorJoinInfo& joinInfo )
152152
QgsFeature f;
153153
while ( fit.nextFeature( f ) )
154154
{
155-
const QgsAttributes& attrs = f.attributes();
155+
QgsAttributes attrs = f.attributes();
156156
QString key = attrs[joinFieldIndex].toString();
157157
if ( hasSubset )
158158
{

src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& featu
237237

238238
QgsSymbolV2* QgsCategorizedSymbolRendererV2::originalSymbolForFeature( QgsFeature& feature )
239239
{
240-
const QgsAttributes& attrs = feature.attributes();
240+
QgsAttributes attrs = feature.attributes();
241241
QVariant value;
242242
if ( mAttrNum == -1 )
243243
{

src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForFeature( QgsFeature& feature
350350

351351
QgsSymbolV2* QgsGraduatedSymbolRendererV2::originalSymbolForFeature( QgsFeature& feature )
352352
{
353-
const QgsAttributes& attrs = feature.attributes();
353+
QgsAttributes attrs = feature.attributes();
354354
QVariant value;
355355
if ( mAttrNum < 0 || mAttrNum >= attrs.count() )
356356
{

src/core/symbology-ng/qgsheatmaprenderer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ bool QgsHeatmapRenderer::renderFeature( QgsFeature& feature, QgsRenderContext& c
125125
}
126126
else
127127
{
128-
const QgsAttributes& attrs = feature.attributes();
128+
QgsAttributes attrs = feature.attributes();
129129
value = attrs.value( mWeightAttrNum );
130130
}
131131
bool ok = false;

src/core/symbology-ng/qgspointdisplacementrenderer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ void QgsPointDisplacementRenderer::printInfoDisplacementGroups()
435435
QString QgsPointDisplacementRenderer::getLabel( const QgsFeature& f )
436436
{
437437
QString attribute;
438-
const QgsAttributes& attrs = f.attributes();
438+
QgsAttributes attrs = f.attributes();
439439
if ( mLabelIndex >= 0 && mLabelIndex < attrs.count() )
440440
{
441441
attribute = attrs[mLabelIndex].toString();

src/gui/qgsformannotationitem.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ QWidget* QgsFormAnnotationItem::createDesignerWidget( const QString& filePath )
9494
if ( mVectorLayer->getFeatures( QgsFeatureRequest().setFilterFid( mFeature ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) )
9595
{
9696
const QgsFields& fields = mVectorLayer->pendingFields();
97-
const QgsAttributes& attrs = f.attributes();
97+
QgsAttributes attrs = f.attributes();
9898
for ( int i = 0; i < attrs.count(); ++i )
9999
{
100100
if ( i < fields.count() )

0 commit comments

Comments
 (0)