Hi everyone.
Just a question: how can I read the mouse event (right click) over a widget and then run a call back?
Is there some function that do it?
thanks
By default, in markup widgets RightButtonClickEvent
interaction event is translated to WidgetEventMenu
widget event, which displays a context menu. You can remove the old mapping and add a new mapping to a custom event using widget.SetEventTranslation
(see examples in the script repository).
For example, you can translate the right-click event to slicer.vtkSlicerMarkupsWidget.WidgetEventCustomAction1
event, which invokes a slicer.vtkMRMLMarkupsDisplayNode.CustomActionEvent1
on the markup’s display node. To get notified about the right-click event you just need to add an observer to this event.
Example:
def someCustomAction(caller, eventId):
markupsDisplayNode = caller
print(f"Custom action activated in {markupsDisplayNode.GetName()}")
views = [
slicer.app.layoutManager().threeDWidget(0).threeDView(),
slicer.app.layoutManager().sliceWidget("Red").sliceView(),
slicer.app.layoutManager().sliceWidget("Yellow").sliceView(),
slicer.app.layoutManager().sliceWidget("Green").sliceView()
]
observations = [] # store the observations so that later can be removed
markupsDisplayNodes = slicer.util.getNodesByClass("vtkMRMLMarkupsDisplayNode")
for markupsDisplayNode in markupsDisplayNodes:
# Add observer to custom actions
observations.append([markupsDisplayNode, markupsDisplayNode.AddObserver(markupsDisplayNode.CustomActionEvent1, someCustomAction)])
for view in views:
markupsDisplayableManager = view.displayableManagerByClassName('vtkMRMLMarkupsDisplayableManager')
# Assign keyboard shortcut to trigger custom actions
widget = markupsDisplayableManager.GetWidget(markupsDisplayNode)
# Remove old event translation
widget.SetEventTranslation(widget.WidgetStateOnWidget, slicer.vtkMRMLInteractionEventData.RightButtonClickEvent, vtk.vtkEvent.NoModifier, vtk.vtkWidgetEvent.NoEvent)
# Add new event translation
widget.SetEventTranslation(widget.WidgetStateOnWidget, slicer.vtkMRMLInteractionEventData.RightButtonClickEvent, vtk.vtkEvent.NoModifier, widget.WidgetEventCustomAction1)
## Remove observations when custom actions are not needed anymore by uncommenting these lines:
# for observedNode, observation in observations:
# observedNode.RemoveObserver(observation)
which widget do you want to react the right click event?
in slicer,it different to react and to hand right click event with different widgets
Hi. It’s the Red widget.
Else, i want to tell you, that its ready a first version of a Leksell path stereotaxia.
It does works fine, but it need some tests. If you hace 2 or 3 different heads-with-frame volumen, please send them to me.
This code started work for only when I added the decorator above the function:
@vtk.calldata_type(vtk.VTK_OBJECT)
def someCustomAction(caller, eventId, callData):