-
Notifications
You must be signed in to change notification settings - Fork 1
/
WordCloudEditor.m
executable file
·505 lines (412 loc) · 21.6 KB
/
WordCloudEditor.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
function varargout = WordCloudEditor(varargin)
% WORDCLOUDEDITOR MATLAB code for WordCloudEditor.fig
% WORDCLOUDEDITOR, by itself, creates a new WORDCLOUDEDITOR or raises the existing
% singleton*.
%
% H = WORDCLOUDEDITOR returns the handle to a new WORDCLOUDEDITOR or the handle to
% the existing singleton*.
%
% WORDCLOUDEDITOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WORDCLOUDEDITOR.M with the given input arguments.
%
% WORDCLOUDEDITOR('Property','Value',...) creates a new WORDCLOUDEDITOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before WordCloudEditor_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to WordCloudEditor_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
%
% Author: Jenny Owen
% Copyright 2015 The MathWorks Inc.
% Edit the above text to modify the response to help WordCloudEditor
% Last Modified by GUIDE v2.5 19-May-2015 19:00:07
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @WordCloudEditor_OpeningFcn, ...
'gui_OutputFcn', @WordCloudEditor_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before WordCloudEditor is made visible.
function WordCloudEditor_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to WordCloudEditor (see VARARGIN)
% Choose default command line output for WordCloudEditor
handles.output = hObject;
% check the parser was provided
if(nargin > 3)
for index = 1:2:(nargin-3),
switch lower(varargin{index})
case 'parser'
fac = WordCloud.WordCloudFactory();
parser = varargin{index+1};
% check if we have factory settings already saved
if exist(fullfile(parser.projectFolder, 'CloudFactory.mat'), 'file')
% if so, give the factory object the saved settings
fac = fac.loadSettingsFromMatFile(...
fullfile(parser.projectFolder, 'CloudFactory.mat'));
end
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
setappdata(handles.wordcloud_editor_figure, 'parser', parser);
initialiseUIObjectsWithFactoryDefaults(handles, fac);
fac = fac.buildCloud(varargin{index+1});
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
break;
otherwise
error(['WordCloudEditor not passed an object of type "ParseFiles". ' ...
'Use the syntax WordCloudEditor(''parser'', ParseFilesObj);']);
end
end
else
error(['WordCloudEditor not passed an object of type "ParseFiles". ' ...
'Use the syntax WordCloudEditor(''parser'', ParseFilesObj);']);
end
% check if there is a statistics toolbox license.
if ~WordCloud.checkForStatisticsToolbox()
% If not disable cluster options and change panel tooltip to explanation.
set(handles.cluster_options_panel.Children, 'Enable', 'off');
set(handles.cluster_options_panel.Children, 'TooltipString', ...
'Cluster options only available with statistics toolbox');
end
% if there is only one file in the parser, clustering is also disabled
if numel(parser.fileList) == 1
% If not disable cluster options and change panel tooltip to explanation.
set(handles.cluster_options_panel.Children, 'Enable', 'off');
set(handles.cluster_options_panel.Children, 'TooltipString', ...
'Cluster options only available when viewing more than one file');
end
% Update handles structure
guidata(hObject, handles);
function initialiseUIObjectsWithFactoryDefaults(handles, fac)
handles.num_words_edit.String = fac.numWords;
handles.fonts_list.String = fac.fonts;
handles.text_size_slider.Value = fac.textScaleFactor;
handles.select_background_colour_btn.BackgroundColor = fac.backgroundColour;
handles.colourmap_menu.String = fac.possibleColourMapNames;
handles.colourmap_menu.Value = fac.getColourMapIdx();
handles.colour_mode_menu.String = fac.colouringModes;
handles.colour_mode_menu.Value = fac.getColourModeIdx();
handles.select_text_colour_btn.BackgroundColor = fac.textColour;
setSelectTextButtonState(handles, fac.colourMode, fac.textColour);
handles.has_logo_chbx.Value = fac.hasLogo;
handles.num_clusters_slider.Value = fac.numClusters;
handles.cluster_separation_slider.Value = fac.clusterDistanceFactor;
handles.cluster_width_slider.Value = fac.clusterWidthRatio;
function setSelectTextButtonState(handles, colourMode, bgColour)
if strcmp(colourMode, 'Uniform word colouring')
% if uniform word colouring, then enable text colour label and button.
handles.select_text_colour_label.Enable = 'on';
handles.select_text_colour_btn.Enable = 'on';
handles.select_text_colour_btn.BackgroundColor = bgColour;
else
% otherwise disable text colour label and button.
handles.select_text_colour_label.Enable = 'off';
handles.select_text_colour_btn.Enable = 'off';
handles.select_text_colour_btn.BackgroundColor = ...
handles.wordcloud_editor_figure.Color;
end
% --- Outputs from this function are returned to the command line.
function varargout = WordCloudEditor_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in colourmap_menu.
function colourmap_menu_Callback(hObject, eventdata, handles)
% hObject handle to colourmap_menu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns colourmap_menu contents as cell array
% contents{get(hObject,'Value')} returns selected item from colourmap_menu
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setColourMap(get(hObject,'Value'));
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
% --- Executes during object creation, after setting all properties.
function colourmap_menu_CreateFcn(hObject, eventdata, handles)
% hObject handle to colourmap_menu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in colour_mode_menu.
function colour_mode_menu_Callback(hObject, eventdata, handles)
% hObject handle to colour_mode_menu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns colour_mode_menu contents as cell array
% contents{get(hObject,'Value')} returns selected item from colour_mode_menu
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setColourMode(get(hObject,'Value'));
setSelectTextButtonState(handles, fac.colourMode, fac.textColour);
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
% --- Executes during object creation, after setting all properties.
function colour_mode_menu_CreateFcn(hObject, eventdata, handles)
% hObject handle to colour_mode_menu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in select_text_colour_btn.
function select_text_colour_btn_Callback(hObject, eventdata, handles)
% hObject handle to select_text_colour_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
col = uisetcolor;
if size(col, 2) == 3
handles.select_text_colour_btn.BackgroundColor = col;
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setTextColour(handles.select_text_colour_btn.BackgroundColor);
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
end
% --- Executes on button press in select_background_colour_btn.
function select_background_colour_btn_Callback(hObject, eventdata, handles)
% hObject handle to select_background_colour_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
col = uisetcolor;
if size(col, 2) == 3
handles.select_background_colour_btn.BackgroundColor = col;
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setBackgroundColour(handles.select_background_colour_btn.BackgroundColor);
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
end
% --- Executes on selection change in fonts_list.
% function fonts_list_Callback(hObject, eventdata, handles)
% hObject handle to fonts_list (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns fonts_list contents as cell array
% contents{get(hObject,'Value')} returns selected item from fonts_list
% --- Executes during object creation, after setting all properties.
function fonts_list_CreateFcn(hObject, eventdata, handles)
% hObject handle to fonts_list (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in remove_fonts_btn.
function remove_fonts_btn_Callback(hObject, eventdata, handles)
% hObject handle to remove_fonts_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectedFonts = get(handles.fonts_list,'Value');
% Do not delete all the fonts or the cloud will break
% if the user wants to delete all the fonts, they must be educated.
if numel(selectedFonts) < numel(handles.fonts_list.String)
% clear all values, otherwise this can cause a value to be larger than
% number of strings in the listbox, causing an error.
handles.fonts_list.Value = [];
% delete selected files
handles.fonts_list.String(selectedFonts) = [];
% update fonts in cloud
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setFonts(handles.fonts_list.String);
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
else
warndlg({'Deleting all the fonts means the word cloud cannot render.'; ...
'Do not delete all the fonts at the same time.'});
end
% --- Executes on button press in add_fonts_btn.
function add_fonts_btn_Callback(hObject, eventdata, handles)
% hObject handle to add_fonts_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get new font and add to listbox
fontinfo = uisetfont;
if ~isequal(fontinfo, 0)
handles.fonts_list.String = [handles.fonts_list.String; fontinfo.FontName];
handles.fonts_list.String = unique(handles.fonts_list.String);
% get fonts in listbox and give to factory.
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setFonts(handles.fonts_list.String);
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
end
% --- Executes on slider movement.
function text_size_slider_Callback(hObject, eventdata, handles)
% hObject handle to text_size_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setTextScale(get(hObject,'Value'));
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
% --- Executes during object creation, after setting all properties.
function text_size_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to text_size_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function num_clusters_slider_Callback(hObject, eventdata, handles)
% hObject handle to num_clusters_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
nclust = ceil(get(hObject,'Value'));
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
parser = getappdata(handles.wordcloud_editor_figure, 'parser');
fac = fac.setNumClusters(nclust, parser);
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
% --- Executes during object creation, after setting all properties.
function num_clusters_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to num_clusters_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function cluster_separation_slider_Callback(hObject, eventdata, handles)
% hObject handle to num_clusters_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setClusterSeparation(get(hObject,'Value'));
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
% --- Executes during object creation, after setting all properties.
function cluster_separation_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to cluster_separation_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on button press in has_logo_chbx.
function has_logo_chbx_Callback(hObject, eventdata, handles)
% hObject handle to has_logo_chbx (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of has_logo_chbx
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setHasLogo(logical(get(hObject,'Value')));
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
function num_words_edit_Callback(hObject, eventdata, handles)
% hObject handle to num_words_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of num_words_edit as text
% str2double(get(hObject,'String')) returns contents of num_words_edit as a double
nwords = str2double(get(hObject,'String'));
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
parser = getappdata(handles.wordcloud_editor_figure, 'parser');
fac = fac.setNumWords(nwords, parser);
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
% --- Executes during object creation, after setting all properties.
function num_words_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to num_words_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function cluster_width_slider_Callback(hObject, eventdata, handles)
% hObject handle to cluster_width_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
fac = fac.setClusterWidthRatio(get(hObject,'Value'));
setappdata(handles.wordcloud_editor_figure, 'factory', fac);
% --- Executes during object creation, after setting all properties.
function cluster_width_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to cluster_width_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on button press in save_settings_btn.
function save_settings_btn_Callback(hObject, eventdata, handles)
% hObject handle to save_settings_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
parser = getappdata(handles.wordcloud_editor_figure, 'parser');
savefilename = fullfile(parser.projectFolder, 'CloudFactory.mat');
backgroundColour = fac.backgroundColour;
textColour = fac.textColour;
numWords = fac.numWords;
colourMap = fac.colourMap;
fonts = fac.fonts;
colourMode = fac.colourMode;
textScaleFactor = fac.textScaleFactor;
numClusters = fac.numClusters;
clusterDistanceFactor = fac.clusterDistanceFactor;
clusterWidthRatio = fac.clusterWidthRatio;
hasLogo = fac.hasLogo;
save(savefilename, ...
'backgroundColour',...
'textColour',...
'numWords',...
'colourMap',...
'fonts',...
'colourMode',...
'textScaleFactor',...
'numClusters',...
'clusterDistanceFactor',...
'clusterWidthRatio',...
'hasLogo');
% --- Executes on button press in save_image_btn.
function save_image_btn_Callback(hObject, eventdata, handles)
% hObject handle to save_image_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fac = getappdata(handles.wordcloud_editor_figure, 'factory');
parser = getappdata(handles.wordcloud_editor_figure, 'parser');
h = fac.getCloudFigureHandle();
[f,p] = uiputfile({ '*.png', 'PNG';...
'*.jpg', 'JPEG';...
'*.tif', 'TIFF';...
'*.gif', 'GIF';...
'*.*', 'All Files'},...
'Set save name',...
parser.projectFolder);
if ~(isequal(f,0) || isequal(p,0))
set(h, 'papertype', 'A0',...
'renderer', 'painters',...
'paperpositionmode', 'auto',...
'InvertHardcopy', 'off');
print(h, fullfile(p, f), '-dpng', '-noui', '-r600');
end