source: trunk/src/uigetfile_uvmat.m @ 611

Last change on this file since 611 was 611, checked in by sommeria, 11 years ago

problem of time display repaired

File size: 12.6 KB
RevLine 
[606]1%'uigetfile_uvmat': browser faster than the Matlab fct uigetfile
2%------------------------------------------------------------------------
3% hfig=uigetfile_uvmat(OutputDir,option)
4%
5% OUTPUT:
[609]6% fileinput: detected file name, including path
[606]7%
8% INPUT:
[609]9% title: = displayed title, 'status': display advancement of a series calculation
[606]10% InputDir: directory to browse at first display
11
[609]12function fileinput=uigetfile_uvmat(title,InputName)
13fileinput=''; %default file selection
[610]14if strcmp(title,'status_display')
15    option='status_display';
[609]16else
17    option='browser';
[606]18end
[610]19if exist(InputName,'dir')
20    InputDir=InputName;
21      InputFileName='';
22elseif exist(InputName,'file')
[609]23    [InputDir,InputFileName,Ext]=fileparts(InputName);
[610]24    if isempty(InputFileName)% if InputName is already the root
25        InputFileName=InputDir;
26        if  ~isempty(strcmp (computer, {'PCWIN','PCWIN64'}))%case of Windows systems
27            InputDir=[InputDir '\'];% append '\' for a correct action of dir
28            InputFileName=[InputFileName '\'];
29        end
30    end
[609]31    if isdir(InputName)
[610]32        InputFileName=['+/' InputFileName Ext];
[609]33    end
34else
35    InputDir=pwd;%look in the current work directory if the input file does not exist
36    InputFileName='';
37end
38hfig=findobj(allchild(0),'tag',option);
[606]39if isempty(hfig)
[609]40    set(0,'Unit','points')
[606]41    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
[609]42    Width=350;% fig width in points (1/72 inch)
43    Height=min(0.8*ScreenSize(4),500);
44    Left=ScreenSize(3)- Width-40; %right edge close to the right, with margin=40
45    Bottom=ScreenSize(4)-Height-40; %put fig at top right
46    hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Unit','points','Position',[Left,Bottom,Width,Height],'UserData',InputDir);
[611]47    BackgroundColor=get(hfig,'Color');
48    uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.97 0.5 0.03],'BackgroundColor',BackgroundColor,...
49            'String','path:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
50    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.89 0.9 0.08],'tag','titlebox','Max',2,'BackgroundColor',[1 1 1],...
51        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold');
52%     uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.85 0.5 0.03],'BackgroundColor',BackgroundColor,...
53%             'String','first modified:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','right');
54%     uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.82 0.5 0.03],'BackgroundColor',BackgroundColor,...
55%             'String','last modified:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','right');
56    uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.08 0.9 0.66], 'Callback', @(src,event)view_file(option,src,event),'tag','list',...
[609]57        'FontUnits','points','FontSize',12);
[611]58    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
[609]59        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
[611]60    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.005 0.2 0.07],'Callback',@refresh_GUI,...
[609]61        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
[606]62    %set(hrefresh,'UserData',StatusData)
[610]63    if strcmp(option,'status_display') %put a run advancement display
[609]64        set(hfig,'DeleteFcn',@stop_status)
[606]65        uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.81 0.01 0.05],'BackgroundColor',[1 0 0],'tag','waitbar');
66        uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
[609]67    else  %put a title and additional pushbuttons
[611]68        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.8 0.03],'BackgroundColor',BackgroundColor,...
[609]69            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[611]70        uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.05 0.75 0.1 0.07],...
[609]71            'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward);
[611]72        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],...
[609]73            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
[606]74    end
75    drawnow
76end
[610]77refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir 
78if ~strcmp(option,'status_display') 
[609]79    uiwait(hfig)
[611]80    htitlebox=findobj(hfig,'Tag','titlebox');
81    fileinput=get(htitlebox,'String');% retrieve the input file selection
[610]82    delete(hfig)
[609]83end
84
[610]85
86
[606]87%------------------------------------------------------------------------   
[610]88% --- launched by refreshing the display figure
89function refresh_GUI(hObject,InputFileName)
90%------------------------------------------------------------------------
91if ~exist('InputFileName','var')
92    InputFileName='';
93end
94hfig=get(hObject,'parent');
[611]95htitlebox=findobj(hfig,'tag','titlebox');
96DirName=get(htitlebox,'String');
[610]97ListFiles=list_files(DirName);% list the directory content
98hlist=findobj(hfig,'tag','list');% find the list object
99set(hlist,'String',ListFiles)
100Value=[];
101if ~isempty(InputFileName)
102    Value=find(strcmp(InputFileName,ListFiles));
103end
104if isempty(Value)
105    Value=1;
106end
107set(hlist,'Value',Value)
108
109if strcmp(get(hfig,'Tag'),'status_display')
110   
111    hseries=findobj(allchild(0),'tag','series');
112    hstatus=findobj(hseries,'tag','status_display');
113    StatusData=get(hstatus,'UserData');
114    TimeStart=0;
115    if isfield(StatusData,'TimeStart')
116        TimeStart=StatusData.TimeStart;
117    end
118   
119   
[611]120
[610]121    hlist=findobj(hfig,'tag','list');
[611]122   % OutputDir=get(hfig,'UserData');
[610]123   
124    testrecent=0;
125    datnum=zeros(numel(ListDisplay),1);
126    for ilist=1:numel(ListDisplay)
127        ListDisplay{ilist}=ListFiles(ilist).name;
128        if ListFiles(ilist).isdir
129            ListDisplay{ilist}=['/' ListDisplay{ilist}];
130        elseif isfield(ListFiles(ilist),'datenum')
131            datnum(ilist)=ListFiles(ilist).datenum;%only available in recent matlab versions
132            testrecent=1;
133            if datnum(ilist)<TimeStart
134                ListDisplay{ilist}=[ListDisplay{ilist} '  --OLD--'];
135            end
136        end
137    end
138   
139    %% Look at date of creation
140    ListDisplay=ListDisplay(datnum~=0);
141    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
142   
143    NbOutputFile=[];
144    if isempty(datnum)
145        if testrecent
146            message='no result created yet';
147        else
148            message='';
149        end
150    else
151        [first,indfirst]=min(datnum);
152        [last,indlast]=max(datnum);
153        NbOutputFile_str='?';
154        if isfield(StatusData,'NbOutputFile')
155            NbOutputFile=StatusData.NbOutputFile;
156            NbOutputFile_str=num2str(NbOutputFile);
157        end
158        message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification:  ' ListDisplay{indfirst} ' : ' datestr(first)];...
159            ['latest modification:  ' ListDisplay{indlast} ' : ' datestr(last)]};
160    end
161    set(htitlebox,'String', [DirName{1};message])
162   
163    %% update the waitbar
164    hwaitbar=findobj(hfig,'tag','waitbar');
165    if ~isempty(NbOutputFile)
166        BarPosition=get(hwaitbar,'Position');
167        BarPosition(3)=0.9*numel(datnum)/NbOutputFile;
168        set(hwaitbar,'Position',BarPosition)
169    end
170end
171
172%------------------------------------------------------------------------   
[609]173% --- launched by selecting an item on the file list
[606]174function view_file(option,hObject,event)
175%------------------------------------------------------------------------
176list=get(hObject,'String');
177index=get(hObject,'Value');
[609]178hfig=get(hObject,'parent');%handle of the fig
[611]179htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
180DirName=get(htitlebox,'String');
[609]181SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
182if strcmp(SelectName,'..')% the upward dir option has been selected
[606]183    FullSelectName=fileparts(DirName);
184else
[609]185    %ind_dot=regexp(SelectName,'\.\.\.');
186    % if ~isempty(ind_dot)
187    %     SelectName=SelectName(1:ind_dot-1);
188    % end
189    FullSelectName=fullfile(DirName,SelectName);
[606]190end
191if exist(FullSelectName,'dir')% a directory has been selected
192%     ListFiles=dir(FullSelectName);
193%     ListDisplay=cell(numel(ListFiles),1);
194%     for ilist=2:numel(ListDisplay)% suppress the first line '.'
195%         ListDisplay{ilist-1}=ListFiles(ilist).name;
196%     end
197%     set(hObject,'Value',1)
198%     set(hObject,'String',ListDisplay)
199%     if strcmp(selectname,'..')
200%         FullSelectName=fileparts(fileparts(FullSelectName));
201%     end
[609]202    hbackward=findobj(hfig,'Tag','backward');
203    set(hbackward,'UserData',DirName); %store the current dir for future backward action
[606]204    ListFiles=list_files(FullSelectName);
205    set(hObject,'Value',1)
206    set(hObject,'String',ListFiles)
[611]207    set(htitlebox,'String',FullSelectName)% record the new dir name
[606]208elseif exist(FullSelectName,'file')%visualise the field if it exists
209    FileType=get_file_type(FullSelectName);
210    if strcmp(FileType,'txt')
211        edit(FullSelectName)
212    elseif strcmp(FileType,'xml')
213        editxml(FullSelectName)
214            elseif strcmp(FileType,'figure')
215        open(FullSelectName)
216    else
217        %uvmat(FullSelectName);
218        switch option
[609]219            case 'browser'
[611]220        set(htitlebox,'String',FullSelectName);
[606]221        uiresume(hfig)
[610]222            case 'status_display'
[606]223           uvmat(FullSelectName);
224        end
225    end
226end
227
[610]228%-------------------------------------------------------------------------   
229% list the content of a directory
[611]230function [ListFiles,ListDates]=list_files(DirName)
[610]231%-------------------------------------------------------------------------
232ListStruct=dir(DirName);% get structure of the current directory
[611]233if numel(ListStruct)<1  % case of empty dir
[610]234    ListFiles={};
[611]235    ListDates={};
[610]236    return
237end
[611]238% if strcmp(ListStruct(1).name,'.')
239%     ListStruct(1)=[];%removes the first line ='.'
240% end
[610]241ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
242ListFiles=ListCells(1,:);%list of file names
[611]243check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
244ListDates=cell2mat(ListCells(5,:));%list of numerical dates
[610]245ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
[611]246ListDates(check_dir)=0; % we set the dir dates to 0
247[tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
[610]248ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
[611]249ListDates=ListDates(index_sort);% sort the corresponding dates
[610]250cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
251check_keep=cellfun('isempty', cell_remove);               
252ListFiles=[{'+/..'} ListFiles(check_keep)];
[611]253ListDates=[0 ListDates(check_keep)];
[610]254
[606]255%------------------------------------------------------------------------   
256% --- launched by selecting home
257function home_dir(hObject,event)
[609]258%------------------------------------------------------------------------
[606]259DirName=pwd;
260ListFiles=list_files(DirName);% list the directory content
261hfig=get(hObject,'parent');
[611]262htitlebox=findobj(hfig,'Tag','titlebox');
263set(htitlebox,'String',DirName)% record the new dir name
264hlist=findobj(hfig,'tag','list');% find the list object
[606]265set(hlist,'String',ListFiles)
266%------------------------------------------------------------------------
267
268%------------------------------------------------------------------------   
[609]269% --- launched by pressing the backward (<--) button
270function backward(hObject,event)
271%------------------------------------------------------------------------
272PrevDir=get(hObject,'UserData');
273if ~isempty(PrevDir)
274hfig=get(hObject,'parent');
275htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
276set(htitlebox,'String',PrevDir)
277refresh_GUI(findobj(hfig,'Tag','refresh'))
278end
279
[606]280%-------------------------------------------------------------------------   
[609]281% launched by deleting the status figure (only used in mode series status')
282function close(option,hObject, eventdata)
[606]283%-------------------------------------------------------------------------
[610]284if strcmp(option,'status_display')
[609]285    hseries=findobj(allchild(0),'tag','series');
[610]286    hstatus=findobj(hfig,'Tag','status_display');
[609]287    set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
288    set(hhciv.status,'BackgroundColor',[0 1 0])
289end
[606]290delete(gcbf)
291
Note: See TracBrowser for help on using the repository browser.