I used vtkcommand mouse events with the interactor style of three render windows (Red, Green, and Yellow). These mouse events triggered my custom callback functions. Everything worked until I add the segment editor widget and active paint effect. In the code, I added observers to the vtkInteractorStyle object after I activated the paint effect.
Expect to see: Both the paint effect and my callback functions work simultaneously.
Actually see: Only the paint effect and the callback function connected with leftbuttonrelease were called when I double-clicked on the render window.
Some thought:
In the paint effect, it uses the same mouse events as well. This might cause a problem.
Already tried:
Set the priority of my observers to 2.0
Add observers before activating the paint effect
Move my observers and callback functions to another thread
Thank you for any help and insight into the reasons behind this.
If you want to hijack events from the Segment Editor then you need to specify an observer with high priority and return with 0 from your event callback function (which means that the event is not aborted but other observers get a chance to process it).
In qMRMLSegmentEditorWidget.cxx, it sets the priority of the observers to 1.0 in setupViewObservations(). I set mine to any number larger than 1.0 but it still couldn’t hijack the events from the Segment Editor.
Thank you, the script was very helpful. It was almost perfect, the only mistake was that the observer was added to the interactor style, while it should have been added to the interactor (because the interactor style only invokes event if the event is not processed):
import logging
import slicer
import vtk
import SampleData
def onPress(arg1, arg2):
logging.info("Press")
def onRelease(arg1, arg2):
logging.info("Release")
lm = slicer.app.layoutManager()
interactor = lm.sliceWidget("Red").sliceView().interactorStyle().GetInteractor()
interactor.AddObserver(vtk.vtkCommand.LeftButtonPressEvent, onPress, 10.0)
interactor.AddObserver(vtk.vtkCommand.LeftButtonReleaseEvent, onRelease, 10.0)
# Load master volume
sampleDataLogic = SampleData.SampleDataLogic()
masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()
# Create segmentation
segmentationNode = slicer.vtkMRMLSegmentationNode()
slicer.mrmlScene.AddNode(segmentationNode)
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
# Create segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
# To show segment editor widget (useful for debugging): segmentEditorWidget.show()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
# Run segmentation
segmentEditorWidget.setActiveEffectByName("Paint")