Initial commit
[pftoolbox.git] / GUI / dnl_action.m
blob7feb32788acbd11efd4d1e70bfb61d029cc3d40f
1 function dnl_action(hObject,action,varargin)\r
2 % Sorry, I didn't have enough time to write nice and documented GUI code.\r
3 \r
4 % Toolbox for nonlinear filtering.\r
5 % Copyright (C) 2005  Jakob Rosén <jakob.rosen@gmail.com>\r
6 %\r
7 % This program is free software; you can redistribute it and/or\r
8 % modify it under the terms of the GNU General Public License\r
9 % as published by the Free Software Foundation; either version 2\r
10 % of the License, or (at your option) any later version.\r
11 %\r
12 % This program is distributed in the hope that it will be useful,\r
13 % but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 % GNU General Public License for more details.\r
16 %\r
17 % You should have received a copy of the GNU General Public License\r
18 % along with this program; if not, write to the Free Software\r
19 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
21 handles=guidata(hObject);\r
23 if strcmp(action,'check')\r
24         caller=varargin{1};\r
25         disp(caller);\r
26 end;\r
28 if strcmp(action,'edit_data')\r
29         caller=varargin{1};\r
30         str=get(handles.([caller, '_edit']),'String');\r
32         if ~isempty(str)\r
33                 % Undefined object! We'll try to assign the expression to the correct class\r
34                 if isa(handles.(caller), 'double')\r
35                         % The object is undefined!\r
36                         tc=eval('str2num(str)',[]);\r
37                         if isempty(tc)\r
38                                 handles.(caller)=xsymbolic;\r
39                         else\r
40                                 handles.(caller)=xlinear;\r
41                                 if strcmp(caller,'f')||strcmp(caller,'h')\r
42                                         handles.(caller)=set(handles.(caller),'evalvar',1);\r
43                                 end\r
44                         end\r
45                         guidata(hObject,handles);\r
46                         model_action_update_select(hObject,caller,'datatypes','str');\r
47                         handles=guidata(hObject);\r
48                 end\r
49         end\r
51         handles.(caller)=set(handles.(caller),'str',str);\r
52         str=get(handles.(caller),'str');\r
53         set(handles.([caller, '_edit']),'String',str);                  % FORMAT DATA\r
55 end;\r
57 if strcmp(action,'save')\r
59         % Check for empty objects\r
60         needed_objects={'f','h','w','e','p0'};\r
61         for i=1:length(needed_objects); \r
62                 if isempty(handles.(needed_objects{i}))\r
63                         msgbox([needed_objects{i},' must not be undefined!']);\r
64                         return;\r
65                 end\r
66         end\r
68         % Assign the saved properties to local variables\r
69         obj=handles.obj;\r
70         props={'f','h','w','e','p0','xvars','uvars','wvars','x0','T'};\r
71         for i=1:length(props)\r
72                 eval([props{i},'=handles.',props{i},';']);\r
73         end\r
75         % Insert xvars into the data objects\r
76         data_objects={'f','h'};\r
77         for i=1:length(data_objects)\r
78                 eval(['set(obj, ''', props{i}, ''', ', props{i}, ');']);\r
79                 eval(['set(', data_objects{i}, ', ''xvars'', xvars);']);\r
80                 eval(['set(', data_objects{i}, ', ''uvars'', uvars);']);\r
81                 eval(['set(', data_objects{i}, ', ''wvars'', wvars);']);\r
82         end\r
84         % Fix the correct varsize\r
85 % FÖR PRESENTATION!!\r
86 %       varsize=[handles.states, 1, parenthandles.usize, get(handles.w,'n')];\r
87         varsize=[handles.states, 1, 0, get(handles.w,'n')];\r
89         eval(['set(f, ''varsize'', varsize);'], ';');   % Try / Catch\r
90         eval(['set(h, ''varsize'', varsize);'], ';');   % Try / Catch\r
92         % Insert the new objects into the model\r
93         for i=1:length(props)\r
94                 eval(['set(obj, ''', props{i}, ''', ', props{i}, ');']);\r
95         end\r
97         parentfig=handles.parentfig;\r
98         parenthandles=guidata(parentfig);\r
99         model_objects=parenthandles.model_objects;\r
100         model_objects{handles.caller}=obj;\r
101         parenthandles.model_objects=model_objects;\r
102         guidata(parentfig,parenthandles);\r
104         eval(handles.callback);\r
106         close(hObject)\r
107         return\r
108 end;\r
111 if strcmp(action,'rnd_edit')\r
113         caller=varargin{1};\r
114         rnd_edit_property=[caller, '_edit'];\r
115         str=get(handles.(rnd_edit_property),'String');\r
117         if ~isempty(str)\r
118                 % Undefined object! We'll try to assign the expression to the correct class\r
119                 if isa(handles.(caller), 'double')\r
120                         % The object is undefined!\r
121                         handles.(caller)=gauss;\r
122                         guidata(hObject,handles);\r
123                         model_action_update_select(hObject,caller,'rndtypes','R_str');\r
124                         handles=guidata(hObject);\r
125                 end\r
126         end\r
128         handles.(caller)=set(handles.(caller),'R_str',str);\r
129         str=get(handles.(caller),'R_str');\r
130         set(handles.(rnd_edit_property),'String',str);\r
131         \r
132         guidata(hObject,handles);\r
133         dnl_action_noise_update(hObject,caller);\r
134         handles=guidata(hObject);       % UGLY FIX LAST ROW BLAH BLAH\r
136 end;\r
139 if strcmp(action,'states_edit')\r
140         handles.x0=str2num(get(handles.edit_x0,'String'));\r
141         handles.states=str2num(get(handles.edit_states,'String'));\r
142         handles.xvars=cellstr2cell(get(handles.xvars_edit,'String'));\r
143         guidata(hObject,handles);\r
145         model_edit_states('init', hObject, ['dnl_action(',num2str(hObject),',''states_refresh'')']);\r
146 end;\r
148 if strcmp(action,'states_refresh')\r
150         set(handles.edit_states,'String',num2str(handles.states));\r
151         set(handles.edit_x0,'String',num2str2(handles.x0));\r
152         set(handles.xvars_edit,'String',cell2cellstr(handles.xvars));\r
154         guidata(hObject,handles);\r
155         model_generate_p0(hObject);\r
156         handles=guidata(hObject);\r
157 end;\r
159 if strcmp(action,'uvars_edit_button')\r
160         handles.uvars=cellstr2cell(get(handles.uvars_edit,'String'));\r
161         guidata(hObject,handles);\r
163         model_edit_uvars('init', hObject, ['dnl_action(',num2str(hObject),',''uvars_refresh'')']);\r
164 end;\r
166 if strcmp(action,'uvars_refresh')\r
168         set(handles.uvars_edit,'String',cell2cellstr(handles.uvars));\r
169         guidata(hObject,handles);\r
170 end;\r
173 if strcmp(action,'wvars_edit_button')\r
174         handles.wvars=cellstr2cell(get(handles.wvars_edit,'String'));\r
175         guidata(hObject,handles);\r
177         model_edit_wvars('init', hObject, ['dnl_action(',num2str(hObject),',''wvars_refresh'')']);\r
178 end;\r
180 if strcmp(action,'wvars_refresh')\r
182         set(handles.wvars_edit,'String',cell2cellstr(handles.wvars));\r
183         guidata(hObject,handles);\r
184 end;\r
187 if strcmp(action,'T_edit')\r
188         T=str2num(get(handles.T_edit,'String'));\r
189         if T<=0\r
190                 msgbox('T must be positive!');\r
191                 return\r
192         end\r
193         handles.T=T;\r
194         set(handles.T_edit,'String',num2str(T));        % Format data\r
195 end;\r
197 if strcmp(action,'statenr_edit')\r
198         states=str2num(get(handles.edit_states,'String'));\r
199         if states<0\r
200                 msgbox('The amount of states must be positive!');\r
201                 return\r
202         end\r
204 %       x0=str2num(get(handles.x0,'String'));\r
205 %       xvars=cellstr2cell(get(handles.xvars,'String'));\r
207         x0=handles.x0;\r
208         xvars=handles.xvars;\r
210         states_old=length(xvars);\r
211         sdiff=states-states_old;\r
213         if sdiff>0\r
214                 % Add more states\r
215                 x0(end+1:states)=0;\r
216                 for i=states_old+1:states\r
217                         xvars{i}=['x',num2str(i)];\r
218                 end;\r
219         elseif sdiff<0\r
220                 % Delete some states\r
221                 x0=x0(1:states);\r
222                 xvars=xvars(1:states);\r
223         else\r
224                 % Nothing has changed. Do nothing.\r
225 %               return;\r
226         end\r
228         if size(x0,2)>1\r
229                 % A row vector! Let's transpone it.\r
230                 x0=x0';\r
231         end;\r
233         set(handles.xvars_edit,'String',cell2cellstr(xvars));\r
234         set(handles.edit_x0,'String',num2str2(x0));\r
236         handles.states=states;\r
237         handles.x0=x0;\r
238         handles.xvars=xvars;\r
240         guidata(hObject,handles);\r
241         model_generate_p0(hObject);\r
242         handles=guidata(hObject);\r
243 end;\r
245 if strcmp(action,'xvars_edit')\r
246         xvars=cellstr2cell(get(handles.xvars_edit,'String'));\r
247         if length(xvars)~=handles.states\r
248                 msgbox('Error! The amount of elements in ''xvars'' must be equal to the amount of states.');\r
249                 return;\r
250         end;\r
251         handles.xvars=xvars;\r
253         set(handles.xvars_edit,'String',cell2cellstr(xvars));   % Format data\r
254 end;\r
256 if strcmp(action,'uvars_edit')\r
257         uvars=cellstr2cell(get(handles.uvars_edit,'String'));\r
258         handles.uvars=uvars;\r
259         set(handles.uvars_edit,'String',cell2cellstr(uvars));   % Format data\r
260 end;\r
262 if strcmp(action,'wvars_edit')\r
263         wvars=cellstr2cell(get(handles.wvars_edit,'String'));\r
264         handles.wvars=wvars;\r
265         set(handles.wvars_edit,'String',cell2cellstr(wvars));   % Format data\r
266 end;\r
268 if strcmp(action,'x0_edit')\r
269         x0=str2num(get(handles.edit_x0,'String'));\r
271         if min(size(x0))>1\r
272                 msgbox('''x0'' must be one-dimensional!');\r
273                 return;\r
274         end;\r
276         if length(x0)~=handles.states\r
277                 msgbox('Error! The amount of elements in ''x0'' must be equal to the amount of states.');\r
278                 return;\r
279         end;\r
281         if size(x0,2)>1\r
282                 % A row vector! Let's transpone it.\r
283                 x0=x0';\r
284         end;\r
286         handles.x0=x0;\r
288         set(handles.edit_x0,'String',num2str2(x0));     % Format data\r
289 end;\r
291 if strcmp(action,'objedit')\r
292         caller=varargin{1};\r
293         dobj=handles.(caller);\r
294         if isa(dobj,'double')\r
295                 msgbox('Error! Can not edit an undefined object.');\r
296         else\r
297                 function_str=[class(dobj),'_edit'];     \r
298                 feval(function_str,'init',hObject,dobj,caller,['dnl_action(',num2str(hObject),',''obj_refresh'',''',caller,''')']);\r
299         end\r
300 end;\r
302 if strcmp(action,'rnd_objedit')\r
303         caller=varargin{1};\r
304         robj=handles.(caller);\r
305         if isa(robj,'double')\r
306                 msgbox('Error! Can not edit an undefined object.');\r
307         else\r
308                 function_str=[class(robj),'_edit'];     \r
309                 feval(function_str,'init',hObject,robj,caller,['dnl_action(',num2str(hObject),',''rnd_refresh'',''',caller,''')']);\r
310         end\r
311 end;\r
314 if strcmp(action,'obj_refresh')\r
315         caller=varargin{1};\r
316         str=get(handles.(caller),'str');\r
317         rnd_edit_property=[caller,'_edit'];\r
318         set(handles.(rnd_edit_property),'String',str);\r
320         obj=handles.(caller);\r
321 end;\r
323 if strcmp(action,'rnd_refresh')\r
324         caller=varargin{1};\r
325         str=get(handles.(caller),'R_str');\r
326         rnd_edit_property=[caller,'_edit'];\r
327         set(handles.(rnd_edit_property),'String',str);\r
328         guidata(hObject,handles);\r
329         dnl_action_noise_update(hObject,caller);\r
330         handles=guidata(hObject);       % UGLY FIX LAST ROW BLAH BLAH\r
331 end;\r
334 if strcmp(action,'select')\r
336         caller=varargin{1};\r
337         select_property=[caller, '_select'];\r
338         edit_property=[caller, '_edit'];\r
339         \r
340         datatypes=handles.datatypes;\r
341         index=get(handles.(select_property),'value')-1;\r
342 %       set(handles.(edit_property),'String','');\r
344         if index>0\r
345                 handles.(caller)=eval(datatypes{index});\r
346                 % Default f and h xlinear objects have evalvar=1\r
347                 if isa(handles.(caller),'xlinear');\r
348                         if findcstr({'f','h'},caller)\r
349                                 obj=handles.(caller);\r
350                                 set(obj,'evalvar',1);\r
351                                 handles.(caller)=obj;\r
352                         end\r
353                 end\r
354         else\r
355                 handles.(caller)=[];\r
356         end;\r
357         set(handles.(edit_property),'String',get(handles.(caller),'str'));\r
359         guidata(hObject,handles);\r
360 end;\r
362 if strcmp(action,'rnd_select')\r
363         caller=varargin{1};\r
364         select_property=[caller, '_select'];\r
365         edit_property=[caller, '_edit'];\r
366         \r
367         rndtypes=handles.rndtypes;\r
368         index=get(handles.(select_property),'value')-1;\r
369 %       set(handles.(edit_property),'String','');\r
371         if index>0\r
372                 handles.(caller)=eval(rndtypes{index});\r
373         else\r
374                 handles.(caller)=[];\r
375         end;\r
376         set(handles.(edit_property),'String',get(handles.(caller),'R_str'));\r
378 end;\r
380 if strcmp(action,'import_button')\r
382         objects={'f','h','w','e','p0'};\r
383         import_select=get(handles.import_select,'Value');\r
384         caller=objects{import_select};\r
385         import_edit=get(handles.import_edit,'String');\r
386         \r
387         if isempty(import_edit)\r
388                 % The edit control is blank\r
389                 msgbox('No object name was specified!');\r
390         else\r
391                 data=evalin('base',import_edit,'[]');\r
392                 if isa(data,'logical')\r
393                         msgbox('Error! There was no object returned.');\r
394                 else\r
395                         % Object imported successfully\r
396                         if isa(data,'double')\r
397                                 if import_select>2\r
398                                         data=gauss(data);\r
399                                 else\r
400                                         data=xlinear(data);\r
401                                 end\r
402                         elseif isa(data,'char')||isa(data,'inline')||isa(data,'sym')||isa(data,'cell')\r
403                                 if import_select>2\r
404                                         msgbox('Sorry, this datatype is not convertable to a noise object');\r
405                                         return;\r
406                                 else\r
407                                         data=xsymbolic(data);\r
408                                 end\r
409                         end\r
410                         handles.(caller)=data;\r
411                         guidata(hObject,handles);\r
412                         if import_select>2\r
413                                 model_action_update_select(hObject,caller,'rndtypes','R_str');\r
414                         else\r
415                                 model_action_update_select(hObject,caller,'datatypes','str');\r
416                         end\r
417                         handles=guidata(hObject);                       % FIX: UGLY (last row...)\r
418                 end\r
419         end\r
420 end\r
422 guidata(hObject,handles);