Skip to content

Commit

Permalink
Timeseries widget: Test if layerChanged is connected before trying to…
Browse files Browse the repository at this point in the history
… disconnect (#63)

* Test if layerChanged is connected before trying to disconnect

* Loop over receivers and try to disconnect n times

* Reconnect on_select in showEvent, after hideEvent
  • Loading branch information
JoerivanEngelen authored Oct 16, 2023
1 parent 36f6f25 commit e0a1722
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions imodqgis/timeseries/timeseries_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
QVBoxLayout,
QWidget,
)
from PyQt5.QtTest import QSignalSpy
from qgis.core import (
QgsCoordinateTransformContext,
QgsMapLayerType,
Expand Down Expand Up @@ -304,10 +305,26 @@ def hideEvent(self, e):
# Explicitly disconnect signal
layer = self.layer_selection.currentLayer()
if (layer is not None) and (layer.type() == QgsMapLayerType.VectorLayer):
layer.selectionChanged.disconnect(self.on_select)

n_receivers = layer.receivers(layer.selectionChanged)
# Methods can be connected multiple times to a signal, therefore
# loop over n_receivers to ensure the signal is fully disconnected.
# If we require more fine-grained control on finding signals:
# https://stackoverflow.com/questions/8166571/how-to-find-if-a-signal-is-connected-to-anything/68621792#68621792
for _ in range(n_receivers):
# Try except to deal with the case where other method than
# on_select is connected to selectionChanged.
try:
layer.selectionChanged.disconnect(self.on_select)
except TypeError:
pass
QWidget.hideEvent(self, e)

def showEvent(self, e):
layer = self.layer_selection.currentLayer()
if (layer is not None) and (layer.type() == QgsMapLayerType.VectorLayer):
layer.selectionChanged.connect(self.on_select)
QWidget.showEvent(self, e)

def clear_plot(self):
self.plot_widget.clear()
self.legend.clear()
Expand Down

0 comments on commit e0a1722

Please sign in to comment.