Skip to content

Commit c4578c3

Browse files
committedApr 26, 2017
QgsVectorLayer is a QgsFeatureSink
Also cleanup API for addFeature(s) in QgsVectorLayer, by removing the unused extra argument from addFeature() and be removing the makeSelected argument from addFeatures() (code should be adapted to manually select added features after adding if desired - this was only used in a single place in the QGIS code and I suspect this was unintentional in any case)
1 parent 8d59833 commit c4578c3

File tree

9 files changed

+39
-40
lines changed

9 files changed

+39
-40
lines changed
 

‎doc/api_break.dox

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,8 @@ displayExpression instead. For the map tip use mapTipTemplate() instead.
21132113
- snapPoint() has been removed - use QgsPointLocator class instead.
21142114
- snapWithContext() has been removed - use QgsPointLocator class instead.
21152115
- insertSegmentVerticesForSnap() has been removed - use addTopologicalPoints() directly.
2116+
- addFeature() no longer accepts an alsoUpdateExtent boolean - this extra argument has been ignored for some time
2117+
- addFeatures() no longer accepts a makeSelected boolean, and will not automatically select newly added features. If desired, features must be manually selected by calling selectByIds() after addFeatures()
21162118

21172119

21182120
QgsVectorLayerEditBuffer {#qgis_api_break_3_0_QgsVectorLayerEditBuffer}

‎python/core/qgsvectorlayer.sip

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef QList<QgsPointV2> QgsPointSequence;
1919

2020

2121

22-
class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
22+
class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSink
2323
{
2424
%Docstring
2525
Represents a vector layer which manages a vector based data sets.
@@ -901,12 +901,16 @@ Return the provider type for this layer
901901
:rtype: QgsFeatureIterator
902902
%End
903903

904-
bool addFeature( QgsFeature &feature, bool alsoUpdateExtent = true );
904+
virtual bool addFeature( QgsFeature &feature /In,Out/ );
905+
905906
%Docstring
906-
Adds a feature
907-
\param feature feature to add
908-
\param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
909-
:return: True in case of success and False in case of error
907+
Adds a single ``feature`` to the layer.
908+
Calling this method causes the layer to recalculate it's extents, which can be
909+
expensive. If multiple features are to be added to the layer then it is more
910+
efficient to call addFeatures(), as addFeatures() will only trigger a single
911+
layer extent recalculation.
912+
\see addFeatures()
913+
:return: true in case of success and false in case of failure
910914
:rtype: bool
911915
%End
912916

@@ -1256,11 +1260,8 @@ Delete an attribute field (but does not commit it)
12561260
:rtype: bool
12571261
%End
12581262

1259-
bool addFeatures( QgsFeatureList features, bool makeSelected = true );
1260-
%Docstring
1261-
Insert a copy of the given features into the layer (but does not commit it)
1262-
:rtype: bool
1263-
%End
1263+
virtual bool addFeatures( QgsFeatureList &features /In,Out/ );
1264+
12641265

12651266
bool deleteFeature( QgsFeatureId fid );
12661267
%Docstring

‎src/app/qgisapp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7632,7 +7632,7 @@ void QgisApp::mergeSelectedFeatures()
76327632
vl->deleteFeature( *feature_it );
76337633
}
76347634

7635-
vl->addFeature( newFeature, false );
7635+
vl->addFeature( newFeature );
76367636

76377637
vl->endEditCommand();
76387638

@@ -7934,7 +7934,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
79347934
// now create new feature using pasted feature as a template. This automatically handles default
79357935
// values and field constraints
79367936
QgsFeature newFeature = QgsVectorLayerUtils::createFeature( pasteVectorLayer, geom, dstAttr, &context );
7937-
pasteVectorLayer->addFeature( newFeature, false );
7937+
pasteVectorLayer->addFeature( newFeature );
79387938
newIds << newFeature.id();
79397939

79407940
++featureIt;
@@ -8131,7 +8131,7 @@ QgsVectorLayer *QgisApp::pasteToNewMemoryVector()
81318131
feature.setGeometry( g );
81328132
}
81338133
}
8134-
if ( ! layer->addFeatures( features, false ) || !layer->commitChanges() )
8134+
if ( ! layer->addFeatures( features ) || !layer->commitChanges() )
81358135
{
81368136
QgsDebugMsg( "Cannot add features or commit changes" );
81378137
delete layer;

‎src/core/qgsofflineediting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ QgsVectorLayer *QgsOfflineEditing::copyVectorLayer( QgsVectorLayer *layer, sqlit
661661
}
662662
f.setAttributes( newAttrs );
663663

664-
newLayer->addFeature( f, false );
664+
newLayer->addFeature( f );
665665

666666
emit progressUpdated( featureCount++ );
667667
}
@@ -786,7 +786,7 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer *offlineLayer, QgsVec
786786

