Home > rest_20090422 > rest_waitbar.m

rest_waitbar

PURPOSE ^

Parent and child waitbar for REST, by Xiao-Wei Song

SYNOPSIS ^

function rest_waitbar(APercent, AMessage, ATitle, AIsUpdateWho, ANeedCancel, ACancelCallback)

DESCRIPTION ^

Parent and child waitbar for REST, by Xiao-Wei Song
------------------------------------------------------------------------------------------------------------------------------
    Copyright(c) 2007~2010
    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
    Written by Xiao-Wei Song 
    http://resting-fmri.sourceforge.net
------------------------------------------------------------------------------------------------------------------------------
 Parent is outside of the for-loop or outside of sub-function or inside of GUI
 Child is inside of the for-loop or inside of sub-function or outside of GUI
 Example can be checked in reho_gui.m
dawnsong, 20070520
     <a href="Dawnwei.Song@gmail.com">Mail to Author</a>: Xiaowei Song
    Version=1.0;
    Release=20070903;

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

rest_waitbar.m

SOURCE CODE ^

0001 function rest_waitbar(APercent, AMessage, ATitle, AIsUpdateWho, ANeedCancel, ACancelCallback)
0002 %Parent and child waitbar for REST, by Xiao-Wei Song
0003 %------------------------------------------------------------------------------------------------------------------------------
0004 %    Copyright(c) 2007~2010
0005 %    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
0006 %    Written by Xiao-Wei Song
0007 %    http://resting-fmri.sourceforge.net
0008 %------------------------------------------------------------------------------------------------------------------------------
0009 % Parent is outside of the for-loop or outside of sub-function or inside of GUI
0010 % Child is inside of the for-loop or inside of sub-function or outside of GUI
0011 % Example can be checked in reho_gui.m
0012 %dawnsong, 20070520
0013 %     <a href="Dawnwei.Song@gmail.com">Mail to Author</a>: Xiaowei Song
0014 %    Version=1.0;
0015 %    Release=20070903;
0016 
0017 global hParent hChild;
0018 persistent Last_UpdateClock;
0019 if isempty(Last_UpdateClock), 
0020     Last_UpdateClock =clock; 
0021     setappdata(0, 'FlagToRaiseError','no'); %Initialize the error flag
0022 end
0023 
0024 %Great priority, 20070530
0025 if isappdata(0, 'FlagToRaiseError') ...
0026     && strcmpi(getappdata(0, 'FlagToRaiseError'), 'Yes'),
0027     
0028     rmappdata(0, 'FlagToRaiseError');
0029     
0030     error('User canceled the operation!');
0031 end
0032 
0033 if nargin==0
0034     %Close wait bar
0035     if rest_misc( 'ForceCheckExistFigure' , hParent)
0036         close(hParent);
0037     end    
0038     if rest_misc( 'ForceCheckExistFigure' , hChild)
0039         close(hChild);
0040     end
0041     clear hParent hChild;
0042 elseif (nargin==1)||(nargin==3)
0043     %Update current child progress display
0044     if rest_misc( 'ForceCheckExistFigure' , hChild)
0045         if (nargin==1)
0046             rest_progress(APercent,hChild);
0047         elseif nargin==3
0048             rest_progress(APercent,hChild, AMessage, 'Name', ATitle);
0049         end        
0050     else
0051         error('progress bar has not initialized.');
0052     end    
0053 elseif nargin==4 || nargin==5 ||nargin==6
0054     %Full parameters
0055     if nargin==4 
0056         ANeedCancel ='I don''t need';
0057         ACancelCallback ='';
0058     elseif nargin==5         
0059         ACancelCallback ='';
0060     elseif nargin==6
0061         %Do nothing because all parameters I need have been set by the Caller
0062     end
0063     if strcmpi(AIsUpdateWho, 'parent')
0064         GetHandleOrCreate('parent', 'true', ANeedCancel, ACancelCallback);
0065         
0066         rest_progress(APercent,hParent,sprintf('Total progress:\n%s',AMessage), 'Name', ATitle);
0067     elseif strcmpi(AIsUpdateWho, 'child')
0068         GetHandleOrCreate('child', 'true', ANeedCancel, ACancelCallback);
0069         
0070         if rest_misc( 'ForceCheckExistFigure' , hParent)
0071             theMsg =sprintf('Current progress:\n%s',AMessage);
0072         else
0073             theMsg =AMessage;
0074         end
0075         
0076         rest_progress(APercent,hChild, theMsg, 'Name', ATitle);        
0077         if etime(clock, Last_UpdateClock)>1
0078             Last_UpdateClock =clock;
0079             if rest_misc( 'ForceCheckExistFigure' , hParent)
0080                 rest_progress(-1,hParent);    
0081             end                    
0082         end    
0083     end
0084     %Adjust progress window's position to make the child always stay on top of the parent
0085     if rest_misc( 'ForceCheckExistFigure' , hParent) ...
0086         && rest_misc( 'ForceCheckExistFigure' , hChild) ,
0087         theParentPosition =get(hParent, 'Position');
0088         theChildPosition =get(hChild, 'Position');    
0089         theChildPosition(1) =theParentPosition(1);
0090         theChildPosition(2) = theParentPosition(4) +theParentPosition(2) +50;
0091         set(hChild, 'Position', theChildPosition);
0092     end
0093 else
0094     error('Bad call to rest_waitbar(AIsUpdateWho, APercent, AMessage, ATitle)');
0095 end
0096 
0097 
0098 function GetHandleOrCreate(AIsCheckWho, AIsCreate, ANeedCancel, ACancelCallback)
0099     global hParent hChild
0100     
0101     if strcmpi(AIsCheckWho, 'parent')
0102         hParent = findobj(allchild(0),'flat','Tag','DParent_Waitbar');
0103         if isempty(hParent) && strcmpi(AIsCreate, 'true') ...
0104             && ~rest_misc( 'ForceCheckExistFigure' , hParent),
0105             %Check whether to Create a Button to allow cancel current operation
0106             if strcmpi(ANeedCancel,'NeedCancelBtn'),
0107                 hParent=rest_progress(0,'Total Progress','Tag','DParent_Waitbar', ...
0108                             'CreateCancelBtn', @BtnCancelCallback);
0109             else
0110                 hParent=rest_progress(0,'Total Progress','Tag','DParent_Waitbar');
0111             end    
0112         end            
0113     elseif strcmpi(AIsCheckWho, 'child')
0114         hChild  = findobj(allchild(0),'flat','Tag','DChild_Waitbar');
0115         if isempty(hChild) && strcmpi(AIsCreate, 'true') ...
0116             && ~rest_misc( 'ForceCheckExistFigure' , hChild),
0117             %Check whether to Create a Button to allow cancel current operation
0118             if strcmpi(ANeedCancel,'NeedCancelBtn'),
0119                 hChild=rest_progress(0,'Current Progress','Tag','DChild_Waitbar', ...
0120                                 'CreateCancelBtn', @BtnCancelCallback);
0121             else
0122                 hChild=rest_progress(0,'Current Progress','Tag','DChild_Waitbar');
0123             end
0124         end    
0125     end
0126 function BtnCancelCallback(h,varargin)
0127     %I can't throw the exception out because Matlab always catch the error in Controls' Callback.
0128     %Then I can't make the program stop by simply issue a error statement
0129     %So I have to set a flag and check the flag in the next time
0130     % dawnsong ,20070530
0131     setappdata(0, 'FlagToRaiseError',questdlg('Cancel calculating?','Confirm','Cancel'));
0132         
0133     % if strcmpi(questdlg('Cancel calculating?','Confirm','Cancel'), 'yes'),
0134         % Evaluate the user-defined Callback
0135         % fprintf('.');
0136         % error(sprintf('User Canceled the Operation'));
0137     % end

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005