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
RevLine 
[606]1%'uigetfile_uvmat': browser faster than the Matlab fct uigetfile
2%------------------------------------------------------------------------
3% hfig=uigetfile_uvmat(OutputDir,option)
4%
5% OUTPUT:
[609]6% fileinput: detected file name, including path
[606]7%
8% INPUT:
[609]9% title: = displayed title, 'status': display advancement of a series calculation
[606]10% InputDir: directory to browse at first display
11
[609]12function fileinput=uigetfile_uvmat(title,InputName)
13fileinput=''; %default file selection
[610]14if strcmp(title,'status_display')
15    option='status_display';
[609]16else
17    option='browser';
[606]18end
[610]19if exist(InputName,'dir')
20    InputDir=InputName;
21      InputFileName='';
22elseif exist(InputName,'file')
[609]23    [InputDir,InputFileName,Ext]=fileparts(InputName);
[610]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
[609]31    if isdir(InputName)
[610]32        InputFileName=['+/' InputFileName Ext];
[609]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);
[606]39if isempty(hfig)
[609]40    set(0,'Unit','points')
[606]41    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
[609]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);
[611]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');
[612]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',...
[609]61        'FontUnits','points','FontSize',12);
[611]62    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
[609]63        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
[611]64    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.005 0.2 0.07],'Callback',@refresh_GUI,...
[609]65        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
[606]66    %set(hrefresh,'UserData',StatusData)
[610]67    if strcmp(option,'status_display') %put a run advancement display
[609]68        set(hfig,'DeleteFcn',@stop_status)
[606]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]);
[609]71    else  %put a title and additional pushbuttons
[612]72        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,...
[609]73            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[612]74
[611]75        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],...
[609]76            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
[606]77    end
78    drawnow
79end
[610]80refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName)% refresh the list of content of the current dir 
81if ~strcmp(option,'status_display') 
[609]82    uiwait(hfig)
[611]83    htitlebox=findobj(hfig,'Tag','titlebox');
84    fileinput=get(htitlebox,'String');% retrieve the input file selection
[610]85    delete(hfig)
[609]86end
87
[610]88
89
[606]90%------------------------------------------------------------------------   
[610]91% --- launched by refreshing the display figure
92function refresh_GUI(hObject,InputFileName)
93%------------------------------------------------------------------------
94if ~exist('InputFileName','var')
95    InputFileName='';
96end
97hfig=get(hObject,'parent');
[611]98htitlebox=findobj(hfig,'tag','titlebox');
99DirName=get(htitlebox,'String');
[612]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
[610]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
[612]118if strcmp(get(hfig,'Tag'),'status_display')
[610]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;
[612]125    end
[610]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%------------------------------------------------------------------------   
[609]176% --- launched by selecting an item on the file list
[612]177function dates_Callback(hObject,event)
[606]178%------------------------------------------------------------------------
[612]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%------------------------------------------------------------------------
[606]193list=get(hObject,'String');
194index=get(hObject,'Value');
[609]195hfig=get(hObject,'parent');%handle of the fig
[611]196htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
197DirName=get(htitlebox,'String');
[609]198SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
[612]199ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
200if ~isempty(ind_dot)
201    SelectName=SelectName(1:ind_dot-1);
202end
[609]203if strcmp(SelectName,'..')% the upward dir option has been selected
[606]204    FullSelectName=fileparts(DirName);
205else
[609]206    FullSelectName=fullfile(DirName,SelectName);
[606]207end
208if exist(FullSelectName,'dir')% a directory has been selected
[612]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
[609]219    hbackward=findobj(hfig,'Tag','backward');
220    set(hbackward,'UserData',DirName); %store the current dir for future backward action
[612]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
[606]228    set(hObject,'Value',1)
229    set(hObject,'String',ListFiles)
[611]230    set(htitlebox,'String',FullSelectName)% record the new dir name
[606]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)
[612]237    elseif strcmp(FileType,'figure')
[606]238        open(FullSelectName)
239    else
240        %uvmat(FullSelectName);
241        switch option
[609]242            case 'browser'
[612]243                set(htitlebox,'String',FullSelectName);
244                uiresume(hfig)
[610]245            case 'status_display'
[612]246                uvmat(FullSelectName);
[606]247        end
248    end
249end
250
[610]251%-------------------------------------------------------------------------   
252% list the content of a directory
[612]253function ListFiles=list_files(DirName,check_date,sort_option)
[610]254%-------------------------------------------------------------------------
255ListStruct=dir(DirName);% get structure of the current directory
[611]256if numel(ListStruct)<1  % case of empty dir
[610]257    ListFiles={};
258    return
259end
260ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
261ListFiles=ListCells(1,:);%list of file names
[611]262check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
[610]263ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
[612]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
[610]272cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
[612]273check_keep=cellfun('isempty', cell_remove);
[610]274ListFiles=[{'+/..'} ListFiles(check_keep)];
[612]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
[610]283
[606]284%------------------------------------------------------------------------   
285% --- launched by selecting home
286function home_dir(hObject,event)
[609]287%------------------------------------------------------------------------
[606]288DirName=pwd;
289hfig=get(hObject,'parent');
[612]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
[611]297htitlebox=findobj(hfig,'Tag','titlebox');
298set(htitlebox,'String',DirName)% record the new dir name
299hlist=findobj(hfig,'tag','list');% find the list object
[606]300set(hlist,'String',ListFiles)
301%------------------------------------------------------------------------
302
303%------------------------------------------------------------------------   
[609]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
[606]315%-------------------------------------------------------------------------   
[609]316% launched by deleting the status figure (only used in mode series status')
317function close(option,hObject, eventdata)
[606]318%-------------------------------------------------------------------------
[610]319if strcmp(option,'status_display')
[609]320    hseries=findobj(allchild(0),'tag','series');
[610]321    hstatus=findobj(hfig,'Tag','status_display');
[609]322    set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
323    set(hhciv.status,'BackgroundColor',[0 1 0])
324end
[606]325delete(gcbf)
326
Note: See TracBrowser for help on using the repository browser.