787787
// respect constraints and provider default values
788788
QgsFeature f = QgsVectorLayerUtils::createFeature( remoteLayer, it->geometry(), newAttrs.toMap(), &context );
789-
remoteLayer->addFeature( f, false );
789+
remoteLayer->addFeature( f );
790790

791791
emit progressUpdated( i++ );
792792
}

‎src/core/qgsvectorlayer.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -955,9 +955,8 @@ QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest &request
955955
return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
956956
}
957957

958-
bool QgsVectorLayer::addFeature( QgsFeature &feature, bool alsoUpdateExtent )
958+
bool QgsVectorLayer::addFeature( QgsFeature &feature )
959959
{
960-
Q_UNUSED( alsoUpdateExtent ); // TODO[MD]
961960
if ( !mValid || !mEditBuffer || !mDataProvider )
962961
return false;
963962

@@ -2559,23 +2558,12 @@ QgsFeatureIterator QgsVectorLayer::selectedFeaturesIterator( QgsFeatureRequest r
25592558
return getFeatures( request );
25602559
}
25612560

2562-
bool QgsVectorLayer::addFeatures( QgsFeatureList features, bool makeSelected )
2561+
bool QgsVectorLayer::addFeatures( QgsFeatureList &features )
25632562
{
25642563
if ( !mEditBuffer || !mDataProvider )
25652564
return false;
25662565

25672566
bool res = mEditBuffer->addFeatures( features );
2568-
2569-
if ( makeSelected )
2570-
{
2571-
QgsFeatureIds ids;
2572-
2573-
for ( QgsFeatureList::iterator iter = features.begin(); iter != features.end(); ++iter )
2574-
ids << iter->id();
2575-
2576-
selectByIds( ids );
2577-
}
2578-
25792567
updateExtents();
25802568

25812569
return res;

‎src/core/qgsvectorlayer.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ typedef QList<QgsPointV2> QgsPointSequence;
348348
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified)
349349
* \see QgsVectorLayerUtils()
350350
*/
351-
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator
351+
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator, public QgsFeatureSink
352352
{
353353
Q_OBJECT
354354

@@ -872,12 +872,16 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
872872
return getFeatures( QgsFeatureRequest( rectangle ) );
873873
}
874874

875-
/** Adds a feature
876-
\param feature feature to add
877-
\param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
878-
\returns True in case of success and False in case of error
875+
/**
876+
* Adds a single \a feature to the layer.
877+
* Calling this method causes the layer to recalculate it's extents, which can be
878+
* expensive. If multiple features are to be added to the layer then it is more
879+
* efficient to call addFeatures(), as addFeatures() will only trigger a single
880+
* layer extent recalculation.
881+
* \see addFeatures()
882+
* \returns true in case of success and false in case of failure
879883
*/
880-
bool addFeature( QgsFeature &feature, bool alsoUpdateExtent = true );
884+
bool addFeature( QgsFeature &feature SIP_INOUT ) override;
881885

882886
/** Updates an existing feature. This method needs to query the datasource
883887
on every call. Consider using changeAttributeValue() or
@@ -1201,8 +1205,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
12011205
*/
12021206
bool deleteAttributes( QList<int> attrs );
12031207

1204-
//! Insert a copy of the given features into the layer (but does not commit it)
1205-
bool addFeatures( QgsFeatureList features, bool makeSelected = true );
1208+
bool addFeatures( QgsFeatureList &features SIP_INOUT ) override;
12061209

12071210
//! Delete a feature from the layer (but does not commit it)
12081211
bool deleteFeature( QgsFeatureId fid );

‎src/core/qgsvectorlayertools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureReq
6060
const QgsFeatureId fid = f.id();
6161
#endif
6262
// paste feature
63-
if ( !layer->addFeature( f, false ) )
63+
if ( !layer->addFeature( f ) )
6464
{
6565
couldNotWriteCount++;
6666
QgsDebugMsg( QString( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) );

‎src/gui/qgsrelationeditorwidget.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ void QgsRelationEditorWidget::linkFeature()
371371
}
372372

373373
mRelation.referencingLayer()->addFeatures( newFeatures );
374+
QgsFeatureIds ids;
375+
Q_FOREACH ( const QgsFeature &f, newFeatures )
376+
ids << f.id();
377+
mRelation.referencingLayer()->selectByIds( ids );
378+
374379

375380
updateUi();
376381
}

‎tests/src/core/testqgstracer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static QgsVectorLayer *make_layer( const QStringList &wkts )
6868
Q_FOREACH ( const QString &wkt, wkts )
6969
{
7070
QgsFeature f( make_feature( wkt ) );
71-
vl->addFeature( f, false );
71+
vl->addFeature( f );
7272
}
7373
vl->commitChanges();
7474

0 commit comments

Comments
 (0)