-
Notifications
You must be signed in to change notification settings - Fork 0
/
DarkAdaptationPlotter_code.m
396 lines (330 loc) · 15.1 KB
/
DarkAdaptationPlotter_code.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
classdef DarkAdaptationPlotter < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
DarkAdaptationPlotterUIFigure matlab.ui.Figure
FileParentMenu matlab.ui.container.Menu
OpenDataMenu matlab.ui.container.Menu
ClearDataMenu matlab.ui.container.Menu
ExportPreviewMenu matlab.ui.container.Menu
ExportAsMenu matlab.ui.container.Menu
ExitMenu matlab.ui.container.Menu
EditMenu matlab.ui.container.Menu
FontMenu matlab.ui.container.Menu
PreferencesMenu matlab.ui.container.Menu
HelpParentMenu matlab.ui.container.Menu
HelpMenu matlab.ui.container.Menu
AboutMenu matlab.ui.container.Menu
AxesPanel matlab.ui.container.Panel
Table matlab.ui.control.Table
end
properties (Access = private)
dap_axes = []
dap_table = []
dap_plots = []
dap_data = []
font_settings = []
dap_input_files = []
dap_output_files = []
dap_preferences = []
end
methods (Access = private)
function update_plot(app, row)
app.update_visible(row);
app.update_color(row);
app.update_marker(row);
app.update_marker_size(row);
end
function update_visible(app, row)
id = app.dap_table.get_id(row);
visible = app.dap_table.get_visible(row);
app.dap_plots.update_visible(id, visible);
end
function update_color(app, row)
id = app.dap_table.get_id(row);
color = app.dap_table.get_color(row);
app.dap_plots.update_color(id, color);
end
function update_marker(app, row)
id = app.dap_table.get_id(row);
marker = app.dap_table.get_marker(row);
app.dap_plots.update_marker(id, marker);
end
function update_marker_size(app, row)
id = app.dap_table.get_id(row);
size = app.dap_table.get_size(row);
app.dap_plots.update_marker_size(id, size);
end
function pick_color(app, row)
color = app.dap_table.get_color(row);
color.rgb = uisetcolor(color.rgb, "Select a plot color");
app.dap_table.set_color(row, color);
drawnow();
end
function resize(app)
position = app.DarkAdaptationPlotterUIFigure.Position;
% table
TABLE_X_FRACTION = 0.33;
table_width = round(TABLE_X_FRACTION .* position(3));
SCROLLBAR_WIDTH = 18;
table_width_for_columns = max(table_width - SCROLLBAR_WIDTH, 0);
table_height = position(4);
app.Table.Position = [1 1 table_width table_height];
width_weights = app.dap_table.WIDTH_WEIGHTS;
width_fractions = width_weights ./ sum(width_weights);
column_widths = table_width_for_columns .* width_fractions;
app.Table.ColumnWidth = num2cell(column_widths);
% panel
panel_x = app.Table.Position(1) + app.Table.Position(3) - 1;
panel_y = 1;
panel_width = position(3) - table_width;
panel_height = position(4);
app.AxesPanel.Position = [panel_x panel_y panel_width panel_height];
end
function close(app)
delete(app);
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
extend_search_path();
config = Config("config.json");
da = dapAxes(app.AxesPanel, config);
recovery_line = dapRecoveryLine();
da.draw_on(@recovery_line.set_parent);
da.register_callback("recovery_line", @recovery_line.update);
da.update();
dd = dapData();
dt = dapPlotTable(app.Table);
dp = dapPlots(config, da);
dof = dapOutputFiles(config, da);
dif = dapInputFiles(config, dd, dt, dp);
dpref = dapPreferences(config, [440, 300], "res/prefs.json");
dpref.register_callback("dapAxes", @da.update);
dpref.register_callback("dapPlots", @dp.update_draw);
dpref.register_callback("dapOutputFiles", @dof.update);
app.dap_axes = da;
app.dap_data = dd;
app.dap_table = dt;
app.dap_plots = dp;
app.dap_output_files = dof;
app.dap_input_files = dif;
app.dap_preferences = dpref;
resize(app);
end
% Size changed function: DarkAdaptationPlotterUIFigure
function DarkAdaptationPlotterUIFigureSizeChanged(app, event)
if isempty(app.dap_axes)
return;
end
resize(app);
end
% Cell edit callback: Table
function TableCellEdit(app, event)
%{
Only the visibility and marker columns are directly editable.
For color editing see PlotTableCellSelection().
%}
%newData = event.NewData;
indices = event.Indices;
row = indices(1);
col = indices(2);
if col == app.dap_table.VISIBLE_COL
app.update_plot(row);
elseif col == app.dap_table.MARKER_COL
app.update_plot(row);
elseif col == app.dap_table.SIZE_COL
app.update_plot(row);
end
end
% Cell selection callback: Table
function TableCellSelection(app, event)
%{
A way to allow editing of color column using a picker.
%}
indices = event.Indices;
if isempty(indices)
return;
end
row = indices(1);
col = indices(2);
if col == app.dap_table.COLOR_COL
app.pick_color(row);
app.update_plot(row);
end
end
% Menu selected function: OpenDataMenu
function OpenDataMenuSelected(app, event)
assert(~isempty(app.dap_input_files));
app.dap_input_files.ui_open_file(app.DarkAdaptationPlotterUIFigure)
end
% Menu selected function: ClearDataMenu
function ClearDataMenuSelected(app, event)
assert(~isempty(app.dap_data));
assert(~isempty(app.dap_plots));
assert(~isempty(app.dap_table));
assert(~isempty(app.dap_axes));
CLEAR_OPT = "Clear all data";
CANCEL_OPT = "Go back";
CANCEL_NUM = 2;
selection = uiconfirm(...
app.DarkAdaptationPlotterUIFigure, ...
["Really clear all data?" "This cannot be undone."], ...
"Clear data?", ...
"options", [CLEAR_OPT CANCEL_OPT], ...
"defaultoption", CANCEL_NUM, ...
"canceloption", CANCEL_NUM ...
);
switch selection
case CLEAR_OPT
app.dap_data.clear();
app.dap_plots.clear();
app.dap_table.clear();
app.dap_axes.update();
case CANCEL_OPT; return;
otherwise; assert(false);
end
end
% Menu selected function: ExportPreviewMenu
function ExportPreviewMenuSelected(app, event)
app.dap_output_files.ui_run_export_preview(app.DarkAdaptationPlotterUIFigure);
end
% Menu selected function: ExportAsMenu
function ExportAsMenuSelected(app, event)
assert(~isempty(app.dap_output_files));
app.dap_output_files.ui_run_export_as(app.DarkAdaptationPlotterUIFigure);
end
% Menu selected function: ExitMenu
function ExitMenuSelected(app, event)
app.DarkAdaptationPlotterUIFigureCloseRequest(event);
end
% Menu selected function: FontMenu
function FontMenuSelected(app, event)
assert(~isempty(app.dap_preferences));
app.dap_preferences.ui_update_font();
end
% Menu selected function: PreferencesMenu
function PreferencesMenuSelected(app, event)
assert(~isempty(app.dap_preferences));
p = app.DarkAdaptationPlotterUIFigure.Position;
x = p(1) + 24;
y = p(4) + p(2) - 1 - 24;
app.dap_preferences.ui_update_preferences(x, y);
end
% Close request function: DarkAdaptationPlotterUIFigure
function DarkAdaptationPlotterUIFigureCloseRequest(app, event)
EXIT_OPT = "Exit now";
CANCEL_OPT = "Go back";
CANCEL_NUM = 2;
selection = uiconfirm(...
app.DarkAdaptationPlotterUIFigure, ...
"Are you sure you want to exit?", ...
"Exit?", ...
"options", [EXIT_OPT, CANCEL_OPT], ...
"defaultoption", CANCEL_NUM, ...
"canceloption", CANCEL_NUM ...
);
switch selection
case EXIT_OPT; app.close();
case CANCEL_OPT; return;
otherwise; assert(false);
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create DarkAdaptationPlotterUIFigure and hide until all components are created
app.DarkAdaptationPlotterUIFigure = uifigure('Visible', 'off');
app.DarkAdaptationPlotterUIFigure.AutoResizeChildren = 'off';
app.DarkAdaptationPlotterUIFigure.Position = [100 100 1200 600];
app.DarkAdaptationPlotterUIFigure.Name = 'Dark Adaptation Plotter';
app.DarkAdaptationPlotterUIFigure.CloseRequestFcn = createCallbackFcn(app, @DarkAdaptationPlotterUIFigureCloseRequest, true);
app.DarkAdaptationPlotterUIFigure.SizeChangedFcn = createCallbackFcn(app, @DarkAdaptationPlotterUIFigureSizeChanged, true);
% Create FileParentMenu
app.FileParentMenu = uimenu(app.DarkAdaptationPlotterUIFigure);
app.FileParentMenu.Text = 'File';
% Create OpenDataMenu
app.OpenDataMenu = uimenu(app.FileParentMenu);
app.OpenDataMenu.MenuSelectedFcn = createCallbackFcn(app, @OpenDataMenuSelected, true);
app.OpenDataMenu.Text = 'Open Data...';
% Create ClearDataMenu
app.ClearDataMenu = uimenu(app.FileParentMenu);
app.ClearDataMenu.MenuSelectedFcn = createCallbackFcn(app, @ClearDataMenuSelected, true);
app.ClearDataMenu.Text = 'Clear Data';
% Create ExportPreviewMenu
app.ExportPreviewMenu = uimenu(app.FileParentMenu);
app.ExportPreviewMenu.MenuSelectedFcn = createCallbackFcn(app, @ExportPreviewMenuSelected, true);
app.ExportPreviewMenu.Separator = 'on';
app.ExportPreviewMenu.Text = 'Export Preview...';
% Create ExportAsMenu
app.ExportAsMenu = uimenu(app.FileParentMenu);
app.ExportAsMenu.MenuSelectedFcn = createCallbackFcn(app, @ExportAsMenuSelected, true);
app.ExportAsMenu.Text = 'Export As...';
% Create ExitMenu
app.ExitMenu = uimenu(app.FileParentMenu);
app.ExitMenu.MenuSelectedFcn = createCallbackFcn(app, @ExitMenuSelected, true);
app.ExitMenu.Separator = 'on';
app.ExitMenu.Text = 'Exit';
% Create EditMenu
app.EditMenu = uimenu(app.DarkAdaptationPlotterUIFigure);
app.EditMenu.Text = 'Edit';
% Create FontMenu
app.FontMenu = uimenu(app.EditMenu);
app.FontMenu.MenuSelectedFcn = createCallbackFcn(app, @FontMenuSelected, true);
app.FontMenu.Text = 'Font...';
% Create PreferencesMenu
app.PreferencesMenu = uimenu(app.EditMenu);
app.PreferencesMenu.MenuSelectedFcn = createCallbackFcn(app, @PreferencesMenuSelected, true);
app.PreferencesMenu.Text = 'Preferences...';
% Create HelpParentMenu
app.HelpParentMenu = uimenu(app.DarkAdaptationPlotterUIFigure);
app.HelpParentMenu.Text = 'Help';
% Create HelpMenu
app.HelpMenu = uimenu(app.HelpParentMenu);
app.HelpMenu.Text = 'Help...';
% Create AboutMenu
app.AboutMenu = uimenu(app.HelpParentMenu);
app.AboutMenu.Separator = 'on';
app.AboutMenu.Text = 'About...';
% Create Table
app.Table = uitable(app.DarkAdaptationPlotterUIFigure);
app.Table.ColumnName = {'ID'; '👁️'; 'Color'; 'Marker'; 'Size'};
app.Table.ColumnWidth = {96, 32, 50, 60, 32};
app.Table.RowName = {};
app.Table.ColumnSortable = [true true false false false];
app.Table.ColumnEditable = [false true true true true];
app.Table.CellEditCallback = createCallbackFcn(app, @TableCellEdit, true);
app.Table.CellSelectionCallback = createCallbackFcn(app, @TableCellSelection, true);
app.Table.Position = [1 1 239.754098360656 600];
% Create AxesPanel
app.AxesPanel = uipanel(app.DarkAdaptationPlotterUIFigure);
app.AxesPanel.AutoResizeChildren = 'off';
app.AxesPanel.Position = [240 1 961 600];
% Show the figure after all components are created
app.DarkAdaptationPlotterUIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = DarkAdaptationPlotter
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.DarkAdaptationPlotterUIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.DarkAdaptationPlotterUIFigure)
end
end
end