source: trunk/src/uigetfile_uvmat.m @ 1036

Last change on this file since 1036 was 1031, checked in by sommeria, 7 years ago

miscellaneous

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