Skip to content

Commit b340d7b

Browse files
authored
Add willBeDeleted() signal to QgsMapLayer (#3998)
This allows pieces of code that depend on map layers to listen directly to the layer's notification rather than having to listen to project's layersWillBeRemoved signal (and cycle through the whole list)
1 parent 6d23b04 commit b340d7b

6 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎python/core/qgsmaplayer.sip‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,14 @@ class QgsMapLayer : QObject
743743
*/
744744
void dependenciesChanged();
745745

746+
/**
747+
* Emitted in the destructor when the layer is about to be deleted,
748+
* but it is still in a perfectly valid state: the last chance for
749+
* other pieces of code for some cleanup if they use the layer.
750+
* @note added in QGIS 3.0
751+
*/
752+
void willBeDeleted();
753+
746754
protected:
747755
/** Set the extent */
748756
virtual void setExtent( const QgsRectangle &rect );

‎src/core/qgsmaplayer.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,14 @@ class CORE_EXPORT QgsMapLayer : public QObject
778778
*/
779779
void dependenciesChanged();
780780

781+
/**
782+
* Emitted in the destructor when the layer is about to be deleted,
783+
* but it is still in a perfectly valid state: the last chance for
784+
* other pieces of code for some cleanup if they use the layer.
785+
* @note added in QGIS 3.0
786+
*/
787+
void willBeDeleted();
788+
781789
protected:
782790
//! Set the extent
783791
virtual void setExtent( const QgsRectangle &rect );

‎src/core/qgspluginlayer.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ QgsPluginLayer::QgsPluginLayer( const QString& layerType, const QString& layerNa
2424
setLegend( QgsMapLayerLegend::defaultPluginLegend( this ) );
2525
}
2626

27+
QgsPluginLayer::~QgsPluginLayer()
28+
{
29+
// TODO: shall we move the responsibility of emitting the signal to plugin
30+
// layer implementations before they start doing their part of cleanup...?
31+
emit willBeDeleted();
32+
}
33+
2734
QString QgsPluginLayer::pluginLayerType()
2835
{
2936
return mPluginLayerType;

‎src/core/qgspluginlayer.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CORE_EXPORT QgsPluginLayer : public QgsMapLayer
3434

3535
public:
3636
QgsPluginLayer( const QString& layerType, const QString& layerName = QString() );
37+
~QgsPluginLayer();
3738

3839
//! Return plugin layer type (the same as used in QgsPluginLayerRegistry)
3940
QString pluginLayerType();

‎src/core/qgsvectorlayer.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ QgsVectorLayer::QgsVectorLayer( const QString& vectorLayerPath,
172172

173173
QgsVectorLayer::~QgsVectorLayer()
174174
{
175+
emit willBeDeleted();
175176

176177
mValid = false;
177178

‎src/core/raster/qgsrasterlayer.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ QgsRasterLayer::QgsRasterLayer( const QString & uri,
170170

171171
QgsRasterLayer::~QgsRasterLayer()
172172
{
173+
emit willBeDeleted();
174+
173175
mValid = false;
174176
// Note: provider and other interfaces are owned and deleted by pipe
175177
}

0 commit comments

Comments
 (0)