Right-click context menu in slice views

How can I programatically disable (and enable again) the right-click context menu in slice views?

You can configure this at multiple levels. If you just want to disable all actions in all views then you can set an empty list for the allowed context menu action names. If you just want to disable right-click action in certain views then you can remove the right-click GUI event translation (assign the right-click GUI event to vtk.vtkWidgetEvent.NoEvent widget event).

1 Like

I believe the input handling has been considerably reworked lately, I can’t figure out how disabling the context menu in current slicer(main branch) would work (3D view / slice views). I tried via displayable manager, but seems I can’t register vtkCommand::RightButtonPressEvent - my ProcessEvents is never called

I also tried via qSlicerSubjectHierarchyPluginLogic::setAllowedViewContextMenuActionNames, as suggested above, but setting the allowed actions to empty does not seem to work. When I look at the check in buildMenuFromActions, it looks like this:

if (!allowedActions.isEmpty() && !allowedActions.contains(action->objectName()))

and thus considers an empty list as “all actions allowed”, right?

I have now a workaround based on eventFilter - I block the right button pressed/released event for the QVTKOpenGLNativeWidget of the respective 3D / slicer view; this is the best I could come up with so far which works, but maybe someone has a better solution?

I recently had to customize this menu and after trying many things ended up doing this:

  @staticmethod
  def showSubjectHierarchyContextMenuActions(show) -> None:
    """
    Show or hide subject hierarchy context menu actions that are not relevant in custom app mode.
    """
    pluginHandler = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
    viewContextMenuPlugin = pluginHandler.pluginByName('ViewContextMenu')
    viewContextMenuActions = slicer.util.findChildren(viewContextMenuPlugin, className='QAction')
    actionTexts = ["Place", "Adjust window/level", "View transform", "Configure slice view annotations..."]
    for action in viewContextMenuActions:
      if action.text in actionTexts:
        # action.visible = show  #TODO: This does not have effect (shown at context menu building)
        action.enabled = show

Back then I didn’t want to spend time modifying the core, so had to do a workaround… It would make sense to make this possible from the API.