source: trunk/src/uigetfile_uvmat.m @ 630

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

bugs repaired in series + cleaning.
Introducing of a demo program (test stage)

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