source: trunk/src/uigetfile_uvmat.m @ 809

Last change on this file since 809 was 809, checked in by g7moreau, 10 years ago
  • Add license
File size: 19.4 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'
[651]17%          'uigetdir'; browser used to select a directory (like the matlab browser 'uigetdir')
[606]18
[809]19%=======================================================================
20% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
21%   http://www.legi.grenoble-inp.fr
22%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
23%
24%     This file is part of the toolbox UVMAT.
25%
26%     UVMAT is free software; you can redistribute it and/or modify
27%     it under the terms of the GNU General Public License as published
28%     by the Free Software Foundation; either version 2 of the license,
29%     or (at your option) any later version.
30%
31%     UVMAT is distributed in the hope that it will be useful,
32%     but WITHOUT ANY WARRANTY; without even the implied warranty of
33%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34%     GNU General Public License (see LICENSE.txt) for more details.
35%=======================================================================
36
[641]37function fileinput=uigetfile_uvmat(title,InputName,FilterExt)
38if ~exist('FilterExt','var')
39    FilterExt='*';
40end
[609]41fileinput=''; %default file selection
[610]42if strcmp(title,'status_display')
43    option='status_display';
[609]44else
45    option='browser';
[606]46end
[615]47InputDir=pwd;%look in the current work directory if the input file does not exist
48InputFileName='';%default
49if ischar(InputName)
50    if exist(InputName,'dir')
51        InputDir=InputName;
52        InputFileName='';
53    elseif exist(InputName,'file')
54        [InputDir,InputFileName,Ext]=fileparts(InputName);
55        if isempty(InputFileName)% if InputName is already the root
56            InputFileName=InputDir;
57            if  ~isempty(strcmp (computer, {'PCWIN','PCWIN64'}))%case of Windows systems
[803]58%                 InputDir=[InputDir '\'];% append '\' for a correct action of dir
[615]59                InputFileName=[InputFileName '\'];
60            end
[610]61        end
[615]62        if isdir(InputName)
63            InputFileName=['+/' InputFileName Ext];
64        end
[610]65    end
[609]66end
[803]67if  ismember(computer,{'PCWIN','PCWIN64'})%case of Windows systems
68      InputDir=[InputDir '\'];% append '\' for a correct action of dir
69end
[609]70hfig=findobj(allchild(0),'tag',option);
[606]71if isempty(hfig)
[609]72    set(0,'Unit','points')
[606]73    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
[609]74    Width=350;% fig width in points (1/72 inch)
75    Height=min(0.8*ScreenSize(4),500);
[651]76    Left=ScreenSize(3)- Width-40; %right edge close to the right, with margin=40
[609]77    Bottom=ScreenSize(4)-Height-40; %put fig at top right
78    hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Unit','points','Position',[Left,Bottom,Width,Height],'UserData',InputDir);
[611]79    BackgroundColor=get(hfig,'Color');
[651]80    path_title=uicontrol('Style','text','Units','normalized', 'Position', [0.02 0.97 0.9 0.03],'BackgroundColor',BackgroundColor,'Tag','Path_title',...
81        'String','path:','FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
82    htitlebox=uicontrol('Style','edit','Units','normalized', 'Position', [0.02 0.89 0.96 0.08],'tag','titlebox','Max',2,'BackgroundColor',[1 1 1],'Callback',@titlebox_Callback,...
83        'String',InputDir,'FontUnits','points','FontSize',12,'FontWeight','bold','TooltipString','''titlebox'':current path');
84    uicontrol('Style','pushbutton','Tag','backward','Units','normalized','Position',[0.02 0.77 0.1 0.05],...
85        'String','<--','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@backward,'TooltipString','move backward');
[653]86    home_button=uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.14 0.77 0.2 0.05],...
87        'String','Work dir','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir,'TooltipString','reach the current Matlab working directory');
88    uicontrol('Style','pushbutton','Tag','refresh','Units','normalized','Position', [0.36 0.77 0.2 0.05],'Callback',@refresh_GUI,...
89        'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12);
90    uicontrol('Style','popupmenu','Units','normalized', 'Position', [0.75 0.74 0.23 0.05],'tag','sort_option','Callback',@refresh_GUI,'Visible','off',...
[651]91        'String',{'sort name';'sort date'},'FontUnits','points','FontSize',12,'FontWeight','bold','TooltipString','''sort_option'': sort the files by names or dates');
92    uicontrol('Style','listbox','Units','normalized', 'Position',[0.02 0.08 0.96 0.66], 'Callback', @(src,event)list_Callback(option,FilterExt,src,event),'tag','list',...
93        'FontUnits','points','FontSize',12,'TooltipString','''list'':current list of directories, marked by +/, and files');
[653]94   
95    OK_button=uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.58 0.005 0.2 0.07],'BackgroundColor',[0 1 0],...
[651]96        'String','OK','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@(src,event)OK_Callback(option,FilterExt,src,event));
[653]97    close_button=uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.78 0.005 0.2 0.07],'Callback',@(src,event)close(option,src,event),...
98        'FontWeight','bold','FontUnits','points','FontSize',12);
[606]99    %set(hrefresh,'UserData',StatusData)
[610]100    if strcmp(option,'status_display') %put a run advancement display
[635]101        set(hfig,'DeleteFcn',@(src,event)close(option,src,event))
[651]102        uicontrol('Style','frame','Units','normalized', 'Position', [0.02 0.85 0.9 0.04]);
103        uicontrol('Style','frame','Units','normalized', 'Position',[0.02 0.85 0.01 0.04],'BackgroundColor',[1 0 0],'tag','waitbar');
104        %             uicontrol('Style','text','Units','normalized', 'Position', [0.4 0.8 0.35 0.03],'BackgroundColor',BackgroundColor,...
105        %             'String','sort: ','FontUnits','points','FontSize',12,'FontWeight','bold','HorizontalAlignment','right');
106        delete(home_button)
[653]107        set(OK_button,'String','Open')
108        set(close_button,'String','Close')
[651]109    elseif strcmp(FilterExt,'uigetdir') %pick a  directory
110        set(path_title,'String',title); %show the input title for path (directory)
[653]111        set(OK_button,'String','Select')
112        set(close_button,'String','Cancel')
[609]113    else  %put a title and additional pushbuttons
[651]114        uicontrol('Style','text','Units','normalized', 'Position', [0.02 0.74 0.6 0.03],'BackgroundColor',BackgroundColor,...
[609]115            'String',title,'FontUnits','points','FontSize',12,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
[651]116        uicontrol('Style','togglebutton','Units','normalized', 'Position', [0.75 0.78 0.23 0.04],'tag','check_date','Callback',@dates_Callback,...
117            'String','show dates','FontUnits','points','FontSize',12,'FontWeight','bold','TooltipString','''check_date'':press button to display dates');
118%         uicontrol('Style','text','Units','normalized', 'Position', [0.37 0.8 0.35 0.03],'BackgroundColor',BackgroundColor,...
119%             'String','sort: ','FontUnits','points','FontSize',12,'FontWeight','bold','HorizontalAlignment','right');
[653]120         set(OK_button,'String','Open')
121         set(close_button,'String','Cancel')   
[606]122    end
123    drawnow
124end
[651]125refresh_GUI(findobj(hfig,'Tag','refresh'),InputFileName,FilterExt)% refresh the list of content of the current dir
126if ~strcmp(option,'status_display')
[609]127    uiwait(hfig)
[620]128    if ishandle(hfig)
[651]129        htitlebox=findobj(hfig,'Tag','titlebox');
130        fileinput=get(htitlebox,'String');% retrieve the input file selection
131        delete(hfig)
[620]132    end
[609]133end
134
[615]135%------------------------------------------------------------------------   
136% --- launched by refreshing the display figure
[651]137%------------------------------------------------------------------------
[615]138function titlebox_Callback(hObject,event)
139refresh_GUI(hObject)
[651]140
141%------------------------------------------------------------------------   
142% --- launched by selecting OK (relevant for FilterExt='uigetdir')
[615]143%------------------------------------------------------------------------
[651]144function OK_Callback(option,filter_ext,hObject,event)
[714]145set(hObject,'backgroundColor',[1 1 0])% indicate button activation
[651]146hfig=get(hObject,'parent');%handle of the fig
147htitlebox=findobj(hfig,'tag','titlebox');  % display the current dir name 
148DirName=get(htitlebox,'String');
[610]149
[651]150if ~strcmp(filter_ext,'uigetdir')% a file is expected as output, not a dir
151    hlist=findobj(hfig,'Tag','list');
152    list=get(hlist,'String');
153    index=get(hlist,'Value');
[664]154    if ~isempty(regexp(list{index},'^\+/'))
155        return % quit if a dir has been opened
156    end
157    %SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
158    SelectName=list{index};
[651]159    ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
160    if ~isempty(ind_dot)
161        SelectName=SelectName(1:ind_dot-1);
162    end
163    FullSelectName=fullfile(DirName,SelectName);
164    if exist(FullSelectName,'file')
165        switch option
166            case 'browser'
167                set(htitlebox,'String',FullSelectName);
168                uiresume(hfig)
169            case 'status_display'
[784]170                FileInfo=get_file_info(FullSelectName);
[783]171                if strcmp(FileInfo.FileType,'txt')
[651]172                    edit(FullSelectName)
[783]173                elseif strcmp(FileInfo.FileType,'xml')
[651]174                    editxml(FullSelectName)
[783]175                elseif strcmp(FileInfo.FileType,'figure')
[651]176                    open(FullSelectName)
177                else
178                    uvmat(FullSelectName);
179                end
180        end
181    end
182end
[714]183set(hObject,'backgroundColor',[0 1 0])% indicate end button activation
[651]184uiresume(get(hObject,'parent'))
185
[606]186%------------------------------------------------------------------------   
[610]187% --- launched by refreshing the display figure
[641]188function refresh_GUI(hObject,InputFileName,FilterExt)
[610]189%------------------------------------------------------------------------
190if ~exist('InputFileName','var')
191    InputFileName='';
192end
[648]193if ~exist('FilterExt','var')
194    FilterExt='*';
195end
[651]196if strcmp(FilterExt,'uigetdir')
197    FilterExt='*';
198end
[610]199hfig=get(hObject,'parent');
[613]200hlist=findobj(hfig,'tag','list');% find the list object
201set(hlist,'BackgroundColor',[1 1 0])
202drawnow
[611]203htitlebox=findobj(hfig,'tag','titlebox');
204DirName=get(htitlebox,'String');
[612]205hsort_option=findobj(hfig,'tag','sort_option');
[635]206if strcmp(get(hfig,'Tag'),'status_display')
[610]207    hseries=findobj(allchild(0),'tag','series');
[635]208    hstatus=findobj(hseries,'tag','status');
[610]209    StatusData=get(hstatus,'UserData');
210    TimeStart=0;
211    if isfield(StatusData,'TimeStart')
212        TimeStart=StatusData.TimeStart;
[635]213    end
[610]214    hlist=findobj(hfig,'tag','list');
[635]215    testrecent=0;   
[610]216    NbOutputFile=[];
[635]217    if isfield(StatusData,'NbOutputFile')
218        NbOutputFile=StatusData.NbOutputFile;
219        NbOutputFile_str=num2str(NbOutputFile);
[610]220    end
[644]221    [ListFiles,NumFiles]=list_files(DirName,1,TimeStart);% list the directory content
[610]222   
223    %% update the waitbar
224    hwaitbar=findobj(hfig,'tag','waitbar');
225    if ~isempty(NbOutputFile)
226        BarPosition=get(hwaitbar,'Position');
[635]227        BarPosition(3)=0.9*max(0.01,NumFiles/NbOutputFile);% the bar width cannot be set to 0, set to 0.01 instead
[610]228        set(hwaitbar,'Position',BarPosition)
229    end
[635]230else
231    sort_option='name';
232    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
233        sort_option='date';
234    end
235    hcheck_date=findobj(hfig,'tag','check_date');
[641]236    [ListFiles,NumFiles]=list_files(DirName,get(hcheck_date,'Value'),sort_option,FilterExt);% list the directory content
[610]237end
238
[635]239set(hlist,'String',ListFiles)
240Value=[];
241if ~isempty(InputFileName)
242    Value=find(strcmp(InputFileName,ListFiles));
243end
244if isempty(Value)
245    Value=1;
246end
247set(hlist,'Value',Value)
248set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[651]249
[610]250%------------------------------------------------------------------------   
[609]251% --- launched by selecting an item on the file list
[651]252%------------------------------------------------------------------------
[612]253function dates_Callback(hObject,event)
[651]254
[612]255hfig=get(hObject,'parent');
256hsort_option=findobj(hfig,'tag','sort_option');
257if get(hObject,'Value')
258    set(hsort_option,'Visible','on')
259    set(hsort_option,'Value',2)
260else
261    set(hsort_option,'Visible','off')
262end
263refresh_GUI(hObject,[])
264
[651]265
[612]266%------------------------------------------------------------------------   
267% --- launched by selecting an item on the file list
[641]268function list_Callback(option,filter_ext,hObject,event)
[612]269%------------------------------------------------------------------------
[617]270hfig=get(hObject,'parent');%handle of the fig
271set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
[615]272    drawnow
[606]273list=get(hObject,'String');
274index=get(hObject,'Value');
[617]275
[611]276htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
277DirName=get(htitlebox,'String');
[609]278SelectName=regexprep(list{index},'^\+/','');% remove the +/ used to mark dir
[612]279ind_dot=regexp(SelectName,'\s*\.\.\.');%remove what is beyond  '...'
280if ~isempty(ind_dot)
281    SelectName=SelectName(1:ind_dot-1);
282end
[609]283if strcmp(SelectName,'..')% the upward dir option has been selected
[606]284    FullSelectName=fileparts(DirName);
285else
[609]286    FullSelectName=fullfile(DirName,SelectName);
[606]287end
288if exist(FullSelectName,'dir')% a directory has been selected
[613]289    set(hObject,'BackgroundColor',[1 1 0])% paint list in yellow to indicate action
290    drawnow
[609]291    hbackward=findobj(hfig,'Tag','backward');
292    set(hbackward,'UserData',DirName); %store the current dir for future backward action
[612]293    hsort_option=findobj(hfig,'tag','sort_option');
294    sort_option='name';%default
295    if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
296        sort_option='date';
297    end
298    hcheck_date=findobj(hfig,'tag','check_date');
[613]299   
[641]300    ListFiles=list_files(FullSelectName,get(hcheck_date,'Value'),sort_option,filter_ext);% list the directory content
[606]301    set(hObject,'Value',1)
302    set(hObject,'String',ListFiles)
[613]303    set(hObject,'BackgroundColor',[0.7 0.7 0.7])
[611]304    set(htitlebox,'String',FullSelectName)% record the new dir name
[606]305end
[615]306set(hObject,'BackgroundColor',[0.7 0.7 0.7])% paint list in grey to indicate action end
[606]307
[610]308%-------------------------------------------------------------------------   
309% list the content of a directory
[641]310function [ListFiles,NumFiles]=list_files(DirName,check_date,sort_option,filter_ext)
[610]311%-------------------------------------------------------------------------
312ListStruct=dir(DirName);% get structure of the current directory
[635]313NumFiles=0; %default
[611]314if numel(ListStruct)<1  % case of empty dir
[610]315    ListFiles={};
316    return
317end
318ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
319ListFiles=ListCells(1,:);%list of file names
[611]320check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
[610]321ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','+/$0');% put '+/' in front of dir name display
[651]322if exist('filter_ext','var') && ~strcmp(filter_ext,'*') &&~strcmp(filter_ext,'uigetdir')
[648]323    if strcmp(filter_ext,'image')
324        check_keep=cellfun(@isimage,ListFiles) ;
325    elseif strcmp(filter_ext(1),'.')
326        ind_ext=regexp(ListFiles,[filter_ext '$']);%look for the input file extension
327        check_keep=~cellfun('isempty',ind_ext);
328    end
329    check_keep=check_keep|check_dir;
330    ListFiles=ListFiles(check_keep);
331    ListCells=ListCells(:,check_keep);
332    check_dir=check_dir(check_keep);
333end
[651]334check_emptydate=cellfun('isempty',ListCells(5,:));% = 1 if datenum undefined
335ListCells(5,find(check_emptydate))={0}; %set to 0 the empty dates
[635]336ListDates=cell2mat(ListCells(5,:));%list of numerical dates
337if isnumeric(sort_option)
338    check_old=ListDates<sort_option-1;% -1 is put to account for a 1 s delay in the record of starting time
339    NumFiles=numel(find(~check_old&~check_dir));
340end
341if ~isempty(find(~check_dir))
342ListDates(check_dir)=max(ListDates(~check_dir))+1000; % we set the dir in front
343end
[641]344
[635]345if isnumeric(sort_option)|| strcmp(sort_option,'date')
[612]346    [tild,index_sort]=sort(ListDates,2,'descend');% sort files by chronological order, recent first, put the dir first in the list
347else
348    [tild,index_sort]=sort(check_dir,2,'descend');% put the dir first in the list
349end
350ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
[610]351cell_remove=regexp(ListFiles,'^(-|\.|\+/\.)');% detect strings beginning by '-' ,'.' or '+/.'(dir beginning by . )
[612]352check_keep=cellfun('isempty', cell_remove);
[610]353ListFiles=[{'+/..'} ListFiles(check_keep)];
[612]354if check_date
[635]355    ListDateString=ListCells(2,:);%list of file dates
356    if isnumeric(sort_option)
357        ListDateString(check_old)={'--OLD--'};
358    end
359    ListDateString(check_dir)={''};
360    ListDateString=ListDateString(index_sort);% sort the corresponding dates
361    ListDateString=[{''} ListDateString(check_keep)];
362    ListFiles=[ListFiles; ListDateString];
363    ListFiles=cell2tab(ListFiles','...');
[612]364end
[610]365
[606]366%------------------------------------------------------------------------   
367% --- launched by selecting home
368function home_dir(hObject,event)
[609]369%------------------------------------------------------------------------
[606]370DirName=pwd;
371hfig=get(hObject,'parent');
[613]372hlist=findobj(hfig,'tag','list');% find the list object
373set(hlist,'BackgroundColor',[1 1 0])
374drawnow
[612]375sort_option='name';%default
376hsort_option=findobj(hfig,'tag','sort_option');
377if strcmp(get(hsort_option,'Visible'),'on')&& isequal(get(hsort_option,'Value'),2)
378    sort_option='date';
379end
380hcheck_date=findobj(hfig,'tag','check_date');
381ListFiles=list_files(DirName,get(hcheck_date,'Value'),sort_option);% list the directory content
[611]382htitlebox=findobj(hfig,'Tag','titlebox');
383set(htitlebox,'String',DirName)% record the new dir name
[613]384set(hlist,'Value',1)
[606]385set(hlist,'String',ListFiles)
[613]386set(hlist,'BackgroundColor',[0.7 0.7 0.7])
[606]387%------------------------------------------------------------------------
388
389%------------------------------------------------------------------------   
[609]390% --- launched by pressing the backward (<--) button
391function backward(hObject,event)
392%------------------------------------------------------------------------
393PrevDir=get(hObject,'UserData');
394if ~isempty(PrevDir)
395hfig=get(hObject,'parent');
396htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name
397set(htitlebox,'String',PrevDir)
398refresh_GUI(findobj(hfig,'Tag','refresh'))
399end
400
[606]401%-------------------------------------------------------------------------   
[641]402% --- launched by deleting the status figure (only used in mode series status')
403%-------------------------------------------------------------------------
[609]404function close(option,hObject, eventdata)
[641]405
[610]406if strcmp(option,'status_display')
[635]407    hseries=findobj(allchild(0),'Tag','series');
408    hstatus=findobj(hseries,'Tag','status');
409    set(hstatus,'value',0) %reset the status uicontrol in the GUI series
410    set(hstatus,'BackgroundColor',[0 1 0])
[609]411end
[606]412delete(gcbf)
413
[641]414%-------------------------------------------------------------------------
415% --- check whether a file is has an image name extension
416%-------------------------------------------------------------------------
417function CheckImage=isimage(filename)
418
419[pp,name,ext]=fileparts(filename);
[809]420CheckImage=~isempty(ext)&&~strcmp(ext,'.')&&~isempty(imformats(regexprep(ext,'^.','')));
Note: See TracBrowser for help on using the repository browser.