This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfgm.m
More file actions
436 lines (407 loc) · 19 KB
/
fgm.m
File metadata and controls
436 lines (407 loc) · 19 KB
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
function fgm()
% FGM - MatlabFigureManager
%
% Launch a GUI to manage and easily save figures currently opened.
% Dependency : GUI Layout ToolBox.
%
% Saving figures:
% - If one figure is selected the name of the file can be choosen at the moment of saving.
% (Default file name will be the name of the figure)
% - If Multiple figures are selected, the files names will be the names of the figures.
% - If a figure has no name, the file's name will be "Figure_#" ("#" being the figure's number).
%
% Shortcuts:
% - 'f' to focus on selected figures
% - 'f2' to focus on the text field
% - 'f5' to refresh the figure list
% - 'del' or 'backSpace' to close selected figures
% (c) 2018-2019 MIT License
% Created by Stephane Roussel <[email protected]>
% Updated by Olivier Leveque <[email protected]>
% -- Layout ToolBox dependency checking
try
layoutRoot();
catch
link = 'https://fr.mathworks.com/matlabcentral/fileexchange/47982-gui-layout-toolbox';
error('Sorry, you need to install the <a href="%s">GUI Layout Toolbox</a>.',link);
end
% -- Updates checking
gitUser = 'Tetane';
gitRepo = 'MatlabFigureManager';
fgmCurrentTag = '1.0';
json = webread(['https://api.github.com/repos/',gitUser,'/',gitRepo,'/releases/latest']);
if ~all(json.tag_name==fgmCurrentTag)
warning('Your are using FigManager v%s\nA new version (v%s) is available. You can download it <a href="%s">here</a>.',fgmCurrentTag,json.tag_name,json.html_url);
end
% -- Main script
if ~isFgmExist()
data = loadBackup();
h = createWindow();
createInterface(h,data);
updateInterface(h);
end
% -- GUI subfunctions
function fgmExists = isFgmExist()
set(0,'ShowHiddenHandles','On');
allFigures = get(0,'Children');
fgmExists = 0;
for index = 1:length(allFigures)
if strcmp(get(allFigures(index),'Tag'),'fgm')
figure(allFigures(index));
fgmExists = 1;
end
end
set(0,'ShowHiddenHandles','Off');
end
function data = loadBackup()
data.dataFilePath = [fgmRoot(),filesep,'backup.mat'];
try
load(data.dataFilePath,'selectedFormats','lastpath');
catch
selectedFormats = [1,1,0,1,0];
lastpath = pwd; % current folder
end
data.selectedFormats = selectedFormats;
data.lastpath = lastpath;
end
function h = createWindow()
screenSize = get(0,'ScreenSize');
windowSize = [240,400];
h = figure(...
'Position' , ceil([(screenSize(3:4)-windowSize)/2,windowSize]),...
'MenuBar' , 'none', ...
'Toolbar' , 'none', ...
'NumberTitle' , 'off', ...
'Name' , 'FigManager',...
'IntegerHandle' , 'off',...
'HandleVisibility' , 'off',...
'Tag' , 'fgm',...
'KeyPressFcn' , @onKeyPressed,...
'CloseRequestFcn' , @onCloseFgm);
% handles = guihandles(h);
end
function createInterface(h,data)
handles = guidata(h);
vbox = uix.VBox('Parent',h);
% Fig list section
handles.list_box = uicontrol('Parent',vbox,'Style','listbox','CallBack',@onClickList,'KeyPressFcn',@onKeyPressed);
set(handles.list_box,'Max',4,'Min',0);
% Fig list context menu
menu_listbox = uicontextmenu('Parent',h);
handles.context_menu.save_button = uimenu(menu_listbox,'Text','Save (Ctrl+S)','CallBack',@onSaveButton,'Enable','Off');
handles.context_menu.close_button = uimenu(menu_listbox,'Text','Close (Del)','CallBack',@onCloseButton,'Enable','Off');
handles.context_menu.focus_button = uimenu(menu_listbox,'Text','Focus (F)','CallBack',@onFocusCtxtmenuButton,'Enable','Off');
handles.context_menu.rename_button = uimenu(menu_listbox,'Text','Rename (F2)','CallBack',@onRenameCtxtmenuButton,'Enable','Off');
set(handles.list_box,'UiContextMenu',menu_listbox);
% Rename section
editNamesHbox = uix.HBox('Parent',vbox);
handles.editNames = uicontrol('Style','edit','Parent',editNamesHbox,'HorizontalAlignment','left','KeyPressFcn',@onKeyPressed,'tag','edit');
handles.rename_button = uicontrol('Style','pushbutton','Parent',editNamesHbox,'String','Rename','CallBack',@onRenameButton,'KeyPressFcn' , @onKeyPressed, 'Enable', 'Off');
set(editNamesHbox,'widths',[-1,handles.rename_button.Extent(3)+10]);
% Fig extensions section
hbox = uix.HBox('parent', vbox);
handles.check_fig = uicontrol('Parent',hbox,'Style','checkbox','String','.fig','Value',data.selectedFormats(1),'KeyPressFcn',@onKeyPressed);
handles.check_eps = uicontrol('Parent',hbox,'Style','checkbox','String','.eps','Value',data.selectedFormats(2),'KeyPressFcn',@onKeyPressed);
handles.check_pdf = uicontrol('Parent',hbox,'Style','checkbox','String','.pdf','Value',data.selectedFormats(3),'KeyPressFcn',@onKeyPressed);
handles.check_svg = uicontrol('Parent',hbox,'Style','checkbox','String','.svg','Value',data.selectedFormats(4),'KeyPressFcn',@onKeyPressed);
handles.check_png = uicontrol('Parent',hbox,'Style','checkbox','String','.png','Value',data.selectedFormats(5),'KeyPressFcn',@onKeyPressed);
set(hbox,'widths',[-1,-1,-1,-1,-1]);
% Buttons section
buttonsHbox = uix.HBox('Parent',vbox);
handles.refresh_button = uicontrol('Style','pushbutton','Parent',buttonsHbox,'String','Refresh (f5)','CallBack',@onRefreshButton,'Tag','refresh_button','KeyPressFcn',@onKeyPressed);
handles.save_button = uicontrol('Style','pushbutton','Parent',buttonsHbox,'String','Save','CallBack',@onSaveButton,'Enable','Off','KeyPressFcn',@onKeyPressed);
set(handles.save_button,'TooltipString',sprintf('If multiple figures are selected, the figure''s name will be used as the file name'));
handles.close_button = uicontrol('Style','pushbutton','Parent',buttonsHbox,'String','Close','CallBack',@onCloseButton,'Enable','Off','Tag','close_button','KeyPressFcn',@onKeyPressed);
set(vbox,'heights',[-1,20,25,25]);
% Save some data
handles.lastpath = data.lastpath;
handles.dataFilePath = data.dataFilePath;
guidata(h,handles);
end
function updateInterface(h)
handles = guidata(h);
objFigs = get(0,'children');
numberOfFigures = length(objFigs);
if isempty(objFigs)
state = 'Off';
listFig = cell(1,3);
listFig{1,2} = '';
listFig{1,3} = '';
else
state = 'On';
listFig = cell(numberOfFigures,3);
nindex = 0; % number of unnumbered figure
for i = 1:numberOfFigures
index = i - nindex; % number of numbered figure
objectFig = objFigs(index);
nameFig = get(objectFig,'Name');
idFig = get(objectFig,'Number');
if ~isempty(idFig)
if isempty(nameFig)
nameFig = 'Untitled';
% set(objectFig,'Name',nameFig);
end
listFig{index,1} = idFig;
listFig{index,2} = nameFig;
if strcmp(nameFig, 'Untitled')
listFig{index,3} = ['Figure ' num2str(idFig)];
else
listFig{index,3} = ['Figure ' num2str(idFig) ': ' nameFig];
end
% createMenu = true;
% figChildren = get(objectFig, 'Children');
% for i = 1:length(figChildren)
% if isa(figChildren(i), 'matlab.ui.container.Menu')
% if strcmp(get(figChildren(i), 'Text'), 'Fgm')
% createMenu = false;
% break;
% end
% end
% end
% if createMenu && ~strcmp(get(objectFig, 'MenuBar'), 'none')
% uimenu(objectFig, 'Text', 'Fgm');
% end
else
objFigs(index) = [];
nindex = nindex + 1;
end
end
listFig = listFig(~all(cellfun(@isempty,listFig),2),:); % delete empty rows
[listFig, sortedxIndexes] = sortrows(listFig);
handles.figures = objFigs(sortedxIndexes);
end
% Enable or disable menu content
set(handles.save_button,'Enable',state);
set(handles.close_button,'Enable',state);
set(handles.rename_button,'Enable',state);
set(handles.context_menu.save_button,'Enable',state);
set(handles.context_menu.close_button,'Enable',state);
set(handles.context_menu.focus_button,'Enable',state);
set(handles.context_menu.rename_button,'Enable',state);
% Update the list of displayed figures
set(handles.list_box,'String',listFig(:,3));
% Update
handles.listFigures = listFig;
set(handles.editNames,'String',strjoin(nameSelectFigs(handles),';'));
% Save
guidata(h,handles);
end
% -- Helper subfunctions
function dlgchoice = overwriteDialog(filename)
screenSize = get(0,'ScreenSize');
windowSize = [380,10+25+25+10];
d = dialog('Name','This file already exists','Position', ceil([(screenSize(3:4)-windowSize)/2+[0,100],windowSize]));
vbox = uix.VBox('Parent',d,'Padding',10,'Spacing',0);
question = uicontrol('Parent',vbox,'Style','text','String',['Overwrite ''',filename,''' ?']);
hbox = uix.HBox('Parent',vbox);
uicontrol('Parent',hbox,'Style','PushButton','String','Yes','Callback',@diagCallback);
uicontrol('Parent',hbox,'Style','PushButton','String','Yes to all','Callback',@diagCallback);
uicontrol('Parent',hbox,'Style','PushButton','String','No','Callback',@diagCallback);
uicontrol('Parent',hbox,'Style','PushButton','String','No to all','Callback',@diagCallback);
uicontrol('Parent',hbox,'Style','PushButton','String','Cancel','Callback',@diagCallback);
set(vbox,'Heights',[25,25]);
if question.Extent(3) > windowSize(1)
set(d,'Position',ceil([(screenSize(3:4)-[question.Extent(3)+20,windowSize(2)])/2,[question.Extent(3)+20,windowSize(2)]]))
end
dlgchoice = 'Cancel'; % Default value
uiwait();
function diagCallback(hObject, ~)
dlgchoice = get(hObject, 'String');
uiresume();
delete(gcf);
end
end
function id = idSelectFigs(handles)
% check if the number of all selected items is equal to or less than the number of digits currently open
if ~all(size(handles.listFigures,1)>=get(handles.list_box,'Value'))
set(handles.list_box,'Value',1);
end
% get selected figure ID
id = cell2mat(handles.listFigures(get(handles.list_box,'Value'),1));
end
function names = nameSelectFigs(handles)
% check if the number of all selected items is equal to or less than the number of digits currently open
if ~all(size(handles.listFigures,1)>=get(handles.list_box,'Value'))
set(handles.list_box,'Value',1);
end
% get selected figure names
names = handles.listFigures(get(handles.list_box,'Value'),2);
end
function figs = objSelectFigs(handles)
% check if the number of all selected items is equal to or less than the number of digits currently open
if ~all(size(handles.listFigures,1)>=get(handles.list_box,'Value'))
set(handles.list_box,'Value',1);
end
% get selected figure objects
figs = handles.figures(get(handles.list_box,'Value'));
end
% -- Window callback functions
function onKeyPressed(~,event)
if strcmp(event.EventName,'KeyPress')
key = event.Key;
tag = event.Source.Tag;
if strcmpi(key,'f5')
onRefreshButton();
elseif strcmpi(key, 'f') && ~strcmp(tag,'edit')
onFocusCtxtmenuButton();
elseif (strcmpi(key,'delete') || strcmpi(key,'backspace')) && ~strcmp(tag,'edit')
onCloseButton();
elseif strcmpi(key, 'f2')
onRenameCtxtmenuButton();
elseif strcmpi(key,'return') && strcmp(tag,'edit')
pause(0.1); % make sure handles.editNames.String is updated
onRenameButton();
elseif length(event.Modifier) == 1 && strcmpi(event.Modifier{1}, 'control') && strcmpi(event.Key,'s')
onSaveButton();
end
end
end
function onCloseFgm(~,~)
try
handles = guidata(gcbo);
selectedFormats = [...
handles.check_fig.Value,...
handles.check_eps.Value,...
handles.check_pdf.Value,...
handles.check_svg.Value,...
handles.check_png.Value];
lastpath = handles.lastpath;
save(handles.dataFilePath, 'selectedFormats', 'lastpath');
catch
warning('Sorry, we can''t save your GUI settings...');
end
delete(gcbo);
end
% -- Button callback functions
function onClickList(~,~)
updateInterface(gcbo);
handles = guidata(gcbo);
persistent chk; % Change to non persistent variable !
if ~isempty(handles.listFigures{1,1}) % Trouver une autre condition
if isempty(chk)
chk = 1;
pause(0.3);
chk = [];
else
chk = [];
figure(idSelectFigs(handles));
% uicontrol(handles.editNames);
end
end
end
function onSaveButton(~,~)
handles = guidata(gcbo);
idSelectedFigures = idSelectFigs(handles);
nameSelectedFigures = nameSelectFigs(handles);
objSelectedFigures = objSelectFigs(handles);
numberOfSelectedFigs = length(idSelectedFigures);
check = nonzeros([...
handles.check_fig.Value,...
handles.check_eps.Value*2,...
handles.check_pdf.Value*3,...
handles.check_svg.Value*4,...
handles.check_png.Value*5]);
ext = {'*.fig';'*.eps';'*.pdf';'*.svg';'*.png'};
formats = {'fig';'epsc';'pdf';'svg';'png'};
ext = ext(check);
formats = formats(check);
if isfield(handles,'lastpath')
lastpath = handles.lastpath;
else
lastpath = pwd;
end
for i = 1:numberOfSelectedFigs
if strcmp(nameSelectedFigures(i), 'Untitled')
nameSelectedFigures{i} = ['Figure_' num2str(idSelectedFigures(i))];
end
end
if numberOfSelectedFigs==1
if length(formats) > 1
[file,path] = uiputfile('*.*','FigManager',fullfile(lastpath,char(nameSelectedFigures(1))));
else
[file,path] = uiputfile(ext,'FigManager',fullfile(lastpath,char(nameSelectedFigures(1))));
end
if all(path~=0)
[~,namefile,~] = fileparts(file);
nameSelectedFigures{1} = namefile;
end
else
path = uigetdir(lastpath,'FigManager');
end
if all(path~=0)
wb = waitbar(0,'');
set(wb.Children.Title, 'Interpreter', 'none');
dlgchoice = 'Yes';
for i = 1:numberOfSelectedFigs
for j = 1:length(formats)
extension = char(ext(j));
extension = extension(2:end);
wbInd = sub2ind([length(formats),numberOfSelectedFigs],j,i);
wbText = ['Saving : ' nameSelectedFigures{i} extension];
fullFilePath = fullfile(path,char(nameSelectedFigures(i)));
if ~strcmpi(dlgchoice, 'Cancel')
waitbar((wbInd-1)/(length(formats)*numberOfSelectedFigs),wb,wbText);
if isfile([fullFilePath,extension]) % if the file already exists
fileAlreadyExist = true;
if (numberOfSelectedFigs > 1 || length(formats) > 1) && (strcmpi(dlgchoice, 'Yes') || strcmpi(dlgchoice, 'No'))
dlgchoice = overwriteDialog([fullFilePath,extension]);
end
else
fileAlreadyExist = false; % save if the file does not exist
end
if (strcmpi(dlgchoice, 'Yes') || strcmpi(dlgchoice, 'Yes to all')) || ~fileAlreadyExist
currentFig = objSelectedFigures(i);
if strcmp(char(formats(j)),'pdf')
currentFig.PaperPositionMode = 'auto';
currentFig.PaperUnits = 'points';
currentFig.PaperSize = [currentFig.PaperPosition(3)+1 currentFig.PaperPosition(4)+1];
end
saveas(currentFig,fullFilePath,char(formats(j)));
waitbar((wbInd+1)/(length(formats)*numberOfSelectedFigs),wb,wbText);
end
end
end
end
close(wb);
handles.selectedFormats = formats;
handles.lastpath = path;
guidata(gcbo, handles);
end
end
function onCloseButton(~,~)
handles = guidata(gcbo);
close(idSelectFigs(handles));
set(handles.list_box,'Value',1);
updateInterface(gcbo);
end
function onRenameButton(~,~)
handles = guidata(gcbo);
idSelectedFigures = idSelectFigs(handles);
newNames = split(get(handles.editNames,'String'),';');
for index = 1:length(idSelectedFigures)
if strcmp(newNames{index}, 'Untitled')
set(figure(idSelectedFigures(index)),'Name','');
else
set(figure(idSelectedFigures(index)),'Name',newNames{index});
end
end
updateInterface(gcbo);
end
function onRefreshButton(~,~)
handles = guidata(gcbo);
set(handles.list_box,'Value',1);
updateInterface(gcbo);
end
% -- Context menu callback functions
function onFocusCtxtmenuButton(~,~)
handles = guidata(gcbo);
idSelectedFigures = idSelectFigs(handles);
for i = 1:length(idSelectedFigures)
figure(idSelectedFigures(i));
end
end
function onRenameCtxtmenuButton(~,~)
handles = guidata(gcbo);
uicontrol(handles.editNames);
end
end