source: trunk/src/uigetfile_uvmat.m @ 646

Last change on this file since 646 was 646, checked in by sommeria, 12 years ago

various update, bugs to be expected

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