napari
napari copied to clipboard
add Shape selection event
🚀 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?
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.
I don't actually think one could use the highlight as I also had the case that hovering over a shape highlights the shape. @OnionKiller would you be interested in doing paired coding to implement this?
@melonora Yes, I would happily do the pair programming session. I'll contact you on the Napari Zulip server if thats okay for you.
Great, for the record my name there is wouter-michiel!
Was there any progress on this? I can attest that it would be a useful event. highlight
is okay for now in that it does trigger when the selection changes, it just also triggers at other times. So as long as you have logic to handle that, it's okay.
There is a selection
event as well as a selected_properties
but neither worked for me. They didn't trigger in all cases I needed.