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)
This commit is contained in:
Nyall Dawson 2017-04-26 11:12:01 +10:00
parent 8d598337f3
commit c4578c3c1d
9 changed files with 39 additions and 40 deletions

View File

@ -2113,6 +2113,8 @@ displayExpression instead. For the map tip use mapTipTemplate() instead.
- snapPoint() has been removed - use QgsPointLocator class instead. - snapPoint() has been removed - use QgsPointLocator class instead.
- snapWithContext() has been removed - use QgsPointLocator class instead. - snapWithContext() has been removed - use QgsPointLocator class instead.
- insertSegmentVerticesForSnap() has been removed - use addTopologicalPoints() directly. - insertSegmentVerticesForSnap() has been removed - use addTopologicalPoints() directly.
- addFeature() no longer accepts an alsoUpdateExtent boolean - this extra argument has been ignored for some time
- 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()
QgsVectorLayerEditBuffer {#qgis_api_break_3_0_QgsVectorLayerEditBuffer} QgsVectorLayerEditBuffer {#qgis_api_break_3_0_QgsVectorLayerEditBuffer}

View File

@ -19,7 +19,7 @@ typedef QList<QgsPointV2> QgsPointSequence;
class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSink
{ {
%Docstring %Docstring
Represents a vector layer which manages a vector based data sets. Represents a vector layer which manages a vector based data sets.
@ -901,12 +901,16 @@ Return the provider type for this layer
:rtype: QgsFeatureIterator :rtype: QgsFeatureIterator
%End %End
bool addFeature( QgsFeature &feature, bool alsoUpdateExtent = true ); virtual bool addFeature( QgsFeature &feature /In,Out/ );
%Docstring %Docstring
Adds a feature Adds a single ``feature`` to the layer.
\param feature feature to add Calling this method causes the layer to recalculate it's extents, which can be
\param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents. expensive. If multiple features are to be added to the layer then it is more
:return: True in case of success and False in case of error efficient to call addFeatures(), as addFeatures() will only trigger a single
layer extent recalculation.
\see addFeatures()
:return: true in case of success and false in case of failure
:rtype: bool :rtype: bool
%End %End
@ -1256,11 +1260,8 @@ Delete an attribute field (but does not commit it)
:rtype: bool :rtype: bool
%End %End
bool addFeatures( QgsFeatureList features, bool makeSelected = true ); virtual bool addFeatures( QgsFeatureList &features /In,Out/ );
%Docstring
Insert a copy of the given features into the layer (but does not commit it)
:rtype: bool
%End
bool deleteFeature( QgsFeatureId fid ); bool deleteFeature( QgsFeatureId fid );
%Docstring %Docstring

View File

@ -7632,7 +7632,7 @@ void QgisApp::mergeSelectedFeatures()
vl->deleteFeature( *feature_it ); vl->deleteFeature( *feature_it );
} }
vl->addFeature( newFeature, false ); vl->addFeature( newFeature );
vl->endEditCommand(); vl->endEditCommand();
@ -7934,7 +7934,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
// now create new feature using pasted feature as a template. This automatically handles default // now create new feature using pasted feature as a template. This automatically handles default
// values and field constraints // values and field constraints
QgsFeature newFeature = QgsVectorLayerUtils::createFeature( pasteVectorLayer, geom, dstAttr, &context ); QgsFeature newFeature = QgsVectorLayerUtils::createFeature( pasteVectorLayer, geom, dstAttr, &context );
pasteVectorLayer->addFeature( newFeature, false ); pasteVectorLayer->addFeature( newFeature );
newIds << newFeature.id(); newIds << newFeature.id();
++featureIt; ++featureIt;
@ -8131,7 +8131,7 @@ QgsVectorLayer *QgisApp::pasteToNewMemoryVector()
feature.setGeometry( g ); feature.setGeometry( g );
} }
} }
if ( ! layer->addFeatures( features, false ) || !layer->commitChanges() ) if ( ! layer->addFeatures( features ) || !layer->commitChanges() )
{ {
QgsDebugMsg( "Cannot add features or commit changes" ); QgsDebugMsg( "Cannot add features or commit changes" );
delete layer; delete layer;

View File

@ -661,7 +661,7 @@ QgsVectorLayer *QgsOfflineEditing::copyVectorLayer( QgsVectorLayer *layer, sqlit
} }
f.setAttributes( newAttrs ); f.setAttributes( newAttrs );
newLayer->addFeature( f, false ); newLayer->addFeature( f );
emit progressUpdated( featureCount++ ); emit progressUpdated( featureCount++ );
} }
@ -786,7 +786,7 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer *offlineLayer, QgsVec
// respect constraints and provider default values // respect constraints and provider default values
QgsFeature f = QgsVectorLayerUtils::createFeature( remoteLayer, it->geometry(), newAttrs.toMap(), &context ); QgsFeature f = QgsVectorLayerUtils::createFeature( remoteLayer, it->geometry(), newAttrs.toMap(), &context );
remoteLayer->addFeature( f, false ); remoteLayer->addFeature( f );
emit progressUpdated( i++ ); emit progressUpdated( i++ );
} }

