Skip to content

Commit

Permalink
Reload the data source when a dependency layer is changed (#37475)
Browse files Browse the repository at this point in the history
 Refreshing the data provider right away causes problems on a layer
 dependent on itself. In that case two `dataChanged` signals are
 expected to be emitted when deleting or creating a feature, but as the
 layer is reloaded, the second `dataChanged` is not emitted.
  • Loading branch information
suricactus committed Jul 10, 2020
1 parent b6a9cce commit b7d3637
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -2672,6 +2672,13 @@ Emitted before changes are committed to the data provider.
void beforeRollBack();
%Docstring
Emitted before changes are rolled back.
%End

void afterCommitChanges();
%Docstring
Emitted after changes are commited to the data provider.

.. versionadded:: 3.16
%End

void afterRollBack();
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -3367,6 +3367,7 @@ bool QgsVectorLayer::commitChanges()
delete mEditBuffer;
mEditBuffer = nullptr;
undoStack()->clear();
emit afterCommitChanges();
emit editingStopped();
}
else
Expand Down Expand Up @@ -5335,6 +5336,12 @@ void QgsVectorLayer::emitDataChanged()
mDataChangedFired = false;
}

void QgsVectorLayer::onAfterCommitChangesDependency()
{
mDataChangedFired = true;
reload();
}

bool QgsVectorLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
{
QSet<QgsMapLayerDependency> deps;
Expand All @@ -5358,6 +5365,7 @@ bool QgsVectorLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
disconnect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
disconnect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
disconnect( lyr, &QgsVectorLayer::repaintRequested, this, &QgsVectorLayer::triggerRepaint );
disconnect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::onAfterCommitChangesDependency );
}

// assign new dependencies
Expand All @@ -5378,6 +5386,7 @@ bool QgsVectorLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
connect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
connect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
connect( lyr, &QgsVectorLayer::repaintRequested, this, &QgsVectorLayer::triggerRepaint );
connect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::onAfterCommitChangesDependency );
}

// if new layers are present, emit a data change
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -2482,6 +2482,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Emitted before changes are rolled back.
void beforeRollBack();

/**
* Emitted after changes are commited to the data provider.
* \since QGIS 3.16
*/
void afterCommitChanges();

/**
* Emitted after changes are rolled back.
* \since QGIS 3.4
Expand Down Expand Up @@ -2709,6 +2715,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
void onSymbolsCounted();
void onDirtyTransaction( const QString &sql, const QString &name );
void emitDataChanged();
void onAfterCommitChangesDependency();

private:
void updateDefaultValues( QgsFeatureId fid, QgsFeature feature = QgsFeature() );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -872,6 +872,8 @@ void QgsRelationEditorWidget::toggleEditing( bool state )
if ( mNmRelation.isValid() )
mEditorContext.vectorLayerTools()->stopEditing( mNmRelation.referencedLayer() );
}

updateButtons();
}

void QgsRelationEditorWidget::saveEdits()
Expand Down

0 comments on commit b7d3637

Please sign in to comment.