-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaicFigure.m
1324 lines (1254 loc) · 48.5 KB
/
mosaicFigure.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function newFigHandle = mosaicFigure(varargin)
% @brief Auto-tiling figure
%
% Manage figure positions. Indeed, by default, MATLAB always creates
% figures at the same position. Figures can be grouped and assigned
% to a monitor (monitor <tt>0</tt> means any monitor).
%
% \par Creating figures
% To create a new figure, the function can be invoked as follows:
% - Without arguments, this function creates a new figure
% which is out of any group and not assigned to any monitor,
% and makes it the active figure.
% - If the first argument is numeric, it is considered as
% the monitor number, that the figure is assigned to.
% The new figure is out of any group, it is assigned to the given monitor
% and it is activated.
% - If the next argument (the first argument, or the second argument,
% if a monitor number was given), is a character array,
% it is considered as the group name.
% The function creates a new figure in specified group,
% assigns it to a monitor (if given), and makes it the active figure.
% - If the second argument is a number, it is considered as
% the group number. The function creates a new figure in specified group,
% assigns it to the given monitor, and makes it the active figure.
%
% In all these cases mosaicFigure() returns the handle to the newly-created
% figure.
%
% The following properties are currently accepted by mosaicFigure():
% - <tt>'Title'</tt> (character array) The title of the
% newly-created window (defaults to <tt>"Figure f (mosaicFigure)"</tt>,
% or <tt>"Figure f (group g)"</tt>, if a group number was given,
% or <tt>"Figure f (groupName)"</tt>, if a group name was given.
% - <tt>'DealStrategy'</tt> (integer 1x1 in the range 1-3)
% Selects the deal stategies. See the code of each deal strategy
% for indication on how they work. The default one should be the best.
% - <tt>'LayoutStrategy'</tt> (integer 1x1 in the range 1-4)
% Selects the layout strategy. See the code of each layout strategy
% for indication on how they work. The default one should be the best.
% - <tt>'UseJava'</tt> (logical 1x1): Whether to use Java
% to obtain monitor positions
%
% \par Special commands
% Relayout the figures with no assigned group
% \code{.m}
% mosaicFigure layout
% \endcode
% Relayout the group with number *groupNumber*
% \code{.m}
% mosaicFigure layout groupNumber
% \endcode
% Relayout the group named *groupName*
% \code{.m}
% mosaicFigure layout groupName
% \endcode
% Closes all the figures managed by mosaicFigure()
% \code{.m}
% mosaicFigure close
% mosaicFigure close all
% \endcode
% Closes all the figures in the group number groupNumber
% \code{.m}
% mosaicFigure close groupNumber
% \endcode
% Closes all the figures in the group named groupName
% \code{.m}
% mosaicFigure close groupName
% \endcode
% \note These commands can also be used under function form.
%
% \par Debug usages
% Returns the state of the function (the list of all mosaic figures)
% \code{.m}
% mosaicFigure debug
% \endcode
% Simulate layout with other monitor sizes than the real ones.
% \code{.m}
% mosaicFigure('debugLayout', group, monitorSizes)
% \endcode
%
% @param varargin Function arguments described above
% @return Nothing or the handle to the new figure
%
% % Copyright: 2015-2023 Pascal COMBES <pascom@orange.fr>
% % Author: Pascal COMBES <pascom@orange.fr>
% % Date: December 31st, 2023
% % Version: 1.0
% % License: GPLv3
% % Requires: parseProperties
%% Properties:
% Available properties:
properties = struct( ...
'name', {'DealStrategy'; 'LayoutStrategy'; 'Title'; 'UseJava'}, ...
'type', {'integer'; 'integer'; 'char'; 'logical'}, ...
'size', {[1, 1]; [1, 1]; [1, 0]; [1, 1]}, ...
'parse', {@parseDealStrategy; @parseLayoutStrategy; []; @parseUseJava}, ...
'min' , {1; 1; []; []}, ...
'max' , {3; 4; []; []} ...
);
% Default settings:
handles.title = [];
handles.getMonitorPositions = @getMonitorPositionsJava;
handles.dealFigures = @dealFigures3;
handles.computeLayout = @computeLayout4;
handles.initLayout = @noop;
handles.layoutScreen = @layoutScreen;
%% Initialisation:
persistent figList
if(isempty(figList))
figList = loadBackup();
end
persistent silentClose
if(isempty(silentClose))
silentClose = false;
end
% Gets the positions of the monitors:
% In versions before 2014b, they are refereced by the coordinates of the
% top-left and bottom-right points in the frame (O, u, v), where:
% -O is the top-left point of the first monitor.
% -u is a vector oriented towards the left of the screen.
% -v is a vectcor oriented towards the bottom of the screen.
% For version 2014b and after, they are referenced by the coordinates
% of their lower-left points in the same frame (O, u, v') and their
% width and height, where:
% -v' is a vector oriented towards the top of the screen.
monitorSizes = handles.getMonitorPositions();
%% DEBUG CODE:
% Allows debuging by returning the state of the function:
if ((nargin == 1) && strcmp(varargin{1}, 'debug'))
newFigHandle = figList;
return;
end
% Allows to debug the layout fuction:
if ((nargin >= 3) && strcmp(varargin{1}, 'debugLayout'))
% Argument parsing and checking:
if (isnumeric(varargin{2}))
group = uint64(varargin{2});
else
error('The second argument must be the number of the group to layout');
end
if (~isnumeric(varargin{3}) || (size(varargin{3}, 2) ~= 4))
error('The third argument must be the monitor sizes to simulate');
end
if (nargin > 3)
handles = parseProperties(handles, properties, varargin{4:end});
end
% Initializes debug handles:
handles.initLayout = @initLayoutDebug;
handles.layoutScreen = @layoutScreenDebug;
% Debugs the layout:
monitorSizes = varargin{3};
if (group == 1)
layout(figList{1}, monitorSizes, handles);
else
layout(figList{group}.contents, monitorSizes, handles, true);
end
return;
end
%% Command parsing:
% The layout command:
if ((nargin >= 1) && strcmp(varargin{1}, 'layout'))
% Argument parsing:
if(nargin < 2)
group = [];
else
group = varargin{2};
end
if (~isempty(group) && isnumeric(group) && (round(group) == group))
group = int64(group);
elseif (ischar(group) && ~isempty(regexp(group, '^[1-9][0-9]*$', 'once')))
group = int64(sscanf(group, '%d'));
end
if (~isempty(group) && ~ischar(group) && ~isinteger(group))
error('MATLAB:BadArgumentType', ...
'Group names must be integers or chars');
end
if (ischar(group) && strcmp(group, 'all'))
error('MATLAB:BadArgumentValue', ...
'The group name "all" is a reserved group name and should not be used');
end
% If the fig list has been reset, try recover from backup:
if ((size(figList, 1) == 1) && isempty(figList{1}))
figList = loadBackup();
end
% Relayout:
g = findGroup(group, figList);
if (isempty(g) || (g == 1))
layout(figList{1}, monitorSizes, handles);
else
layout(figList{g}.contents, monitorSizes, handles, true);
end
return
end
% The close command:
if ((nargin >= 1) && strcmp(varargin{1}, 'close'))
% Argument parsing:
if(nargin < 2)
group = 'all';
else
group = varargin{2};
end
if (nargin > 2)
warning('MATLAB:TooManyArguments', ...
['MosaicFigure close command only needs 1 argument.', ...
'Others will be ignored']);
end
if (ischar(group) && ~isempty(regexp(group, '^[1-9][0-9]*$', 'once')))
group = int64(sscanf(group, '%d'));
elseif (isnumeric(group) && (round(group) == group))
group = int64(group);
elseif (isa(group, 'matlab.ui.Figure') || isempty(group))
silentClose = true;
for fig = 1:length(group)
close(fig);
end
silentClose = false;
return;
end
if (~ischar(group) && ~isinteger(group))
error('MATLAB:BadArgumentType', ...
'Group names must be integers or chars');
end
% If the fig list has been reset, try recover from backup:
if ((size(figList, 1) == 1) && isempty(figList{1}))
figList = loadBackup();
end
% Closes the figures:
if (strcmp(group, 'all'))
for g = 1:size(figList, 2)
if (g == 1)
closeGroup(figList{g});
else
closeGroup(figList{g}.contents);
end
end
figList = {[]};
else
g = findGroup(group, figList);
if (isempty(g))
warning('MosaicFigure:GroupNotFound', ...
'Could not find the specified group "%s".', ...
num2str(group));
else
closeGroup(figList{g}.contents);
figList(g) = [];
end
end
saveBackup(figList);
return
end
%% Argument parsing:
group = [];
monitor = uint8(0);
% First argument is the monitor number (or 0 if not assigned) if it is
% not a string. Otherwise its the group name and the second argument is
% in the property list.
if (nargin > 0)
if (ischar(varargin{1}))
group = varargin{1};
else
monitor = uint8(varargin{1});
end
end
% Second argument is the group name if it has not already been read
% or number (defaults to empty).
if ((nargin > 1) && isempty(group))
group = varargin{2};
if (isnumeric(group))
group = int64(group);
end
beginPropertyList = 3;
else
beginPropertyList = 2;
end
% Other arguments are properties
if (nargin >= beginPropertyList)
handles = parseProperties(handles, properties, varargin{beginPropertyList:end});
end
%% Argument cheching:
if (~ischar(group) && ~isinteger(group) && ~isempty(group))
error('MATLAB:BadArgumentType', ...
'Group must be either a string, an integer or an empty array');
end
if (ischar(group) && strcmp(group, 'all'))
error('MATLAB:BadArgumentValue', ...
'The group name "all" is a reserved group name and should not be used');
end
if (ischar(group) && ~isempty(regexp(group, '^[1-9][0-9]*$', 'once')))
error('MATLAB:BadArgumentValue', ...
['The group name must not be an integer number as a string.', ...
'However you can used the integer number as is.']);
end
if (monitor > size(monitorSizes, 1))
warning('MATLAB:ArgumentOutOfRange', ...
'There is only %d monitors.', size(monitorSizes, 1));
monitor = 0;
end
%% Handler of close events for the figures:
function closeMosaicFigure(src, ~, group)
% @brief Close event handler
%
% Handle close events for mosaicFigure(): Close the figure
% and relayout the remaining figures.
% If the figure is part of a group, asks the user whether
% all other figures in the group should be closed as well.
% @param src The figure which raised the close event
% @param group The group the figure belongs to
% Find the group number of the figure being deleted:
try
gr = findGroup(group, figList);
catch e %#ok I don't want information on an error in findgroup
% The listof figures has probably been cleared out:
% Tries to recover from backup.
figList = loadBackup();
try
gr = findGroup(group, figList);
catch e %#ok I don't want information on an error in findgroup
delete(src);
return;
end
end
if(isempty(gr))
% The figure list may have been cleared out and overwritten...
warning('MosaicFigure:GroupNotFound', 'Group "%s" not found', group2str(group));
delete(src);
return;
elseif (gr == 1)
% Clears the figure being deleted from the list:
for f=1:(length(figList{1}) + 1)
if (f == length(figList{1}) + 1)
% The figure was not in the list.
warning('MosaicFigure:FigureNotFound', 'Figure not found');
delete(src);
break;
end
if (figList{1}(f).handle == src)
% Found the figure in the list.
delete(figList{1}(f).handle);
figList{1}(f) = [];
break;
end
end
else
% Possible answers:
ALL = 'all';
ONE = 'one';
CANCEL = 'cancel';
% The name of the group as a string:
groupname = [];
if (ischar(group))
groupname = group;
elseif (isinteger(group))
groupname = sprintf('%d', group);
end
% Ask only if there is more than one window in group:
if (~silentClose && (size(figList{gr}.contents, 1) > 1))
rep = questdlg(['You are closing figure of group "', groupname, '". Do you want to close the whole group or only this figure?'], ...
['Closing group "', groupname, '"'], ...
ALL, ONE, CANCEL, ALL);
else
rep = ONE;
end
% If cancel was chosen, returns:
if (strcmp(rep, CANCEL))
return;
end
% Clears wanted figures:
for f=size(figList{gr}.contents, 1):-1:0
if (f == 0)
% Ensure the figure has been deleted.
if (ishandle(src))
warning('MosaicFigure:FigureNotFound', 'Figure not found');
delete(src);
end
break;
end
if (strcmp(rep, ALL) || (figList{gr}.contents(f).handle == src))
delete(figList{gr}.contents(f).handle);
figList{gr}.contents(f) = [];
if (~strcmp(rep, ALL))
break;
end
end
end
% If group is empty, then remove it:
if (isempty(figList{gr}.contents))
figList(gr) = [];
saveBackup(figList);
return;
end
end
% Saves figure list:
saveBackup(figList);
% Relayout (refresh monitor positions because monitors may have
% been added or disconnected
if (gr == 1)
layout(figList{1}, handles.getMonitorPositions(), handles);
else
layout(figList{gr}.contents, handles.getMonitorPositions(), handles);
end
end
%% Initializes the new figure:
newFigHandle = figure;
set(newFigHandle, 'CloseRequestFcn', {@closeMosaicFigure, group});
set(newFigHandle, 'NumberTitle', 'off');
set(newFigHandle, 'DockControls', 'off');
fig = struct('handle', getNumberHandle(newFigHandle), ...
'screen', monitor);
%% Adds the new figure to figure list:
g = findGroup(group, figList);
if (isempty(g))
newGroup.group = group;
newGroup.contents = fig;
figList = [figList, newGroup];
g = size(figList, 2);
if (~isempty(handles.title))
setFigureName(newFigHandle, handles.title);
else
setFigureName(newFigHandle, 1, group);
end
elseif (g == 1)
figList{1} = [figList{1}; fig];
if (~isempty(handles.title))
setFigureName(newFigHandle, handles.title);
else
setFigureName(newFigHandle, size(figList{1}, 1));
end
else
oldGroup = figList{g};
oldGroup.contents = [oldGroup.contents; fig];
figList{g} = oldGroup;
if (~isempty(handles.title))
setFigureName(newFigHandle, handles.title);
else
setFigureName(newFigHandle, size(oldGroup.contents, 1), group);
end
end
saveBackup(figList);
%% Relayout:
if (g == 1)
layout(figList{1}, monitorSizes, handles);
else
layout(figList{g}.contents, monitorSizes, handles, true);
end
%% Ensures the new figure is the active figure:
figure(newFigHandle);
end
function closeGroup(group)
% @brief Close a group of figures
%
% Close all the figures in the given group.
% @param group A column structure array with the following fields:
% - <tt>handle</tt> The handle to the corresponding figure.
% - <tt>screen</tt> The screen ion which the figure must be displayed.
for f=1:length(group)
%set(group(f).handle, 'CloseRequestFcn', closereq);
if (ishandle(group(f).handle) && isvalid(group(f).handle))
delete(group(f).handle);
end
end
end
function layout(group, screenSizes, varargin)
% @brief Lay a group of figures out
%
% @param group A column structure array with the following fields:
% - <tt>handle</tt> The handle to the corresponding figure.
% - <tt>screen</tt> The screen on which the figure must be displayed.
% @param screenSizes The sizes of the available monitors (as given by MATLAB).
% @param varargin The following optionnal arguments are accepted:
% - **handles** Handles to virtual functions (used for
% debugging and choosing the layout strategy).
% - **activate** Whether to activate the figure after
% laying out or not.
%% Argument parsing and checking:
if (nargin >= 3)
handles = varargin{1};
else
handles.initLayout = @noop;
handles.layoutScreen = @layoutScreen;
end
if (nargin >= 4)
activate = varargin{2};
else
activate = false;
end
%% Filter out invisible figures (especially the ones created by live scripts)
group = group(arrayfun(@(fig) get(fig.handle, 'Visible'), group));
if (isempty(group))
return;
end
%% Sort figures by monitor:
figs0 = zeros(size(group, 1), 1);
figs = cell(1, size(screenSizes, 1));
f0 = 1;
for f=1:size(group, 1)
if (group(f).screen == 0)
figs0(f0) = group(f).handle;
f0 = f0 + 1;
else
if (group(f).screen <= size(figs, 2))
figs{group(f).screen} = [figs{group(f).screen}; group(f).handle];
else
figs0(f0) = group(f).handle;
f0 = f0 + 1;
end
end
end
figs0 = figs0(1:(f0-1), 1);
%% Deals the figures of screen 0 between all the screens:
[occs, areas] = computeOccupation(size(figs0, 1), cellfun('size', figs, 1), screenSizes, handles);
deal = handles.dealFigures(occs, areas);
%% Layout each screen
f = 1;
handles.initLayout(screenSizes(1, :));
for m = 1:size(screenSizes, 1)
handles.layoutScreen([figs{m}; figs0(f:(f + deal(m) - 1))], screenSizes(m, :), handles, activate);
f = f + deal(m);
end
end
function [occ, area] = computeOccupation(n0, ns, screenSizes, handles)
% @brief Compute screen space used by a figure
%
% Tell how much screen is used and how big the figures are,
% for each layout possibility.
% @param n0 The number of figures not assigned to a sceen.
% @param ns The number of figures assigned to each screen (a line array).
% @param screenSizes The sizes of the available monitors
% (correctGeometry() must have been used before).
% @param handles Handles to virtual functions (used for debugging and
% choosing the layout strategy).
% @return The following information is returned:
% - **occ** How much of the screen is occuped.
% - **area** How big are the figures.
%% Stop recursion:
if (size(screenSizes, 1) == 0)
area = ones(1, 0);
if (all(size(ns) == [1 1]) && (ns == 0) && (n0 == 0))
occ = 1;
else
occ = 0;
end
return;
end
%% Recursion body:
occ = [];
area = [];
for n = 0:n0
if (n + ns(1) == 0)
% There won't be any figure on this screen.
% This is probably not the good choice when there is a lot of
% figs to allocate, but it should still be considered when
% there are less figures.
% Area of the figures will be taken to be 0.
if (size(ns, 2) > 1)
[occp, areap] = computeOccupation(n0 - n, ns(2:end), screenSizes(2:end, :), handles);
else
[occp, areap] = computeOccupation(n0 - n, 0, screenSizes(2:end, :), handles);
end
if (~isempty(occp) && (any(size(occp) ~= [1 1]) || (occp ~= 0)))
% All the figures can be allocated to a screen.
% Case is valid. Otherwise case is ignored.
occ = [occ; n*ones(size(occp, 1), 1), occp]; %#ok There won't be so much figures assigned to screen 0.
area = [area; zeros(size(areap, 1), 1), areap]; %#ok There won't be so much figures assigned to screen 0.
end
continue;
end
% Computes for the next screens:
if (size(ns, 2) > 1)
[occp, areap] = computeOccupation(n0 - n, ns(2:end), screenSizes(2:end, :), handles);
else
[occp, areap] = computeOccupation(n0 - n, 0, screenSizes(2:end, :), handles);
end
if (isempty(occp) || (all(size(occp) == [1 1]) && (occp == 0)))
% There is one figure which cannot be allocated. Skip case.
continue;
end
% Compute best layout and occupation for this setting on this
% screen.
w = screenSizes(1, 3);
h = screenSizes(1, 4);
[~, ~, wf, hf] = handles.computeLayout(ns(1) + n, w, h);
o = (wf*hf*(ns(1) + n))/w/h;
if (o > 1)
warning('mosaicFigure:assertionFailed', 'Found occupation greather than 1. This should not be possible.');
else
occp(:, end) = o*occp(:, end);
occ = [occ; n*ones(size(occp, 1), 1), occp]; %#ok There won't be so much figures assigned to screen 0.
area = [area; ones(size(areap, 1), 1)*wf*hf, areap]; %#ok There won't be so much figures assigned to screen 0.
end
end
end
function layoutScreen(figs, screenSize, handles, activate)
% @brief Lay one screen out
%
% Lay the given figures out on on screen
% @param figs A column cell array containing the handles to the figures
% to assign to the screen.
% @param screenSize The size of the screen being laid out
% (correctGeometry() must have been used before).
% @param handles Handles to virtual functions (used for debugging and
% choosing the layout strategy).
% @param activate Whether to activate the figure after laying out or not.
%% Arguement checking:
if (isempty(figs))
return;
end
%% Computing layout:
n = size(figs, 1);
[~, nc, ~, ~] = handles.computeLayout(n, screenSize(3), screenSize(4));
%% Doing layout:
for c=1:nc
w0 = floor((c - 1)*screenSize(3)/nc);
w1 = floor(c*screenSize(3)/nc);
nl = ceil((n - c + 1)/nc);
for l = 1:nl
h0 = floor((nl - l)*screenSize(4)/nl);
h1 = floor((nl - l + 1)*screenSize(4)/nl);
if (~strcmp(get(figs((l - 1)*nc + c), 'WindowStyle'), 'docked'))
set(figs((l - 1)*nc + c), 'OuterPosition', correctOuterPosition([screenSize(1) + w0, screenSize(2) + h0, w1 - w0, h1 - h0]));
if (activate)
figure(figs((l - 1)*nc + c));
end
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PROPERTIES PARSING FUNCTIONS :
function handles = parseDealStrategy(handles, value)
% @brief Parse the deal strategy
%
% Assign the good deal strategy in the handles array.
% @param handles Handles to virtual functions. Field <tt>dealFigures</tt> will be
% modified.
% @param value The number of the deal strategy to use.
% @return handles Handles to virtual functions. Field <tt>dealFigures</tt> has
% been modified.
handles.dealFigures = str2func(sprintf('dealFigures%d', value));
end
function handles = parseLayoutStrategy(handles, value)
% @brief Parse the layout strategy
%
% Assign the good layout strategy in the handles array.
% @param handles Handles to virtual functions. Field <tt>computeLayout</tt> will be
% modified.
% @param value The number of the deal strategy to use.
% @return handles Handles to virtual functions. Field <tt>computeLayout</tt> has
% been modified.
handles.computeLayout = str2func(sprintf('computeLayout%d', value));
end
function handles = parseUseJava(handles, value)
% @brief Parse Java usage property
%
% Modify the handles structure so that Java is not used.
% Otherwise the monitor positions are obtained using Java.
% @param handles Handles to virtual functions. Field <tt>getMonitorPositions</tt>
% will be modified.
% @param value The number of the deal strategy to use.
% @return handles Handles to virtual functions. Field
% <tt>getMonitorPositions</tt> has been modified.
if (~value)
handles.getMonitorPositions = @getMonitorPositions;
end
end
function handles = parseMonitorPositions(handles, value)
% @brief Parse monitor positions
%
% Ensure that the given monitor positions are used when laying out
% the figures.
% @param handles Handles to virtual functions. Field <tt>getMonitorPositions</tt>
% will be modified.
% @param value The number of the deal strategy to use.
% @return handles Handles to virtual functions. Field
% <tt>getMonitorPositions</tt> has been modified.
handles.getMonitorPositions = @() value;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% SCREEN DEALING FUNCTIONS :
function [deal] = dealFigures1(occs, areas) %#ok This is a virtual function which may be called when selecting DealStrategy 1
% @brief Deal figures between monitors
%
% Find the best deal between monitors for the figures.
% This function tries to find the best tradeoff between areas and screen
% occupation. It does it by a dichotomy selecting occupations greather than
% a level and relative difference in areas smaller than (1 - level)^2.
% @param occs Screen occupations.
% @param areas Figures areas.
% @return deal A line vector containing the best deal of the figures
% between all the available screens.
minAreas = min(areas + 1./areas.*(areas == 0), [], 2);
%% Find the best possible deal:
levelMin = 0;
levelMax = 1;
while(true)
level = (levelMin + levelMax) / 2;
acceptableAreas = (max(minAreas) - minAreas) < (1 - level)^2*max(minAreas);
acceptableOccs = occs(:, end) >= level;
% Precision of dichotomy reached:
if (levelMax - levelMin < 5e-3)
break;
end
% There is only on layout acceptable, perfect:
if (sum(acceptableAreas & acceptableOccs) == 1)
break;
end
if (sum(acceptableAreas & acceptableOccs) == 0)
% No layout is accetable, decrease level.
levelMax = level;
else
% To many layouts are acceptable, increase level.
levelMin = level;
end
end
i = find(acceptableAreas & acceptableOccs, 1);
deal = occs(i, :);
end
function [deal] = dealFigures2(occs, areas) %#ok This is a virtual function which may be called when selecting DealStrategy 2
% @brief Deal figures between monitors
%
% Find the best deal between monitors for the figures
% This function tries to find the best tradeoff between areas and screen
% occupation. It does it by a dichotomy selecting occupations greather than
% a level and relative difference in total areas covered by figures smaller
% than (1 - level)^2 and then selecting the deal with the biggest possible
% areas.
% @param occs Screen occupations.
% @param areas Figures areas.
% @return deal A line vector containing the best deal of the figures
% between all the available screens.
sumAreas = sum(areas.*occs(:, 1:size(areas, 2)), 2);
%% Find the best possible deal:
levelMin = 0;
levelMax = 1;
while(true)
level = (levelMin + levelMax) / 2;
acceptableAreas = (max(sumAreas) - sumAreas) < (1 - level)^2*max(sumAreas);
acceptableOccs = occs(:, end) >= level;
% Precision of dichotomy reached:
if (levelMax - levelMin < 5e-3)
break;
end
% There is only one layout acceptable, perfect:
if (sum(acceptableAreas & acceptableOccs) == 1)
break;
end
if (sum(acceptableAreas & acceptableOccs) == 0)
% No layout is accetable, decrease level.
levelMax = level;
else
% To many layouts are acceptable, increase level.
levelMin = level;
end
end
[~, i] = max(min(areas + 1./areas.*(areas == 0), [], 2).*(acceptableAreas & acceptableOccs));
%i = find(acceptableAreas & acceptableOccs, 1);
deal = occs(i, :);
end
function [deal] = dealFigures3(occs, areas)
% @brief Deal figures between monitors
%
% Find the best deal between monitors for the figures
% This function tries to find the best tradeoff between areas and screen
% occupation. It does it by a dichotomy selecting occupations greather than
% a level and relative difference in total areas covered by figures smaller
% than (1 - level)^2 and then selecting the deal with the smallest possible
% area difference.
% @param occs Screen occupations.
% @param areas Figures areas.
% @return deal A line vector containing the best deal of the figures
% between all the available screens.
sumAreas = sum(areas.*occs(:, 1:size(areas, 2)), 2);
%% Find the best possible deal:
levelMin = 0;
levelMax = 1;
while(true)
level = (levelMin + levelMax) / 2;
acceptableAreas = (max(sumAreas) - sumAreas) < (1 - level)^2*max(sumAreas);
acceptableOccs = occs(:, end) >= level;
% Precision of dichotomy reached:
if (levelMax - levelMin < 5e-3)
break;
end
% There is only one layout acceptable, perfect:
if (sum(acceptableAreas & acceptableOccs) == 1)
break;
end
if (sum(acceptableAreas & acceptableOccs) == 0)
% No layout is accetable, decrease level.
levelMax = level;
else
% To many layouts are acceptable, increase level.
levelMin = level;
end
end
[~, i] = min((max(areas, [], 2) - min(areas, [], 2))./(acceptableAreas & acceptableOccs));
%i = find(acceptableAreas & acceptableOccs, 1);
deal = occs(i, :);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LAYOUT COMPUTING FUNCTIONS :
function [l, c, wf, hf] = computeLayout1(n, w, h) %#ok This is a virtual function which may be called when selecting LayoutStrategy 1
% @brief Lay a screen out
%
% Tries to find the best line and columns number to lay out
% the given number of figures on a screen.
% This function tries to have the quotient hf/wf close to 1 (square figure).
% @param n The number or figures which will be eventually displayed on
% this screen.
% @param w The width of the screen.
% @param h The height of the screen.
% @return The following informarion:
% - **l** The number of lines of figures.
% - **c** The number of colums of figures.
% - **wf** The width of the figures.
% - **hf** The height of the figures.
%% Determines the best values for l and c:
% Tries to get the best square figure.
l = 1;
while (l <= n)
c = ceil(n/l);
wf = floor(w/c);
hf = floor(h/l);
sqrf = hf/wf;
if (sqrf < 1)
if (l == 1)
% If with one line width is greather than height, then
% stop.
return
else
% Are figures with previous setting more square?
% If yes, then return to it. Otherwise keep current.
if ((sqrfp - 1) - eps < (1 - sqrf))
l = l-1;
c = ceil(n/l);
wf = floor(w/c);
hf = floor(h/l);
end
return;
end
end
sqrfp = sqrf;
l = l + 1;
end
end
function [l, c, wf, hf] = computeLayout2(n, w, h) %#ok This is a virtual function which may be called when selecting LayoutStrategy 2
% @brief Lay a screen out
%
% Tries to find the best line and columns number to lay out
% the given number of figures on a screen.
% It tries to have c and l integers close to c0 = w/h*l0 by rounding.
% @param n The number or figures which will be eventually displayed on
% this screen.
% @param w The width of the screen.
% @param h The height of the screen.
% @return The following informarion:
% - **l** The number of lines of figures.
% - **c** The number of colums of figures.
% - **wf** The width of the figures.
% - **hf** The height of the figures.
l = round(sqrt(h*n/w));
if (l == 0)
l = 1;
end
c = ceil(n/l);
wf = floor(w/c);
hf = floor(h/l);
end
function [l, c, wf, hf] = computeLayout3(n, w, h) %#ok This is a virtual function which may be called when selecting LayouStrategy 3
% @brief Lay a screen out
%
% Tries to find the best line and columns number to lay out
% the given number of figures on a screen.
% It tries to have c and l integers close to c0 = w/h*l0 and then selects
% the solution which give the closest solution.
% @param n The number or figures which will be eventually displayed on
% this screen.
% @param w The width of the screen.
% @param h The height of the screen.
% @return The following informarion:
% - **l** The number of lines of figures.
% - **c** The number of colums of figures.
% - **wf** The width of the figures.
% - **hf** The height of the figures.
l0 = floor(sqrt(h*n/w));
l1 = ceil(sqrt(h*n/w));
if ((l0 == 0) || (l1^2 - h*n/w < h*n/w - l0^2))
l = l1;
else
l = l0;
end
c = ceil(n/l);
wf = floor(w/c);
hf = floor(h/l);
end
function [l, c, wf, hf] = computeLayout4(n, w, h)
% @brief Lay a screen out
%
% Tries to find the best line and columns number to lay out
% the given number of figures on a screen.
% It tries to have c and l integers close to c0 = w/h*l0 and then selects
% the solution which give the most square figs.
% @param n The number or figures which will be eventually displayed on
% this screen.
% @param w The width of the screen.
% @param h The height of the screen.
% @return The following informarion:
% - **l** The number of lines of figures.
% - **c** The number of colums of figures.
% - **wf** The width of the figures.
% - **hf** The height of the figures.
l0 = floor(sqrt(h*n/w));
l1 = ceil(sqrt(h*n/w));
if ((l0 == 0) || (abs(1 - sqrt(floor(w/ceil(n/l1))/floor(h/l1))) < abs(1 - sqrt(floor(w/ceil(n/l0))/floor(h/l0)))))
l = l1;
else
l = l0;
end
c = ceil(n/l);
wf = floor(w/c);
hf = floor(h/l);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% HELPER FUNCTIONS :
function noop(varargin)
% @brief No-op
%
% Do nothing with any number of arguments
% @param varargin Any arguments
end
function v = versionNumber()
% @brief MATLAB version Number
%
% Return MATLAB version as a number.
%
% Indeed, it is better to have the version as a number in code (for comparisons).
% The returned number is equal to the year part, plus 1/2 if the version is a b
% release.
%
% @returns An number conveying MATLAB version.
%
% \par Examples
% - When invoked on MATLAB 2011a, this function returns <tt>2011.0</tt>
% - When invoked on MATLAB 2014b, this function returns <tt>2014.5</tt>
ver = version('-release');
v = sscanf(ver(1:4), '%d');
if (ver(5) == 'b')
v = v + 0.5;
end
end
function v = windowsVersionNumber()
% @brief Windows version Number
%
% Returns the version of Windows as a number.