# Use same mouse event as segment editor paint effect

**URL:** https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306
**Category:** Development
**Created:** [September 16, 2022, 1:44pm UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306 "2022-09-16T13:44:11Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![SCY](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@SCY](https://discourse.slicer.org/u/SCY)
#### Post date: [September 16, 2022, 1:44pm UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/1 "2022-09-16T13:44:11Z")

</div>

Hello everyone,

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:

1. Set the priority of my observers to 2.0
2. Add observers before activating the paint effect
3. Move my observers and callback functions to another thread

Thank you for any help and insight into the reasons behind this.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 16, 2022, 9:00pm UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/2 "2022-09-16T21:00:43Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![SCY](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@SCY](https://discourse.slicer.org/u/SCY)
#### Post date: [September 19, 2022, 7:56am UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/3 "2022-09-19T07:56:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 19, 2022, 1:52pm UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/4 "2022-09-19T13:52:38Z")

</div>

Could you send a link to the source code of your module or send a standalone Python code snippet that reproduces the behavior?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 19, 2022, 8:50pm UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/6 "2022-09-19T20:50:15Z")

</div>

Please provide code that I can copy-paste into the Python console in Slicer and you would expect to work.

---

<div class="post-metadata">

### Author: ![SCY](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@SCY](https://discourse.slicer.org/u/SCY)
#### Post date: [September 19, 2022, 10:17pm UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/7 "2022-09-19T22:17:07Z")

</div>

```auto
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()
rw = lm.sliceWidget("Red").sliceView().renderWindow()
style = rw.GetInteractor().GetInteractorStyle()

style.AddObserver(vtk.vtkCommand.LeftButtonPressEvent, onPress, 10.0)
style.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")

```

After activating the paint effect, onPress isn’t called when left button is pressed, and onRelease is called when left button is double clicked.

Thank you for your help!

---

<div class="post-metadata">

### Author: ![SCY](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@SCY](https://discourse.slicer.org/u/SCY)
#### Post date: [September 25, 2022, 9:15am UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/8 "2022-09-25T09:15:42Z")

</div>

Hi @lassoan, do you have any idea about this? Any thoughts would be helpful. Thank you!

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 28, 2022, 4:05am UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/9 "2022-09-28T04:05:06Z")

</div>

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):

```python
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")

```

---

<div class="post-metadata">

### Author: ![SCY](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@SCY](https://discourse.slicer.org/u/SCY)
#### Post date: [September 29, 2022, 12:36pm UTC](https://discourse.slicer.org/t/use-same-mouse-event-as-segment-editor-paint-effect/25306/10 "2022-09-29T12:36:58Z")

</div>

It works. Thank you for the solution!
