How to prevent accidental deletion of axes by user (select tool and delete key)?

3 views (last 30 days)
Some of our users have been complaining that when modifying/deleting annotations from plots, they inadvertently delete the axes if their selection wasn't precise enough.
I have been trying to intercept the axes delete request in some way to present a confirmation dialog, but have been unsuccessful so far.
  • I have tried the 'DeleteFcn' callback for the axes, but it executes after the object has been removed.
  • I have tried the parent figure's 'KeyPressFcn' callback, but pressing the delete key still deletes the axes prior to callback execution.
Here is some example code:
function testAxesDelete
hs = struct('fig', figure);
hs.fig.MenuBar = 'none';
hs.fig.ToolBar = 'figure';
hs.fig.KeyPressFcn = @myKeyCallback;
hs.ax = plot([0 0], [-1 1]);
hs.ax.DeleteFcn = @myDeleteCallback;
hs.ax.ButtonDownFcn = @myButtonCallback;
end
function myKeyCallback(hobj, event, varargin)
disp('Pressed a key on figure!');
disp('Axes still is deleted if you press delete with the axis selected');
disp('Axis is deleted prior to executing this function')
keyboard
end
function myDeleteCallback(hobj, event, varargin)
disp('Deleting axes!');
disp('Axis is deleted prior to executing this function')
keyboard
end
function myButtonCallback(hobj, event, varargin)
disp('Does not intercept delete key');
keyboard
end
To reproduce the behavior, do the following steps:
  1. Run the testAxesDelete function
  2. Select the "Edit Plot" tool (the mouse arrow cursor in the toolbar)
  3. Click inside the axes (the actual plot area)
  4. Press the delete key (on a Mac, maybe Backspace on a PC or Linux)
The axis will be deleted from the figure and you will be in the keyboard session for myDeleteCallback.
Any help will be greatly appreciated!

Answers (1)

Nick Counts
Nick Counts on 3 Nov 2016
I know it's bad form to answer your own question, but here I go anyway:
I still can't find a way to present our users an option to delete or cancel, but I can prevent them from being able to select the axes in the first place:
By setting the axes property 'HitTest' to 'off' selection and mouse events are effectively disabled. You can still right-click and get the properties options in a context menu, so that functionality is preserved.
Users can still interact with children of the axes and annotations.
All-in-all, it's a solution I'm happy with.
hs.ax.HitTest = 'off'

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!