source: trunk/src/uigetfile_uvmat.m @ 651

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

various bugs corrected. Introduction of campaign lists in Open menu

File size: 18.4 KB
Line 
1%'uigetfile_uvmat': browser, and display of directories, faster than the Matlab fct uigetfile
2%------------------------------------------------------------------------
3% fileinput=uigetfile_uvmat(title,InputName,FilterExt)
4%
5% OUTPUT:
6% fileinput: detected file name, including path
7%
8% INPUT:
9% title: = displayed title,
10%        if title='status_display': display advancement of a series calculation,
11%        else uigetfile_uvmat used as browser.
12% InputName: initial file or directory selection for the browser
13% FilterExt: string to filter the file display:
14%          '*' (default) all files displayed
15%          'image': any image or movie
16%          '.ext': display only files with extension '.ext'
17%          'uigetdir'; browser used to select a directory (like the matlab browser 'uigetdir')
18
19function fileinput=uigetfile_uvmat(title,InputName,FilterExt)
20if ~exist('FilterExt','var')
21    FilterExt='*';
22end
23fileinput=''; %default file selection
24if strcmp(title,'status_display')
25    option='status_display';
26else
27    option='browser';
28end
29InputDir=pwd;%look in the current work directory if the input file does not exist
30InputFileName='';%default
31if ischar(InputName)
32    if exist(InputName,'dir')
33        InputDir=InputName;
34        InputFileName='';
35    elseif exist(InputName,'file')
36        [InputDir,InputFileName,Ext]=fileparts(InputName);
37        if isempty(InputFileName)% if InputName is already the root
38            InputFileName=InputDir;
39            if  ~isempty(strcmp (computer, {'PCWIN','PCWIN64'}))%case of Windows systems
40                InputDir=[InputDir '\'];% append '\' for a correct action of dir
41                InputFileName=[InputFileName '\'];
42            end
43        end
44        if isdir(InputName)
45            InputFileName=['+/' InputFileName Ext];
46        end
47    end
48end
49hfig=findobj(allchild(0),'tag',option);
50if isempty(hfig)
51    set(0,'Unit','points')
52    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
53    Width=350;% fig width in points (1/72 inch)
54    Height=min(0.8*ScreenSize(4),500);
55    Left=ScreenSize(3)- Width-40; %right edge close to the right, with margin=40
56    Bottom=ScreenSize(4)-Height-40; %put fig at top right
57    hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Unit','points','Position',[Left,Bottom,Width,Height],'UserData',InputDir);
58    BackgroundColor=get(hfig,'Color');
59    path_title=uicontrol('Style','text','Units','normalized', 'Position', [0.02 0.97 0.9 0.03],'BackgroundColor',BackgroundColor,'Tag','Path_title',...
60        'String','path:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
61    htitlebox=uicontrol('Style','edit','Units','normalized', 'Position', [0.02 0.89 0.96 0.08],'tag','titlebox','Max',2,'BackgroundColor',[1 1 1],'Callback',@titlebox_Callback,...
62        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold','TooltipString','''titlebox'':current path');
63    uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.02 0.77 0.1 0.05],...
64        'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward,'TooltipString','move backward');
65    home_button=uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.35 0.005 0.3 0.07],...
66        'String','Work dir','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir,'TooltipString','reach the current Matlab working directory');
67   
68    uicontrol('Style','popupmenu','Units','normalized', 'Position', [0.75 0.75 0.23 0.04],'tag','sort_option','Callback',@refresh_GUI,'Visible','off',...
69        'String',{'sort name';'sort date'},'FontUnits','points','FontSize',12,'FontWeight','bold','TooltipString','''sort_option'': sort the files by names or dates');
70    uicontrol('Style','listbox','Units','normalized', 'Position',[0.02 0.08 0.96 0.66], 'Callback', @(src,event)list_Callback(option,FilterExt,src,event),'tag','list',...
71        'FontUnits','points','FontSize',12,'TooltipString','''list'':current list of directories, marked by +/, and files');
72    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.78 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
73        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
74    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.02 0.005 0.2 0.07],'Callback',@refresh_GUI,...
75        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
76    OK_button=uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.75 0.81 0.23 0.07],'BackgroundColor',[0 1 0],...
77        'String','OK','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@(src,event)OK_Callback(option,FilterExt,src,event));
78    %set(hrefresh,'UserData',StatusData)
79    if strcmp(option,'status_display') %put a run advancement display
80        set(hfig,'DeleteFcn',@(src,event)close(option,src,event))
81        uicontrol('Style','frame','Units','normalized', 'Position', [0.02 0.85 0.9 0.04]);
82        uicontrol('Style','frame','Units','normalized', 'Position',[0.02 0.85 0.01 0.04],'BackgroundColor',[1 0 0],'tag','waitbar');
83        %             uicontrol('Style','text','Units','normalized', 'Position', [0.4 0.8 0.35 0.03],'BackgroundColor',BackgroundColor,...
84        %             'String','sort: ','FontUnits','points','FontSize',12,'FontWeight','bold','HorizontalAlignment','right');
85        delete(home_button)
86        set(OK_button,'String','OPEN')
87    elseif strcmp(FilterExt,'uigetdir') %pick a  directory
88        set(path_title,'String',title); %show the input title for path (directory)
89    else  %put a title and additional pushbuttons
90        uicontrol('Style','text','Units','normalized', 'Position', [0.02 0.74 0.6 0.03],'BackgroundColor',BackgroundColor,...
91            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
92        uicontrol('Style','togglebutton','Units','normalized', 'Position', [0.75 0.78 0.23 0.04],'tag','check_date','Callback',@dates_Callback,...
93            'String','show dates','FontUnits','points','FontSize',12,'FontWeight','bold','TooltipString','''check_date'':press button to display dates');
94%         uicontrol('Style','text','Units','normalized', 'Position', [0.37 0.8 0.35 0.03],'BackgroundColor',BackgroundColor,...
95%             'String','sort: ','FontUnits','points','FontSize',12,'FontWeight','bold','HorizontalAlignment','right');
96       
97    end
98    drawnow
99end
100refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName,FilterExt)% refresh the list of content of the current dir
101if ~strcmp(option,'status_display')
102    uiwait(hfig)
103    if ishandle(hfig)
104        htitlebox=findobj(hfig,'Tag','titlebox');
105        fileinput=get(htitlebox,'String');% retrieve the input file selection
106        delete(hfig)
107    end
108end
109
110%------------------------------------------------------------------------   
111% --- launched by refreshing the display figure
112%------------------------------------------------------------------------
113function titlebox_Callback(hObject,event)
114refresh_GUI(hObject)
115
116%------------------------------------------------------------------------   
117% --- launched by selecting OK (relevant for FilterExt='uigetdir')
118%------------------------------------------------------------------------
119function OK_Callback(option,filter_ext,hObject,event)
120hfig=get(hObject,'parent');%handle of the fig
121% if ~strcmp(get(hfig,'SelectionType'),'open')
122%     return %select double click
123% end
124%set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
125%     drawnow
126htitlebox=findobj(hfig,'tag','titlebox');  % display the current dir name 
127DirName=get(htitlebox,'String');
128
129if ~strcmp(filter_ext,'uigetdir')% a file is expected as output, not a dir
130    hlist=findobj(hfig,'Tag','list');
131    list=get(hlist,'String');
132    index=get(hlist,'Value');
133    SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
134    ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
135    if ~isempty(ind_dot)
136        SelectName=SelectName(1:ind_dot-1);
137    end
138    % if strcmp(SelectName,'..')% the upward dir option has been selected
139    %     FullSelectName=fileparts(DirName);
140    % else
141    FullSelectName=fullfile(DirName,SelectName);
142    % end
143    if exist(FullSelectName,'file')
144        switch option
145            case 'browser'
146                set(htitlebox,'String',FullSelectName);
147                uiresume(hfig)
148            case 'status_display'
149                FileType=get_file_type(FullSelectName);
150                if strcmp(FileType,'txt')
151                    edit(FullSelectName)
152                elseif strcmp(FileType,'xml')
153                    editxml(FullSelectName)
154                elseif strcmp(FileType,'figure')
155                    open(FullSelectName)
156                else
157                    uvmat(FullSelectName);
158                end
159        end
160    end
161end
162%set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
163
164uiresume(get(hObject,'parent'))
165
166%------------------------------------------------------------------------   
167% --- launched by refreshing the display figure
168function refresh_GUI(hObject,InputFileName,FilterExt)
169%------------------------------------------------------------------------
170if ~exist('InputFileName','var')
171    InputFileName='';
172end
173if ~exist('FilterExt','var')
174    FilterExt='*';
175end
176if strcmp(FilterExt,'uigetdir')
177    FilterExt='*';
178end
179hfig=get(hObject,'parent');
180hlist=findobj(hfig,'tag','list');% find the list object
181set(hlist,'BackgroundColor',[1 1 0])
182drawnow
183htitlebox=findobj(hfig,'tag','titlebox');
184DirName=get(htitlebox,'String');
185hsort_option=findobj(hfig,'tag','sort_option');
186if strcmp(get(hfig,'Tag'),'status_display')
187    hseries=findobj(allchild(0),'tag','series');
188    hstatus=findobj(hseries,'tag','status');
189    StatusData=get(hstatus,'UserData');
190    TimeStart=0;
191    if isfield(StatusData,'TimeStart')
192        TimeStart=StatusData.TimeStart;
193    end
194    hlist=findobj(hfig,'tag','list');
195    testrecent=0;   
196    NbOutputFile=[];
197    if isfield(StatusData,'NbOutputFile')
198        NbOutputFile=StatusData.NbOutputFile;
199        NbOutputFile_str=num2str(NbOutputFile);
200    end
201    [ListFiles,NumFiles]=list_files(DirName,1,TimeStart);% list the directory content
202   
203    %% update the waitbar
204    hwaitbar=findobj(hfig,'tag','waitbar');
205    if ~isempty(NbOutputFile)
206        BarPosition=get(hwaitbar,'Position');
207        BarPosition(3)=0.9*max(0.01,NumFiles/NbOutputFile);% the bar width cannot be set to 0, set to 0.01 instead
208        set(hwaitbar,'Position',BarPosition)
209    end
210else
211    sort_option='name';
212    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
213        sort_option='date';
214    end
215    hcheck_date=findobj(hfig,'tag','check_date');
216    [ListFiles,NumFiles]=list_files(DirName,get(hcheck_date,'Value'),sort_option,FilterExt);% list the directory content
217end
218
219set(hlist,'String',ListFiles)
220Value=[];
221if ~isempty(InputFileName)
222    Value=find(strcmp(InputFileName,ListFiles));
223end
224if isempty(Value)
225    Value=1;
226end
227set(hlist,'Value',Value)
228set(hlist,'BackgroundColor',[0.7 0.7 0.7])
229
230%------------------------------------------------------------------------   
231% --- launched by selecting an item on the file list
232%------------------------------------------------------------------------
233function dates_Callback(hObject,event)
234
235hfig=get(hObject,'parent');
236hsort_option=findobj(hfig,'tag','sort_option');
237if get(hObject,'Value')
238    set(hsort_option,'Visible','on')
239    set(hsort_option,'Value',2)
240else
241    set(hsort_option,'Visible','off')
242end
243refresh_GUI(hObject,[])
244
245
246%------------------------------------------------------------------------   
247% --- launched by selecting an item on the file list
248function list_Callback(option,filter_ext,hObject,event)
249%------------------------------------------------------------------------
250hfig=get(hObject,'parent');%handle of the fig
251% if ~strcmp(get(hfig,'SelectionType'),'open')
252%     return %select double click
253% end
254set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
255    drawnow
256list=get(hObject,'String');
257index=get(hObject,'Value');
258
259htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
260DirName=get(htitlebox,'String');
261SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
262ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
263if ~isempty(ind_dot)
264    SelectName=SelectName(1:ind_dot-1);
265end
266if strcmp(SelectName,'..')% the upward dir option has been selected
267    FullSelectName=fileparts(DirName);
268else
269    FullSelectName=fullfile(DirName,SelectName);
270end
271if exist(FullSelectName,'dir')% a directory has been selected
272    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
273    drawnow
274    hbackward=findobj(hfig,'Tag','backward');
275    set(hbackward,'UserData',DirName); %store the current dir for future backward action
276    hsort_option=findobj(hfig,'tag','sort_option');
277    sort_option='name';%default
278    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
279        sort_option='date';
280    end
281    hcheck_date=findobj(hfig,'tag','check_date');
282   
283    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option,filter_ext);% list the directory content
284    set(hObject,'Value',1)
285    set(hObject,'String',ListFiles)
286    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
287    set(htitlebox,'String',FullSelectName)% record the new dir name
288end
289set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
290
291%-------------------------------------------------------------------------   
292% list the content of a directory
293function [ListFiles,NumFiles]=list_files(DirName,check_date,sort_option,filter_ext)
294%-------------------------------------------------------------------------
295ListStruct=dir(DirName);% get structure of the current directory
296NumFiles=0; %default
297if numel(ListStruct)<1  % case of empty dir
298    ListFiles={};
299    return
300end
301ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
302ListFiles=ListCells(1,:);%list of file names
303check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
304ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
305if exist('filter_ext','var') && ~strcmp(filter_ext,'*') &&~strcmp(filter_ext,'uigetdir')
306    if strcmp(filter_ext,'image')
307        check_keep=cellfun(@isimage,ListFiles) ;
308    elseif strcmp(filter_ext(1),'.')
309        ind_ext=regexp(ListFiles,[filter_ext '$']);%look for the input file extension
310        check_keep=~cellfun('isempty',ind_ext);
311    end
312    check_keep=check_keep|check_dir;
313    ListFiles=ListFiles(check_keep);
314    ListCells=ListCells(:,check_keep);
315    check_dir=check_dir(check_keep);
316end
317check_emptydate=cellfun('isempty',ListCells(5,:));% = 1 if datenum undefined
318ListCells(5,find(check_emptydate))={0}; %set to 0 the empty dates
319ListDates=cell2mat(ListCells(5,:));%list of numerical dates
320if isnumeric(sort_option)
321    check_old=ListDates<sort_option-1;% -1 is put to account for a 1 s delay in the record of starting time
322    NumFiles=numel(find(~check_old&~check_dir));
323end
324if ~isempty(find(~check_dir))
325ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front
326end
327
328if isnumeric(sort_option)|| strcmp(sort_option,'date')
329    [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list
330else
331    [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
332end
333ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
334cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
335check_keep=cellfun('isempty', cell_remove);
336ListFiles=[{'+/..'} ListFiles(check_keep)];
337if check_date
338    ListDateString=ListCells(2,:);%list of file dates
339    if isnumeric(sort_option)
340        ListDateString(check_old)={'--OLD--'};
341    end
342    ListDateString(check_dir)={''};
343    ListDateString=ListDateString(index_sort);% sort the corresponding dates
344    ListDateString=[{''} ListDateString(check_keep)];
345    ListFiles=[ListFiles; ListDateString];
346    ListFiles=cell2tab(ListFiles','...');
347end
348
349%------------------------------------------------------------------------   
350% --- launched by selecting home
351function home_dir(hObject,event)
352%------------------------------------------------------------------------
353DirName=pwd;
354hfig=get(hObject,'parent');
355hlist=findobj(hfig,'tag','list');% find the list object
356set(hlist,'BackgroundColor',[1 1 0])
357drawnow
358sort_option='name';%default
359hsort_option=findobj(hfig,'tag','sort_option');
360if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
361    sort_option='date';
362end
363hcheck_date=findobj(hfig,'tag','check_date');
364ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
365htitlebox=findobj(hfig,'Tag','titlebox');
366set(htitlebox,'String',DirName)% record the new dir name
367set(hlist,'Value',1)
368set(hlist,'String',ListFiles)
369set(hlist,'BackgroundColor',[0.7 0.7 0.7])
370%------------------------------------------------------------------------
371
372%------------------------------------------------------------------------   
373% --- launched by pressing the backward (<--) button
374function backward(hObject,event)
375%------------------------------------------------------------------------
376PrevDir=get(hObject,'UserData');
377if ~isempty(PrevDir)
378hfig=get(hObject,'parent');
379htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
380set(htitlebox,'String',PrevDir)
381refresh_GUI(findobj(hfig,'Tag','refresh'))
382end
383
384%-------------------------------------------------------------------------   
385% --- launched by deleting the status figure (only used in mode series status')
386%-------------------------------------------------------------------------
387function close(option,hObject, eventdata)
388
389if strcmp(option,'status_display')
390    hseries=findobj(allchild(0),'Tag','series');
391    hstatus=findobj(hseries,'Tag','status');
392    set(hstatus,'value',0) %reset the status uicontrol in the GUI series
393    set(hstatus,'BackgroundColor',[0 1 0])
394end
395delete(gcbf)
396
397%-------------------------------------------------------------------------
398% --- check whether a file is has an image name extension
399%-------------------------------------------------------------------------
400function CheckImage=isimage(filename)
401
402[pp,name,ext]=fileparts(filename);
403CheckImage=~isempty(ext)&&~strcmp(ext,'.')&&~isempty(imformats(regexprep(ext,'^.','')));
Note: See TracBrowser for help on using the repository browser.