source: trunk/src/uigetfile_uvmat.m @ 648

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

get_field updated, several bugs corrected,open_uvmat suppressd

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