source: trunk/src/uigetfile_uvmat.m @ 613

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

a few bugs repaired, GUI sizes optimized

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