Mercurial > hg > ltpda
comparison m-toolbox/test/command_list/cmdlist.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 function cmdlist | |
2 | |
3 % A simple test GUI for generating a command list | |
4 % | |
5 % Explore the idea to generate a command list - each command has | |
6 % configurable inputs and outputs | |
7 % | |
8 | |
9 | |
10 %% Some initial setup | |
11 versions = {}; | |
12 ltpda_versions; | |
13 | |
14 Gproperties.Gcol = [240 240 240]/255; | |
15 Gproperties.Gwidth = 800; | |
16 Gproperties.Gheight = 600; | |
17 Gproperties.Gborder = 10; | |
18 fontsize = getappdata(0, 'ltpda_repo_gui_fontsize'); | |
19 | |
20 Gproperties.Screen = get(0,'screensize'); | |
21 Gproperties.Gposition = [150 ... | |
22 150 ... | |
23 Gproperties.Gwidth... | |
24 Gproperties.Gheight]; | |
25 | |
26 % Initialize and hide the GUI as it is being constructed. | |
27 mainfig = figure('Name', 'Script Builder',... | |
28 'NumberTitle', 'off',... | |
29 'Visible','off',... | |
30 'Position',Gproperties.Gposition,... | |
31 'Color', Gproperties.Gcol,... | |
32 'Toolbar', 'none',... | |
33 'Resize', 'off',... | |
34 'ToolBar', 'none', ... | |
35 'NextPlot', 'new', ... | |
36 'HandleVisibility', 'callback', ... | |
37 'MenuBar', 'none',... | |
38 'Tag', 'ltpdaCmdList'); | |
39 | |
40 setappdata(mainfig, 'commands', {}); | |
41 | |
42 % Add command table to JScrollPane | |
43 | |
44 data = {}; | |
45 cnames = {'output', 'command', 'inputs'}; | |
46 columnformat = {'char', 'char', 'char'}; | |
47 columneditable = [true false false]; | |
48 | |
49 cmdtbl = uitable('Parent', mainfig,... | |
50 'Units','normalized', ... | |
51 'BackgroundColor', [1 1 0.6; 1 1 1], ... | |
52 'RowStriping', 'on', ... | |
53 'ColumnName', cnames, ... | |
54 'Fontsize', 14, ... | |
55 'Data', data, ... | |
56 'ColumnEditable', columneditable, ... | |
57 'ColumnFormat', columnformat, ... | |
58 'CellEditCallback', @cellChanged,... | |
59 'CellSelectionCallback', {@cellEdit, mainfig},... | |
60 'ButtonDownFcn', {@tblClick, mainfig},... | |
61 'Tag', 'commandTable', ... | |
62 'Position', [0.05 0.2 0.5 0.7]... | |
63 ); | |
64 | |
65 get(cmdtbl) | |
66 | |
67 setappdata(mainfig, 'cmdTable', cmdtbl); | |
68 | |
69 cmenu = uicontextmenu('Parent', mainfig, 'Callback', {@setContext, mainfig}); | |
70 % Define the line and associate it with the context menu | |
71 set(cmdtbl, 'UIContextMenu', cmenu); | |
72 | |
73 | |
74 | |
75 | |
76 % Add button | |
77 h = uicontrol(mainfig, 'Style', 'pushbutton', ... | |
78 'String', 'Add',... | |
79 'Units','normalized', ... | |
80 'Position', [0.05 0.1 0.1 0.05],... | |
81 'Callback', {@addCommand, mainfig}); | |
82 | |
83 % Build button | |
84 h = uicontrol(mainfig, 'Style', 'pushbutton', ... | |
85 'String', 'Build',... | |
86 'Units','normalized', ... | |
87 'Position', [0.15 0.1 0.1 0.05],... | |
88 'Callback', {@buildCommand, mainfig}); | |
89 | |
90 % Command panel | |
91 hpnl = uipanel(mainfig, ... | |
92 'BackgroundColor', Gproperties.Gcol, ... | |
93 'Position', [0.6 0.2 0.35 0.7]); | |
94 | |
95 setappdata(mainfig, 'epanel', hpnl); | |
96 | |
97 % data = {'a2', 'ao', 'a1', 'E';... | |
98 % 'a3', 'ltpda_pwelch', 'a2', 'E';... | |
99 % }; | |
100 | |
101 % Make the GUI visible. | |
102 set(mainfig,'Visible','on') | |
103 | |
104 function editCell(varargin) | |
105 | |
106 myh = varargin{1}; | |
107 mainfig = findobj('Tag', 'ltpdaCmdList'); | |
108 scells = getappdata(mainfig, 'selectedCells'); | |
109 cmds = getappdata(mainfig, 'commands'); | |
110 tbl = getappdata(mainfig, 'cmdTable'); | |
111 | |
112 cols = unique(scells(:,2)); | |
113 edt = get(tbl, 'ColumnEditable'); | |
114 edt(cols) = true; | |
115 set(tbl, 'ColumnEditable', edt) | |
116 | |
117 end | |
118 | |
119 function setContext(varargin) | |
120 | |
121 | |
122 cmenu = varargin{1}; | |
123 mainfig = varargin{3}; | |
124 | |
125 delete(get(cmenu, 'Children')); | |
126 | |
127 scells = getappdata(mainfig, 'selectedCells'); | |
128 | |
129 if ~isempty(scells) | |
130 % commands | |
131 if all(scells(:,2)==2) | |
132 buildCommandsMenu(cmenu) | |
133 else | |
134 % inputs | |
135 buildInputsMenu(cmenu) | |
136 end | |
137 end % end of isempty | |
138 item1 = uimenu(cmenu, 'Label', 'edit', 'Callback', @editCell); | |
139 | |
140 end | |
141 | |
142 function buildInputsMenu(cmenu) | |
143 | |
144 mainfig = findobj('Tag', 'ltpdaCmdList'); | |
145 scells = getappdata(mainfig, 'selectedCells'); | |
146 cmds = getappdata(mainfig, 'commands'); | |
147 | |
148 % get the outputs of all other lines above | |
149 mc = min(scells(:,1))-1; | |
150 if mc > 0 | |
151 outs = []; | |
152 for k=1:mc | |
153 cmds(k,1) | |
154 if ~isempty(cmds{k,1}) | |
155 outs = [outs cmds(k,1)]; | |
156 end | |
157 end | |
158 outs = unique(outs); | |
159 | |
160 % Build menu | |
161 for k=1:length(outs) | |
162 o = outs{k}; | |
163 sitem1 = uimenu(cmenu, 'Label', o, 'Callback', @cmcallback); | |
164 end | |
165 end | |
166 end | |
167 | |
168 | |
169 function buildCommandsMenu(cmenu) | |
170 | |
171 % Define the context menu items | |
172 | |
173 sitem1 = uimenu(cmenu, 'Label', 'classes'); | |
174 sitem2 = uimenu(cmenu, 'Label', 'other'); | |
175 | |
176 % Classes sub items | |
177 classes = ltpda_user_classes; | |
178 for j=1:length(classes) | |
179 cl = classes{j}; | |
180 ss = uimenu(sitem1, 'Label', cl); | |
181 | |
182 % Collect categories | |
183 ms = methods(cl); | |
184 cats = {}; | |
185 for k=1:length(ms) | |
186 m = ms{k}; | |
187 if ~strcmp(m, 'Contents') | |
188 ct = unique(versions(find(strcmp(versions(:,2), m)), 9)); | |
189 if ~isempty(ct) && ~any(strcmp(ct, 'Internal')) | |
190 cats = [cats; ct]; | |
191 end | |
192 end | |
193 end % end categories loop | |
194 cats = squeeze(unique(cats)); | |
195 | |
196 % add categories | |
197 for k=1:length(cats) | |
198 if isempty(cats{k}) | |
199 c = 'unknown'; | |
200 else | |
201 c = cats{k}; | |
202 sss = uimenu(ss, 'Label', c); | |
203 end | |
204 | |
205 % Add methods | |
206 thisClass = versions(strcmp(versions(:,10), cl), :); | |
207 ms = thisClass(strcmp(thisClass(:, 9), c),2); | |
208 for l=1:length(ms) | |
209 m = ms{l}; | |
210 ssss = uimenu(sss, 'Label', m, 'Callback', @cmcallback); | |
211 end | |
212 | |
213 end | |
214 end % end classes loop | |
215 | |
216 % Other commands | |
217 otherCommands = versions(strcmp(versions(:,10), 'None'),:); | |
218 | |
219 % category list | |
220 cats = unique(otherCommands(:,9)); | |
221 for j=1:length(cats) | |
222 ct = cats{j}; | |
223 if ~strcmp(ct, 'Internal') | |
224 if isempty(ct) | |
225 ct = 'unknown'; | |
226 end | |
227 sss = uimenu(sitem2, 'Label', ct); | |
228 | |
229 % Functions | |
230 if strcmp(ct, 'unknown') | |
231 cfs = otherCommands(strcmp(otherCommands(:,9), ''),2); | |
232 else | |
233 cfs = otherCommands(strcmp(otherCommands(:,9), ct),2); | |
234 end | |
235 for k=1:length(cfs) | |
236 cf = cfs{k}; | |
237 ssss = uimenu(sss, 'Label', cf, 'Callback', @cmcallback); | |
238 end | |
239 | |
240 end | |
241 | |
242 end | |
243 end | |
244 | |
245 function cmcallback(varargin) | |
246 | |
247 myh = varargin{1}; | |
248 fcn = get(myh, 'Label'); | |
249 | |
250 mainfig = findobj('Tag', 'ltpdaCmdList'); | |
251 scells = getappdata(mainfig, 'selectedCells'); | |
252 cmds = getappdata(mainfig, 'commands'); | |
253 | |
254 for j=1:size(scells,1) | |
255 | |
256 row = scells(j,1); | |
257 col = scells(j,2); | |
258 | |
259 cmds(row,col) = {fcn}; | |
260 | |
261 end | |
262 | |
263 setappdata(mainfig, 'commands', cmds); | |
264 setTable(mainfig); | |
265 | |
266 end | |
267 | |
268 | |
269 function tblClick(varargin) | |
270 | |
271 myh = varargin{1}; | |
272 cinfo = varargin{2}; | |
273 mainfig = varargin{3}; | |
274 | |
275 | |
276 | |
277 | |
278 end | |
279 | |
280 function optCellSelect(varargin) | |
281 | |
282 myh = varargin{1}; | |
283 cinfo = varargin{2}; | |
284 mainfig = varargin{3}; | |
285 | |
286 | |
287 % get cell text | |
288 idx = cinfo.Indices; | |
289 row = idx(1); | |
290 col = idx(2); | |
291 d = get(myh, 'Data'); | |
292 cval = d{row,col}; | |
293 | |
294 | |
295 end | |
296 | |
297 function buildCommand(varargin) | |
298 | |
299 mainfig = varargin{3}; | |
300 | |
301 cmds = getappdata(mainfig, 'commands'); | |
302 | |
303 fname = [tempname '.m']; | |
304 % get tmp filename | |
305 fd = fopen(fname, 'w+'); | |
306 fprintf(fd, 'clear all; \n'); | |
307 | |
308 for j=1:size(cmds,1) | |
309 | |
310 cm = cmds(j,:); | |
311 | |
312 if ~isempty(cm{2}) | |
313 if isempty(cm{1}) | |
314 cmd = sprintf('%s(%s)', cm{2}, cm{3}) | |
315 else | |
316 cmd = sprintf('%s = %s(%s)', cm{1}, cm{2}, cm{3}) | |
317 end | |
318 fprintf(fd, '%s\n', cmd); | |
319 end | |
320 end | |
321 fclose(fd); | |
322 cmd = sprintf('run(''%s'')', fname) | |
323 evalin('base', cmd) | |
324 | |
325 delete(fname) | |
326 | |
327 end | |
328 | |
329 function addCommand(varargin) | |
330 | |
331 mainfig = varargin{end}; | |
332 | |
333 cmds = getappdata(mainfig, 'commands'); | |
334 outs = getOutputs(cmds); | |
335 cmd = {'', '', ''}; | |
336 | |
337 cmds = [cmds; cmd]; | |
338 | |
339 setappdata(mainfig, 'commands', cmds); | |
340 | |
341 setTable(mainfig); | |
342 | |
343 end | |
344 | |
345 function setTable(mainfig) | |
346 | |
347 cmds = getappdata(mainfig, 'commands'); | |
348 tbl = findobj(mainfig, 'Tag', 'commandTable'); | |
349 outs = getOutputs(cmds); | |
350 | |
351 set(tbl, 'Data', cmds); | |
352 | |
353 end | |
354 | |
355 function cellChanged(varargin) | |
356 | |
357 tbl = varargin{1}; | |
358 edata = varargin{2}; | |
359 mainfig = get(tbl, 'Parent'); | |
360 cmds = getappdata(mainfig, 'commands'); | |
361 | |
362 row = edata.Indices(1); | |
363 col = edata.Indices(2); | |
364 cmds{row,col} = edata.NewData; | |
365 setappdata(mainfig, 'commands', cmds); | |
366 | |
367 setTable(mainfig) | |
368 | |
369 edt = get(tbl, 'ColumnEditable'); | |
370 edt(col) = false; | |
371 set(tbl, 'ColumnEditable', edt); | |
372 | |
373 | |
374 end | |
375 | |
376 function o = getOutputs(commands) | |
377 | |
378 o = {}; | |
379 | |
380 | |
381 | |
382 % Go through each command and get outputs | |
383 for j=1:size(commands,1) | |
384 os = commands{j,1}; | |
385 a = strrep(strrep(regexp(os, ',', 'split'), '[', ''), ']', ''); | |
386 o = [o a]; | |
387 end | |
388 | |
389 if isempty(o) | |
390 o = {''}; | |
391 end | |
392 | |
393 | |
394 end | |
395 | |
396 function cellEdit(varargin) | |
397 | |
398 myh = varargin{1}; | |
399 edata = varargin{2}; | |
400 mainfig = varargin{end}; | |
401 | |
402 tbl = getappdata(mainfig, 'optTable'); | |
403 | |
404 setappdata(mainfig, 'selectedCells', edata.Indices); | |
405 | |
406 % buildOptions(tbl); | |
407 | |
408 end | |
409 | |
410 end | |
411 |