Navigation Menu

Skip to content

Commit

Permalink
Add isPassthrough() to edit buffer classes
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Apr 6, 2021
1 parent b013991 commit c824e4d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
Expand Up @@ -212,6 +212,14 @@ Returns ``True`` if the specified feature ID has been deleted but not committed.
Updates ``fields`` Updates ``fields``
%End %End


virtual bool isPassthrough();
%Docstring
Returns ``True`` if the edit buffer makes direct changes to the data provider, such in case of transactions.
Subclasses may override this method to indicate that they support this behavior, the default implementation returns ``False``.

.. versionadded:: 3.18
%End



protected slots: protected slots:
void undoIndexChanged( int index ); void undoIndexChanged( int index );
Expand Down
Expand Up @@ -40,6 +40,8 @@ class QgsVectorLayerEditPassthrough : QgsVectorLayerEditBuffer


virtual bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() ); virtual bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() );


virtual bool isPassthrough();



virtual bool changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues ); virtual bool changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues );


Expand Down
18 changes: 9 additions & 9 deletions src/core/vector/qgsvectorlayer.cpp
Expand Up @@ -877,7 +877,7 @@ QgsRectangle QgsVectorLayer::extent() const
} }


if ( !mEditBuffer || if ( !mEditBuffer ||
( !mDataProvider->transaction() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) || ( !mEditBuffer->isPassthrough() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) ||
QgsDataSourceUri( mDataProvider->dataSourceUri() ).useEstimatedMetadata() ) QgsDataSourceUri( mDataProvider->dataSourceUri() ).useEstimatedMetadata() )
{ {
mDataProvider->updateExtents(); mDataProvider->updateExtents();
Expand All @@ -890,7 +890,7 @@ QgsRectangle QgsVectorLayer::extent() const
rect.combineExtentWith( r ); rect.combineExtentWith( r );
} }


if ( mEditBuffer && !mDataProvider->transaction() ) if ( mEditBuffer && !mEditBuffer->isPassthrough() )
{ {
const auto addedFeatures = mEditBuffer->addedFeatures(); const auto addedFeatures = mEditBuffer->addedFeatures();
for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it ) for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
Expand Down Expand Up @@ -3367,13 +3367,13 @@ long QgsVectorLayer::featureCount() const
if ( ! mDataProvider ) if ( ! mDataProvider )
return -1; return -1;
return mDataProvider->featureCount() + return mDataProvider->featureCount() +
( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures().size() - mEditBuffer->deletedFeatureIds().size() : 0 ); ( mEditBuffer && ! mEditBuffer->isPassthrough() ? mEditBuffer->addedFeatures().size() - mEditBuffer->deletedFeatureIds().size() : 0 );
} }


QgsFeatureSource::FeatureAvailability QgsVectorLayer::hasFeatures() const QgsFeatureSource::FeatureAvailability QgsVectorLayer::hasFeatures() const
{ {
const QgsFeatureIds deletedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->deletedFeatureIds() : QgsFeatureIds() ); const QgsFeatureIds deletedFeatures( mEditBuffer && ! mEditBuffer->isPassthrough() ? mEditBuffer->deletedFeatureIds() : QgsFeatureIds() );
const QgsFeatureMap addedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures() : QgsFeatureMap() ); const QgsFeatureMap addedFeatures( mEditBuffer && ! mEditBuffer->isPassthrough() ? mEditBuffer->addedFeatures() : QgsFeatureMap() );


if ( mEditBuffer && !deletedFeatures.empty() ) if ( mEditBuffer && !deletedFeatures.empty() )
{ {
Expand Down Expand Up @@ -3457,7 +3457,7 @@ bool QgsVectorLayer::rollBack( bool deleteBuffer )
return false; return false;
} }


bool rollbackExtent = !mDataProvider->transaction() && ( !mEditBuffer->deletedFeatureIds().isEmpty() || bool rollbackExtent = !mEditBuffer->isPassthrough() && ( !mEditBuffer->deletedFeatureIds().isEmpty() ||
!mEditBuffer->addedFeatures().isEmpty() || !mEditBuffer->addedFeatures().isEmpty() ||
!mEditBuffer->changedGeometries().isEmpty() ); !mEditBuffer->changedGeometries().isEmpty() );


