napari / napari

napari: a fast, interactive, multi-dimensional image viewer for python
https://napari.org
BSD 3-Clause "New" or "Revised" License
2.07k stars 410 forks source link

add Shape selection event #6886

Open OnionKiller opened 2 weeks ago

OnionKiller commented 2 weeks ago

🚀 Feature

selected event for shapes in Shape layer

Motivation

When selecting a shape (Poligon, Line, etc.) it is uesful if there is an event which can capture the fact this happend.

Pitch

I am trying to implement a segmentation visualization where the generated segments are displayed as Polygon shapes in a Layer. I would like to create a visualization, which displays specific information of a single Polygon, selected by the user.

Alternatives

Mouse click event, especially the doubleclick event can be captured, and by finding if the ursor position is inside a Shape, te event can be possibly deduced, but it is not ideal.

Additional context

The request was mentioned here originally. I'll try to implement this feature, @brisvag could you please help where to start?

brisvag commented 2 weeks ago

Hi @OnionKiller, thanks for giving it a go! There are a few ways to go about it.

The "best" way (=harder lift and requires more discussion) would be refactor the Shapes layer to use our Selection object just like Points already do (see https://github.com/napari/napari/blob/36bb0d33bea985254fe883d49feefe5fab1f44b7/napari/layers/points/points.py#L543 and all the lines there that refer to self._selected_data). This way you'd contribute to unifying our layers and get "for free" the Shapes.selection.events to connect to.

The quicker way is to instead add an event to here: https://github.com/napari/napari/blob/36bb0d33bea985254fe883d49feefe5fab1f44b7/napari/layers/shapes/shapes.py#L489-L500

and fire it whenever selected_data is changed. In fact, this makes me notice that you can technically use the highlight event as a stand-in for a selection event, now that I think about it... it won't work when the layer invisible maybe, but that probably won't matter.