source: trunk/src/uigetfile_uvmat.m @ 612

Last change on this file since 612 was 612, checked in by sommeria, 11 years ago

a few bugs repaired

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