source: trunk/src/uigetfile_uvmat.m @ 606

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

bug on compilation solved (still to test with transform_field fct). faster browser inrtroduced. Various bug corrections

File size: 8.5 KB
Line 
1%'uigetfile_uvmat': browser faster than the Matlab fct uigetfile
2%------------------------------------------------------------------------
3% hfig=uigetfile_uvmat(OutputDir,option)
4%
5% OUTPUT:
6% hfig: handles of the browser fig, the selected file name is obtained as File=get(hfig,'UserData')
7%
8% INPUT:
9% option: ='file browser': usual browser, 'series status': display advancement of a series calculation
10% InputDir: directory to browse at first display
11
12
13function hfig=uigetfile_uvmat(option,InputDir)
14if ~exist(InputDir,'dir')
15    InputDir=pwd;
16end
17hfig=findobj(allchild(0),'name',option);
18if isempty(hfig)
19    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
20    hfig=figure('name',option,'tag',option,'MenuBar','none','NumberTitle','off','Position',[ScreenSize(3)-600 ScreenSize(4)-640 560 600],'DeleteFcn',@stop_status,'UserData',InputDir);
21    uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @(src,event)view_file(option,src,event),'tag','list','FontSize',12);
22    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','titlebox','Max',2,'String',InputDir,'FontSize',12,'FontWeight','bold');
23    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'String','Close','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@stop_status);
24    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.1 0.01 0.2 0.07],'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@refresh_GUI);
25    %set(hrefresh,'UserData',StatusData)
26    if strcmp(option,'series status') %put a run advancement display
27        uicontrol('Style','frame','Units','normalized', 'Position',[0.05 0.81 0.01 0.05],'BackgroundColor',[1 0 0],'tag','waitbar');
28        uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
29    else  %put a title
30        uicontrol('Style','text','Units','normalized', 'Position', [0.05 0.81 0.9 0.03],'String','select an input file:',...
31            'FontSize',14,'FontWeight','bold','ForegroundColor','blue','HorizontalAlignment','left');
32        uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.4 0.01 0.2 0.07],'String','Home','FontWeight','bold','FontUnits','points','FontSize',12,'Callback',@home_dir);
33    end
34    drawnow
35end
36refresh_GUI(hfig)
37   
38%------------------------------------------------------------------------   
39% --- launched by selecting a file on the list
40function view_file(option,hObject,event)
41%------------------------------------------------------------------------
42list=get(hObject,'String');
43index=get(hObject,'Value');
44hfig=get(hObject,'parent');
45DirName=get(get(hObject,'parent'),'UserData');
46SelectName=regexprep(list{index},'^/','');% remove the / used to mark dir
47if strcmp(SelectName,'..')
48    FullSelectName=fileparts(DirName);
49else
50%ind_dot=regexp(SelectName,'\.\.\.');
51% if ~isempty(ind_dot)
52%     SelectName=SelectName(1:ind_dot-1);
53% end
54FullSelectName=fullfile(DirName,SelectName);
55end
56if exist(FullSelectName,'dir')% a directory has been selected
57%     ListFiles=dir(FullSelectName);
58%     ListDisplay=cell(numel(ListFiles),1);
59%     for ilist=2:numel(ListDisplay)% suppress the first line '.'
60%         ListDisplay{ilist-1}=ListFiles(ilist).name;
61%     end
62%     set(hObject,'Value',1)
63%     set(hObject,'String',ListDisplay)
64%     if strcmp(selectname,'..')
65%         FullSelectName=fileparts(fileparts(FullSelectName));
66%     end
67    ListFiles=list_files(FullSelectName);
68    set(hObject,'Value',1)
69    set(hObject,'String',ListFiles)
70    set(hfig,'UserData',FullSelectName)% record the new dir name
71    htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
72    set(htitlebox,'String',FullSelectName)
73elseif exist(FullSelectName,'file')%visualise the field if it exists
74    FileType=get_file_type(FullSelectName);
75    if strcmp(FileType,'txt')
76        edit(FullSelectName)
77    elseif strcmp(FileType,'xml')
78        editxml(FullSelectName)
79            elseif strcmp(FileType,'figure')
80        open(FullSelectName)
81    else
82        %uvmat(FullSelectName);
83        switch option
84            case 'file browser'
85        hfig=get(hObject,'parent');
86        set(hfig,'UserData',FullSelectName);
87        uiresume(hfig)
88            case 'series status'
89           uvmat(FullSelectName);
90        end
91    end
92    set(gcbo,'Value',1)
93end
94
95%------------------------------------------------------------------------   
96% --- launched by selecting home
97function home_dir(hObject,event)
98DirName=pwd;
99
100ListFiles=list_files(DirName);% list the directory content
101hfig=get(hObject,'parent');
102    set(hfig,'UserData',DirName)% record the new dir name
103    htitlebox=findobj(hfig,'tag','titlebox');  % display the new dir name 
104    set(htitlebox,'String',DirName)
105    hlist=findobj(hfig,'tag','list');% find the list object
106set(hlist,'String',ListFiles)
107%------------------------------------------------------------------------
108
109%------------------------------------------------------------------------   
110% --- launched by refreshing the display figure
111function refresh_GUI(hfig,event)
112%------------------------------------------------------------------------
113DirName=get(hfig,'UserData');
114ListFiles=list_files(DirName);% list the directory content
115hlist=findobj(hfig,'tag','list');% find the list object
116set(hlist,'String',ListFiles)
117return
118
119%TODO adapt to series status
120hseries=findobj(allchild(0),'tag','series');
121hstatus=findobj(hseries,'tag','status');
122StatusData=get(hstatus,'UserData');
123TimeStart=0;
124if isfield(StatusData,'TimeStart')
125    TimeStart=StatusData.TimeStart;
126end
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
143%% Look at date of creation
144ListDisplay=ListDisplay(datnum~=0);
145datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
146
147NbOutputFile=[];
148if isempty(datnum)
149    if testrecent
150        message='no result created yet';
151    else
152        message='';
153    end
154else
155    [first,indfirst]=min(datnum);
156    [last,indlast]=max(datnum);
157    NbOutputFile_str='?';
158    if isfield(StatusData,'NbOutputFile')
159        NbOutputFile=StatusData.NbOutputFile;
160        NbOutputFile_str=num2str(NbOutputFile);
161    end
162    message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification:  ' ListDisplay{indfirst} ' : ' datestr(first)];...
163        ['latest modification:  ' ListDisplay{indlast} ' : ' datestr(last)]};
164end
165set(htitlebox,'String', [DirName{1};message])
166
167%% update the waitbar
168hwaitbar=findobj(hfig,'tag','waitbar');
169if ~isempty(NbOutputFile)
170    BarPosition=get(hwaitbar,'Position');
171    BarPosition(3)=0.9*numel(datnum)/NbOutputFile;
172    set(hwaitbar,'Position',BarPosition)
173end
174
175%-------------------------------------------------------------------------   
176% list the content of a directory
177function ListFiles=list_files(DirName)
178%-------------------------------------------------------------------------
179ListStruct=dir(DirName);
180if numel(ListStruct)<1
181    ListFiles={};
182    return
183end
184if strcmp(ListStruct(1).name,'.')
185    ListStruct(1)=[];%removes the first line ='.'
186end
187ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
188ListFiles=ListCells(1,:);%list of file names
189check_dir=cell2mat(ListCells(4,:));% check directories
190ListFiles(check_dir)=regexprep(ListFiles(check_dir),'^.+','/$0');% put '/' in front of dir name display
191[tild,index_sort]=sort(check_dir,2,'descend');% sort
192ListFiles=ListFiles(index_sort);% list of names sorted by alaphabetical order and dir and file
193cell_remove=regexp(ListFiles,'^(-|\.|/\.)');% remove strings beginning by '/.',';' or '-'
194check_remove=cellfun('isempty',cell_remove);
195ListFiles=[{'/..'} ListFiles(check_remove)];
196
197%-------------------------------------------------------------------------   
198% launched by deleting the status figure
199function stop_status(hObject, eventdata)
200%-------------------------------------------------------------------------
201hciv=findobj(allchild(0),'tag','series');
202hhciv=guidata(hciv);
203set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
204set(hhciv.status,'BackgroundColor',[0 1 0])
205delete(gcbf)
206
Note: See TracBrowser for help on using the repository browser.