source: trunk/src/uigetfile_uvmat.m @ 648

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

get_field updated, several bugs corrected,open_uvmat suppressd

File size: 16.0 KB
RevLine 
[646]1%'uigetfile_uvmat': browser, and display of directories, faster than the Matlab fct uigetfile
[606]2%------------------------------------------------------------------------
[648]3% fileinput=uigetfile_uvmat(title,InputName,FilterExt)
[606]4%
5% OUTPUT:
[609]6% fileinput: detected file name, including path
[606]7%
8% INPUT:
[648]9% title: = displayed title,
10%        if title='status_display': display advancement of a series calculation,
11%        else uigetfile_uvmat used as browser.
12% InputName: initial file or directory selection for the browser
13% FilterExt: string to filter the file display:
14%          '*' (default) all files displayed
15%          'image': any image or movie
16%          '.ext': display only files with extension '.ext'
[606]17
[641]18function fileinput=uigetfile_uvmat(title,InputName,FilterExt)
19if ~exist('FilterExt','var')
20    FilterExt='*';
21end
[609]22fileinput=''; %default file selection
[610]23if strcmp(title,'status_display')
24    option='status_display';
[609]25else
26    option='browser';
[606]27end
[615]28InputDir=pwd;%look in the current work directory if the input file does not exist
29InputFileName='';%default
30if ischar(InputName)
31    if exist(InputName,'dir')
32        InputDir=InputName;
33        InputFileName='';
34    elseif exist(InputName,'file')
35        [InputDir,InputFileName,Ext]=fileparts(InputName);
36        if isempty(InputFileName)% if InputName is already the root
37            InputFileName=InputDir;
38            if  ~isempty(strcmp (computer, {'PCWIN','PCWIN64'}))%case of Windows systems
39                InputDir=[InputDir '\'];% append '\' for a correct action of dir
40                InputFileName=[InputFileName '\'];
41            end
[610]42        end
[615]43        if isdir(InputName)
44            InputFileName=['+/' InputFileName Ext];
45        end
[610]46    end
[609]47end
48hfig=findobj(allchild(0),'tag',option);
[606]49if isempty(hfig)
[609]50    set(0,'Unit','points')
[606]51    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
[609]52    Width=350;% fig width in points (1/72 inch)
53    Height=min(0.8*ScreenSize(4),500);
54    Left=ScreenSize(3)- Width-40; %right edge close to the right, with margin=40
55    Bottom=ScreenSize(4)-Height-40; %put fig at top right
56    hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Unit','points','Position',[Left,Bottom,Width,Height],'UserData',InputDir);
[611]57    BackgroundColor=get(hfig,'Color');
58    uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.97 0.5 0.03],'BackgroundColor',BackgroundColor,...
59            'String','path:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[615]60    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,...
[611]61        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold');
[612]62    uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.05 0.75 0.1 0.07],...
63            'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward);
64    uicontrol('Style','togglebutton','Units','normalized', 'Position', [0.75 0.75 0.2 0.04],'tag','check_date','Callback',@dates_Callback,...
65            'String','dates','FontUnits','points','FontSize',12,'FontWeight','bold');
66    uicontrol('Style','text','Units','normalized', 'Position', [0.4 0.8 0.35 0.03],'BackgroundColor',BackgroundColor,...
67            'String','sort: ','FontUnits','points','FontSize',12,'FontWeight','bold','HorizontalAlignment','right');
68    uicontrol('Style','popupmenu','Units','normalized', 'Position', [0.75 0.8 0.2 0.04],'tag','sort_option','Callback',@refresh_GUI,'Visible','off',...
69            'String',{'name';'date'},'FontUnits','points','FontSize',12,'FontWeight','bold');   
[641]70    uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.08 0.9 0.66], 'Callback', @(src,event)list_Callback(option,FilterExt,src,event),'tag','list',...
[609]71        'FontUnits','points','FontSize',12);
[611]72    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
[609]73        'String','Close','FontWeight','bold','FontUnits','points','FontSize',12);
[611]74    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.1 0.005 0.2 0.07],'Callback',@refresh_GUI,...
[609]75        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
[606]76    %set(hrefresh,'UserData',StatusData)
[610]77    if strcmp(option,'status_display') %put a run advancement display
[635]78        set(hfig,'DeleteFcn',@(src,event)close(option,src,event))
79              uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.85 0.9 0.04]);
80        uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.85 0.01 0.04],'BackgroundColor',[1 0 0],'tag','waitbar');
81
[609]82    else  %put a title and additional pushbuttons
[612]83        uicontrol('Style','text','Units','normalized', 'Position', [0.15 0.75 0.6 0.03],'BackgroundColor',BackgroundColor,...
[609]84            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[612]85
[611]86        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.005 0.2 0.07],...
[609]87            'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
[606]88    end
89    drawnow
90end
[641]91refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName,FilterExt)% refresh the list of content of the current dir 
[610]92if ~strcmp(option,'status_display') 
[609]93    uiwait(hfig)
[620]94    if ishandle(hfig)
[611]95    htitlebox=findobj(hfig,'Tag','titlebox');
96    fileinput=get(htitlebox,'String');% retrieve the input file selection
[610]97    delete(hfig)
[620]98    end
[609]99end
100
[615]101%------------------------------------------------------------------------   
102% --- launched by refreshing the display figure
103function titlebox_Callback(hObject,event)
104refresh_GUI(hObject)
105%------------------------------------------------------------------------
[610]106
[606]107%------------------------------------------------------------------------   
[610]108% --- launched by refreshing the display figure
[641]109function refresh_GUI(hObject,InputFileName,FilterExt)
[610]110%------------------------------------------------------------------------
111if ~exist('InputFileName','var')
112    InputFileName='';
113end
[648]114if ~exist('FilterExt','var')
115    FilterExt='*';
116end
[610]117hfig=get(hObject,'parent');
[613]118hlist=findobj(hfig,'tag','list');% find the list object
119set(hlist,'BackgroundColor',[1 1 0])
120drawnow
[611]121htitlebox=findobj(hfig,'tag','titlebox');
122DirName=get(htitlebox,'String');
[612]123hsort_option=findobj(hfig,'tag','sort_option');
[635]124if strcmp(get(hfig,'Tag'),'status_display')
[610]125    hseries=findobj(allchild(0),'tag','series');
[635]126    hstatus=findobj(hseries,'tag','status');
[610]127    StatusData=get(hstatus,'UserData');
128    TimeStart=0;
129    if isfield(StatusData,'TimeStart')
130        TimeStart=StatusData.TimeStart;
[635]131    end
[610]132    hlist=findobj(hfig,'tag','list');
[635]133    testrecent=0;   
[610]134    NbOutputFile=[];
[635]135    if isfield(StatusData,'NbOutputFile')
136        NbOutputFile=StatusData.NbOutputFile;
137        NbOutputFile_str=num2str(NbOutputFile);
[610]138    end
[644]139    [ListFiles,NumFiles]=list_files(DirName,1,TimeStart);% list the directory content
[610]140   
141    %% update the waitbar
142    hwaitbar=findobj(hfig,'tag','waitbar');
143    if ~isempty(NbOutputFile)
144        BarPosition=get(hwaitbar,'Position');
[635]145        BarPosition(3)=0.9*max(0.01,NumFiles/NbOutputFile);% the bar width cannot be set to 0, set to 0.01 instead
[610]146        set(hwaitbar,'Position',BarPosition)
147    end
[635]148else
149    sort_option='name';
150    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
151        sort_option='date';
152    end
153    hcheck_date=findobj(hfig,'tag','check_date');
[641]154    [ListFiles,NumFiles]=list_files(DirName,get(hcheck_date,'Value'),sort_option,FilterExt);% list the directory content
[610]155end
156
[635]157set(hlist,'String',ListFiles)
158Value=[];
159if ~isempty(InputFileName)
160    Value=find(strcmp(InputFileName,ListFiles));
161end
162if isempty(Value)
163    Value=1;
164end
165set(hlist,'Value',Value)
166set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[610]167%------------------------------------------------------------------------   
[609]168% --- launched by selecting an item on the file list
[612]169function dates_Callback(hObject,event)
[606]170%------------------------------------------------------------------------
[612]171hfig=get(hObject,'parent');
172hsort_option=findobj(hfig,'tag','sort_option');
173if get(hObject,'Value')
174    set(hsort_option,'Visible','on')
175    set(hsort_option,'Value',2)
176else
177    set(hsort_option,'Visible','off')
178end
179refresh_GUI(hObject,[])
180
181%------------------------------------------------------------------------   
182% --- launched by selecting an item on the file list
[641]183function list_Callback(option,filter_ext,hObject,event)
[612]184%------------------------------------------------------------------------
[617]185hfig=get(hObject,'parent');%handle of the fig
186% if ~strcmp(get(hfig,'SelectionType'),'open')
187%     return %select double click
188% end
189set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
[615]190    drawnow
[606]191list=get(hObject,'String');
192index=get(hObject,'Value');
[617]193
[611]194htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
195DirName=get(htitlebox,'String');
[609]196SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
[612]197ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
198if ~isempty(ind_dot)
199    SelectName=SelectName(1:ind_dot-1);
200end
[609]201if strcmp(SelectName,'..')% the upward dir option has been selected
[606]202    FullSelectName=fileparts(DirName);
203else
[609]204    FullSelectName=fullfile(DirName,SelectName);
[606]205end
206if exist(FullSelectName,'dir')% a directory has been selected
[612]207    %     ListFiles=dir(FullSelectName);
208    %     ListDisplay=cell(numel(ListFiles),1);
209    %     for ilist=2:numel(ListDisplay)% suppress the first line '.'
210    %         ListDisplay{ilist-1}=ListFiles(ilist).name;
211    %     end
212    %     set(hObject,'Value',1)
213    %     set(hObject,'String',ListDisplay)
214    %     if strcmp(selectname,'..')
215    %         FullSelectName=fileparts(fileparts(FullSelectName));
216    %     end
[613]217    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
218    drawnow
[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');
[613]227   
[641]228    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option,filter_ext);% list the directory content
[606]229    set(hObject,'Value',1)
230    set(hObject,'String',ListFiles)
[613]231    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
[611]232    set(htitlebox,'String',FullSelectName)% record the new dir name
[648]233elseif exist(FullSelectName,'file')%visualise the field if it exists 
234    switch option
235        case 'browser'
236            set(htitlebox,'String',FullSelectName);
237            uiresume(hfig)
238        case 'status_display'
239            FileType=get_file_type(FullSelectName);
240            if strcmp(FileType,'txt')
241                edit(FullSelectName)
242            elseif strcmp(FileType,'xml')
243                editxml(FullSelectName)
244            elseif strcmp(FileType,'figure')
245                open(FullSelectName)
246            else
[612]247                uvmat(FullSelectName);
[648]248            end
[606]249    end
250end
[615]251set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
[606]252
[610]253%-------------------------------------------------------------------------   
254% list the content of a directory
[641]255function [ListFiles,NumFiles]=list_files(DirName,check_date,sort_option,filter_ext)
[610]256%-------------------------------------------------------------------------
257ListStruct=dir(DirName);% get structure of the current directory
[635]258NumFiles=0; %default
[611]259if numel(ListStruct)<1  % case of empty dir
[610]260    ListFiles={};
261    return
262end
263ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
264ListFiles=ListCells(1,:);%list of file names
[611]265check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
[610]266ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
[648]267if exist('filter_ext','var') && ~strcmp(filter_ext,'*')
268    if strcmp(filter_ext,'image')
269        check_keep=cellfun(@isimage,ListFiles) ;
270    elseif strcmp(filter_ext(1),'.')
271        ind_ext=regexp(ListFiles,[filter_ext '$']);%look for the input file extension
272        check_keep=~cellfun('isempty',ind_ext);
273    end
274    check_keep=check_keep|check_dir;
275    ListFiles=ListFiles(check_keep);
276    ListCells=ListCells(:,check_keep);
277    check_dir=check_dir(check_keep);
278end
[635]279ListDates=cell2mat(ListCells(5,:));%list of numerical dates
280if isnumeric(sort_option)
281    check_old=ListDates<sort_option-1;% -1 is put to account for a 1 s delay in the record of starting time
282    NumFiles=numel(find(~check_old&~check_dir));
283end
284if ~isempty(find(~check_dir))
285ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front
286end
[641]287
[635]288if isnumeric(sort_option)|| strcmp(sort_option,'date')
[612]289    [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list
290else
291    [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
292end
293ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
[610]294cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
[612]295check_keep=cellfun('isempty', cell_remove);
[610]296ListFiles=[{'+/..'} ListFiles(check_keep)];
[612]297if check_date
[635]298    ListDateString=ListCells(2,:);%list of file dates
299    if isnumeric(sort_option)
300        ListDateString(check_old)={'--OLD--'};
301    end
302    ListDateString(check_dir)={''};
303    ListDateString=ListDateString(index_sort);% sort the corresponding dates
304    ListDateString=[{''} ListDateString(check_keep)];
305    ListFiles=[ListFiles; ListDateString];
306    ListFiles=cell2tab(ListFiles','...');
[612]307end
[610]308
[606]309%------------------------------------------------------------------------   
310% --- launched by selecting home
311function home_dir(hObject,event)
[609]312%------------------------------------------------------------------------
[606]313DirName=pwd;
314hfig=get(hObject,'parent');
[613]315hlist=findobj(hfig,'tag','list');% find the list object
316set(hlist,'BackgroundColor',[1 1 0])
317drawnow
[612]318sort_option='name';%default
319hsort_option=findobj(hfig,'tag','sort_option');
320if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
321    sort_option='date';
322end
323hcheck_date=findobj(hfig,'tag','check_date');
324ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
[611]325htitlebox=findobj(hfig,'Tag','titlebox');
326set(htitlebox,'String',DirName)% record the new dir name
[613]327set(hlist,'Value',1)
[606]328set(hlist,'String',ListFiles)
[613]329set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[606]330%------------------------------------------------------------------------
331
332%------------------------------------------------------------------------   
[609]333% --- launched by pressing the backward (<--) button
334function backward(hObject,event)
335%------------------------------------------------------------------------
336PrevDir=get(hObject,'UserData');
337if ~isempty(PrevDir)
338hfig=get(hObject,'parent');
339htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
340set(htitlebox,'String',PrevDir)
341refresh_GUI(findobj(hfig,'Tag','refresh'))
342end
343
[606]344%-------------------------------------------------------------------------   
[641]345% --- launched by deleting the status figure (only used in mode series status')
346%-------------------------------------------------------------------------
[609]347function close(option,hObject, eventdata)
[641]348
[610]349if strcmp(option,'status_display')
[635]350    hseries=findobj(allchild(0),'Tag','series');
351    hstatus=findobj(hseries,'Tag','status');
352    set(hstatus,'value',0) %reset the status uicontrol in the GUI series
353    set(hstatus,'BackgroundColor',[0 1 0])
[609]354end
[606]355delete(gcbf)
356
[641]357%-------------------------------------------------------------------------
358% --- check whether a file is has an image name extension
359%-------------------------------------------------------------------------
360function CheckImage=isimage(filename)
361
362[pp,name,ext]=fileparts(filename);
363CheckImage=~isempty(ext)&&~strcmp(ext,'.')&&~isempty(imformats(regexprep(ext,'^.','')));
Note: See TracBrowser for help on using the repository browser.