From d134d0d63d508df54d7bef22c082c7213c2d81f5 Mon Sep 17 00:00:00 2001 From: alessandrofelder Date: Thu, 31 Aug 2023 11:39:56 +0100 Subject: [PATCH] simplify mouse move callback tooltip does not need to depend on annotation improve docstrings --- brainrender_napari/napari_atlas_representation.py | 12 +++++++----- tests/test_unit/test_napari_atlas_representation.py | 10 ++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/brainrender_napari/napari_atlas_representation.py b/brainrender_napari/napari_atlas_representation.py index 7efb18d..ced235b 100644 --- a/brainrender_napari/napari_atlas_representation.py +++ b/brainrender_napari/napari_atlas_representation.py @@ -19,6 +19,7 @@ class NapariAtlasRepresentation: mesh_blending: str = "translucent_no_depth" def __post_init__(self) -> None: + """Setup a custom QLabel tooltip and enable napari layer tooltips""" self._tooltip = QLabel(self.viewer.window.qt_viewer.parent()) self._tooltip.setWindowFlags( Qt.Window | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint @@ -32,7 +33,7 @@ def __post_init__(self) -> None: def add_to_viewer(self): """Adds the reference and annotation images as layers to the viewer. - The layers are connected to self._tooltip + The layers are connected to the mouse move callback to set tooltip. The reference image's visibility is off, the annotation's is on. """ reference = self.viewer.add_image( @@ -42,13 +43,13 @@ def add_to_viewer(self): visible=False, ) - self.annotation = self.viewer.add_labels( + annotation = self.viewer.add_labels( self.bg_atlas.annotation, scale=self.bg_atlas.resolution, name=f"{self.bg_atlas.atlas_name}_annotation", ) - self.annotation.mouse_move_callbacks.append(self._on_mouse_move) + annotation.mouse_move_callbacks.append(self._on_mouse_move) reference.mouse_move_callbacks.append(self._on_mouse_move) def add_structure_to_viewer(self, structure_name: str): @@ -86,6 +87,9 @@ def _add_mesh(self, mesh: Mesh, name: str, color=None): self.viewer.add_surface((points, cells), **viewer_kwargs) def add_additional_reference(self, additional_reference_key: str): + """Adds a given additional reference as a layer to the viewer. + and connects it to the mouse move callback to set tooltip. + """ additional_reference = self.viewer.add_image( self.bg_atlas.additional_references[additional_reference_key], scale=self.bg_atlas.resolution, @@ -108,7 +112,6 @@ def _on_mouse_move(self, _, event): ) if ( tooltip_visibility - and self.annotation and np.all(np.array(cursor_position) > 0) and self.viewer.dims.ndisplay == 2 ): @@ -131,6 +134,5 @@ def _on_mouse_move(self, _, event): # cursor position outside the image or in the image background # so no tooltip to be displayed # this saves us a bunch of assertions and extra computation - # TODO assert arguments of key errors/index errors here self._tooltip.setText("") self._tooltip.hide() diff --git a/tests/test_unit/test_napari_atlas_representation.py b/tests/test_unit/test_napari_atlas_representation.py index f3df34f..61870d2 100644 --- a/tests/test_unit/test_napari_atlas_representation.py +++ b/tests/test_unit/test_napari_atlas_representation.py @@ -156,6 +156,7 @@ def test_viewer_tooltip( atlas = BrainGlobeAtlas(atlas_name=atlas_name) atlas_representation = NapariAtlasRepresentation(atlas, viewer) atlas_representation.add_to_viewer() + annotation = viewer.layers[1] event = QMouseEvent( QEvent.MouseMove, @@ -168,9 +169,7 @@ def test_viewer_tooltip( # the napari read-only wrapper around qt events mock_event = mocker.patch.object(event, "pos", return_value=(50, 50)) viewer.cursor.position = cursor_position - atlas_representation._on_mouse_move( - atlas_representation.annotation, mock_event - ) + atlas_representation._on_mouse_move(annotation, mock_event) assert atlas_representation._tooltip.text() == expected_tooltip_text @@ -183,6 +182,7 @@ def test_too_quick_mouse_move_keyerror(make_napari_viewer, mocker): atlas = BrainGlobeAtlas(atlas_name=atlas_name) atlas_representation = NapariAtlasRepresentation(atlas, viewer) atlas_representation.add_to_viewer() + annotation = viewer.layers[1] event = QMouseEvent( QEvent.MouseMove, @@ -204,8 +204,6 @@ def test_too_quick_mouse_move_keyerror(make_napari_viewer, mocker): side_effect=KeyError(), ) - atlas_representation._on_mouse_move( - atlas_representation.annotation, mock_event - ) + atlas_representation._on_mouse_move(annotation, mock_event) mock_structure_from_coords.assert_called_once() assert atlas_representation._tooltip.text() == ""