Skip to content

Commit

Permalink
simplify mouse move callback
Browse files Browse the repository at this point in the history
tooltip does not need to depend on annotation
improve docstrings
  • Loading branch information
alessandrofelder committed Aug 31, 2023
1 parent bf94aec commit d134d0d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 7 additions & 5 deletions brainrender_napari/napari_atlas_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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
):
Expand All @@ -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()
10 changes: 4 additions & 6 deletions tests/test_unit/test_napari_atlas_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand All @@ -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,
Expand All @@ -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() == ""

0 comments on commit d134d0d

Please sign in to comment.