-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathasCmplxChooserClass.m
311 lines (268 loc) · 11.5 KB
/
asCmplxChooserClass.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
% Copyright (C) 2009-2013 Biomedizinische NMR Forschungs GmbH
% http://www.biomednmr.mpg.de
% Author: Tilman Johannes Sumpf <tsumpf@gwdg.de>
%
% Distributed under the Boost Software License, Version 1.0.
% (See accompanying file LICENSE_1_0.txt or copy at
% http://www.boost.org/LICENSE_1_0.txt)
classdef asCmplxChooserClass < handle
properties (GetAccess = private, SetAccess = private)
ph = 0; % panel handle
sendButtonH = [];
ddmh = 0; % dropdownmenu handle
updFigCb = '';
apply2allCb = []; % send to all relatives callback
cmh = struct; % context menu handle
phaseRepr = 1; % 1 = show phase in degrees,
% 2 = show phase in radiants
selectionValue = []; % this property is (...should be) always equal
% to "get(obj.ddmh,'Value')" and allows for
% recovering the value even if the according
% UI has been deleted. This can be necessary
% when rebuilding an asObj which has been
% previously stored by the matlab save command
enabled = true;
iscomplex = true;
end
properties (Constant)
ALL_STATES = {'Abs','Re','Im','Com','Pha'};
ALL_STATES_FULL = {'Magnitude','Real','Imaginary','Complex','Phase'};
REAL_TEXT = {'Magnitude (m)','Real (r)'};
CPLX_TEXT = {'Magnitude (m)','Real (r)','Imaginary (i)','Complex (M)','Phase (p)'};
end
properties (GetAccess = public, SetAccess = private)
% context menu toggles which can be read from the outside
sendToggleState = false;
end
methods
function obj = asCmplxChooserClass(...
parentPanel,...
updFigCb,...
apply2allCb,...
sendIcon)
obj.apply2allCb = apply2allCb;
obj.updFigCb = updFigCb;
obj.ph = parentPanel;
% create send button
obj.sendButtonH = uicontrol('Style','togglebutton',...
'Parent', obj.ph,...
'Units','normalized',...
'Position',[.85,0,.15,.31],...
'tooltip','Send selection to relatives',...
'Callback',@(src,evnt)obj.toggleSend2all(),...
'CData',sendIcon);
% create dropdown menu
obj.ddmh = uicontrol('Style','popupmenu','String',obj.CPLX_TEXT,...
'Units','normalized','pos',[0. -.04 .85 .34],'parent',obj.ph,'HandleVisibility','off',...
'Callback',@(src,evnt)obj.stateChangeCb(false));
% select 'complex' per default
set(obj.ddmh,'Value',4);
obj.selectionValue = 4;
% context menu -----------
obj.cmh.base = uicontextmenu;
obj.cmh.send2all = uimenu(obj.cmh.base,'Label','Send selection to relatives' ,...
'Checked','off',...
'callback',@(src,evnt)obj.toggleSend2all);
obj.cmh.degrees = uimenu(obj.cmh.base,'Label','Represent phase in degrees' ,...
'Checked','on','Separator','on',...
'callback',@(src,evnt)obj.togglePhaseRepresentationCb(src));
obj.cmh.radiants= uimenu(obj.cmh.base,'Label','Represent phase in radiants' ,...
'Checked','off',...
'callback',@(src,evnt)obj.togglePhaseRepresentationCb(src));
% assign context menu to the dropdown menu and the parent panel
set(obj.ddmh,'uicontextmenu',obj.cmh.base);
set(obj.ph,'uicontextmenu',obj.cmh.base);
end
function sel = getSelection(obj, returnType)
% get selected complex part
% return type can be:
% 1 (default) : a string with the standard abbreviation of
% the complex parts (Abs, Re, Im,...)
% 2 : a string with the full name of the complex
% part (Magnitude, Real, Imaginary...)
% 3 : a number corresponding to the part
% (1 = Abs, 2 = Re, 3 = Im, 4 = Com, 5 = Pha)
if nargin < 2
returnType = 1;
end
if ishandle(obj.ddmh)
% get selected value from dropdown menu
v = get(obj.ddmh,'Value');
else
% restore selection from this object's property
v = obj.selectionValue;
end
switch returnType
case 1
% return the according (abbreviated human readable) state string
sel = obj.ALL_STATES{v};
case 2
% return the according (human readable) state string
sel = obj.ALL_STATES_FULL{v};
case 3
sel = v;
end
end
function togglePhaseRepresentationCb(obj, src)
if obj.enabled
switch(src)
case obj.cmh.degrees
obj.setPhaseToDegrees();
case obj.cmh.radiants
obj.setPhaseToRadiants();
end
end
end
function setPhaseToDegrees(obj)
if obj.enabled
obj.phaseRepr = 1;
set(obj.cmh.degrees,'Checked','on');
set(obj.cmh.radiants,'Checked','off');
obj.updFigCb();
end
end
function setPhaseToRadiants(obj)
if obj.enabled
obj.phaseRepr = 2;
set(obj.cmh.degrees,'Checked','off');
set(obj.cmh.radiants,'Checked','on');
obj.updFigCb();
end
end
function focus(obj)
% enables the uicontrol
% (this was written to put the focus away from the selection
% class at initialization of arrShow objects. As a result, key
% press calbacks can be evaluated without initial mouseclick on
% the figure window)
if obj.enabled
uicontrol(obj.ddmh);
end
end
function fun = getFunPointer(obj)
sel = obj.getSelection;
switch sel
case 'Abs'
fun = @abs;
case 'Re'
fun = @real;
case 'Im'
fun = @imag;
case 'Pha'
if obj.phaseRepr == 1
fun = @myAngle;
else
fun = @angle;
end
case 'Com'
fun = @mynop;
end
function y = myAngle(x)
y = angle(x) * 180 / pi;
end
function x = mynop(x)
end
end
function ph = getPanelHandle(obj)
ph = obj.ph;
end
function setSelection(obj, str, suppressUpdFigCb, sendToRelatives)
if obj.enabled
if nargin < 4
sendToRelatives = obj.sendToggleState;
if nargin < 3
suppressUpdFigCb = false;
end
end
switch lower(str)
case {'magnitude','mag','abs','a','m'}
set(obj.ddmh,'Value',1);
case {'real','re','r'}
set(obj.ddmh,'Value',2);
case {'imaginary','im','ima','imag','i'}
if obj.iscomplex
set(obj.ddmh,'Value',3);
end
case {'complex','cplx','com','c'}
if obj.iscomplex
set(obj.ddmh,'Value',4);
end
case {'phase','pha','p'}
if obj.iscomplex
set(obj.ddmh,'Value',5);
end
end
obj.stateChangeCb(suppressUpdFigCb, sendToRelatives);
end
end
function lockImagAndPhase(obj, suppressCallback)
if obj.enabled
if nargin < 2
suppressCallback = true;
end
obj.iscomplex = false;
switch obj.getSelection
case {'Im','Pha','Com'}
obj.setSelection('Re', suppressCallback);
end
set(obj.ddmh,'String',obj.REAL_TEXT);
end
end
function unlockImagAndPhase(obj)
if obj.enabled
obj.iscomplex = true;
set(obj.ddmh,'String',obj.CPLX_TEXT);
end
end
function toggleSend2all(obj,bool)
if obj.enabled
if nargin > 1
set(obj.cmh.send2all,'Checked',arrShow.boolToOnOff(~bool));
end
switch get(obj.cmh.send2all,'Checked')
case 'off'
obj.sendToggleState = true;
set(obj.cmh.send2all,'Checked','on');
set(obj.sendButtonH,'Value',1);
obj.stateChangeCb(true);
case 'on'
obj.sendToggleState = false;
set(obj.cmh.send2all,'Checked','off');
set(obj.sendButtonH,'Value',0);
end
end
end
function enable(obj,state)
obj.enabled = state;
onOff = arrShow.boolToOnOff(state);
set(obj.ddmh,'enable',onOff);
set(obj.sendButtonH,'enable',onOff);
end
% defining this delete functions didn't seem to bee a good idea. Apparently
% it dometimes happens, that an orphaned object is deleted which then might
% destroy panelhandles which are still in use...
% function delete(obj)
% if ishandle(obj.ph)
% delete(obj.ph);
% end
% clear obj;
% end
end
methods (Access = private)
function stateChangeCb(obj, suppressUpdFigCb, sendToRelatives)
if nargin < 3
sendToRelatives = obj.sendToggleState;
end
% backup the selection in the local property
obj.selectionValue = get(obj.ddmh,'Value');
if nargin < 2 || suppressUpdFigCb == false
% run update figure callback
obj.updFigCb();
end
if sendToRelatives
sel = obj.getSelection;
obj.apply2allCb('complexSelect.setSelection', false, sel, false, false);
end
end
end
end