Skip to content

Commit

Permalink
Merge pull request #260 from dstansby/valid-layer-selection
Browse files Browse the repository at this point in the history
Only update layers when selection is valid
  • Loading branch information
dstansby authored Jul 12, 2024
2 parents a70cd37 + 25b9b0e commit 0501fab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

2.0.3
-----
Bug fixes
~~~~~~~~~
- Fix an error that happened when the histogram widget was open, but a layer that doesn't support
histogramming (e.g., a labels layer) was selected.

2.0.2
-----
Dependencies
Expand Down
17 changes: 12 additions & 5 deletions src/napari_matplotlib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,24 @@ def _setup_callbacks(self) -> None:
self._update_layers
)

@property
def _valid_layer_selection(self) -> bool:
"""
Return `True` if layer selection is valid.
"""
return self.n_selected_layers in self.n_layers_input and all(
isinstance(layer, self.input_layer_types) for layer in self.layers
)

def _update_layers(self, event: napari.utils.events.Event) -> None:
"""
Update the ``layers`` attribute with currently selected layers and re-draw.
"""
self.layers = list(self.viewer.layers.selection)
self.layers = sorted(self.layers, key=lambda layer: layer.name)
self.on_update_layers()
self._draw()
if self._valid_layer_selection:
self._draw()

def _draw(self) -> None:
"""
Expand All @@ -243,10 +253,7 @@ def _draw(self) -> None:
with mplstyle.context(self.napari_theme_style_sheet):
# everything should be done in the style context
self.clear()
if self.n_selected_layers in self.n_layers_input and all(
isinstance(layer, self.input_layer_types)
for layer in self.layers
):
if self._valid_layer_selection:
self.draw()
self.canvas.draw() # type: ignore[no-untyped-call]

Expand Down
12 changes: 8 additions & 4 deletions src/napari_matplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ def on_update_layers(self) -> None:
Called when the selected layers are updated.
"""
super().on_update_layers()
for layer in self.viewer.layers:
layer.events.contrast_limits.connect(self._update_contrast_lims)
if self._valid_layer_selection:
self.layers[0].events.contrast_limits.connect(
self._update_contrast_lims
)

def _update_contrast_lims(self) -> None:
for lim, line in zip(
Expand Down Expand Up @@ -209,10 +211,12 @@ def draw(self) -> None:
# get the colormap from the layer depending on its type
if isinstance(self.layers[0], napari.layers.Points):
colormap = self.layers[0].face_colormap
self.layers[0].face_color = self.x_axis_key
if self.x_axis_key:
self.layers[0].face_color = self.x_axis_key
elif isinstance(self.layers[0], napari.layers.Vectors):
colormap = self.layers[0].edge_colormap
self.layers[0].edge_color = self.x_axis_key
if self.x_axis_key:
self.layers[0].edge_color = self.x_axis_key
else:
colormap = None

Expand Down

0 comments on commit 0501fab

Please sign in to comment.