-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbdPanel.m
394 lines (352 loc) · 17.1 KB
/
bdPanel.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
classdef (Abstract) bdPanel < handle
% Base class for display panels in the Brain Dynamics Toolbox GUI.
% User-defined panels must be drived from this class. Among other
% things, it manages the construction and destruction of the panel
% tab as well as the visibility of the panel's menu in the toolbar.
%
% AUTHORS
% Stewart Heitmann (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 %(Access=protected)
menu % Handle to the panel's toolbar uimenu
tab % Handle to the panel's uitab
end
methods
function this = bdPanel(tabgroup)
% Constructs a new display panel in the given tabgroup.
% handle to the parent figure
fig = ancestor(tabgroup,'figure');
% construct an empty menu and make it invisible.
this.menu = uimenu('Parent',fig, 'Label','noname', 'Visible','off');
% construct an empty tab
this.tab = uitab(tabgroup,'title','noname','Unit','pixels');
% associated the menu with this tab
this.tab.UserData.menu = this.menu;
% ensure the menu for the selected tab (possibly this tab) is visible
tabgroup.SelectedTab.UserData.menu.Visible = 'on';
end
% Close the panel
function close(this)
%disp('bdPanel.close()');
% Get the tabgroup handle
tabgroup = this.tab.Parent;
% Delete the toolbar menu and the panel tab
delete(this.menu);
delete(this.tab);
% Reveal the menu of the newly selected tab (if it exists)
if ~isempty(tabgroup.SelectedTab)
tabgroup.SelectedTab.UserData.menu.Visible = 'on';
end
% Delete the panel object itself
delete(this);
end
end
methods (Static)
function [ax,cmenu,spanel] = Subpanel(parent,panelpos,axespos)
% Construct a graphical subpanel in the parent tab.
%
% [ax,cmenu,spanel] = Subpanel(parent,panelpos,axespos)
%
% The position of the subpanel within the parent is dictated
% by panelpos. The position of the axes within the subpanel
% is dictated by axespos. Both positions are in normal
% coordinates.
%
% Returns handles to the subpanel's axes (ax), its context
% menu (cmenu) and its uipanel container (spanel).
% handle to the parent figure
fig = ancestor(parent,'figure');
% Construct the uipanel container
spanel = uipanel(parent, ...
'Units','normal', ...
'Position',panelpos, ...
'BorderType','beveledout');
% Construct the axes
ax = axes('Parent',spanel, ...
'Units','normal', ...
'OuterPosition',axespos, ...
'NextPlot','add', ...
'FontUnits','pixels', ...
'FontSize',12, ...
'Box','on');
% Define the icon for the menu button
cdata = ones(10,10,3);
cdata([1 2 5 6 9 10],:,:) = 0;
% Construct an empty context menu
cmenu = uicontextmenu();
% Construct the menu button and attach the context menu to it
mb = uicontrol(spanel, ...
'Style','PushButton', ...
'Position',[5 5 20 20], ...
'CData',cdata, ...
'UIContextMenu',cmenu, ...
'Callback', @ButtonCallback, ...
'DeleteFcn', @(~,~) DeleteFcn(cmenu), ...
'ToolTipString', 'Plot menu');
% Assign the callback function responsible for resizing the subpanel
spanel.SizeChangedFcn = @SizeChanged;
% Callback function for resizing the subpanel
function SizeChanged(~,~)
parentpos = getpixelposition(spanel);
mb.Position(2) = parentpos(4) - mb.Position(4) - 6;
end
% Callback function for the subpanel menu button
function ButtonCallback(src,~)
pos = getpixelposition(src,true);
mb.UIContextMenu.Position = pos([1 2]);
mb.UIContextMenu.Visible='on';
end
% Callback function for cleaning up the context menu
function DeleteFcn(cmenu)
% Delete the context menu
delete(cmenu);
end
end
function menuitem = SelectorMenu(menu,xxxdef,callback,separator,tag,checkindx)
% Construct a set of selector menu items for each entry in xxxdef.
%
% menuitem = SelectorMenu(menu,xxxdef,callback,separator,tag,checkindx)
%
% The menu items are assigned to the specified parent menu.
% Each one of those menu items are assigned the specified tag
% and callback function. The Check state of all menu items are
% set to 'off' except for the checkindx'th menu item which is
% set to 'on'. The separator flag ('on' or 'off') only applies
% to the first menu item. It is useful when concatenating
% several selector menus to the same parent menu.
%
% Returns a handle to the checked menu item
n = numel(xxxdef);
for xxxindx=1:n
UserData.xxxname = xxxdef(xxxindx).name;
UserData.xxxindx = xxxindx;
UserData.valindx = 1:numel(xxxdef(xxxindx).value);
UserData.label = xxxdef(xxxindx).name;
UserData.rootmenu = menu;
if xxxindx==checkindx
% this menu item is checked 'on'
menuitem = uimenu('Parent',menu, ...
'Label',UserData.xxxname, ...
'Separator',separator, ...
'Checked','on', ...
'Tag',tag, ...
'UserData',UserData, ...
'Callback',callback);
else
% this menu item is checked 'off'
uimenu('Parent',menu, ...
'Label',UserData.xxxname, ...
'Separator',separator, ...
'Checked','off', ...
'Tag',tag, ...
'UserData',UserData, ...
'Callback',callback);
end
separator='off';
end
end
function menuitem = SelectorMenuFull(menu,xxxdef,callback,separator,tag,checkindx,checkvalindx)
% Construct a fully enumerated selector menu for each entry in xxxdef.
%
% menuitem = SelectorMenuFull(menu,xxxdef,callback,separator,tag,checkindx,checkvalindx)
%
% Similar to SelectorMenu except that the individual elements
% of vector and matrix values in xxxdef are all enumerated as
% seperate menu items.
n = numel(xxxdef);
for xxxindx=1:n
xxxname = xxxdef(xxxindx).name;
[nr nc] = size(xxxdef(xxxindx).value);
% Test whether the xxxdef.value is a scalar, vector or matrix
if nr*nc==1
% The xxxdef value is a scalar.
% No nested menu items are required.
UserData.xxxname = xxxname;
UserData.xxxindx = xxxindx;
UserData.valindx = 1;
UserData.label = xxxname;
UserData.rootmenu = menu;
if xxxindx==checkindx
% this menu item is checked 'on'
menuitem = uimenu('Parent',menu, ...
'Label',xxxname, ...
'Separator',separator, ...
'Tag',tag, ...
'Checked','on', ...
'UserData',UserData, ...
'Callback',callback);
else
% this menu item is checked 'off'
uimenu('Parent',menu, ...
'Label',xxxname, ...
'Separator',separator, ...
'Tag',tag, ...
'Checked','off', ...
'UserData',UserData, ...
'Callback',callback);
end
elseif nr==1 || nc==1
% The xxxdef value is a vector (1 x nc) or (nr x 1).
% Construct a nested menu with one item per entry in the vector.
submenu = uimenu('Parent',menu, ...
'Label',xxxname, ...
'Separator',separator);
for valindx=1:(nr*nc)
% break up large menu lists into nested submenus
if mod(valindx,26)==0
submenu = uimenu('Parent',submenu, 'Label','more');
end
% gather the menuitem details
label = num2str(valindx,[xxxname,'(%d)']);
UserData.xxxname = xxxname;
UserData.xxxindx = xxxindx;
UserData.valindx = valindx;
UserData.label = num2str(valindx,[xxxname,'_{%d}']);
UserData.rootmenu = menu;
if xxxindx==checkindx && valindx==checkvalindx
% this menu item is checked 'on'
menuitem = uimenu('Parent',submenu,...
'Label',label, ...
'Tag',tag, ...
'Checked','on', ...
'UserData',UserData, ...
'Callback',callback);
else
% this menu item is checked 'off'
uimenu('Parent',submenu,...
'Label',label, ...
'Tag',tag, ...
'Checked','off', ...
'UserData',UserData, ...
'Callback',callback);
end
end
else
% The xxxdef value is a matrix (nr x nc)
% Construct a doubly-nested menu with one submenu for each row
% and one item per entry in that row.
submenu = uimenu('Parent',menu, ...
'Label',xxxname, ...
'Separator',separator);
valindx = 1;
for r=1:nr
% break up large menu lists into nested submenus
if mod(r,26)==0
submenu = uimenu('Parent',submenu, 'Label','more');
end
% construct a subsubmenu for the column values
subsubmenu = uimenu('Parent',submenu, 'Label',num2str(r,[xxxname,'(%d,-)']));
for c=1:nc
% break up large menu lists into nested submenus
if mod(c,26)==0
subsubmenu = uimenu('Parent',subsubmenu, 'Label','more');
end
% gather menuitem details
label = num2str([r,c],[xxxname,'(%d,%d)']);
UserData.xxxname = xxxname;
UserData.xxxindx = xxxindx;
UserData.valindx = valindx;
UserData.label = num2str([r,c],[xxxname,'_{%d,%d}']);
UserData.rootmenu = menu;
if xxxindx==checkindx && valindx==checkvalindx
% this menu item is checked 'on'
menuitem = uimenu('Parent',subsubmenu,...
'Label',label, ...
'Tag',tag, ...
'Checked','on', ...
'UserData',UserData, ...
'Callback',callback);
else
% this menu item is checked 'off'
uimenu('Parent',subsubmenu,...
'Label',label, ...
'Tag',tag, ...
'Checked','off', ...
'UserData',UserData, ...
'Callback',callback);
end
valindx = valindx + 1;
end
end
end
% The separator only applies to the first menu item
separator='off';
end
end
function SelectorCheckItem(menuitem)
% Check 'on' the selected menu item and check 'off' the others.
%
% SelectorCheckItem(menuitem)
%
% The Checked state of the given menu item is set to 'on'
% while the Checked state of all other menu items with the
% same tag as that menuitem are set to 'off'.
objs = findobj(menuitem.UserData.rootmenu,'Tag',menuitem.Tag);
for indx=1:numel(objs)
objs(indx).Checked='off';
end
% now check 'on' the selected menu item
menuitem.Checked='on';
end
function lim = RoundLim(lo,hi)
% Utility function for rounding limits to 2 significant digits.
%
% lim = RoundLim(lo,hi)
%
% Returns a [lo hi] limit that brackets the specified range.
% As a safety precaution, the span of the returned limit will
% not be larger than 2.4e100 and no smaller than 2e-4.
if lo < -1e100
lo = -1e100;
end
if hi > 1e100
hi = 1e100;
end
% number of significant digits we want
sig = 2;
% compute the significant base (with small safety margin)
lobase = 10^floor(log10(abs(lo)+1e-4)-sig+1);
hibase = 10^floor(log10(abs(hi)+1e-4)-sig+1);
% compute the new rounded limits (with a small safety margin)
newlo = floor(lo/lobase-0.5)*lobase;
newhi = ceil(hi/hibase+0.5)*hibase;
lim = [newlo newhi];
%d = hi-lo;
%lim = round([lo-0.1*d, hi+0.1*d],2,'significant') + [-1e-4 1e-4];
end
function PanelSelectionChangedFcn(~,evnt)
% Callback executed by the parent tabgroup when the selected tab changes.
%disp('bdPanel.PanelSelectionChangedFcn');
% Hide the panel menu associated with the old tab
evnt.OldValue.UserData.menu.Visible = 'off';
% Reveal the panel menu associated with the new tab
evnt.NewValue.UserData.menu.Visible = 'on';
end
end
end