imrect object - delete notification

조회 수: 6 (최근 30일)
Gerti Tuzi
Gerti Tuzi 2016년 11월 3일
댓글: Gerti Tuzi 2016년 11월 4일
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
Image Analyst
Image Analyst 2016년 11월 4일
Why do you need to use the delete option?
Gerti Tuzi
Gerti Tuzi 2016년 11월 4일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam
Adam 2016년 11월 4일
편집: Adam 2016년 11월 4일
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
Adam
Adam 2016년 11월 4일
편집: Adam 2016년 11월 4일
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 2016년 11월 4일
Adam, please see my answer for a possible solution.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Gerti Tuzi
Gerti Tuzi 2016년 11월 4일
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.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by