View File

@ -955,9 +955,8 @@ QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest &request
return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) ); return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
} }
bool QgsVectorLayer::addFeature( QgsFeature &feature, bool alsoUpdateExtent ) bool QgsVectorLayer::addFeature( QgsFeature &feature )
{ {
Q_UNUSED( alsoUpdateExtent ); // TODO[MD]
if ( !mValid || !mEditBuffer || !mDataProvider ) if ( !mValid || !mEditBuffer || !mDataProvider )
return false; return false;
@ -2559,23 +2558,12 @@ QgsFeatureIterator QgsVectorLayer::selectedFeaturesIterator( QgsFeatureRequest r
return getFeatures( request ); return getFeatures( request );
} }
bool QgsVectorLayer::addFeatures( QgsFeatureList features, bool makeSelected ) bool QgsVectorLayer::addFeatures( QgsFeatureList &features )
{ {
if ( !mEditBuffer || !mDataProvider ) if ( !mEditBuffer || !mDataProvider )
return false; return false;
bool res = mEditBuffer->addFeatures( features ); bool res = mEditBuffer->addFeatures( features );
if ( makeSelected )
{
QgsFeatureIds ids;
for ( QgsFeatureList::iterator iter = features.begin(); iter != features.end(); ++iter )
ids << iter->id();
selectByIds( ids );
}
updateExtents(); updateExtents();
return res; return res;

View File

@ -348,7 +348,7 @@ typedef QList<QgsPointV2> QgsPointSequence;
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified) * TODO QGIS3: Remove virtual from non-inherited methods (like isModified)
* \see QgsVectorLayerUtils() * \see QgsVectorLayerUtils()
*/ */
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator, public QgsFeatureSink
{ {
Q_OBJECT Q_OBJECT
@ -872,12 +872,16 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
return getFeatures( QgsFeatureRequest( rectangle ) ); return getFeatures( QgsFeatureRequest( rectangle ) );
} }
/** Adds a feature /**
\param feature feature to add * Adds a single \a feature to the layer.
\param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents. * Calling this method causes the layer to recalculate it's extents, which can be
\returns True in case of success and False in case of error * expensive. If multiple features are to be added to the layer then it is more
* efficient to call addFeatures(), as addFeatures() will only trigger a single
* layer extent recalculation.
* \see addFeatures()
* \returns true in case of success and false in case of failure
*/ */
bool addFeature( QgsFeature &feature, bool alsoUpdateExtent = true ); bool addFeature( QgsFeature &feature SIP_INOUT ) override;
/** Updates an existing feature. This method needs to query the datasource /** Updates an existing feature. This method needs to query the datasource
on every call. Consider using changeAttributeValue() or on every call. Consider using changeAttributeValue() or
@ -1201,8 +1205,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/ */
bool deleteAttributes( QList<int> attrs ); bool deleteAttributes( QList<int> attrs );
//! Insert a copy of the given features into the layer (but does not commit it) bool addFeatures( QgsFeatureList &features SIP_INOUT ) override;
bool addFeatures( QgsFeatureList features, bool makeSelected = true );
//! Delete a feature from the layer (but does not commit it) //! Delete a feature from the layer (but does not commit it)
bool deleteFeature( QgsFeatureId fid ); bool deleteFeature( QgsFeatureId fid );

View File

@ -60,7 +60,7 @@ bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureReq
const QgsFeatureId fid = f.id(); const QgsFeatureId fid = f.id();
#endif #endif
// paste feature // paste feature
if ( !layer->addFeature( f, false ) ) if ( !layer->addFeature( f ) )
{ {
couldNotWriteCount++; couldNotWriteCount++;
QgsDebugMsg( QString( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) ); QgsDebugMsg( QString( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) );

View File

@ -371,6 +371,11 @@ void QgsRelationEditorWidget::linkFeature()
} }
mRelation.referencingLayer()->addFeatures( newFeatures ); mRelation.referencingLayer()->addFeatures( newFeatures );
QgsFeatureIds ids;
Q_FOREACH ( const QgsFeature &f, newFeatures )
ids << f.id();
mRelation.referencingLayer()->selectByIds( ids );
updateUi(); updateUi();
} }

View File

@ -68,7 +68,7 @@ static QgsVectorLayer *make_layer( const QStringList &wkts )
Q_FOREACH ( const QString &wkt, wkts ) Q_FOREACH ( const QString &wkt, wkts )
{ {
QgsFeature f( make_feature( wkt ) ); QgsFeature f( make_feature( wkt ) );
vl->addFeature( f, false ); vl->addFeature( f );
} }
vl->commitChanges(); vl->commitChanges();