source: trunk/src/uigetfile_uvmat.m @ 635

Last change on this file since 635 was 635, checked in by sommeria, 8 years ago
File size: 14.7 KB
Line 
1%'uigetfile_uvmat': browser faster than the Matlab fct uigetfile
2%------------------------------------------------------------------------
3% hfig=uigetfile_uvmat(OutputDir,option)
4%
5% OUTPUT:
6% fileinput: detected file name, including path
7%
8% INPUT:
9% title: = displayed title, 'status': display advancement of a series calculation
10% InputDir: directory to browse at first display
11
12function fileinput=uigetfile_uvmat(title,InputName)
13fileinput=''; %default file selection
14if strcmp(title,'status_display')
15    option='status_display';
16else
17    option='browser';
18end
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
33        end
34        if isdir(InputName)
35            InputFileName=['+/' InputFileName Ext];
36        end
37    end
38end
39hfig=findobj(allchild(0),'tag',option);
40if isempty(hfig)
41    set(0,'Unit','points')
42    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
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);
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');
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,...
52        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold');
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',...
62        'FontUnits','points','FontSize',12);
63    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
64        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
65    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.005 0.2 0.07],'Callback',@refresh_GUI,...
66        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
67    %set(hrefresh,'UserData',StatusData)
68    if strcmp(option,'status_display') %put a run advancement display
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
73    else  %put a title and additional pushbuttons
74        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,...
75            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
76
77        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],...
78            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
79    end
80    drawnow
81end
82refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir 
83if ~strcmp(option,'status_display') 
84    uiwait(hfig)
85    if ishandle(hfig)
86    htitlebox=findobj(hfig,'Tag','titlebox');
87    fileinput=get(htitlebox,'String');% retrieve the input file selection
88    delete(hfig)
89    end
90end
91
92%------------------------------------------------------------------------   
93% --- launched by refreshing the display figure
94function titlebox_Callback(hObject,event)
95refresh_GUI(hObject)
96%------------------------------------------------------------------------
97
98%------------------------------------------------------------------------   
99% --- launched by refreshing the display figure
100function refresh_GUI(hObject,InputFileName)
101%------------------------------------------------------------------------
102if ~exist('InputFileName','var')
103    InputFileName='';
104end
105hfig=get(hObject,'parent');
106hlist=findobj(hfig,'tag','list');% find the list object
107set(hlist,'BackgroundColor',[1 1 0])
108drawnow
109htitlebox=findobj(hfig,'tag','titlebox');
110DirName=get(htitlebox,'String');
111hsort_option=findobj(hfig,'tag','sort_option');
112if strcmp(get(hfig,'Tag'),'status_display')
113    hseries=findobj(allchild(0),'tag','series');
114    hstatus=findobj(hseries,'tag','status');
115    StatusData=get(hstatus,'UserData');
116    TimeStart=0;
117    if isfield(StatusData,'TimeStart')
118        TimeStart=StatusData.TimeStart;
119    end
120    hlist=findobj(hfig,'tag','list');
121    testrecent=0;   
122    NbOutputFile=[];
123    if isfield(StatusData,'NbOutputFile')
124        NbOutputFile=StatusData.NbOutputFile;
125        NbOutputFile_str=num2str(NbOutputFile);
126    end
127    [ListFiles,NumFiles]=list_files(DirName,1,TimeStart);% list the directory content
128   
129    %% update the waitbar
130    hwaitbar=findobj(hfig,'tag','waitbar');
131    if ~isempty(NbOutputFile)
132        BarPosition=get(hwaitbar,'Position');
133        BarPosition(3)=0.9*max(0.01,NumFiles/NbOutputFile);% the bar width cannot be set to 0, set to 0.01 instead
134        set(hwaitbar,'Position',BarPosition)
135    end
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
143end
144
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])
155%------------------------------------------------------------------------   
156% --- launched by selecting an item on the file list
157function dates_Callback(hObject,event)
158%------------------------------------------------------------------------
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%------------------------------------------------------------------------
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
178    drawnow
179list=get(hObject,'String');
180index=get(hObject,'Value');
181
182htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
183DirName=get(htitlebox,'String');
184SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
185ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
186if ~isempty(ind_dot)
187    SelectName=SelectName(1:ind_dot-1);
188end
189if strcmp(SelectName,'..')% the upward dir option has been selected
190    FullSelectName=fileparts(DirName);
191else
192    FullSelectName=fullfile(DirName,SelectName);
193end
194if exist(FullSelectName,'dir')% a directory has been selected
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
205    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
206    drawnow
207    hbackward=findobj(hfig,'Tag','backward');
208    set(hbackward,'UserData',DirName); %store the current dir for future backward action
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');
215   
216    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option);% list the directory content
217    set(hObject,'Value',1)
218    set(hObject,'String',ListFiles)
219    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
220    set(htitlebox,'String',FullSelectName)% record the new dir name
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)
227    elseif strcmp(FileType,'figure')
228        open(FullSelectName)
229    else
230        %uvmat(FullSelectName);
231        switch option
232            case 'browser'
233                set(htitlebox,'String',FullSelectName);
234                uiresume(hfig)
235            case 'status_display'
236                uvmat(FullSelectName);
237        end
238    end
239end
240set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
241
242%-------------------------------------------------------------------------   
243% list the content of a directory
244function [ListFiles,NumFiles]=list_files(DirName,check_date,sort_option)
245%-------------------------------------------------------------------------
246ListStruct=dir(DirName);% get structure of the current directory
247NumFiles=0; %default
248if numel(ListStruct)<1  % case of empty dir
249    ListFiles={};
250    return
251end
252ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
253ListFiles=ListCells(1,:);%list of file names
254check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
255ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
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')
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
270cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
271check_keep=cellfun('isempty', cell_remove);
272ListFiles=[{'+/..'} ListFiles(check_keep)];
273if check_date
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','...');
283end
284
285%------------------------------------------------------------------------   
286% --- launched by selecting home
287function home_dir(hObject,event)
288%------------------------------------------------------------------------
289DirName=pwd;
290hfig=get(hObject,'parent');
291hlist=findobj(hfig,'tag','list');% find the list object
292set(hlist,'BackgroundColor',[1 1 0])
293drawnow
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
301htitlebox=findobj(hfig,'Tag','titlebox');
302set(htitlebox,'String',DirName)% record the new dir name
303set(hlist,'Value',1)
304set(hlist,'String',ListFiles)
305set(hlist,'BackgroundColor',[0.7 0.7 0.7])
306%------------------------------------------------------------------------
307
308%------------------------------------------------------------------------   
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
320%-------------------------------------------------------------------------   
321% launched by deleting the status figure (only used in mode series status')
322function close(option,hObject, eventdata)
323%-------------------------------------------------------------------------
324if strcmp(option,'status_display')
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])
329end
330delete(gcbf)
331
Note: See TracBrowser for help on using the repository browser.