Skip to content
Permalink
Browse files

Add isPassthrough() to edit buffer classes

  • Loading branch information
elpaso committed Apr 6, 2021
1 parent b013991 commit c824e4deaf35ec6a37f6ff2661164fbeb4953fb8
@@ -212,6 +212,14 @@ Returns ``True`` if the specified feature ID has been deleted but not committed.
Updates ``fields``
%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:
void undoIndexChanged( int index );
@@ -40,6 +40,8 @@ class QgsVectorLayerEditPassthrough : QgsVectorLayerEditBuffer

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 );

@@ -877,7 +877,7 @@ QgsRectangle QgsVectorLayer::extent() const
}

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

if ( mEditBuffer && !mDataProvider->transaction() )
if ( mEditBuffer && !mEditBuffer->isPassthrough() )
{
const auto addedFeatures = mEditBuffer->addedFeatures();
for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
@@ -3367,13 +3367,13 @@ long QgsVectorLayer::featureCount() const
if ( ! mDataProvider )
return -1;
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
{
const QgsFeatureIds deletedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->deletedFeatureIds() : QgsFeatureIds() );
const QgsFeatureMap addedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures() : QgsFeatureMap() );
const QgsFeatureIds deletedFeatures( mEditBuffer && ! mEditBuffer->isPassthrough() ? mEditBuffer->deletedFeatureIds() : QgsFeatureIds() );
const QgsFeatureMap addedFeatures( mEditBuffer && ! mEditBuffer->isPassthrough() ? mEditBuffer->addedFeatures() : QgsFeatureMap() );

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

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

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

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

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

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

bool QgsVectorLayerEditBuffer::addAttribute( const QgsField &field )
{
@@ -189,6 +189,13 @@ class CORE_EXPORT QgsVectorLayerEditBuffer : public QObject
*/
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();

protected slots:
@@ -66,6 +66,11 @@ bool QgsVectorLayerEditPassthrough::addFeatures( QgsFeatureList &features )
return true;
}

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

bool QgsVectorLayerEditPassthrough::deleteFeature( QgsFeatureId fid )
{
return modify( new QgsVectorLayerUndoPassthroughCommandDeleteFeatures( this, QgsFeatureIds() << fid ) );
@@ -54,6 +54,7 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
bool deleteFeatures( const QgsFeatureIds &fids ) override;
bool changeGeometry( QgsFeatureId fid, const QgsGeometry &geom ) 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).
@@ -87,6 +88,7 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
// utility function to avoid cpy/paste
bool modify( QgsVectorLayerUndoPassthroughCommand *cmd );


};

#endif // QGSVECTORLAYEREDITPASSTHROUGH_H

0 comments on commit c824e4d

Please sign in to comment.