How to programmatically disable dragging layers?


I would like to ask a question about dragging layers. By default we can drag around any layer loaded into the viewer:

Is it possible to disable/enable programmatically this behavior?

Thank you very much!

here’s one way, there are definitely other better, more “surgical” ways to attack this. but it depends on what your goal is. This function will toggle all mouse move events on the canvas (including, for instance, drawing labels). it’s a quick way to disable dragging layers with minimal code… but

def toggle_drag(viewer):
    canvas_widget = viewer.window.qt_viewer.canvas.native
    if not hasattr(canvas_widget, '_native_mme'):
        # store the original mouseMoveEvent handler
        canvas_widget._native_mme = canvas_widget.mouseMoveEvent
        # override it with a do-nothing function
        canvas_widget.mouseMoveEvent = lambda e: None
        # restore the original mouseMoveEventHandler
        canvas_widget.mouseMoveEvent = canvas_widget._native_mme
        # delete our stored copy (also serves as the flag used above)
        del canvas_widget._native_mme

another possibility, is to set = False  # or True to undo

This will disable both zoom and panning on the canvas (and rotation in 3D), but will keep label editing possible, etc…

again there are many ways to attack this, so if you have more specific needs, just let us know

1 Like

Thank you very much @talley, the solution using the function toggle_drag works just fine!, using this function I was able to disable dragging layers, but it was still possible to zoom in/out and detect click events as I wanted too :wink: