Skip to content

Commit

Permalink
add test for viewer tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofelder committed Aug 31, 2023
1 parent 98f2ac9 commit f725498
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
13 changes: 8 additions & 5 deletions brainrender_napari/napari_atlas_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ def add_additional_reference(self, additional_reference_key: str):
def _on_mouse_move(self, _, event):
"""Adapts the tooltip according to the cursor position.
No tooltip is displayed if
* the viewer is in 3D display
* or the cursor is outside annotations image
* or the user has chosen to switch off layer tooltips.
The tooltip is only displayed if
* the viewer is in 2D display
* and the cursor is inside the annotation
* and the user has not switched off layer tooltips.
"""
cursor_position = self.viewer.cursor.position
print(type(event))
print(event)
print(event.pos)
napari_settings = get_settings()
tooltip_visibility = (
napari_settings.appearance.layer_tooltip_visibility
Expand All @@ -123,7 +126,7 @@ def _on_mouse_move(self, _, event):
hemisphere = self.bg_atlas.hemisphere_from_coords(
cursor_position, as_string=True, microns=True
).capitalize()
tooltip_text = f"{structure_name} | {hemisphere}."
tooltip_text = f"{structure_name} | {hemisphere}"
self._tooltip.setText(tooltip_text)
self._tooltip.adjustSize()
self._tooltip.show()
Expand Down
37 changes: 37 additions & 0 deletions tests/test_unit/test_napari_atlas_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from bg_atlasapi import BrainGlobeAtlas
from napari.layers import Image, Labels
from numpy import all, allclose
from qtpy.QtCore import QEvent, QPoint, Qt
from qtpy.QtGui import QMouseEvent

from brainrender_napari.napari_atlas_representation import (
NapariAtlasRepresentation,
Expand Down Expand Up @@ -123,3 +125,38 @@ def test_add_additional_reference(make_napari_viewer):
viewer.layers[0].name
== f"{atlas_name}_{additional_reference_name}_reference"
)


@pytest.mark.parametrize(
"cursor_position, expected_tooltip_text",
[
((6500.0, 4298.5, 9057.6), "Caudoputamen | Left"),
((-1000, 0, 0), ""), # outside image
],
)
def test_viewer_tooltip(
make_napari_viewer, mocker, cursor_position, expected_tooltip_text
):
"""Checks that the custom callback for mouse movement sets the expected
tooltip text."""
viewer = make_napari_viewer()
atlas_name = "allen_mouse_100um"
atlas = BrainGlobeAtlas(atlas_name=atlas_name)
atlas_representation = NapariAtlasRepresentation(atlas, viewer)
atlas_representation.add_to_viewer()

event = QMouseEvent(
QEvent.MouseMove,
QPoint(0, 0), # any pos will do to check text
Qt.MouseButton.NoButton,
Qt.MouseButton.NoButton,
Qt.KeyboardModifier.NoModifier,
)
# a slight hacky mock of event.pos to circumvent
# 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
)
assert atlas_representation._tooltip.text() == expected_tooltip_text

0 comments on commit f725498

Please sign in to comment.