source: MML/trunk/applications/orbit/lib/toggle.m

Last change on this file was 4, checked in by zhangj, 11 years ago

Initial import--MML version from SOLEIL@2013

File size: 8.0 KB
Line 
1function varargout = toggle(action, varargin)
2
3switch action
4
5%toggle(''GenFig'',names,istatus,iavail,ifit,title,templisttag];
6%=============================================================
7case 'GenFig'
8%=============================================================
9% Generates the figure which holds
10% Radio buttons for element selection by the user.
11% Only re-draw figure after final OK (delParFig)
12name=varargin(1);     name=char(name{1});
13status=varargin(2);   status=status{1};
14avail=varargin(3);    avail=avail{1};
15isel=varargin(4);     isel=isel{1};
16title=varargin(5);    title=title{1};
17tag=varargin(6);      tag=tag{1};
18tlist=varargin(1:4);   %temporary lists
19
20%test if toggle panel already exists
21h= findobj(0,'tag',['toggle_' tag]);
22if ~isempty(h)
23    delete(h);
24end
25
26
27
28newFig = figure('Position',[100 400 800 350],...
29                'UserData',tlist,...
30                'NumberTitle','off',...
31        'tag',['toggle_' tag],...
32                'Name',['Element Selection: ' title],...
33        'MenuBar','none');
34               
35xpos = 20;              %initial x and y offsets of the button
36ypos = 330;
37
38for ind = 1:length(name)
39
40        ypos = ypos - 20;
41        if ypos < 50                    %go to new column if full
42                ypos = 310;
43                xpos = xpos + 120;
44        end
45
46    %default no status
47            val = 0;        %bad status or not available ==> red
48            color = 'r';
49            uitype='text';
50            uicall=' ';
51
52        if ~isempty(avail)
53                  if ~isempty(find(avail==ind))
54                        val = 0;
55                        color = 'y';    %is available      ==> yellow
56            uitype='radiobutton';
57            uicall='toggle(''ToggleElem'')';
58                  end
59        end
60
61        if ~isempty(avail) & ~isempty(isel)
62                  if ~isempty(find(avail==ind)) & ...
63             ~isempty(find(isel==ind))
64                        val = 1;
65                        color = 'g';    %available and on ==> green
66            uitype='radiobutton';
67            uicall='toggle(''ToggleElem'')';
68                  end
69        end
70
71                if ~isempty(avail) & ~isempty(status)
72          if  isempty(find(status==ind)) | ...
73              isempty(find(avail==ind))
74          end
75        end
76
77        uicontrol('Style','frame',...
78        'Position',[xpos-1 ypos-1 72 22]);
79        uicontrol('Style',uitype, ...
80                'Position',[xpos ypos 70 20], ...
81                'BackgroundColor',color, ...
82                'String',name(ind,:), ...
83                'Value',val,...
84                'Callback',uicall,...                   
85                'tag',['tg' num2str(ind)]);                             
86end   %end loop on elements
87
88uicontrol('Style','pushbutton',...
89          'Position',[10 10 200 30],...
90          'String','Load Selection',...
91          'Callback','toggle(''LoadSelection'')',...
92          'Tag','loadtoggle',...
93          'Userdata',tag);                         %Load New Values
94uicontrol('Style','pushbutton',...
95          'Position',[300 10 80 30],...
96          'String','Select All',...
97          'Callback','toggle(''SelectAll'')');     %Select All
98uicontrol('Style','pushbutton',...
99          'Position',[400 10 80 30],...
100          'String','Select None',...
101          'Callback','toggle(''SelectNone'')');    %Select None
102uicontrol('Style','pushbutton',...
103          'Position',[500 10 60 30],...
104          'String','Cancel',...
105          'Callback','delete(gcf)')                %Cancel button
106     
107%=============================================================
108case 'LoadSelection'                   % *** LoadSelection ***
109%=============================================================
110%Callback of the 'LoadSelection' button in the GenFig
111 tlist = get(gcf,'UserData');   
112 name=tlist{1};
113 status=tlist{2};
114 avail=tlist{3};
115 isel=tlist{4};
116%
117% % for ind = 1:length(name)
118% %       if ~isempty(find(status==ind))
119% %               h=findobj(gcf,'tag',['tg' num2str(ind)]);
120% %           value(ind)=get(h,'Value');
121% %       end
122% % end   
123% tlist=[tlist{1} tlist{2} tlist{3} isel];
124% set(gcf,'UserData',tlist);     
125h1=findobj(0,'Tag','loadtoggle');   %get handle of 'load' button
126tag=get(h1,'Userdata');         %get tag of calling gui
127h1=findobj(0,'Tag',tag);        %get handle of calling gui
128instr=get(h1,'Userdata');       %get instruction set from calling gui
129eval(instr)      %evaluate instruction set
130
131%=============================================================
132case 'ToggleElem'                         % *** ToggleElem ***
133%=============================================================
134% Callback for each radio button.
135% Toggle the radio color
136
137ind = get(gcbo,'tag');            %tag of radio button
138ind = str2num(ind(3:length(ind)));
139
140%plot is unaffected until the 'OK' button is pushed.
141tlist = get(gcf,'UserData');   
142tindx=tlist{4};
143if get(gcbo,'Value') == 1         %radio button true
144   set(gcbo,'BackgroundColor','g');
145   tindx=sort([tindx ind]);
146end
147if get(gcbo,'Value') == 0         %radio button false
148   set(gcbo,'BackgroundColor','y');
149   tindx(find(tindx==ind))=0;     %set element to zero
150   tindx=tindx(find(tindx));
151 end
152   tindx=tindx(find(tindx));
153
154tlist=[{tlist{1}} {tlist{2}} {tlist{3}} {tindx}];
155set(gcf,'UserData',tlist);
156
157%=============================================================
158case 'SelectAll'                      % *** SelectAll ***
159%=============================================================
160tlist = get(gcf,'UserData');   
161name=tlist{1};
162status=tlist{2};
163avail=tlist{3};
164tindx=[];
165
166for ind = 1:length(name)
167      if ~isempty(find(avail==ind)) & ...
168         ~isempty(find(status==ind))  %available & status ok
169                   h=findobj(gcf,'tag',['tg' num2str(ind)]);
170        set(h,'BackGroundColor','g','Value',1);
171        tindx(ind)=ind;
172      end
173end   
174tindx=tindx(find(tindx));
175tlist=[{tlist{1}} {tlist{2}} {tlist{3}} {tindx}];
176set(gcf,'UserData',tlist);
177
178%=============================================================
179case 'SelectNone'                      % *** SelectNone ***
180%=============================================================
181tlist = get(gcf,'UserData');   
182name=tlist{1};
183status=tlist{2};
184avail=tlist{3};
185tindx=[];
186
187for ind = 1:length(name)
188      if ~isempty(find(avail==ind)) & ...
189         ~isempty(find(status==ind)) %available & status ok
190                   h=findobj(gcf,'tag',['tg' num2str(ind)]);
191        set(h,'BackGroundColor','y','Value',0);
192      end
193end   
194tlist=[{tlist{1}} {tlist{2}} {tlist{3}} {tindx}];
195set(gcf,'UserData',tlist);     
196
197%=============================================================
198otherwise
199disp('Warning: no CASE found in toggle');
200disp(action);
201end   % End Switchyard
202
203
204
205
206
207%********************
208function [word,k] = getWord(line, i)
209%********************
210
211%getWord reads one 'word' or token at a time from a line of char.
212
213%Input: line is the line being read, i is the pointer to the index
214%        within that line.
215
216%Output:word is the token being returned to the main program.
217%       k is the returned value of i for the main to keep track of.
218
219
220word = [];
221
222if ~isempty(line)
223
224while line(i) == ' '
225  i = i + 1;
226end
227
228while line(i) ~= ' '
229  word = [word line(i)];
230  i = i+1;
231
232  if i > length(line)
233        break;
234  end
235end
236
237end
238
239k = i;
240
241
242
243%*************************
244function [index,num,k] = getElmnt(line,i)
245%*************************
246
247% getElmnt reads the values stored for a specific element in the file
248% for use in the array.
249
250% Input: line is the line of text from the file being read.
251%        i is the index pointer in that line.
252
253% Output:index and num are data returned to the main program.
254%        index is the element index (ex: ibpmx)
255%        num is the data for that element (ex: 1.83E-01).
256
257%        k is the value of i returned to the main program
258%        to keep track of things.
259
260                 
261while line(i) == ' '
262        i = i + 1;
263end
264
265index = [];
266while ~(line(i) == ' ') & ~(line(i) == '-')
267        index = [index line(i)];
268        i = i + 1;
269end
270index = str2num(index);
271
272if line(i) == ' '
273        i = i + 1;
274end
275
276num = [];
277while line(i) ~= ' '
278        num = [num line(i)];
279        i = i + 1;
280
281        if i > length(line)
282                break;
283        end
284end
285num = sci2num(num);
286
287k = i;
288
289
290
291%*********************
292function num = sci2num(str)
293%*********************
294
295%Transforms a number from scientific notation to standard form
296%for the program to manipulate.
297
298a = [];
299
300i = 1;
301while str(i) ~= 'E'
302        a = [a str(i)];
303        i = i + 1;
304end
305
306a = str2num(a);
307
308sign = str(i + 1);
309i = i + 2;
310
311p = [str(i) str(i + 1)];
312p = str2num(p);
313
314if sign == '+'
315        num = a*(10^p);
316else
317        num = a*(10^(p * -1));
318end   
Note: See TracBrowser for help on using the repository browser.