imrect object - delete notification

1 view (last 30 days)
Gerti Tuzi
Gerti Tuzi on 3 Nov 2016
Commented: Gerti Tuzi on 4 Nov 2016
After creating an 'imrect' interactive object, in the context menu there is a delete option. Is there a callback to register when the 'imrect' object is deleted.
What I have so far:
im_rect = imrect(ax, draw_rect);
rectId = addNewPositionCallback(im_rect, @(pos)positionUpdateCallback(pos));
So I have access to the position update callback id. But I don't see how can I register any notifiers for the delete event. There is a 'removeNewPositionCallback', but I don't know whether this callback will be triggered on a delete. I checked both 'imrect' and 'imroi' help sections.
Maybe an alternative would be to add a custom delete command to the context menu, with a callback. Has anyone tried that ?
  2 Comments
Image Analyst
Image Analyst on 4 Nov 2016
Why do you need to use the delete option?
Gerti Tuzi
Gerti Tuzi on 4 Nov 2016
Because it is an interactive tool, for which I need to be notified when the user interacts with it. The drawn box highlights a section of the image. Hence, when the user says "no", I would like my model/data to also reflect that.

Sign in to comment.

Accepted Answer

Adam
Adam on 4 Nov 2016
Edited: Adam on 4 Nov 2016
Unfortunately imrect and its similar functions are rather arcaic and don't really conform to more modern Matlab graphics objects, but it does derive from 'handle' and the handle class includes the following event
ObjectBeingDestroyed
which you can listen to as e.g.
addlistener( im_rect , 'ObjectBeingDestroyed', @(src,evt) disp( 'Rectangle died' ) )
  6 Comments
Adam
Adam on 4 Nov 2016
Edited: Adam on 4 Nov 2016
Can you not catch this under the image load button? If an imrect object is existing and valid when the user loads a new image then do something with the old image to keep it or set some variable so that when the imrect is deleted then you will know whether it was the user deleting it or it being deleted as a result of a new image being loaded.
Gerti Tuzi
Gerti Tuzi on 4 Nov 2016
Adam, please see my answer for a possible solution.

Sign in to comment.

More Answers (1)

Gerti Tuzi
Gerti Tuzi on 4 Nov 2016
For anyone that may need it, I was able to "override" the context's menu "delete" behavior.
1 - Extend "imrect" to your custom class. Note that "imrect" derives from "imroi"
classdef imrectcust < imrect
2 - Copy "imroi"'s 'addRoiDeleteMenuItem(obj, hGroup)' function into your extended class and rename it to something else. Note that this is a static function in "imroi"
3 - Instead of adding a new delete menu item, search 'hMenu''s children for the delete menu item's callback and set it to your desired callback.
function addCustRoiDeleteMenuItem(thisObj, hGroup)
...
% ---- This is code from the original 'addRoiDeleteMenuItem()' ----
% Because the children all have the same context menu handle, grab the
% first if there's more than one.
if iscell(hMenu)
hMenu = hMenu{1};
else
hMenu = hMenu(1);
end
% -------------
% Find the delete item, and over-write the call back method
for ii = 1:length(hMenu.Children)
hm = hMenu.Children(ii);
if(strcmpi(hm.Tag, 'delete cmenu item'))
% 'onUserDelete()' is the custom call back
hm.Callback = @(~,~) onUserDelete(thisObj);
end
end
end
Make sure that in your desired callback you also call your own destructor to retain original "imroi" behavior
function onUserDelete(thisObj)
... handle your custom "delete" behavior business
% Make sure you call your own destructor, which will destroy the object
thisObj.delete();
end
This should also allow you to add additional context menu items in the extended 'imrect' class.

Categories

Find more on Data Type Identification 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!