source: trunk/src/uigetfile_uvmat.m @ 615

Last change on this file since 615 was 615, checked in by sommeria, 8 years ago

a few bugs repaired

File size: 15.2 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',@stop_status)
70        uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.81 0.01 0.05],'BackgroundColor',[1 0 0],'tag','waitbar');
71        uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
72    else  %put a title and additional pushbuttons
73        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,...
74            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
75
76        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],...
77            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
78    end
79    drawnow
80end
81refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir 
82if ~strcmp(option,'status_display') 
83    uiwait(hfig)
84    htitlebox=findobj(hfig,'Tag','titlebox');
85    fileinput=get(htitlebox,'String');% retrieve the input file selection
86    delete(hfig)
87end
88
89%------------------------------------------------------------------------   
90% --- launched by refreshing the display figure
91function titlebox_Callback(hObject,event)
92refresh_GUI(hObject)
93%------------------------------------------------------------------------
94
95%------------------------------------------------------------------------   
96% --- launched by refreshing the display figure
97function refresh_GUI(hObject,InputFileName)
98%------------------------------------------------------------------------
99if ~exist('InputFileName','var')
100    InputFileName='';
101end
102hfig=get(hObject,'parent');
103hlist=findobj(hfig,'tag','list');% find the list object
104set(hlist,'BackgroundColor',[1 1 0])
105drawnow
106htitlebox=findobj(hfig,'tag','titlebox');
107DirName=get(htitlebox,'String');
108hsort_option=findobj(hfig,'tag','sort_option');
109sort_option='name';
110if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
111    sort_option='date';
112end
113hcheck_date=findobj(hfig,'tag','check_date');
114ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
115
116set(hlist,'String',ListFiles)
117Value=[];
118if ~isempty(InputFileName)
119    Value=find(strcmp(InputFileName,ListFiles));
120end
121if isempty(Value)
122    Value=1;
123end
124set(hlist,'Value',Value)
125set(hlist,'BackgroundColor',[0.7 0.7 0.7])
126if strcmp(get(hfig,'Tag'),'status_display')
127    hseries=findobj(allchild(0),'tag','series');
128    hstatus=findobj(hseries,'tag','status_display');
129    StatusData=get(hstatus,'UserData');
130    TimeStart=0;
131    if isfield(StatusData,'TimeStart')
132        TimeStart=StatusData.TimeStart;
133    end
134    hlist=findobj(hfig,'tag','list');
135    testrecent=0;
136    datnum=zeros(numel(ListDisplay),1);
137    for ilist=1:numel(ListDisplay)
138        ListDisplay{ilist}=ListFiles(ilist).name;
139        if ListFiles(ilist).isdir
140            ListDisplay{ilist}=['/' ListDisplay{ilist}];
141        elseif isfield(ListFiles(ilist),'datenum')
142            datnum(ilist)=ListFiles(ilist).datenum;%only available in recent matlab versions
143            testrecent=1;
144            if datnum(ilist)<TimeStart
145                ListDisplay{ilist}=[ListDisplay{ilist} '  --OLD--'];
146            end
147        end
148    end
149   
150    %% Look at date of creation
151    ListDisplay=ListDisplay(datnum~=0);
152    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
153   
154    NbOutputFile=[];
155    if isempty(datnum)
156        if testrecent
157            message='no result created yet';
158        else
159            message='';
160        end
161    else
162        [first,indfirst]=min(datnum);
163        [last,indlast]=max(datnum);
164        NbOutputFile_str='?';
165        if isfield(StatusData,'NbOutputFile')
166            NbOutputFile=StatusData.NbOutputFile;
167            NbOutputFile_str=num2str(NbOutputFile);
168        end
169        message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification:  ' ListDisplay{indfirst} ' : ' datestr(first)];...
170            ['latest modification:  ' ListDisplay{indlast} ' : ' datestr(last)]};
171    end
172    set(htitlebox,'String', [DirName{1};message])
173   
174    %% update the waitbar
175    hwaitbar=findobj(hfig,'tag','waitbar');
176    if ~isempty(NbOutputFile)
177        BarPosition=get(hwaitbar,'Position');
178        BarPosition(3)=0.9*numel(datnum)/NbOutputFile;
179        set(hwaitbar,'Position',BarPosition)
180    end
181end
182
183%------------------------------------------------------------------------   
184% --- launched by selecting an item on the file list
185function dates_Callback(hObject,event)
186%------------------------------------------------------------------------
187hfig=get(hObject,'parent');
188hsort_option=findobj(hfig,'tag','sort_option');
189if get(hObject,'Value')
190    set(hsort_option,'Visible','on')
191    set(hsort_option,'Value',2)
192else
193    set(hsort_option,'Visible','off')
194end
195refresh_GUI(hObject,[])
196
197%------------------------------------------------------------------------   
198% --- launched by selecting an item on the file list
199function list_Callback(option,hObject,event)
200%------------------------------------------------------------------------
201    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
202    drawnow
203list=get(hObject,'String');
204index=get(hObject,'Value');
205hfig=get(hObject,'parent');%handle of the fig
206htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
207DirName=get(htitlebox,'String');
208SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
209ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
210if ~isempty(ind_dot)
211    SelectName=SelectName(1:ind_dot-1);
212end
213if strcmp(SelectName,'..')% the upward dir option has been selected
214    FullSelectName=fileparts(DirName);
215else
216    FullSelectName=fullfile(DirName,SelectName);
217end
218if exist(FullSelectName,'dir')% a directory has been selected
219    %     ListFiles=dir(FullSelectName);
220    %     ListDisplay=cell(numel(ListFiles),1);
221    %     for ilist=2:numel(ListDisplay)% suppress the first line '.'
222    %         ListDisplay{ilist-1}=ListFiles(ilist).name;
223    %     end
224    %     set(hObject,'Value',1)
225    %     set(hObject,'String',ListDisplay)
226    %     if strcmp(selectname,'..')
227    %         FullSelectName=fileparts(fileparts(FullSelectName));
228    %     end
229    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
230    drawnow
231    hbackward=findobj(hfig,'Tag','backward');
232    set(hbackward,'UserData',DirName); %store the current dir for future backward action
233    hsort_option=findobj(hfig,'tag','sort_option');
234    sort_option='name';%default
235    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
236        sort_option='date';
237    end
238    hcheck_date=findobj(hfig,'tag','check_date');
239   
240    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option);% list the directory content
241    set(hObject,'Value',1)
242    set(hObject,'String',ListFiles)
243    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
244    set(htitlebox,'String',FullSelectName)% record the new dir name
245elseif exist(FullSelectName,'file')%visualise the field if it exists
246    FileType=get_file_type(FullSelectName);
247    if strcmp(FileType,'txt')
248        edit(FullSelectName)
249    elseif strcmp(FileType,'xml')
250        editxml(FullSelectName)
251    elseif strcmp(FileType,'figure')
252        open(FullSelectName)
253    else
254        %uvmat(FullSelectName);
255        switch option
256            case 'browser'
257                set(htitlebox,'String',FullSelectName);
258                uiresume(hfig)
259            case 'status_display'
260                uvmat(FullSelectName);
261        end
262    end
263end
264set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
265
266%-------------------------------------------------------------------------   
267% list the content of a directory
268function ListFiles=list_files(DirName,check_date,sort_option)
269%-------------------------------------------------------------------------
270ListStruct=dir(DirName);% get structure of the current directory
271if numel(ListStruct)<1  % case of empty dir
272    ListFiles={};
273    return
274end
275ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
276ListFiles=ListCells(1,:);%list of file names
277check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
278ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
279if strcmp(sort_option,'date')
280    ListDates=cell2mat(ListCells(5,:));%list of numerical dates
281    ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front
282    [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list
283else
284    [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
285end
286ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
287cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
288check_keep=cellfun('isempty', cell_remove);
289ListFiles=[{'+/..'} ListFiles(check_keep)];
290if check_date
291ListDateString=ListCells(2,:);%list of file dates
292ListDateString(check_dir)={''};
293ListDateString=ListDateString(index_sort);% sort the corresponding dates
294ListDateString=[{''} ListDateString(check_keep)];
295ListFiles=[ListFiles; ListDateString];
296ListFiles=cell2tab(ListFiles','...');
297end
298
299%------------------------------------------------------------------------   
300% --- launched by selecting home
301function home_dir(hObject,event)
302%------------------------------------------------------------------------
303DirName=pwd;
304hfig=get(hObject,'parent');
305hlist=findobj(hfig,'tag','list');% find the list object
306set(hlist,'BackgroundColor',[1 1 0])
307drawnow
308sort_option='name';%default
309hsort_option=findobj(hfig,'tag','sort_option');
310if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
311    sort_option='date';
312end
313hcheck_date=findobj(hfig,'tag','check_date');
314ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
315htitlebox=findobj(hfig,'Tag','titlebox');
316set(htitlebox,'String',DirName)% record the new dir name
317set(hlist,'Value',1)
318set(hlist,'String',ListFiles)
319set(hlist,'BackgroundColor',[0.7 0.7 0.7])
320%------------------------------------------------------------------------
321
322%------------------------------------------------------------------------   
323% --- launched by pressing the backward (<--) button
324function backward(hObject,event)
325%------------------------------------------------------------------------
326PrevDir=get(hObject,'UserData');
327if ~isempty(PrevDir)
328hfig=get(hObject,'parent');
329htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
330set(htitlebox,'String',PrevDir)
331refresh_GUI(findobj(hfig,'Tag','refresh'))
332end
333
334%-------------------------------------------------------------------------   
335% launched by deleting the status figure (only used in mode series status')
336function close(option,hObject, eventdata)
337%-------------------------------------------------------------------------
338if strcmp(option,'status_display')
339    hseries=findobj(allchild(0),'tag','series');
340    hstatus=findobj(hfig,'Tag','status_display');
341    set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
342    set(hhciv.status,'BackgroundColor',[0 1 0])
343end
344delete(gcbf)
345
Note: See TracBrowser for help on using the repository browser.