-
Notifications
You must be signed in to change notification settings - Fork 9
/
newSource.m
163 lines (139 loc) · 5.02 KB
/
newSource.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
% Copyright (c) 2012 Howard Hughes Medical Institute.
% All rights reserved.
% Use is subject to Janelia Farm Research Campus Software Copyright 1.1 license terms.
% http://license.janelia.org/license/jfrc_copyright_1_1.html
function [label, parent] = newSource()
handles.label = '';
handles.parent = '';
handles.figure = dialog(...
'Units', 'points', ...
'Name', 'New Source', ...
'Position', centerWindowOnScreen(350, 120), ...
'WindowKeyPressFcn', @(hObject, eventdata)keyPressCallback(hObject, eventdata, guidata(hObject)), ...
'Tag', 'figure');
% Label controls
uicontrol(...
'Parent', handles.figure,...
'Units', 'points', ...
'FontSize', 12,...
'HorizontalAlignment', 'right', ...
'Position', [10 80 100 18], ...
'String', 'Label:',...
'Style', 'text');
handles.labelEdit = uicontrol(...
'Parent', handles.figure,...
'Units', 'points', ...
'FontSize', 12,...
'HorizontalAlignment', 'left', ...
'Position', [115 80 225 26], ...
'String', '',...
'Style', 'edit', ...
'Tag', 'labelEdit');
% Source controls
uicontrol(...
'Parent', handles.figure,...
'Units', 'points', ...
'FontSize', 12,...
'HorizontalAlignment', 'right', ...
'Position', [10 50 100 18], ...
'String', 'Parent:',...
'Style', 'text');
handles.parentPopup = uicontrol(...
'Parent', handles.figure,...
'Units', 'points', ...
'FontSize', 12,...
'HorizontalAlignment', 'left', ...
'Position', [115 45 175 26], ...
'String', 'None',...
'Style', 'popupmenu', ...
'Tag', 'parentPopup');
handles.newSourceButton = uicontrol(...
'Parent', handles.figure,...
'Units', 'points', ...
'Callback', @(hObject,eventdata)createNewParent(hObject,eventdata,guidata(hObject)), ...
'Position', [287 52 50 22], ...
'String', 'New...', ...
'Tag', 'newParentButton');
updateParentsPopup(handles);
handles.cancelButton = uicontrol(...
'Parent', handles.figure,...
'Units', 'points', ...
'Callback', @(hObject,eventdata)cancelNewSource(hObject,eventdata,guidata(hObject)), ...
'Position', [10 10 56 20], ...
'String', 'Cancel', ...
'Tag', 'cancelButton');
handles.saveButton = uicontrol(...
'Parent', handles.figure,...
'Units', 'points', ...
'Callback', @(hObject,eventdata)saveNewSource(hObject,eventdata,guidata(hObject)), ...
'Position', [80 10 56 20], ...
'String', 'Save', ...
'Tag', 'saveButton');
guidata(handles.figure, handles);
uiwait;
handles = guidata(handles.figure);
label = handles.label;
parent = handles.parent;
close(handles.figure);
end
function updateParentsPopup(handles, sourceToSelect)
if nargin == 1
sourceToSelect = 'None';
end
sources = struct('label', 'None', 'parent', '');
if ispref('Symphony', 'Sources')
savedSources = getpref('Symphony', 'Sources');
sources = horzcat(sources, savedSources);
end
labels = sort({sources.label});
set(handles.parentPopup, 'String', labels);
value = find(strcmp(labels, sourceToSelect));
if isempty(value)
errordlg(['There is no source with the label ''' sourceToSelect ''''], 'Symphony');
value = 1;
end
set(handles.parentPopup, 'Value', value);
end
function keyPressCallback(hObject, eventdata, handles)
if strcmp(eventdata.Key, 'return')
% Move focus off of any edit text so the changes can be seen.
uicontrol(handles.saveButton);
saveNewSource(hObject, eventdata, handles);
elseif strcmp(eventdata.Key, 'escape')
cancelNewSource(hObject, eventdata, handles);
end
end
function createNewParent(~, ~, handles)
[label, ~] = newSource();
if ~isempty(label)
updateParentsPopup(handles, label);
end
end
function cancelNewSource(~, ~, ~)
uiresume;
end
function saveNewSource(~, ~, handles)
% TODO: validate inputs
handles.label = get(handles.labelEdit, 'String');
parentLabels = get(handles.parentPopup, 'String');
handles.parent = parentLabels{get(handles.parentPopup, 'Value')};
if strcmp(handles.parent, 'None')
handles.parent = '';
end
guidata(handles.figure, handles);
% Remember these parameters for the next time the protocol is used.
newSource.label = handles.label;
newSource.parent = handles.parent;
if ispref('Symphony', 'Sources')
sources = getpref('Symphony', 'Sources');
if strcmp('None', newSource.label) || any(strcmp({sources.label}, newSource.label))
errordlg('A source with that label already exists.', 'Symphony');
return
end
sources(end + 1) = newSource;
else
sources = newSource;
end
setpref('Symphony', 'Sources', sources);
uiresume;
end