Skip to content

Commit 0501fab

Browse files
authored
Merge pull request #260 from dstansby/valid-layer-selection
Only update layers when selection is valid
2 parents a70cd37 + 25b9b0e commit 0501fab

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

docs/changelog.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
2.0.3
5+
-----
6+
Bug fixes
7+
~~~~~~~~~
8+
- Fix an error that happened when the histogram widget was open, but a layer that doesn't support
9+
histogramming (e.g., a labels layer) was selected.
10+
411
2.0.2
512
-----
613
Dependencies

src/napari_matplotlib/base.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,24 @@ def _setup_callbacks(self) -> None:
224224
self._update_layers
225225
)
226226

227+
@property
228+
def _valid_layer_selection(self) -> bool:
229+
"""
230+
Return `True` if layer selection is valid.
231+
"""
232+
return self.n_selected_layers in self.n_layers_input and all(
233+
isinstance(layer, self.input_layer_types) for layer in self.layers
234+
)
235+
227236
def _update_layers(self, event: napari.utils.events.Event) -> None:
228237
"""
229238
Update the ``layers`` attribute with currently selected layers and re-draw.
230239
"""
231240
self.layers = list(self.viewer.layers.selection)
232241
self.layers = sorted(self.layers, key=lambda layer: layer.name)
233242
self.on_update_layers()
234-
self._draw()
243+
if self._valid_layer_selection:
244+
self._draw()
235245

236246
def _draw(self) -> None:
237247
"""
@@ -243,10 +253,7 @@ def _draw(self) -> None:
243253
with mplstyle.context(self.napari_theme_style_sheet):
244254
# everything should be done in the style context
245255
self.clear()
246-
if self.n_selected_layers in self.n_layers_input and all(
247-
isinstance(layer, self.input_layer_types)
248-
for layer in self.layers
249-
):
256+
if self._valid_layer_selection:
250257
self.draw()
251258
self.canvas.draw() # type: ignore[no-untyped-call]
252259

src/napari_matplotlib/histogram.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ def on_update_layers(self) -> None:
5555
Called when the selected layers are updated.
5656
"""
5757
super().on_update_layers()
58-
for layer in self.viewer.layers:
59-
layer.events.contrast_limits.connect(self._update_contrast_lims)
58+
if self._valid_layer_selection:
59+
self.layers[0].events.contrast_limits.connect(
60+
self._update_contrast_lims
61+
)
6062

6163
def _update_contrast_lims(self) -> None:
6264
for lim, line in zip(
@@ -209,10 +211,12 @@ def draw(self) -> None:
209211
# get the colormap from the layer depending on its type
210212
if isinstance(self.layers[0], napari.layers.Points):
211213
colormap = self.layers[0].face_colormap
212-
self.layers[0].face_color = self.x_axis_key
214+
if self.x_axis_key:
215+
self.layers[0].face_color = self.x_axis_key
213216
elif isinstance(self.layers[0], napari.layers.Vectors):
214217
colormap = self.layers[0].edge_colormap
215-
self.layers[0].edge_color = self.x_axis_key
218+
if self.x_axis_key:
219+
self.layers[0].edge_color = self.x_axis_key
216220
else:
217221
colormap = None
218222

0 commit comments

Comments
 (0)