diff --git a/brainrender_napari/napari_atlas_representation.py b/brainrender_napari/napari_atlas_representation.py index f22518d..13b6306 100644 --- a/brainrender_napari/napari_atlas_representation.py +++ b/brainrender_napari/napari_atlas_representation.py @@ -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 @@ -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() diff --git a/tests/test_unit/test_napari_atlas_representation.py b/tests/test_unit/test_napari_atlas_representation.py index 481d184..0251b96 100644 --- a/tests/test_unit/test_napari_atlas_representation.py +++ b/tests/test_unit/test_napari_atlas_representation.py @@ -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, @@ -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