source: trunk/src/uigetfile_uvmat.m @ 609

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

various bugs corrected after tests with Windows OS.

File size: 10.8 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')
15    option='status';
16else
17    option='browser';
18end
19if exist(InputName,'file')||exist(InputName,'dir')
20    [InputDir,InputFileName,Ext]=fileparts(InputName);
21    InputFileName=[InputFileName Ext];
22    if isdir(InputName)
23        InputFileName=['+/' InputFileName];
24    end
25else
26    InputDir=pwd;%look in the current work directory if the input file does not exist
27    InputFileName='';
28end
29hfig=findobj(allchild(0),'tag',option);
30if isempty(hfig)
31    set(0,'Unit','points')
32    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
33    Width=350;% fig width in points (1/72 inch)
34    Height=min(0.8*ScreenSize(4),500);
35    Left=ScreenSize(3)- Width-40; %right edge close to the right, with margin=40
36    Bottom=ScreenSize(4)-Height-40; %put fig at top right
37    hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Unit','points','Position',[Left,Bottom,Width,Height],'UserData',InputDir);
38    uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @(src,event)view_file(option,src,event),'tag','list',...
39        'FontUnits','points','FontSize',12);
40    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','titlebox','Max',2,...
41        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold');
42    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
43        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
44    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.01 0.2 0.07],'Callback',@refresh_GUI,...
45        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
46    %set(hrefresh,'UserData',StatusData)
47    if strcmp(option,'status') %put a run advancement display
48        set(hfig,'DeleteFcn',@stop_status)
49        uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.81 0.01 0.05],'BackgroundColor',[1 0 0],'tag','waitbar');
50        uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
51    else  %put a title and additional pushbuttons
52        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.81 0.8 0.03],...
53            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
54        uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.05 0.8 0.1 0.07],...
55            'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward);
56        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.01 0.2 0.07],...
57            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
58    end
59    drawnow
60end
61refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName) 
62if ~strcmp(option,'status') 
63    uiwait(hfig)
64end
65fileinput=get(hfig,'UserData');% retrieve the input file selection
66delete(hfig)
67
68%------------------------------------------------------------------------   
69% --- launched by selecting an item on the file list
70function view_file(option,hObject,event)
71%------------------------------------------------------------------------
72list=get(hObject,'String');
73index=get(hObject,'Value');
74hfig=get(hObject,'parent');%handle of the fig
75DirName=get(hfig,'UserData');
76SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
77if strcmp(SelectName,'..')% the upward dir option has been selected
78    FullSelectName=fileparts(DirName);
79else
80    %ind_dot=regexp(SelectName,'\.\.\.');
81    % if ~isempty(ind_dot)
82    %     SelectName=SelectName(1:ind_dot-1);
83    % end
84    FullSelectName=fullfile(DirName,SelectName);
85end
86if exist(FullSelectName,'dir')% a directory has been selected
87%     ListFiles=dir(FullSelectName);
88%     ListDisplay=cell(numel(ListFiles),1);
89%     for ilist=2:numel(ListDisplay)% suppress the first line '.'
90%         ListDisplay{ilist-1}=ListFiles(ilist).name;
91%     end
92%     set(hObject,'Value',1)
93%     set(hObject,'String',ListDisplay)
94%     if strcmp(selectname,'..')
95%         FullSelectName=fileparts(fileparts(FullSelectName));
96%     end
97    hbackward=findobj(hfig,'Tag','backward');
98    set(hbackward,'UserData',DirName); %store the current dir for future backward action
99    ListFiles=list_files(FullSelectName);
100    set(hObject,'Value',1)
101    set(hObject,'String',ListFiles)
102    set(hfig,'UserData',FullSelectName)% record the new dir name
103    htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
104    set(htitlebox,'String',FullSelectName)
105elseif exist(FullSelectName,'file')%visualise the field if it exists
106    FileType=get_file_type(FullSelectName);
107    if strcmp(FileType,'txt')
108        edit(FullSelectName)
109    elseif strcmp(FileType,'xml')
110        editxml(FullSelectName)
111            elseif strcmp(FileType,'figure')
112        open(FullSelectName)
113    else
114        %uvmat(FullSelectName);
115        switch option
116            case 'browser'
117        hfig=get(hObject,'parent');
118        set(hfig,'UserData',FullSelectName);
119        uiresume(hfig)
120            case 'status'
121           uvmat(FullSelectName);
122        end
123        set(gcbo,'Value',1)
124    end
125end
126
127%------------------------------------------------------------------------   
128% --- launched by selecting home
129function home_dir(hObject,event)
130%------------------------------------------------------------------------
131DirName=pwd;
132ListFiles=list_files(DirName);% list the directory content
133hfig=get(hObject,'parent');
134    set(hfig,'UserData',DirName)% record the new dir name
135    htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
136    set(htitlebox,'String',DirName)
137    hlist=findobj(hfig,'tag','list');% find the list object
138set(hlist,'String',ListFiles)
139%------------------------------------------------------------------------
140
141%------------------------------------------------------------------------   
142% --- launched by pressing the backward (<--) button
143function backward(hObject,event)
144%------------------------------------------------------------------------
145PrevDir=get(hObject,'UserData');
146if ~isempty(PrevDir)
147hfig=get(hObject,'parent');
148set(hfig,'UserData',PrevDir)
149htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
150set(htitlebox,'String',PrevDir)
151refresh_GUI(findobj(hfig,'Tag','refresh'))
152end
153
154%------------------------------------------------------------------------
155
156%------------------------------------------------------------------------   
157% --- launched by refreshing the display figure
158function refresh_GUI(hObject,InputFileName)
159%------------------------------------------------------------------------
160if ~exist('InputFileName','var')
161    InputFileName='';
162end
163hfig=get(hObject,'parent');
164DirName=get(hfig,'UserData');
165ListFiles=list_files(DirName);% list the directory content
166hlist=findobj(hfig,'tag','list');% find the list object
167set(hlist,'String',ListFiles)
168Value=[];
169if ~isempty(InputFileName)
170    Value=find(strcmp(InputFileName,ListFiles));
171end
172if isempty(Value)
173    Value=1;
174end
175set(hlist,'Value',Value)
176return
177
178%TODO adapt to series status
179hseries=findobj(allchild(0),'tag','series');
180hstatus=findobj(hseries,'tag','status');
181StatusData=get(hstatus,'UserData');
182TimeStart=0;
183if isfield(StatusData,'TimeStart')
184    TimeStart=StatusData.TimeStart;
185end
186% testrecent=0;
187% datnum=zeros(numel(ListDisplay),1);
188% for ilist=1:numel(ListDisplay)
189%     ListDisplay{ilist}=ListFiles(ilist).name;
190%     if ListFiles(ilist).isdir
191%         ListDisplay{ilist}=['/' ListDisplay{ilist}];   
192%     elseif isfield(ListFiles(ilist),'datenum')
193%         datnum(ilist)=ListFiles(ilist).datenum;%only available in recent matlab versions
194%         testrecent=1;
195%         if datnum(ilist)<TimeStart
196%             ListDisplay{ilist}=[ListDisplay{ilist} '  --OLD--'];
197%         end
198%     end
199% end
200
201
202%% Look at date of creation
203ListDisplay=ListDisplay(datnum~=0);
204datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
205
206NbOutputFile=[];
207if isempty(datnum)
208    if testrecent
209        message='no result created yet';
210    else
211        message='';
212    end
213else
214    [first,indfirst]=min(datnum);
215    [last,indlast]=max(datnum);
216    NbOutputFile_str='?';
217    if isfield(StatusData,'NbOutputFile')
218        NbOutputFile=StatusData.NbOutputFile;
219        NbOutputFile_str=num2str(NbOutputFile);
220    end
221    message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification:  ' ListDisplay{indfirst} ' : ' datestr(first)];...
222        ['latest modification:  ' ListDisplay{indlast} ' : ' datestr(last)]};
223end
224set(htitlebox,'String', [DirName{1};message])
225
226%% update the waitbar
227hwaitbar=findobj(hfig,'tag','waitbar');
228if ~isempty(NbOutputFile)
229    BarPosition=get(hwaitbar,'Position');
230    BarPosition(3)=0.9*numel(datnum)/NbOutputFile;
231    set(hwaitbar,'Position',BarPosition)
232end
233
234%-------------------------------------------------------------------------   
235% list the content of a directory
236function ListFiles=list_files(DirName)
237%-------------------------------------------------------------------------
238ListStruct=dir(DirName);
239if numel(ListStruct)<1
240    ListFiles={};
241    return
242end
243if strcmp(ListStruct(1).name,'.')
244    ListStruct(1)=[];%removes the first line ='.'
245end
246ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
247ListFiles=ListCells(1,:);%list of file names
248check_dir=cell2mat(ListCells(4,:));% check directories
249ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
250[tild,index_sort]=sort(check_dir,2,'descend');% sort
251ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
252cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
253check_keep=cellfun('isempty', cell_remove);               
254ListFiles=[{'+/..'} ListFiles(check_keep)];
255
256%-------------------------------------------------------------------------   
257% launched by deleting the status figure (only used in mode series status')
258function close(option,hObject, eventdata)
259%-------------------------------------------------------------------------
260if strcmp(option,'status')
261    hseries=findobj(allchild(0),'tag','series');
262    hstatus=findobj(hfig,'Tag','status');
263    set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
264    set(hhciv.status,'BackgroundColor',[0 1 0])
265end
266delete(gcbf)
267
Note: See TracBrowser for help on using the repository browser.