Expand Down Expand Up @@ -4044,7 +4044,7 @@ QSet<QVariant> QgsVectorLayer::uniqueValues( int index, int limit ) const
{ {
uniqueValues = mDataProvider->uniqueValues( index, limit ); uniqueValues = mDataProvider->uniqueValues( index, limit );


if ( mEditBuffer && ! mDataProvider->transaction() ) if ( mEditBuffer && ! mEditBuffer->isPassthrough() )
{ {
QSet<QString> vals; QSet<QString> vals;
const auto constUniqueValues = uniqueValues; const auto constUniqueValues = uniqueValues;
Expand Down Expand Up @@ -4152,7 +4152,7 @@ QStringList QgsVectorLayer::uniqueStringsMatching( int index, const QString &sub
{ {
results = mDataProvider->uniqueStringsMatching( index, substring, limit, feedback ); results = mDataProvider->uniqueStringsMatching( index, substring, limit, feedback );


if ( mEditBuffer && ! mDataProvider->transaction() ) if ( mEditBuffer && ! mEditBuffer->isPassthrough() )
{ {
QgsFeatureMap added = mEditBuffer->addedFeatures(); QgsFeatureMap added = mEditBuffer->addedFeatures();
QMapIterator< QgsFeatureId, QgsFeature > addedIt( added ); QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
Expand Down Expand Up @@ -4262,7 +4262,7 @@ QVariant QgsVectorLayer::minimumOrMaximumValue( int index, bool minimum ) const
case QgsFields::OriginProvider: //a provider field case QgsFields::OriginProvider: //a provider field
{ {
QVariant val = minimum ? mDataProvider->minimumValue( index ) : mDataProvider->maximumValue( index ); QVariant val = minimum ? mDataProvider->minimumValue( index ) : mDataProvider->maximumValue( index );
if ( mEditBuffer && ! mDataProvider->transaction() ) if ( mEditBuffer && ! mEditBuffer->isPassthrough() )
{ {
QgsFeatureMap added = mEditBuffer->addedFeatures(); QgsFeatureMap added = mEditBuffer->addedFeatures();
QMapIterator< QgsFeatureId, QgsFeature > addedIt( added ); QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
Expand Down
4 changes: 4 additions & 0 deletions src/core/vector/qgsvectorlayereditbuffer.cpp
Expand Up @@ -256,6 +256,10 @@ bool QgsVectorLayerEditBuffer::changeAttributeValue( QgsFeatureId fid, int field
return true; return true;
} }


bool QgsVectorLayerEditBuffer::isPassthrough()
{
return false;
}


bool QgsVectorLayerEditBuffer::addAttribute( const QgsField &field ) bool QgsVectorLayerEditBuffer::addAttribute( const QgsField &field )
{ {
Expand Down
7 changes: 7 additions & 0 deletions src/core/vector/qgsvectorlayereditbuffer.h
Expand Up @@ -189,6 +189,13 @@ class CORE_EXPORT QgsVectorLayerEditBuffer : public QObject
*/ */
void updateFields( QgsFields &fields ); void updateFields( QgsFields &fields );


/**
* Returns TRUE if the edit buffer makes direct changes to the data provider, such in case of transactions.
* Subclasses may override this method to indicate that they support this behavior, the default implementation returns FALSE.
* \since QGIS 3.18
*/
virtual bool isPassthrough();

//QString dumpEditBuffer(); //QString dumpEditBuffer();


protected slots: protected slots:
Expand Down
5 changes: 5 additions & 0 deletions src/core/vector/qgsvectorlayereditpassthrough.cpp
Expand Up @@ -66,6 +66,11 @@ bool QgsVectorLayerEditPassthrough::addFeatures( QgsFeatureList &features )
return true; return true;
} }


bool QgsVectorLayerEditPassthrough::isPassthrough()
{
return true;
}

bool QgsVectorLayerEditPassthrough::deleteFeature( QgsFeatureId fid ) bool QgsVectorLayerEditPassthrough::deleteFeature( QgsFeatureId fid )
{ {
return modify( new QgsVectorLayerUndoPassthroughCommandDeleteFeatures( this, QgsFeatureIds() << fid ) ); return modify( new QgsVectorLayerUndoPassthroughCommandDeleteFeatures( this, QgsFeatureIds() << fid ) );
Expand Down
2 changes: 2 additions & 0 deletions src/core/vector/qgsvectorlayereditpassthrough.h
Expand Up @@ -54,6 +54,7 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
bool deleteFeatures( const QgsFeatureIds &fids ) override; bool deleteFeatures( const QgsFeatureIds &fids ) override;
bool changeGeometry( QgsFeatureId fid, const QgsGeometry &geom ) override; bool changeGeometry( QgsFeatureId fid, const QgsGeometry &geom ) override;
bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() ) override; bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() ) override;
bool isPassthrough() override;


/** /**
* Changes values of attributes (but does not commit it). * Changes values of attributes (but does not commit it).
Expand Down Expand Up @@ -87,6 +88,7 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
// utility function to avoid cpy/paste // utility function to avoid cpy/paste
bool modify( QgsVectorLayerUndoPassthroughCommand *cmd ); bool modify( QgsVectorLayerUndoPassthroughCommand *cmd );



}; };


#endif // QGSVECTORLAYEREDITPASSTHROUGH_H #endif // QGSVECTORLAYEREDITPASSTHROUGH_H

0 comments on commit c824e4d

Please sign in to comment.