function progressBar(varargin)
% Progress bar for the LTPDA GUI.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This function is called whenever the user starts an analysis by clicking
% on the 'Start' button in the LTPDA GUI.
% It calculates how many blocks in the current Simulink model are
% M-S-function blocks calling 'ltpdasim.m', ie. how many LTPDA analysis
% blocks are present.
% This excludes all 'service' blocks which do not perform any real
% calculation, such as the input 'Object from list' block or the output
% 'Send to output' block: the execution of these blocks is almost immediate
% and there would be non sense in considering them for the sake of the
% 'progress bar'.
%
% Actually, the progress bar do NOT provide an estimate of the remaining
% time, since this would be impossible to achieve (the time required by
% every function/block is different and depending on its relative input,
% so there's no way to estimate it). It only shows how many blocks there
% are, how many have already been executed and how many to go.
% Its primary purpose thus is provide the user with a perception of how the
% analysis is going, and in case which block requires too much time to be
% executed.
%
% $Id: progressBar.m,v 1.8 2008/11/06 10:04:36 nicola Exp $
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  close(findobj('Name','LTPDA Progress Bar'));

  funcBlocks = utils.prog.find_in_models(bdroot,'LookUnderMasks','all','FunctionName','ltpdasim');
     
  screenSize = get(0,'ScreenSize');
  backColor   = [0.98 , 0.98 , 0.98];
  
  loopStatus = getappdata(0,'loopStatus');
  if isempty(loopStatus)
     position   = [(screenSize(3)-400)/2,(screenSize(4)-200)/2,400,180];
     progressWind = figure('Position',position,'Name','LTPDA Progress Bar','Tag','LTPDAProgressBar','Resize','off','NumberTitle','off','Toolbar','none','Menubar','none');

     barBackSize      = [position(3)-20,14];
     barBackDimension = [(position(3)-barBackSize(1))/2 , (position(4)-barBackSize(2))/2-25 , barBackSize];
     progressBack  = axes('Parent',progressWind,'Units','pixels','Position',barBackDimension);
       image(imread('progressbarg.jpg'),'Parent',progressBack);
       axis off;
       barSize      = [1,14];
       barDimension = [(position(3)-barBackSize(1))/2 , (position(4)-barBackSize(2))/2-25 , barSize];
       set(progressBack,'HandleVisibility','callback');
     progressBar   = axes('Parent',progressWind,'Units','pixels','Position',barDimension);
       image(imread('progressbar.jpg'),'Parent',progressBar);
       set(progressBar,'Tag','progressaxes')
       set(progressBar,'UserData',barBackDimension)
       axis off;
       set(progressBar,'HandleVisibility','callback');
       set(gcf,'NextPlot','new');

     % Total number of blocks
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 position(4)-40 position(3) 20],'String',['Total number of blocks to compute: ',num2str(numel(funcBlocks))],'FontName','Arial','FontSize',9,'FontWeight','normal','Visible','on','Tag','blockstotal','UserData',numel(funcBlocks),'Style','text');
     % Currently executed block
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 position(4)-85 position(3) 35],'String','','FontName','Arial','FontSize',9,'FontWeight','bold','Visible','on','Tag','currentexec','Style','text');
     % Blocks done
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','left','Position',[10 (position(4)-barBackSize(2))/2-5 60 14],'String','Done: 0','FontName','Arial','FontSize',8,'FontWeight','normal','Visible','on','Tag','done','UserData',0,'Style','text');
     % Blocks to go
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','right','Position',[position(3)-60-10 (position(4)-barBackSize(2))/2-5 60 14],'String',['To go: ',num2str(numel(funcBlocks))],'FontName','Arial','FontSize',8,'FontWeight','normal','Visible','on','Tag','togo','UserData',numel(funcBlocks),'Style','text');
     % Press X to stop
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 20 position(3) 20],'String','Press X to stop the analysis','FontName','Arial','FontSize',9,'FontWeight','normal','Visible','on','Style','text');
     
  else % loop execution
     position   = [(screenSize(3)-400)/2,(screenSize(4)-320)/2,400,300];
     progressWind = figure('Position',position,'Name','LTPDA Progress Bar','Tag','LTPDAProgressBar','Resize','off','NumberTitle','off','Toolbar','none','Menubar','none');

     barBackSize      = [position(3)-20,14];
     barBackDimension = [(position(3)-barBackSize(1))/2 , (position(4)-barBackSize(2))/2+35 , barBackSize];
     progressBack  = axes('Parent',progressWind,'Units','pixels','Position',barBackDimension);
       image(imread('progressbarg.jpg'),'Parent',progressBack);
       axis off;
       barSize      = [1,14];
       set(progressBack,'HandleVisibility','callback');
     barDimension = [(position(3)-barBackSize(1))/2 , (position(4)-barBackSize(2))/2+35 , barSize];
     progressBar   = axes('Parent',progressWind,'Units','pixels','Position',barDimension);
       image(imread('progressbar.jpg'),'Parent',progressBar);
       set(progressBar,'Tag','progressaxes')
       set(progressBar,'UserData',barBackDimension)
       axis off;
       set(progressBar,'HandleVisibility','callback');
       
     barBackDimension(2) = (position(4)-barBackSize(2))/2-60;
     barDimension(2)     = (position(4)-barBackSize(2))/2-60;
     progressBack2  = axes('Parent',progressWind,'Units','pixels','Position',barBackDimension);
       image(imread('progressbarg.jpg'),'Parent',progressBack2);
       axis off;
       set(progressBack2,'HandleVisibility','callback');
     progressBarLoop = axes('Parent',progressWind,'Units','pixels','Position',barDimension);
       image(imread('progressbarb.jpg'),'Parent',progressBarLoop);
       set(progressBarLoop,'Tag','progressaxes2')
       set(progressBarLoop,'UserData',barBackDimension)
       axis off;
       set(progressBarLoop,'HandleVisibility','callback');

       set(gcf,'NextPlot','new');
  
     % Total number of blocks
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 position(4)-40 position(3) 20],'String',['Total number of blocks to compute: ',num2str(numel(funcBlocks))],'FontName','Arial','FontSize',9,'FontWeight','normal','Visible','on','Tag','blockstotal','UserData',numel(funcBlocks),'Style','text');
     % Currently executed block
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 position(4)-70 position(3) 20],'String','','FontName','Arial','FontSize',9,'FontWeight','bold','Visible','on','Tag','currentexec','Style','text');
     % Blocks done
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','left','Position',[10 (position(4)-barBackSize(2))/2+50 90 14],'String','Blocks done: 0','FontName','Arial','FontSize',8,'FontWeight','normal','Visible','on','Tag','done','UserData',0,'Style','text');
     % Blocks to go
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','right','Position',[position(3)-60-10 (position(4)-barBackSize(2))/2+50 60 14],'String',['To go: ',num2str(numel(funcBlocks))],'FontName','Arial','FontSize',8,'FontWeight','normal','Visible','on','Tag','togo','UserData',numel(funcBlocks),'Style','text');
     % Total number of loops
       loopStatus  = getappdata(0,'loopStatus');
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 (position(4)-barBackSize(2))/2-15 position(3) 20],'String',['Total number of loops to compute: ',num2str(loopStatus(1))],'FontName','Arial','FontSize',9,'FontWeight','normal','Visible','on','Style','text');
     % Loops done
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','left','Position',[10 (position(4)-barBackSize(2))/2-45 90 14],'String',['Loops done: ',num2str(loopStatus(2))],'FontName','Arial','FontSize',8,'FontWeight','normal','Visible','on','Style','text');
     % Loops to go
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','right','Position',[position(3)-60-10 (position(4)-barBackSize(2))/2-45 60 14],'String',['To go: ',num2str(loopStatus(1)-loopStatus(2))],'FontName','Arial','FontSize',8,'FontWeight','normal','Visible','on','Style','text');
     % Press X to stop
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 10 position(3) 20],'String','Press X to stop the analysis','FontName','Arial','FontSize',9,'FontWeight','normal','Visible','on','Style','text');
     % Estimated time to completion:
       if nargin>0 && ~isempty(varargin{1})
          executionTime = varargin{1};
          time2go = datestr(mean(executionTime)*(loopStatus(1)-loopStatus(2)+1),'MM:SS');
       else
          time2go = '--:--';
       end
       uicontrol('Parent',progressWind,'BackgroundColor',backColor,'HorizontalAlignment','center','Position',[0 40 position(3) 20],'String',['Estimated time to completion: ',time2go],'FontName','Arial','FontSize',9,'FontWeight','normal','Visible','on','Style','text');
     
  end
  

  
  
  
  
  

end
