-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbdControlVector.m
380 lines (326 loc) · 15.6 KB
/
bdControlVector.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
classdef bdControlVector < handle
%bdControlVector Control panel widget for vector values in bdGUI.
% This class is specialised for use with bdControlPanel.
% It is not intended to be called directly by users.
%
%AUTHORS
% Stewart Heitmann (2017d,2018a)
% Copyright (C) 2016-2019 QIMR Berghofer Medical Research Institute
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions
% are met:
%
% 1. Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the
% distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
properties (Constant)
rowh = 24;
roww = 220;
end
properties (Access=private)
parent
panel
minbox
maxbox
plusbtn
minusbtn
randbtn
baxes
bgraph
labelbtn
listener1
listener2
dialog
end
methods
function this = bdControlVector(control,xxxdef,xxxindx,parent,ypos,modecheckbox)
%disp('bdControlVector()');
% init empty handle to dialog box
this.dialog = bdControlVectorDialog.empty(0);
% extract the relevant fields from control.sys.xxxdef
xxxname = control.sys.(xxxdef)(xxxindx).name;
xxxvalue = control.sys.(xxxdef)(xxxindx).value;
xxxlim = control.sys.(xxxdef)(xxxindx).lim;
% remember our parent and the vertical offset
this.parent = parent;
% define widget geometry
colw = 22.5; % column width
gap = 5; % column gap
col1 = 2;
col2 = ceil(col1 + colw + gap);
col3 = floor(col2 + colw + gap);
col4 = ceil(col3 + colw + gap);
col5 = floor(col4 + colw + gap);
col6 = ceil(col5 + colw + gap);
col7 = floor(col6 + colw + gap);
col8 = ceil(col7 + colw + gap);
col9 = floor(col8 + colw + gap);
% Construct the panel container
this.panel = uipanel('Parent',parent, ...
'Units','pixels', ...
'Position',[2 ypos this.roww this.rowh], ...
'BorderType','none', ...
'DeleteFcn', @(~,~) delete(this.dialog) );
% Construct the min box
this.minbox = uicontrol('Parent',this.panel, ...
'Style', 'edit', ...
'Units','pixels',...
'Position',[col1 2 col3-col1-gap this.rowh-4], ...
'String',num2str(xxxlim(1),'%0.4g'), ...
'Value',xxxlim(1), ...
'HorizontalAlignment','center', ...
'Visible','off', ...
'Callback', @(~,~) this.minboxCallback(control,xxxdef,xxxindx), ...
'ToolTipString',['lower limit for ''' xxxname '''']);
% Construct the max box
this.maxbox = uicontrol('Parent',this.panel, ...
'Style', 'edit', ...
'Units','pixels',...
'Position',[col3 2 col5-col3-gap this.rowh-4], ...
'String',num2str(xxxlim(2),'%0.4g'), ...
'Value',xxxlim(2), ...
'HorizontalAlignment','center', ...
'Visible','off', ...
'Callback', @(~,~) this.maxboxCallback(control,xxxdef,xxxindx), ...
'ToolTipString',['upper limit for ''' xxxname '''']);
% Construct the PLUS button
this.plusbtn = uicontrol('Parent',this.panel, ...
'Style','pushbutton', ...
'Units','pixels', ...
'Position',[col1 2 col2-col1-gap this.rowh-5], ...
'String','+', ...
'HorizontalAlignment','center', ...
'FontUnits','pixels', ...
'FontSize',12, ...
'Callback', @(~,~) this.PlusCallback(control,xxxdef,xxxindx), ...
'ToolTipString',['Increment the values of ''' xxxname '''']);
% Construct the MINUS button
this.minusbtn = uicontrol('Parent',this.panel, ...
'Style','pushbutton', ...
'Units','pixels', ...
'Position',[col2 2 col3-col2-gap this.rowh-5], ...
'String','-', ...
'HorizontalAlignment','center', ...
'FontUnits','pixels', ...
'FontSize',12, ...
'Callback', @(~,~) this.MinusCallback(control,xxxdef,xxxindx), ...
'ToolTipString',['Decrement the values of ''' xxxname '''']);
% Construct the RAND button
this.randbtn = uicontrol('Parent',this.panel, ...
'Style','pushbutton', ...
'Units','pixels', ...
'Position',[col3 2 col5-col3-gap this.rowh-5], ...
'String','RAND', ...
'HorizontalAlignment','center', ...
'FontUnits','pixels', ...
'FontSize',12, ...
'Callback', @(~,~) this.RandCallback(control,xxxdef,xxxindx), ...
'ToolTipString',['Assign Uniform Random values to ''' xxxname '''']);
% construct bar graph widget for the vector
this.baxes = axes('Parent', this.panel, ...
'Units','pixels', ...
'Position',[col5+1 3 col7-col5-gap-2 this.rowh-6]);
this.bgraph = bar(this.baxes,xxxvalue);
xlim(this.baxes,[0.5 numel(xxxvalue)+0.5]);
ylim(this.baxes,xxxlim);
this.baxes.XTick=[];
this.baxes.YTick=[];
this.baxes.XColor =[0.7 0.7 0.7];
this.baxes.YColor =[0.7 0.7 0.7];
% Construct the label button
this.labelbtn = uicontrol('Parent',this.panel, ...
'Style', 'pushbutton', ...
'Units','pixels',...
'Position',[col7 2 col9-col7-gap this.rowh-5], ...
'String',xxxname, ...
... 'BackgroundColor','g', ...
'FontWeight','normal', ...
'Callback', @(~,~) this.labelbtnCallback(control,xxxdef,xxxindx,xxxname), ...
'ToolTipString',xxxname);
% force a refresh at startup
this.refresh(control,xxxdef,xxxindx,modecheckbox);
% listen for widget refresh events from the control panel
this.listener1 = addlistener(control,'refresh', @(~,~) this.refresh(control,xxxdef,xxxindx,modecheckbox));
this.listener2 = addlistener(control,xxxdef, @(~,~) this.refresh(control,xxxdef,xxxindx,modecheckbox));
end
% Destructor
function delete(this)
delete(this.listener2);
delete(this.listener1);
end
function mode(this,flag)
%disp('bdControlVector.mode()');
if flag
this.minbox.Visible = 'off';
this.maxbox.Visible = 'off';
this.plusbtn.Visible = 'on';
this.minusbtn.Visible = 'on';
this.randbtn.Visible = 'on';
else
this.minbox.Visible = 'on';
this.maxbox.Visible = 'on';
this.plusbtn.Visible = 'off';
this.minusbtn.Visible = 'off';
this.randbtn.Visible = 'off';
end
end
end
methods (Access=private)
% min box callback function
function minboxCallback(this,control,xxxdef,xxxindx)
%disp('bdControlVector.minboxCallback()');
% read the minbox string and convert to a number
str = this.minbox.String;
minval = str2double(str);
if isnan(minval)
hndl = errordlg(['Invalid number: ',str], 'Invalid Number', 'modal');
uiwait(hndl);
% restore the minbox string to its previous value
this.minbox.String = num2str(this.minbox.Value,'%0.4g');
else
% adjust the max box if necessary
maxval = max(this.maxbox.Value, minval);
% update control.sys
control.sys.(xxxdef)(xxxindx).lim = [minval maxval];
% notify all widgets (which includes ourself) that sys.xxxdef has changed
%notify(control,'refresh');
notify(control,xxxdef);
% notify all display panels to redraw themselves
notify(control,'redraw');
end
end
% max box callback function
function maxboxCallback(this,control,xxxdef,xxxindx)
%disp('bdControlVector.maxboxCallback()');
% read the maxbox string and convert to a number
str = this.maxbox.String;
maxval = str2double(str);
if isnan(maxval)
hndl = errordlg(['Invalid number: ',str], 'Invalid Number', 'modal');
uiwait(hndl);
% restore the maxbox string to its previous value
this.maxbox.String = num2str(this.maxbox.Value,'%0.4g');
else
% adjust the min box if necessary
minval = min(this.minbox.Value, maxval);
% update control.sys
control.sys.(xxxdef)(xxxindx).lim = [minval maxval];
% notify all widgets (which includes ourself) that sys.xxxdef has changed
%notify(control,'refresh');
notify(control,xxxdef);
% notify all display panels to redraw themselves
notify(control,'redraw');
end
end
% PLUS button callback. Increments the current values by 5 percent
% of the limit specified in xxxdef.
function PlusCallback(this,control,xxxdef,xxxindx)
% determine the limits of the values
xxxlim = control.sys.(xxxdef)(xxxindx).lim;
lo = xxxlim(1);
hi = xxxlim(2);
% update the control panel with an incremented version of the data
valsize = size(control.sys.(xxxdef)(xxxindx).value);
control.sys.(xxxdef)(xxxindx).value = ...
control.sys.(xxxdef)(xxxindx).value + 0.05*(hi-lo);
% notify all widgets (which includes ourself) that sys.xxxdef has changed
notify(control,xxxdef);
% tell the solver to recompute the solution
notify(control,'recompute');
end
% MINUS button callback. Decrements the current values by 5 percent
% of the limit specified in xxxdef.
function MinusCallback(this,control,xxxdef,xxxindx)
% determine the limits of the values
xxxlim = control.sys.(xxxdef)(xxxindx).lim;
lo = xxxlim(1);
hi = xxxlim(2);
% update the control panel with a decremented version of the data
valsize = size(control.sys.(xxxdef)(xxxindx).value);
control.sys.(xxxdef)(xxxindx).value = ...
control.sys.(xxxdef)(xxxindx).value - 0.05*(hi-lo);
% notify all widgets (which includes ourself) that sys.xxxdef has changed
notify(control,xxxdef);
% tell the solver to recompute the solution
notify(control,'recompute');
end
% RAND button callback. Replaces the current value with a uniform
% random number drawn from the limits specified in xxxdef.
function RandCallback(this,control,xxxdef,xxxindx)
% determine the limits of the random values
xxxlim = control.sys.(xxxdef)(xxxindx).lim;
lo = xxxlim(1);
hi = xxxlim(2);
% update the control panel.
valsize = size(control.sys.(xxxdef)(xxxindx).value);
control.sys.(xxxdef)(xxxindx).value = (hi-lo)*rand(valsize) + lo;
% notify all widgets (which includes ourself) that sys.xxxdef has changed
notify(control,xxxdef);
% tell the solver to recompute the solution
notify(control,'recompute');
end
% label button callback function
function labelbtnCallback(this,control,xxxdef,xxxindx,xxxname)
if isvalid(this.dialog)
% a dialog box already exists, make it visible
this.dialog.visible('on');
else
% contruct a new dialog box
this.dialog = bdControlVectorDialog(control,xxxdef,xxxindx,['Edit Vector ',xxxname]);
end
end
% Update the widgets according to the values in control.sys.xxxdef
function refresh(this,control,xxxdef,xxxindx,modecheckbox)
%disp(['bdControlVector.refresh:' xxxdef]);
% extract the relevant fields from control.sys.xxxdef
xxxvalue = control.sys.(xxxdef)(xxxindx).value;
xxxlim = control.sys.(xxxdef)(xxxindx).lim;
% update the min box widget
this.minbox.Value = xxxlim(1);
this.minbox.String = num2str(xxxlim(1),'%0.4g');
% update the max box widget
this.maxbox.Value = xxxlim(2);
this.maxbox.String = num2str(xxxlim(2),'%0.4g');
% update the bar graph
this.bgraph.YData = xxxvalue;
this.baxes.YLim = xxxlim + [-1e-6 1e-6];
% show/hide the slider widget according to the state of the caller's modecheckbox
this.mode(modecheckbox.Value);
% special case: if this is a vardef control and the evolve button
% is ON then disable the plus/minus/rand buttons.
switch xxxdef
case 'vardef'
if control.sys.evolve
% disable the buttons
this.plusbtn.Enable = 'off';
this.minusbtn.Enable = 'off';
this.randbtn.Enable = 'off';
else
% enable the buttons
this.plusbtn.Enable = 'on';
this.minusbtn.Enable = 'on';
this.randbtn.Enable = 'on';
end
end
end
end
end