-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenericDialog.m
449 lines (364 loc) · 12.6 KB
/
GenericDialog.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
classdef GenericDialog < handle
%GENERICDIALOG A generic dialog, similar to ImageJ's.
%
% Class GenericDialog
%
% Example
% % creates a new dialog, and populates it with some fields
% gd = GenericDialog('Create Image');
% addTextField(gd, 'Name: ', 'New Image');
% addNumericField(gd, 'Width: ', 320, 0);
% addNumericField(gd, 'Height: ', 200, 0);
% addChoice(gd, 'Type: ', {'uint8', 'uint16', 'double'}, 'uint8');
% addCheckBox(gd, 'Display', true);
%
% % displays the dialog, and waits for user
% showDialog(gd);
% % check if ok or cancel button was clicked
% if wasCanceled(gd)
% return;
% end
%
% % retrieve the user inputs
% name = getNextString(gd);
% width = getNextNumber(gd);
% height = getNextNumber(gd);
% type = getNextString(gd);
% display = getNextBoolean(gd);
%
% % create a new image based on user inputs
% img = zeros([height width], type);
% if display
% imshow(img);
% title(name);
% end
%
% See also
% inputdlg, figure, uix.VBox
% ------
% Author: David Legland
% e-mail: [email protected]
% Created: 2014-04-18, using Matlab 7.9.0.529 (R2009b)
% Copyright 2014 INRA - Cepia Software Platform.
%% Properties
properties
handles;
controlHandles = [];
currentIndex = 1;
boxSizes = [];
% a list of answers (one cell for each item that was added).
answers = {};
% the string corresponding to the button used to close the dialog.
% can be one of 'ok', 'cancel'.
closingButton = '';
end % end properties
%% Constructor
methods
function obj = GenericDialog(varargin)
% Constructor for GenericDialog class.
createLayout(obj, varargin{:});
end
end % end constructors
methods (Access = private)
function createLayout(obj, varargin)
% Initialize the layout (figure and widgets).
hf = createFigure(obj, varargin{:});
obj.handles.figure = hf;
% vertical layout for widgets and control panels
vb = uix.VBox('Parent', hf, 'Spacing', 5, 'Padding', 5);
% create an empty panel that will contain widgets
obj.handles.mainPanel = uix.VBox('Parent', vb);
% button for control panel
buttonsPanel = uix.HButtonBox('Parent', vb, 'Padding', 5);
uicontrol( 'Parent', buttonsPanel, ...
'String', 'OK', ...
'Callback', @obj.onButtonOK);
uicontrol( 'Parent', buttonsPanel, ...
'String', 'Cancel', ...
'Callback', @obj.onButtonCancel);
set(vb, 'Heights', [-1 40] );
end
function hf = createFigure(obj, varargin)
% Create new figure and return its handle.
% parse dialog title
if isempty(varargin)
dlgName = 'Generic Dialog';
else
dlgName = varargin{1};
end
% computes a new handle index large enough not to collide with
% common figure handles
while true
newFigHandle = 30000 + randi(10000);
if ~ishandle(newFigHandle)
break;
end
end
% create the figure that will contains the display
hf = figure(newFigHandle);
set(hf, ...
'Name', dlgName, ...
'NumberTitle', 'off', ...
'MenuBar', 'none', ...
'Toolbar', 'none', ...
'NextPlot', 'new', ...
'WindowStyle', 'modal', ...
'Visible', 'off');
set(hf, 'units', 'pixels');
pos = get(hf, 'Position');
pos(3:4) = [200 250];
set(hf, 'Position', pos);
obj.handles.figure = hf;
end
end
%% Generic Methods
methods
function showDialog(obj)
% Make the dialog visible, and waits for user validation.
setVisible(obj, true);
waitForUser(obj);
end
function setSize(obj, newSize, unit)
if ~isa('unit', 'var')
unit = 'pixels';
end
set(obj.handles.figure, 'units', unit);
pos = get(obj.handles.figure, 'Position');
pos(3:4) = newSize;
set(obj.handles.figure, 'Position', pos);
end
function setVisible(obj, value)
if value
set(obj.handles.figure, 'Visible', 'on');
set(obj.handles.figure, 'WindowStyle', 'modal');
else
set(obj.handles.figure, 'Visible', 'off');
end
end
function b = wasOked(obj)
b = strcmp(obj.closingButton, 'ok');
end
function b = wasCanceled(obj)
b = strcmp(obj.closingButton, 'cancel');
end
function closeFigure(obj)
% close the current figure
if ~isempty(obj.handles.figure)
close(obj.handles.figure);
end
end
end
%% Management of choice items
methods
function [h, ht] = addTextField(obj, label, text, cb)
% Add a text field to this dialog.
%
% usage:
% addTextField(GD, LABEL, INPUTTEXT);
hLine = uix.HBox('Parent', obj.handles.mainPanel, ...
'Spacing', 5, 'Padding', 5);
% Label of the widget
ht = addLabel(hLine, label);
% creates the new control
% bgColor = getWidgetBackgroundColor(this);
bgColor = [1 1 1];
h = uicontrol(...
'Style', 'Edit', ...
'Parent', hLine, ...
'String', text, ...
'BackgroundColor', bgColor);
if exist('cb', 'var')
set(h, 'Callback', cb);
end
% keep widget handle for future use
obj.controlHandles = [obj.controlHandles h];
% setup size in horizontal direction
set(hLine, 'Widths', [-5 -5]);
% update vertical size of widgets
obj.boxSizes = [obj.boxSizes 35];
set(obj.handles.mainPanel, 'Heights', obj.boxSizes);
end
function [h, ht] = addNumericField(obj, label, value, nDigits, cb)
% Add a numeric text field to this dialog.
%
% usage:
% addNumericField(GD, LABEL, INPUTTEXT);
% create horizontal box
hLine = uix.HBox('Parent', obj.handles.mainPanel, ...
'Spacing', 5, 'Padding', 5);
% Label of the widget
ht = addLabel(hLine, label);
% create initial text from value
pattern = ['%.' num2str(nDigits) 'f'];
text = sprintf(pattern, value);
% creates the new control
% bgColor = getWidgetBackgroundColor(this);
bgColor = [1 1 1];
h = uicontrol(...
'Style', 'Edit', ...
'Parent', hLine, ...
'String', text, ...
'BackgroundColor', bgColor);
if exist('cb', 'var')
set(h, 'Callback', cb);
set(h, 'KeyPressFcn', cb);
end
% % try to add a key press listener
% jtf = findjobj(h);
% jtfh = handle(jtf, 'callbackproperties');
% set(jtfh, 'KeyPressedCallback', 'disp(''text modified'')')
% h2 = uicontrol('style', 'edit', 'string', 'Hello!');
% keep widget handle for future use
obj.controlHandles = [obj.controlHandles h];
% setup size in horizontal direction
set(hLine, 'Widths', [-5 -5]);
% update vertical size of widgets
obj.boxSizes = [obj.boxSizes 35];
set(obj.handles.mainPanel, 'Heights', obj.boxSizes);
end
function h = addCheckBox(obj, label, checked, cb)
% Add a check box for choosing a boolean to this dialog.
%
% usage:
% addCheckBox(GD, LABEL, STATE);
% create horizontal box
hLine = uix.HBox('Parent', obj.handles.mainPanel, ...
'Spacing', 5, 'Padding', 5);
% use default value if not specified
if ~exist('checked', 'var')
checked = false;
end
% creates the new control
% bgColor = getWidgetBackgroundColor(this);
% bgColor = [1 1 1];
h = uicontrol(...
'Style', 'Checkbox ', ...
'Parent', hLine, ...
'String', label, ...
'Value', checked);
if exist('cb', 'var')
set(h, 'Callback', cb);
end
% keep widget handle for future use
obj.controlHandles = [obj.controlHandles h];
% setup size in horizontal direction
set(hLine, 'Widths', -5);
% update vertical size of widgets
obj.boxSizes = [obj.boxSizes 25];
set(obj.handles.mainPanel, 'Heights', obj.boxSizes);
end
function [h, ht] = addChoice(obj, label, choiceLabels, initialValue, cb)
% Add choice as a popupmenu.
%
% usage:
% addChoice(GD, LABEL, CHOICES, INITIALVALUE);
% create horizontal box
hLine = uix.HBox('Parent', obj.handles.mainPanel, ...
'Spacing', 5, 'Padding', 5);
% Label of the widget
ht = addLabel(hLine, label);
% set initial value as numeric if not the case
if ~exist('initialValue', 'var')
initialValue = 1;
elseif ischar(initialValue)
ind = find(strcmp(choiceLabels, initialValue), 1);
if isempty(ind)
error(['Could not find initial value [' initialValue ...
'] within the list of choices']);
end
initialValue = ind;
end
% creates the new control
% bgColor = getWidgetBackgroundColor(this);
bgColor = [1 1 1];
h = uicontrol(...
'Style', 'PopupMenu', ...
'Parent', hLine, ...
'String', choiceLabels, ...
'Value', initialValue, ...
'BackgroundColor', bgColor);
if isa('cb', 'var')
set(h, 'Callback', cb);
end
% keep widget handle for future use
obj.controlHandles = [obj.controlHandles h];
% setup size in horizontal direction
set(hLine, 'Widths', [-5 -5]);
% update vertical size of widgets
obj.boxSizes = [obj.boxSizes 35];
set(obj.handles.mainPanel, 'Heights', obj.boxSizes);
end
end % end methods
%% get widget results
methods
function string = getNextString(obj)
h = getNextControlHandle(obj);
string = get(h, 'String');
if strcmp(get(h, 'style'), 'popupmenu')
index = get(h, 'value');
string = string{index};
end
end
function index = getNextChoiceIndex(obj)
h = getNextControlHandle(obj);
if ~strcmp(get(h, 'style'), 'popupmenu')
error('Next control must be a popup menu');
end
index = get(h, 'value');
end
function value = getNextNumber(obj)
h = getNextControlHandle(obj);
string = get(h, 'String');
value = str2double(string);
if isnan(value)
error(['Could not parse value in string: ' string]);
end
end
function value = getNextBoolean(obj)
h = getNextControlHandle(obj);
type = get(h, 'style');
if ~strcmp(type, 'checkbox')
error(['Next item must be a checkbox, not a ' type]);
end
value = get(h, 'value');
end
function h = getNextControlHandle(obj)
% Iterate along the widgets, and returns the next handle.
% throw an error if no more
if obj.currentIndex > length(obj.controlHandles)
error('No more widget to process');
end
h = obj.controlHandles(obj.currentIndex);
obj.currentIndex = obj.currentIndex + 1;
end
function resetCounter(this)
this.currentIndex = 1;
end
end
%% Figure and control Callback
methods
function onButtonOK(obj, varargin)
obj.closingButton = 'ok';
set(obj.handles.figure, 'Visible', 'off');
end
function onButtonCancel(obj, varargin)
obj.closingButton = 'cancel';
set(obj.handles.figure, 'Visible', 'off');
end
function button = waitForUser(obj)
waitfor(obj.handles.figure, 'Visible', 'off');
button = obj.closingButton;
end
end
end % end classdef
%% Utility functions
function ht = addLabel(parent, label)
% Add a label to a widget, with predefined settings.
ht = uicontrol('Style', 'Text', ...
'Parent', parent, ...
'String', label, ...
'FontWeight', 'Normal', ...
'FontSize', 10, ...
'FontWeight', 'Normal', ...
'HorizontalAlignment', 'Right');
end