source: trunk/src/uigetfile_uvmat.m @ 617

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

minor cleaning. Option 'double -click' on mouse selection introduced on browser but commented.

File size: 15.3 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%------------------------------------------------------------------------
201hfig=get(hObject,'parent');%handle of the fig
202% if ~strcmp(get(hfig,'SelectionType'),'open')
203%     return %select double click
204% end
205set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
206    drawnow
207list=get(hObject,'String');
208index=get(hObject,'Value');
209
210htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
211DirName=get(htitlebox,'String');
212SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
213ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
214if ~isempty(ind_dot)
215    SelectName=SelectName(1:ind_dot-1);
216end
217if strcmp(SelectName,'..')% the upward dir option has been selected
218    FullSelectName=fileparts(DirName);
219else
220    FullSelectName=fullfile(DirName,SelectName);
221end
222if exist(FullSelectName,'dir')% a directory has been selected
223    %     ListFiles=dir(FullSelectName);
224    %     ListDisplay=cell(numel(ListFiles),1);
225    %     for ilist=2:numel(ListDisplay)% suppress the first line '.'
226    %         ListDisplay{ilist-1}=ListFiles(ilist).name;
227    %     end
228    %     set(hObject,'Value',1)
229    %     set(hObject,'String',ListDisplay)
230    %     if strcmp(selectname,'..')
231    %         FullSelectName=fileparts(fileparts(FullSelectName));
232    %     end
233    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
234    drawnow
235    hbackward=findobj(hfig,'Tag','backward');
236    set(hbackward,'UserData',DirName); %store the current dir for future backward action
237    hsort_option=findobj(hfig,'tag','sort_option');
238    sort_option='name';%default
239    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
240        sort_option='date';
241    end
242    hcheck_date=findobj(hfig,'tag','check_date');
243   
244    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option);% list the directory content
245    set(hObject,'Value',1)
246    set(hObject,'String',ListFiles)
247    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
248    set(htitlebox,'String',FullSelectName)% record the new dir name
249elseif exist(FullSelectName,'file')%visualise the field if it exists
250    FileType=get_file_type(FullSelectName);
251    if strcmp(FileType,'txt')
252        edit(FullSelectName)
253    elseif strcmp(FileType,'xml')
254        editxml(FullSelectName)
255    elseif strcmp(FileType,'figure')
256        open(FullSelectName)
257    else
258        %uvmat(FullSelectName);
259        switch option
260            case 'browser'
261                set(htitlebox,'String',FullSelectName);
262                uiresume(hfig)
263            case 'status_display'
264                uvmat(FullSelectName);
265        end
266    end
267end
268set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
269
270%-------------------------------------------------------------------------   
271% list the content of a directory
272function ListFiles=list_files(DirName,check_date,sort_option)
273%-------------------------------------------------------------------------
274ListStruct=dir(DirName);% get structure of the current directory
275if numel(ListStruct)<1  % case of empty dir
276    ListFiles={};
277    return
278end
279ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
280ListFiles=ListCells(1,:);%list of file names
281check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
282ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
283if strcmp(sort_option,'date')
284    ListDates=cell2mat(ListCells(5,:));%list of numerical dates
285    ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front
286    [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list
287else
288    [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
289end
290ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
291cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
292check_keep=cellfun('isempty', cell_remove);
293ListFiles=[{'+/..'} ListFiles(check_keep)];
294if check_date
295ListDateString=ListCells(2,:);%list of file dates
296ListDateString(check_dir)={''};
297ListDateString=ListDateString(index_sort);% sort the corresponding dates
298ListDateString=[{''} ListDateString(check_keep)];
299ListFiles=[ListFiles; ListDateString];
300ListFiles=cell2tab(ListFiles','...');
301end
302
303%------------------------------------------------------------------------   
304% --- launched by selecting home
305function home_dir(hObject,event)
306%------------------------------------------------------------------------
307DirName=pwd;
308hfig=get(hObject,'parent');
309hlist=findobj(hfig,'tag','list');% find the list object
310set(hlist,'BackgroundColor',[1 1 0])
311drawnow
312sort_option='name';%default
313hsort_option=findobj(hfig,'tag','sort_option');
314if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
315    sort_option='date';
316end
317hcheck_date=findobj(hfig,'tag','check_date');
318ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
319htitlebox=findobj(hfig,'Tag','titlebox');
320set(htitlebox,'String',DirName)% record the new dir name
321set(hlist,'Value',1)
322set(hlist,'String',ListFiles)
323set(hlist,'BackgroundColor',[0.7 0.7 0.7])
324%------------------------------------------------------------------------
325
326%------------------------------------------------------------------------   
327% --- launched by pressing the backward (<--) button
328function backward(hObject,event)
329%------------------------------------------------------------------------
330PrevDir=get(hObject,'UserData');
331if ~isempty(PrevDir)
332hfig=get(hObject,'parent');
333htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
334set(htitlebox,'String',PrevDir)
335refresh_GUI(findobj(hfig,'Tag','refresh'))
336end
337
338%-------------------------------------------------------------------------   
339% launched by deleting the status figure (only used in mode series status')
340function close(option,hObject, eventdata)
341%-------------------------------------------------------------------------
342if strcmp(option,'status_display')
343    hseries=findobj(allchild(0),'tag','series');
344    hstatus=findobj(hfig,'Tag','status_display');
345    set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
346    set(hhciv.status,'BackgroundColor',[0 1 0])
347end
348delete(gcbf)
349
Note: See TracBrowser for help on using the repository browser.