source: trunk/src/uigetfile_uvmat.m @ 635

Last change on this file since 635 was 635, checked in by sommeria, 11 years ago
File size: 14.7 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
[615]19InputDir=pwd;%look in the current work directory if the input file does not exist
20InputFileName='';%default
21if ischar(InputName)
22    if exist(InputName,'dir')
23        InputDir=InputName;
24        InputFileName='';
25    elseif exist(InputName,'file')
26        [InputDir,InputFileName,Ext]=fileparts(InputName);
27        if isempty(InputFileName)% if InputName is already the root
28            InputFileName=InputDir;
29            if  ~isempty(strcmp (computer, {'PCWIN','PCWIN64'}))%case of Windows systems
30                InputDir=[InputDir '\'];% append '\' for a correct action of dir
31                InputFileName=[InputFileName '\'];
32            end
[610]33        end
[615]34        if isdir(InputName)
35            InputFileName=['+/' InputFileName Ext];
36        end
[610]37    end
[609]38end
39hfig=findobj(allchild(0),'tag',option);
[606]40if isempty(hfig)
[609]41    set(0,'Unit','points')
[606]42    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
[609]43    Width=350;% fig width in points (1/72 inch)
44    Height=min(0.8*ScreenSize(4),500);
45    Left=ScreenSize(3)- Width-40; %right edge close to the right, with margin=40
46    Bottom=ScreenSize(4)-Height-40; %put fig at top right
47    hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Unit','points','Position',[Left,Bottom,Width,Height],'UserData',InputDir);
[611]48    BackgroundColor=get(hfig,'Color');
49    uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.97 0.5 0.03],'BackgroundColor',BackgroundColor,...
50            'String','path:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[615]51    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.89 0.9 0.08],'tag','titlebox','Max',2,'BackgroundColor',[1 1 1],'Callback',@titlebox_Callback,...
[611]52        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold');
[612]53    uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.05 0.75 0.1 0.07],...
54            'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward);
55    uicontrol('Style','togglebutton','Units','normalized', 'Position', [0.75 0.75 0.2 0.04],'tag','check_date','Callback',@dates_Callback,...
56            'String','dates','FontUnits','points','FontSize',12,'FontWeight','bold');
57    uicontrol('Style','text','Units','normalized', 'Position', [0.4 0.8 0.35 0.03],'BackgroundColor',BackgroundColor,...
58            'String','sort: ','FontUnits','points','FontSize',12,'FontWeight','bold','HorizontalAlignment','right');
59    uicontrol('Style','popupmenu','Units','normalized', 'Position', [0.75 0.8 0.2 0.04],'tag','sort_option','Callback',@refresh_GUI,'Visible','off',...
60            'String',{'name';'date'},'FontUnits','points','FontSize',12,'FontWeight','bold');   
61    uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.08 0.9 0.66], 'Callback', @(src,event)list_Callback(option,src,event),'tag','list',...
[609]62        'FontUnits','points','FontSize',12);
[611]63    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
[609]64        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
[611]65    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.005 0.2 0.07],'Callback',@refresh_GUI,...
[609]66        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
[606]67    %set(hrefresh,'UserData',StatusData)
[610]68    if strcmp(option,'status_display') %put a run advancement display
[635]69        set(hfig,'DeleteFcn',@(src,event)close(option,src,event))
70              uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.85 0.9 0.04]);
71        uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.85 0.01 0.04],'BackgroundColor',[1 0 0],'tag','waitbar');
72
[609]73    else  %put a title and additional pushbuttons
[612]74        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,...
[609]75            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[612]76
[611]77        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],...
[609]78            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
[606]79    end
80    drawnow
81end
[610]82refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir 
83if ~strcmp(option,'status_display') 
[609]84    uiwait(hfig)
[620]85    if ishandle(hfig)
[611]86    htitlebox=findobj(hfig,'Tag','titlebox');
87    fileinput=get(htitlebox,'String');% retrieve the input file selection
[610]88    delete(hfig)
[620]89    end
[609]90end
91
[615]92%------------------------------------------------------------------------   
93% --- launched by refreshing the display figure
94function titlebox_Callback(hObject,event)
95refresh_GUI(hObject)
96%------------------------------------------------------------------------
[610]97
[606]98%------------------------------------------------------------------------   
[610]99% --- launched by refreshing the display figure
100function refresh_GUI(hObject,InputFileName)
101%------------------------------------------------------------------------
102if ~exist('InputFileName','var')
103    InputFileName='';
104end
105hfig=get(hObject,'parent');
[613]106hlist=findobj(hfig,'tag','list');% find the list object
107set(hlist,'BackgroundColor',[1 1 0])
108drawnow
[611]109htitlebox=findobj(hfig,'tag','titlebox');
110DirName=get(htitlebox,'String');
[612]111hsort_option=findobj(hfig,'tag','sort_option');
[635]112if strcmp(get(hfig,'Tag'),'status_display')
[610]113    hseries=findobj(allchild(0),'tag','series');
[635]114    hstatus=findobj(hseries,'tag','status');
[610]115    StatusData=get(hstatus,'UserData');
116    TimeStart=0;
117    if isfield(StatusData,'TimeStart')
118        TimeStart=StatusData.TimeStart;
[635]119    end
[610]120    hlist=findobj(hfig,'tag','list');
[635]121    testrecent=0;   
[610]122    NbOutputFile=[];
[635]123    if isfield(StatusData,'NbOutputFile')
124        NbOutputFile=StatusData.NbOutputFile;
125        NbOutputFile_str=num2str(NbOutputFile);
[610]126    end
[635]127    [ListFiles,NumFiles]=list_files(DirName,1,TimeStart);% list the directory content
[610]128   
129    %% update the waitbar
130    hwaitbar=findobj(hfig,'tag','waitbar');
131    if ~isempty(NbOutputFile)
132        BarPosition=get(hwaitbar,'Position');
[635]133        BarPosition(3)=0.9*max(0.01,NumFiles/NbOutputFile);% the bar width cannot be set to 0, set to 0.01 instead
[610]134        set(hwaitbar,'Position',BarPosition)
135    end
[635]136else
137    sort_option='name';
138    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
139        sort_option='date';
140    end
141    hcheck_date=findobj(hfig,'tag','check_date');
142    [ListFiles,NumFiles]=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
[610]143end
144
[635]145set(hlist,'String',ListFiles)
146Value=[];
147if ~isempty(InputFileName)
148    Value=find(strcmp(InputFileName,ListFiles));
149end
150if isempty(Value)
151    Value=1;
152end
153set(hlist,'Value',Value)
154set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[610]155%------------------------------------------------------------------------   
[609]156% --- launched by selecting an item on the file list
[612]157function dates_Callback(hObject,event)
[606]158%------------------------------------------------------------------------
[612]159hfig=get(hObject,'parent');
160hsort_option=findobj(hfig,'tag','sort_option');
161if get(hObject,'Value')
162    set(hsort_option,'Visible','on')
163    set(hsort_option,'Value',2)
164else
165    set(hsort_option,'Visible','off')
166end
167refresh_GUI(hObject,[])
168
169%------------------------------------------------------------------------   
170% --- launched by selecting an item on the file list
171function list_Callback(option,hObject,event)
172%------------------------------------------------------------------------
[617]173hfig=get(hObject,'parent');%handle of the fig
174% if ~strcmp(get(hfig,'SelectionType'),'open')
175%     return %select double click
176% end
177set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
[615]178    drawnow
[606]179list=get(hObject,'String');
180index=get(hObject,'Value');
[617]181
[611]182htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
183DirName=get(htitlebox,'String');
[609]184SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
[612]185ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
186if ~isempty(ind_dot)
187    SelectName=SelectName(1:ind_dot-1);
188end
[609]189if strcmp(SelectName,'..')% the upward dir option has been selected
[606]190    FullSelectName=fileparts(DirName);
191else
[609]192    FullSelectName=fullfile(DirName,SelectName);
[606]193end
194if exist(FullSelectName,'dir')% a directory has been selected
[612]195    %     ListFiles=dir(FullSelectName);
196    %     ListDisplay=cell(numel(ListFiles),1);
197    %     for ilist=2:numel(ListDisplay)% suppress the first line '.'
198    %         ListDisplay{ilist-1}=ListFiles(ilist).name;
199    %     end
200    %     set(hObject,'Value',1)
201    %     set(hObject,'String',ListDisplay)
202    %     if strcmp(selectname,'..')
203    %         FullSelectName=fileparts(fileparts(FullSelectName));
204    %     end
[613]205    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
206    drawnow
[609]207    hbackward=findobj(hfig,'Tag','backward');
208    set(hbackward,'UserData',DirName); %store the current dir for future backward action
[612]209    hsort_option=findobj(hfig,'tag','sort_option');
210    sort_option='name';%default
211    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
212        sort_option='date';
213    end
214    hcheck_date=findobj(hfig,'tag','check_date');
[613]215   
[612]216    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option);% list the directory content
[606]217    set(hObject,'Value',1)
218    set(hObject,'String',ListFiles)
[613]219    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
[611]220    set(htitlebox,'String',FullSelectName)% record the new dir name
[606]221elseif exist(FullSelectName,'file')%visualise the field if it exists
222    FileType=get_file_type(FullSelectName);
223    if strcmp(FileType,'txt')
224        edit(FullSelectName)
225    elseif strcmp(FileType,'xml')
226        editxml(FullSelectName)
[612]227    elseif strcmp(FileType,'figure')
[606]228        open(FullSelectName)
229    else
230        %uvmat(FullSelectName);
231        switch option
[609]232            case 'browser'
[612]233                set(htitlebox,'String',FullSelectName);
234                uiresume(hfig)
[610]235            case 'status_display'
[612]236                uvmat(FullSelectName);
[606]237        end
238    end
239end
[615]240set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
[606]241
[610]242%-------------------------------------------------------------------------   
243% list the content of a directory
[635]244function [ListFiles,NumFiles]=list_files(DirName,check_date,sort_option)
[610]245%-------------------------------------------------------------------------
246ListStruct=dir(DirName);% get structure of the current directory
[635]247NumFiles=0; %default
[611]248if numel(ListStruct)<1  % case of empty dir
[610]249    ListFiles={};
250    return
251end
252ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
253ListFiles=ListCells(1,:);%list of file names
[611]254check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
[610]255ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
[635]256ListDates=cell2mat(ListCells(5,:));%list of numerical dates
257if isnumeric(sort_option)
258    check_old=ListDates<sort_option-1;% -1 is put to account for a 1 s delay in the record of starting time
259    NumFiles=numel(find(~check_old&~check_dir));
260end
261if ~isempty(find(~check_dir))
262ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front
263end
264if isnumeric(sort_option)|| strcmp(sort_option,'date')
[612]265    [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list
266else
267    [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
268end
269ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
[610]270cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
[612]271check_keep=cellfun('isempty', cell_remove);
[610]272ListFiles=[{'+/..'} ListFiles(check_keep)];
[612]273if check_date
[635]274    ListDateString=ListCells(2,:);%list of file dates
275    if isnumeric(sort_option)
276        ListDateString(check_old)={'--OLD--'};
277    end
278    ListDateString(check_dir)={''};
279    ListDateString=ListDateString(index_sort);% sort the corresponding dates
280    ListDateString=[{''} ListDateString(check_keep)];
281    ListFiles=[ListFiles; ListDateString];
282    ListFiles=cell2tab(ListFiles','...');
[612]283end
[610]284
[606]285%------------------------------------------------------------------------   
286% --- launched by selecting home
287function home_dir(hObject,event)
[609]288%------------------------------------------------------------------------
[606]289DirName=pwd;
290hfig=get(hObject,'parent');
[613]291hlist=findobj(hfig,'tag','list');% find the list object
292set(hlist,'BackgroundColor',[1 1 0])
293drawnow
[612]294sort_option='name';%default
295hsort_option=findobj(hfig,'tag','sort_option');
296if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
297    sort_option='date';
298end
299hcheck_date=findobj(hfig,'tag','check_date');
300ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
[611]301htitlebox=findobj(hfig,'Tag','titlebox');
302set(htitlebox,'String',DirName)% record the new dir name
[613]303set(hlist,'Value',1)
[606]304set(hlist,'String',ListFiles)
[613]305set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[606]306%------------------------------------------------------------------------
307
308%------------------------------------------------------------------------   
[609]309% --- launched by pressing the backward (<--) button
310function backward(hObject,event)
311%------------------------------------------------------------------------
312PrevDir=get(hObject,'UserData');
313if ~isempty(PrevDir)
314hfig=get(hObject,'parent');
315htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
316set(htitlebox,'String',PrevDir)
317refresh_GUI(findobj(hfig,'Tag','refresh'))
318end
319
[606]320%-------------------------------------------------------------------------   
[609]321% launched by deleting the status figure (only used in mode series status')
322function close(option,hObject, eventdata)
[606]323%-------------------------------------------------------------------------
[610]324if strcmp(option,'status_display')
[635]325    hseries=findobj(allchild(0),'Tag','series');
326    hstatus=findobj(hseries,'Tag','status');
327    set(hstatus,'value',0) %reset the status uicontrol in the GUI series
328    set(hstatus,'BackgroundColor',[0 1 0])
[609]329end
[606]330delete(gcbf)
331
Note: See TracBrowser for help on using the repository browser.