-
Notifications
You must be signed in to change notification settings - Fork 4
/
magnifyOnFigure.m
2306 lines (1970 loc) · 94.8 KB
/
magnifyOnFigure.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
% NAME: magnifyOnFigure
%
% AUTHOR: David Fernandez Prim ([email protected])
%
% PURPOSE: Shows a functional zoom tool, suitable for publishing of zoomed
% images and 2D plots
%
% INPUT ARGUMENTS:
% figureHandle [double 1x1]: graphic handle of the target figure
% axesHandle [double 1x1]: graphic handle of the target axes.
%
% OUTPUT ARGUMENTS:
% none
%
% SINTAX:
% 1) magnifyOnFigure;
% $ Adds magnifier on the first axes of the current figure, with
% default behavior.
%
% 2) magnifyOnFigure( figureHandle );
% $ Adds magnifier on the first axes of the figure with handle
% 'figureHandle', with default behavior.
%
% 3) magnifyOnFigure( figureHandle, 'property1', value1,... );
% $ Adds magnifier on the first axes of the figure with handle
% 'figureHandle', with modified behavior.
%
% 4) magnifyOnFigure( axesHandle );
% $ Adds magnifier on the axes with handle 'axesHandle', with
% default behavior.
%
% 5) magnifyOnFigure( axesHandle, 'property1', value1,... );
% $ Adds magnifier on the axes with handle 'axesHandle', with
% modified behavior.
%
% 6) Consecutive calls to this function (in any of the syntaxes
% exposed above) produce multiple selectable magnifiers on the target axes.
%
% USAGE EXAMPLES: see script 'magnifyOnFigure_examples.m'
%
% PROPERTIES:
% 'magnifierShape': 'Shape of the magnifier ('rectangle' or 'ellipse' allowed, 'rectangle' as default)
% 'secondaryAxesFaceColor': ColorSpec
% 'edgeWidth' Color of the box surrounding the secondary
% axes, magnifier and link. Default 1
% 'edgeColor': Color of the box surrounding the secondary
% axes, magnifier and link. Default 'black'
% 'displayLinkStyle': Style of the link. 'none', 'straight' or
% 'edges', with 'straight' as default.
% 'mode': 'manual' or 'interactive' (allowing
% adjustments through mouse/keyboard). Default
% 'interactive'.
% 'units' Units in which the position vectors are
% given. Only 'pixels' currently supported
% 'initialPositionSecondaryAxes': Initial position vector ([left bottom width height])
% of secondary axes, in pixels
% 'initialPositionMagnifier': Initial position vector ([left bottom width height])
% of magnifier, in pixels
% 'secondaryAxesXLim': Initial XLim value of the secondary axes
% 'secondaryAxesYLim': Initial YLim value of the secondary axes
% 'frozenZoomAspectRatio': Specially useful for images, forces the use of the same zoom
% factor on both X and Y axes, in order to keep the aspect ratio
% ('on' or 'off' allowed, 'off' by default
%
% HOT KEYS (active if 'mode' set to 'interactive')
%
% -In a figure with multiple tool instances
% 'Tab': Switch the focus from one magnifier instance
% to the next one on the current figure.
% 'Mouse pointer on secondary axes or magnifier of a tool+double left click'
% Regain focus
%
% -On the focused magnifier instance
% 'up arrow': Moves magnifier 1 pixel upwards
% 'down arrow': Moves magnifier 1 pixel downwards
% 'left arrow': Moves magnifier 1 pixel to the left
% 'right arrow': Moves magnifier 1 pixel to the right
% 'Shift+up arrow': Expands magnifier 10% on the Y-axis
% 'Shift+down arrow': Compress magnifier 10% on the Y-axis
% 'Shift+left arrow': Compress magnifier 10% on the X-axis
% 'Shift+right arrow': Expands magnifier 10% on the X-axis
% 'Control+up arrow': Moves secondary axes 1 pixel upwards
% 'Control+down arrow': Moves secondary axes 1 pixel downwards
% 'Control+left arrow': Moves secondary axes 1 pixel to the left
% 'Control+right arrow': Moves secondary axes 1 pixel to the right
% 'Alt+up arrow': Expands secondary axes 10% on the Y-axis
% 'Alt+down arrow': Compress secondary axes 10% on the Y-axis
% 'Alt+left arrow': Compress secondary axes 10% on the X-axis
% 'Alt+right arrow': Expands secondary axes 10% on the X-axis
% 'PageUp': Increase additional zooming factor on X-axis
% 'PageDown': Decrease additional zooming factor on X-axis
% 'Shift+PageUp': Increase additional zooming factor on Y-axis
% 'Shift+PageDown': Decrease additional zooming factor on Y-axis
% 'Control+Q': Resets the additional zooming factors to 0
% 'Control+A': Displays position of secondary axes and
% magnifier in the command window
% 'Control+D': Deletes the focused tool
% 'Control+I': Shows/hides the tool identifier (red
% background color when the tool has the focus,
% black otherwise)
% 'Mouse pointer on magnifier+left click' Drag magnifier to any
% direction
% 'Mouse pointer on secondary axes+left click' Drag secondary axes in any
% direction
%
% TODO:
% - Use another axes copy as magnifier instead of rectangle (no ticks).
% - Adapt to work on 3D plots.
% - Add tip tool with interface description?.
%
% KNOWN ISSUES:
% - Secondary axes are not updated when the zoomming or panning tools of the figure are used.
% - Degraded performance for big data sets or big window sizes.
% - The size and position of the magnifier are modified for
% 'PaperPositionMode' equal to 'auto', when the figure is printed to file
% through 'print'
%
% CHANGE HISTORY:
%
% Version | Date | Author | Description
%---------------|---------------|-------------------|---------------------------------------
% 1.0 | 28/11/2009 | D. Fernandez | First version
% 1.1 | 29/11/2009 | D. Fernandez | Added link from magnifier to secondary axes
% 1.2 | 30/11/2009 | D. Fernandez | Keyboard support added
% 1.3 | 01/12/2009 | D. Fernandez | Properties added
% 1.4 | 02/12/2009 | D. Fernandez | Manual mode supported
% 1.5 | 03/12/2009 | D. Fernandez | New link style added ('edges')
% 1.6 | 03/12/2009 | D. Fernandez | Bug solved in display of link style 'edges'
% 1.7 | 04/12/2009 | D. Fernandez | Target axes selection added
% 1.8 | 07/12/2009 | D. Fernandez | Solved bug when any of the axes are reversed.
% 1.9 | 08/12/2009 | D. Fernandez | Adapted to work under all axes modes (tight, square, image, ...)
% 1.10 | 08/12/2009 | D. Fernandez | Added frozenZoomAspectRatio zoom mode, useful for images
% 1.11 | 08/12/2009 | D. Fernandez | Solved bug when axes contain other than 'line' or 'image' objects
% 1.12 | 05/01/2010 | D. Fernandez | Added support to multiple instances
% 1.13 | 05/01/2010 | D. Fernandez | Added 'delete' functionality
% 1.14 | 05/01/2010 | D. Fernandez | Solved bug when initial positions for secondary axes and/or magnifier are specified
% 1.15 | 07/01/2010 | D. Fernandez | Solved bug when resizing window
% 1.16 | 07/01/2010 | D. Fernandez | Improved documentation
% 1.17 | 28/03/2010 | D. Fernandez | Added tool identifications feature
%
function magnifyOnFigure( varargin )
clear global appDataStruct
global appDataStruct
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CHECK OUTPUT ARGUMENTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch nargout
case 0
%Correct
outputObjectExpected = false;
case 1
%tool object expected at the output
outputObjectExpected = false;
otherwise
error('Number of output arguments not supported.');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CHECK INPUT ARGUMENTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin == 0
%Initialize 'appDataStructuct' with default values
appDataStruct = initializeToolStruct();
%Set figure handle
appDataStruct.figureHandle = gcf;
% Get number of axes in the same figure
childHandle = get(appDataStruct.figureHandle, 'Children');
iAxes = find(strcmpi(get(childHandle, 'Type'), 'axes'));
% If no target axes specified, select the first found as mainAxes
appDataStruct.mainAxesHandle = childHandle( iAxes(end) );
elseif nargin > 0
if isstruct(varargin{1})
%Initialize 'appDataStructuct' with existent structure
appDataStruct = initializeToolStruct( varargin{1} );
elseif ishandle(varargin{1}) && strcmpi(get(varargin{1}, 'Type'), 'figure')
%Initialize 'appDataStructuct' with default values
appDataStruct = initializeToolStruct();
%Set figure handle
appDataStruct.figureHandle = varargin{1};
% Get number of axes in the same figure
childHandle = get(appDataStruct.figureHandle, 'Children');
iAxes = find(strcmpi(get(childHandle, 'Type'), 'axes'));
% If no target axes specified, select the first found as mainAxes
appDataStruct.mainAxesHandle = childHandle( iAxes(end) );
elseif ishandle(varargin{1}) && strcmpi(get(varargin{1}, 'Type'), 'axes')
%Initialize 'appDataStructuct' with default values
appDataStruct = initializeToolStruct();
appDataStruct.mainAxesHandle = varargin{1};
% Get figure handle
parentHandle = get(varargin{1}, 'Parent');
iHandle = find(strcmpi(get(parentHandle, 'Type'), 'figure'));
% Figure is the parent of the axes
appDataStruct.figureHandle = parentHandle( iHandle(1) );
else
if ishandle(varargin{1})
warning('Wrong figure/axes handle specified. The magnifier will be applied on the current figure.');
elseif isobject(varargin{1})
error('Wrong object specified.');
else
error('Wrong input class specified.');
end
end
if mod(nargin-1, 2) == 0
%Check input properties
for i = 2:2:nargin
if ~strcmpi( varargin{i}, 'frozenZoomAspectRatio' ) &&...
~strcmpi( varargin{i}, 'magnifierShape' ) &&...
~strcmpi( varargin{i}, 'secondaryaxesxlim' ) &&...
~strcmpi( varargin{i}, 'secondaryaxesylim' ) &&...
~strcmpi( varargin{i}, 'secondaryaxesfacecolor' ) &&...
~strcmpi( varargin{i}, 'edgewidth' ) &&...
~strcmpi( varargin{i}, 'edgecolor' ) &&...
~strcmpi( varargin{i}, 'displayLinkStyle' ) &&...
~strcmpi( varargin{i}, 'mode' ) &&...
~strcmpi( varargin{i}, 'units' ) &&...
~strcmpi( varargin{i}, 'initialpositionsecondaryaxes' ) &&...
~strcmpi( varargin{i}, 'initialpositionmagnifier' )
error('Illegal property specified. Please check.');
end
if strcmpi( varargin{i}, 'frozenZoomAspectRatio' )
if ischar(varargin{i+1}) &&...
( strcmpi(varargin{i+1}, 'on') || strcmpi(varargin{i+1}, 'off') )
appDataStruct.globalZoomMode = lower(varargin{i+1});
else
warning(sprintf('Specified zoom mode not supported. Default values will be applied [%s].', appDataStruct.globalZoomMode));
end
end
if strcmpi( varargin{i}, 'mode' )
if ischar(varargin{i+1}) &&...
( strcmpi(varargin{i+1}, 'manual') || strcmpi(varargin{i+1}, 'interactive') )
appDataStruct.globalMode = lower(varargin{i+1});
else
warning(sprintf('Specified mode descriptor not supported. Default values will be applied [%s].', appDataStruct.globalMode));
end
end
if strcmpi( varargin{i}, 'magnifierShape' )
if ischar(varargin{i+1}) &&...
( strcmpi(varargin{i+1}, 'rectangle') || strcmpi(varargin{i+1}, 'ellipse') )
appDataStruct.magnifierShape = lower(varargin{i+1});
else
warning(sprintf('Specified magnifier shape not supported. Default values will be applied [%s].', appDataStruct.magnifierShape));
end
end
if strcmpi( varargin{i}, 'displayLinkStyle' )
if ischar(varargin{i+1}) &&...
( strcmpi(varargin{i+1}, 'straight') || strcmpi(varargin{i+1}, 'none') || strcmpi(varargin{i+1}, 'edges') )
if ~strcmpi(appDataStruct.magnifierShape, 'rectangle') && strcmpi(varargin{i+1}, 'edges')
warning(sprintf('Specified link style not supported. Default values will be applied for ''displayLinkStyle''[%s].', appDataStruct.linkDisplayStyle));
else
appDataStruct.linkDisplayStyle = lower(varargin{i+1});
end
else
warning(sprintf('Specified descriptor not supported. Default values will be applied for ''displayLink''[%s].', appDataStruct.linkDisplayStyle));
end
end
if strcmpi( varargin{i}, 'units' )
if ischar(varargin{i+1}) && strcmpi(varargin{i+1}, 'pixels')
appDataStruct.globalUnits = lower(varargin{i+1});
else
warning(sprintf('Specified units descriptor not supported. Default values will be applied [%s].', appDataStruct.globalUnits));
end
end
if strcmpi( varargin{i}, 'edgewidth' )
if length(varargin{i+1})==1 && isnumeric(varargin{i+1})
appDataStruct.globalEdgeWidth = varargin{i+1};
else
warning(sprintf('Incorrect edge width value. Default value will be applied [%g].', appDataStruct.globalEdgeWidth ))
end
end
if strcmpi( varargin{i}, 'edgecolor' )
if ( length(varargin{i+1})==3 && isnumeric(varargin{i+1}) ) ||...
( ischar(varargin{i+1}) )
appDataStruct.globalEdgeColor = varargin{i+1};
else
warning('Incorrect edge color value. Default black will be applied.');
end
end
if strcmpi( varargin{i}, 'secondaryaxesfacecolor' )
if ( length(varargin{i+1})==3 && isnumeric(varargin{i+1}) ) ||...
( ischar(varargin{i+1}) )
appDataStruct.secondaryAxesFaceColor = varargin{i+1};
else
warning('Incorrect secondary axes face color value. Default white will be applied.');
end
end
if strcmpi( varargin{i}, 'secondaryaxesxlim' )
if ( length(varargin{i+1})==2 && isnumeric(varargin{i+1}) )
appDataStruct.secondaryAxesXLim = varargin{i+1};
else
warning('Incorrect secondary axes XLim value. Default white will be applied.');
end
end
if strcmpi( varargin{i}, 'secondaryaxesylim' )
if ( length(varargin{i+1})==2 && isnumeric(varargin{i+1}) )
appDataStruct.secondaryAxesYLim = varargin{i+1};
else
warning('Incorrect secondary axes YLim value. Default white will be applied.');
end
end
if strcmpi( varargin{i}, 'initialpositionsecondaryaxes' )
if length(varargin{i+1})==4 && isnumeric(varargin{i+1})
appDataStruct.secondaryAxesPosition = varargin{i+1};
else
warning('Incorrect initial position of secondary axes. Default values will be applied.')
end
end
if strcmpi( varargin{i}, 'initialpositionmagnifier' )
if length(varargin{i+1})==4 && isnumeric(varargin{i+1})
appDataStruct.magnifierPosition = varargin{i+1};
else
warning('Incorrect initial position of magnifier. Default values will be applied.')
end
end
end
else
error('Number of input arguments not supported.');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ENTRY POINT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create secondary axes
if isempty(appDataStruct.secondaryAxesHandle)
appDataStruct.secondaryAxesHandle = copyobj(appDataStruct.mainAxesHandle, appDataStruct.figureHandle);
end
%Configure secondary axis
set( appDataStruct.secondaryAxesHandle, 'Color', get(appDataStruct.mainAxesHandle,'Color'), 'Box','on');
set( appDataStruct.secondaryAxesHandle, 'FontWeight', 'bold',...
'LineWidth', appDataStruct.globalEdgeWidth,...
'XColor', appDataStruct.globalEdgeColor,...
'YColor', appDataStruct.globalEdgeColor,...
'Color', appDataStruct.secondaryAxesFaceColor );
set( appDataStruct.figureHandle, 'CurrentAxes', appDataStruct.secondaryAxesHandle );
xlabel(''); ylabel(''); zlabel(''); title('');
axis( appDataStruct.secondaryAxesHandle, 'normal'); %Ensure that secondary axes are not resizing
set( appDataStruct.figureHandle, 'CurrentAxes', appDataStruct.mainAxesHandle );
%Default magnifier position
if isempty(appDataStruct.magnifierPosition)
appDataStruct.magnifierPosition = computeMagnifierDefaultPosition();
end
%Default secondary axes position
if isempty(appDataStruct.secondaryAxesPosition)
appDataStruct.secondaryAxesPosition = computeSecondaryAxesDefaultPosition();
end
% #PATCH 1 (part 1)
toolArrayAux = get(appDataStruct.figureHandle, 'userdata');
set(appDataStruct.figureHandle, 'userdata', []);
% #END PATCH 1 (part 1)
%Set initial position of secondary axes
setSecondaryAxesPositionInPixels( appDataStruct.secondaryAxesPosition );
%Set initial position of magnifier
setMagnifierPositionInPixels( appDataStruct.magnifierPosition );
% #PATCH 1 (part 2)
set(appDataStruct.figureHandle, 'userdata', toolArrayAux);
% #END PATCH 1 (part 2)
%Update view limits on secondary axis
refreshSecondaryAxisLimits();
%Update link between secondary axes and magnifier
refreshMagnifierToSecondaryAxesLink();
%Set actions for interactive mode
if strcmpi( appDataStruct.globalMode, 'interactive')
toolArray = get(appDataStruct.figureHandle, 'userdata');
nTools = length(toolArray);
if nTools == 0
%Store figure position
appDataStruct.figurePosition = getFigurePositionInPixels();
%Store old callbacks
appDataStruct.figureOldWindowButtonDownFcn = get( appDataStruct.figureHandle, 'WindowButtonDownFcn');
appDataStruct.figureOldWindowButtonUpFcn = get( appDataStruct.figureHandle, 'WindowButtonUpFcn');
appDataStruct.figureOldWindowButtonMotionFcn = get( appDataStruct.figureHandle, 'WindowButtonMotionFcn');
appDataStruct.figureOldKeyPressFcn = get( appDataStruct.figureHandle, 'KeyPressFcn');
appDataStruct.figureOldDeleteFcn = get( appDataStruct.figureHandle, 'DeleteFcn');
appDataStruct.figureOldResizeFcn = get( appDataStruct.figureHandle, 'ResizeFcn');
%Set service funcions to events
set( appDataStruct.figureHandle, ...
'WindowButtonDownFcn', @ButtonDownCallback, ...
'WindowButtonUpFcn', @ButtonUpCallback, ...
'WindowButtonMotionFcn', @ButtonMotionCallback, ...
'KeyPressFcn', @KeyPressCallback, ...
'DeleteFcn', @DeleteCallback,...
'ResizeFcn', @ResizeCallback...
);
end
else
%Set service funcions to events
set( appDataStruct.figureHandle, ...
'WindowButtonDownFcn', '', ...
'WindowButtonUpFcn', '', ...
'WindowButtonMotionFcn', '', ...
'KeyPressFcn', '', ...
'DeleteFcn', '',...
'ResizeFcn', ''...
);
end
%Set focus
appDataStruct.focusOnThisTool = true;
%Compute unique ID of this magnifying tool, from handles of its elements
toolId = appDataStruct.figureHandle +...
appDataStruct.mainAxesHandle +...
appDataStruct.magnifierHandle +...
appDataStruct.linkHandle +...
appDataStruct.secondaryAxesHandle;
%Set ID of this magnifying tool
appDataStruct.toolId = toolId;
%Get active figure
figureHandle = appDataStruct.figureHandle;
%Save object of this tool to userdata in figure object
toolArray = get(figureHandle, 'UserData');
if isempty(toolArray)
toolArray = struct(appDataStruct);
toolArray.focusOnThisTool = true;
else
%Set focus to this tool
focusedTool = find([toolArray.focusOnThisTool] == 1);
toolArray(focusedTool).focusOnThisTool = false;
%search tool ID
indexFoundToolId = find([toolArray.toolId] == toolId);
if isempty(indexFoundToolId)
%If not found, create new
indexFoundToolId = length(toolArray)+1;
end
toolArray(indexFoundToolId) = struct(appDataStruct);
toolArray(indexFoundToolId).focusOnThisTool = true;
end
set( figureHandle, 'UserData', toolArray );
%Set callback global behaviour
set(appDataStruct.figureHandle, 'Interruptible', 'off');
set(appDataStruct.figureHandle, 'BusyAction', 'cancel');
%Return created object if requested
if outputObjectExpected == true
varargout{1} = appDataStruct;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NAME: refreshSecondaryAxisLimits
%
% PURPOSE: Updates the view on the secondary axis, based on position and
% span of magnifier, and extend of secondary axis.
%
% INPUT ARGUMENTS:
% appDataStructuct [struct 1x1]: global variable
% OUTPUT ARGUMENTS:
% change 'XLim' and 'YLim' of secondary axis (ACTION)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function refreshSecondaryAxisLimits()
global appDataStruct;
if isempty(appDataStruct)
return;
end
%If limits specified
if ~(isempty(appDataStruct.secondaryAxesXLim) ||...
isempty(appDataStruct.secondaryAxesYLim))
initialXLim = appDataStruct.secondaryAxesXLim;
initialYLim = appDataStruct.secondaryAxesYLim;
limitsToSet = [...
initialXLim(1)...
initialXLim(2)...
initialYLim(1)...
initialYLim(2)...
];
axis(appDataStruct.secondaryAxesHandle, limitsToSet);
appDataStruct.secondaryAxesXLim = [];
appDataStruct.secondaryAxesYLim = [];
else
%Get main axes limits, in axes units
mainAxesXLim = get( appDataStruct.mainAxesHandle, 'XLim' );
mainAxesYLim = get( appDataStruct.mainAxesHandle, 'YLim' );
mainAxesXDir = get( appDataStruct.mainAxesHandle, 'XDir' );
mainAxesYDir = get( appDataStruct.mainAxesHandle, 'YDir' );
%Get position and size of main axes in pixels
mainAxesPositionInPixels = getMainAxesPositionInPixels();
%Compute Pixels-to-axes units conversion factors
xMainAxisPixels2UnitsFactor = determineSpan( mainAxesXLim(1), mainAxesXLim(2) )/mainAxesPositionInPixels(3);
yMainAxisPixels2UnitsFactor = determineSpan( mainAxesYLim(1), mainAxesYLim(2) )/mainAxesPositionInPixels(4);
%Get position and extend of magnifier, in pixels
magnifierPosition = getMagnifierPositionInPixels(); %In pixels
%Relative to the lower-left corner of the axes
magnifierPosition(1) = magnifierPosition(1) - mainAxesPositionInPixels(1);
magnifierPosition(2) = magnifierPosition(2) - mainAxesPositionInPixels(2);
%Compute position and exted of magnifier, in axes units
magnifierPosition(3) = magnifierPosition(3) * xMainAxisPixels2UnitsFactor;
magnifierPosition(4) = magnifierPosition(4) * yMainAxisPixels2UnitsFactor;
if strcmpi(mainAxesXDir, 'normal') && strcmpi(mainAxesYDir, 'normal')
magnifierPosition(1) = mainAxesXLim(1) + magnifierPosition(1)*xMainAxisPixels2UnitsFactor;
magnifierPosition(2) = mainAxesYLim(1) + magnifierPosition(2)*yMainAxisPixels2UnitsFactor;
end
if strcmpi(mainAxesXDir, 'normal') && strcmpi(mainAxesYDir, 'reverse')
magnifierPosition(1) = mainAxesXLim(1) + magnifierPosition(1)*xMainAxisPixels2UnitsFactor;
magnifierPosition(2) = mainAxesYLim(2) - magnifierPosition(2)*yMainAxisPixels2UnitsFactor - magnifierPosition(4);
end
if strcmpi(mainAxesXDir, 'reverse') && strcmpi(mainAxesYDir, 'normal')
magnifierPosition(1) = mainAxesXLim(2) - magnifierPosition(1)*xMainAxisPixels2UnitsFactor - magnifierPosition(3);
magnifierPosition(2) = mainAxesYLim(1) + magnifierPosition(2)*yMainAxisPixels2UnitsFactor;
end
if strcmpi(mainAxesXDir, 'reverse') && strcmpi(mainAxesYDir, 'reverse')
magnifierPosition(1) = mainAxesXLim(2) - magnifierPosition(1)*xMainAxisPixels2UnitsFactor - magnifierPosition(3);
magnifierPosition(2) = mainAxesYLim(2) - magnifierPosition(2)*yMainAxisPixels2UnitsFactor - magnifierPosition(4);
end
secondaryAxisXlim = [magnifierPosition(1) magnifierPosition(1)+magnifierPosition(3)];
secondaryAxisYlim = [magnifierPosition(2) magnifierPosition(2)+magnifierPosition(4)];
zoomFactor = appDataStruct.secondaryAxesAdditionalZoomingFactor;
xZoom = zoomFactor(1);
yZoom = zoomFactor(2);
aux_secondaryAxisXlim(1) = mean(secondaryAxisXlim) -...
determineSpan( secondaryAxisXlim(1), mean(secondaryAxisXlim) )*(1-xZoom);
aux_secondaryAxisXlim(2) = mean(secondaryAxisXlim) +...
determineSpan( secondaryAxisXlim(2), mean(secondaryAxisXlim) )*(1-xZoom);
aux_secondaryAxisYlim(1) = mean(secondaryAxisYlim) -...
determineSpan( secondaryAxisYlim(1), mean(secondaryAxisYlim) )*(1-yZoom);
aux_secondaryAxisYlim(2) = mean(secondaryAxisYlim) +...
determineSpan( secondaryAxisYlim(2), mean(secondaryAxisYlim) )*(1-yZoom);
if aux_secondaryAxisXlim(1)<aux_secondaryAxisXlim(2) &&...
all(isfinite(aux_secondaryAxisXlim))
set( appDataStruct.secondaryAxesHandle, 'XLim', aux_secondaryAxisXlim );
end
if aux_secondaryAxisYlim(1)<aux_secondaryAxisYlim(2) &&...
all(isfinite(aux_secondaryAxisYlim))
set( appDataStruct.secondaryAxesHandle, 'YLim', aux_secondaryAxisYlim );
end
end
%Increase line width in plots on secondary axis
childHandle = get( appDataStruct.secondaryAxesHandle, 'Children');
for iChild = 1:length(childHandle)
if strcmpi(get(childHandle(iChild), 'Type'), 'line')
set(childHandle(iChild), 'LineWidth', 2);
end
if strcmpi(get(childHandle(iChild), 'Type'), 'image')
%Do nothing for now
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NAME: determineSpan
%
% PURPOSE: Computes the distance between two real numbers on a 1D space.
%
% INPUT ARGUMENTS:
% v1 [double 1x1]: first number
% v2 [double 1x1]: second number
% OUTPUT ARGUMENTS:
% span [double 1x1]: computed span
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function span = determineSpan( v1, v2 )
if v1>=0 && v2>=0
span = max(v1,v2) - min(v1,v2);
end
if v1>=0 && v2<0
span = v1 - v2;
end
if v1<0 && v2>=0
span = -v1 + v2;
end
if v1<0 && v2<0
span = max(-v1,-v2) - min(-v1,-v2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NAME: ResizeCallback
%
% PURPOSE: Service routine to Resize event.
%
% INPUT ARGUMENTS:
%
% OUTPUT ARGUMENTS:
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ResizeCallback(src,eventdata)
global appDataStruct;
if isempty(appDataStruct)
return;
end
%Get userdata
toolArray = get(src, 'userdata');
if isempty(toolArray)
return;
end
nTools = length( toolArray );
%Store Old&New Figure positions
oldFigurePosition = toolArray(1).figurePosition;
newFigurePosition = getFigurePositionInPixels();
%Backup global appDataStruct
appDataStructAux = appDataStruct;
for i=1:nTools
%Modify global vaiable, accessed by functions called below
appDataStruct = initializeToolStruct( toolArray(i) );
%Set position of secondaryAxes (automatically modified)
toolArray(i).secondaryAxesPosition = getSecondaryAxesPositionInPixels();
toolArray(i).magnifierPosition(1) = toolArray(i).magnifierPosition(1) * newFigurePosition(3)/oldFigurePosition(3);
toolArray(i).magnifierPosition(2) = toolArray(i).magnifierPosition(2) * newFigurePosition(4)/oldFigurePosition(4);
toolArray(i).magnifierPosition(3) = toolArray(i).magnifierPosition(3) * newFigurePosition(3)/oldFigurePosition(3);
toolArray(i).magnifierPosition(4) = toolArray(i).magnifierPosition(4) * newFigurePosition(4)/oldFigurePosition(4);
setMagnifierPositionInPixels( toolArray(i).magnifierPosition );
%Update view limits on secondary axis
refreshSecondaryAxisLimits();
%Update link between secondary axes and magnifier
refreshMagnifierToSecondaryAxesLink();
if i==1
%Update figure position
toolArray(i).figurePosition = getFigurePositionInPixels();
appDataStruct.figurePosition = toolArray(i).figurePosition;
end
end
%Update userdata
set(toolArray(1).figureHandle, 'userdata', toolArray);
%Update appDataStruct
appDataStruct = appDataStructAux;
%Get current position of seconday axes (in pixels)
appDataStruct.secondaryAxesPosition = getSecondaryAxesPositionInPixels();
%Get magnifier current position and size
appDataStruct.magnifierPosition = getMagnifierPositionInPixels();
clear appDataStructAux;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NAME: KeyPressCallback
%
% PURPOSE: Service routine to KeyPress event.
%
% INPUT ARGUMENTS:
%
% OUTPUT ARGUMENTS:
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function KeyPressCallback(src,eventdata)
global appDataStruct
if isempty(appDataStruct)
return;
end
currentCaracter = eventdata.Key;
currentModifier = eventdata.Modifier;
switch(currentCaracter)
case {'leftarrow'} % left arrow
%Move magnifier to the left
if isempty(currentModifier)
position = getMagnifierPositionInPixels();
magnifierPosition(1) = position(1)-1;
magnifierPosition(2) = position(2);
magnifierPosition(3) = position(3);
magnifierPosition(4) = position(4);
setMagnifierPositionInPixels( magnifierPosition );
toolArray = get( src, 'UserData' );
focusedTool = find([toolArray.focusOnThisTool] == 1);
toolArray(focusedTool) = updateToolId( toolArray(focusedTool), focusedTool, 'noToggle' );
set( src, 'UserData', toolArray );
end
%Compress magnifier on the X axis
if strcmp(currentModifier, 'shift')
position = getMagnifierPositionInPixels();
magnifierPosition(3) = position(3)*(1 - 0.1);
if strcmpi( appDataStruct.globalZoomMode, 'off')
magnifierPosition(4) = position(4);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
magnifierPosition(4) = position(4)*(1 - 0.1);
end
magnifierPosition(1) = position(1)-(-position(3)+magnifierPosition(3))/2;
magnifierPosition(2) = position(2)-(-position(4)+magnifierPosition(4))/2;
setMagnifierPositionInPixels( magnifierPosition );
end
%Move secondary axes to the left
if strcmp(currentModifier, 'control')
position = getSecondaryAxesPositionInPixels();
secondaryAxesPosition(1) = position(1)-1;
secondaryAxesPosition(2) = position(2);
secondaryAxesPosition(3) = position(3);
secondaryAxesPosition(4) = position(4);
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
%Compress secondary axes on the X axis
if strcmp(currentModifier, 'alt')
position = getSecondaryAxesPositionInPixels();
secondaryAxesPosition(3) = position(3)*(1 - 0.1);
if strcmpi( appDataStruct.globalZoomMode, 'off')
secondaryAxesPosition(4) = position(4);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
secondaryAxesPosition(4) = position(4)*(1 - 0.1);
end
secondaryAxesPosition(1) = position(1)-(-position(3)+secondaryAxesPosition(3))/2;
secondaryAxesPosition(2) = position(2)-(-position(4)+secondaryAxesPosition(4))/2;
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
case {'rightarrow'} % right arrow
%Move magnifier to the right
if isempty(currentModifier)
position = getMagnifierPositionInPixels();
magnifierPosition(1) = position(1)+1;
magnifierPosition(2) = position(2);
magnifierPosition(3) = position(3);
magnifierPosition(4) = position(4);
setMagnifierPositionInPixels( magnifierPosition );
toolArray = get( src, 'UserData' );
focusedTool = find([toolArray.focusOnThisTool] == 1);
toolArray(focusedTool) = updateToolId( toolArray(focusedTool), focusedTool, 'noToggle' );
set( src, 'UserData', toolArray );
end
%Expand magnifier on the X axis
if strcmp(currentModifier, 'shift')
position = getMagnifierPositionInPixels();
magnifierPosition(3) = position(3)*(1 + 0.1);
if strcmpi( appDataStruct.globalZoomMode, 'off')
magnifierPosition(4) = position(4);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
magnifierPosition(4) = position(4)*(1 + 0.1);
end
magnifierPosition(1) = position(1)-(-position(3)+magnifierPosition(3))/2;
magnifierPosition(2) = position(2)-(-position(4)+magnifierPosition(4))/2;
setMagnifierPositionInPixels( magnifierPosition );
end
%Move secondary axes to the right
if strcmp(currentModifier, 'control')
position = getSecondaryAxesPositionInPixels();
secondaryAxesPosition(1) = position(1)+1;
secondaryAxesPosition(2) = position(2);
secondaryAxesPosition(3) = position(3);
secondaryAxesPosition(4) = position(4);
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
%Expand secondary axes on the X axis
if strcmp(currentModifier, 'alt')
position = getSecondaryAxesPositionInPixels();
secondaryAxesPosition(3) = position(3)*(1 + 0.1);
if strcmpi( appDataStruct.globalZoomMode, 'off')
secondaryAxesPosition(4) = position(4);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
secondaryAxesPosition(4) = position(4)*(1 + 0.1);
end
secondaryAxesPosition(1) = position(1)-(-position(3)+secondaryAxesPosition(3))/2;
secondaryAxesPosition(2) = position(2)-(-position(4)+secondaryAxesPosition(4))/2;
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
case {'uparrow'} % up arrow
%Move magnifier to the top
if isempty(currentModifier)
position = getMagnifierPositionInPixels();
magnifierPosition(1) = position(1);
magnifierPosition(2) = position(2)+1;
magnifierPosition(3) = position(3);
magnifierPosition(4) = position(4);
setMagnifierPositionInPixels( magnifierPosition );
toolArray = get( src, 'UserData' );
focusedTool = find([toolArray.focusOnThisTool] == 1);
toolArray(focusedTool) = updateToolId( toolArray(focusedTool), focusedTool, 'noToggle' );
set( src, 'UserData', toolArray );
end
%Expand magnifier on the Y axis
if strcmp(currentModifier, 'shift')
position = getMagnifierPositionInPixels();
if strcmpi( appDataStruct.globalZoomMode, 'off')
magnifierPosition(3) = position(3);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
magnifierPosition(3) = position(3)*(1 + 0.1);
end
magnifierPosition(4) = position(4)*(1 + 0.1);
magnifierPosition(1) = position(1)-(-position(3)+magnifierPosition(3))/2;
magnifierPosition(2) = position(2)-(-position(4)+magnifierPosition(4))/2;
setMagnifierPositionInPixels( magnifierPosition );
end
%Move secondary axes to the top
if strcmp(currentModifier, 'control')
position = getSecondaryAxesPositionInPixels();
secondaryAxesPosition(1) = position(1);
secondaryAxesPosition(2) = position(2)+1;
secondaryAxesPosition(3) = position(3);
secondaryAxesPosition(4) = position(4);
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
%Expand secondary axes on the Y axis
if strcmp(currentModifier, 'alt')
position = getSecondaryAxesPositionInPixels();
if strcmpi( appDataStruct.globalZoomMode, 'off')
secondaryAxesPosition(3) = position(3);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
secondaryAxesPosition(3) = position(3)*(1 + 0.1);
end
secondaryAxesPosition(4) = position(4)*(1 + 0.1);
secondaryAxesPosition(1) = position(1)-(-position(3)+secondaryAxesPosition(3))/2;
secondaryAxesPosition(2) = position(2)-(-position(4)+secondaryAxesPosition(4))/2;
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
case {'downarrow'} % down arrow
%Move magnifier to the bottom
if isempty(currentModifier)
position = getMagnifierPositionInPixels();
magnifierPosition(1) = position(1);
magnifierPosition(2) = position(2)-1;
magnifierPosition(3) = position(3);
magnifierPosition(4) = position(4);
setMagnifierPositionInPixels( magnifierPosition );
toolArray = get( src, 'UserData' );
focusedTool = find([toolArray.focusOnThisTool] == 1);
toolArray(focusedTool) = updateToolId( toolArray(focusedTool), focusedTool, 'noToggle' );
set( src, 'UserData', toolArray );
end
%Compress magnifier on the Y axis
if strcmp(currentModifier, 'shift')
position = getMagnifierPositionInPixels();
if strcmpi( appDataStruct.globalZoomMode, 'off')
magnifierPosition(3) = position(3);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
magnifierPosition(3) = position(3)*(1 - 0.1);
end
magnifierPosition(4) = position(4)*(1 - 0.1);
magnifierPosition(1) = position(1)-(-position(3)+magnifierPosition(3))/2;
magnifierPosition(2) = position(2)-(-position(4)+magnifierPosition(4))/2;
setMagnifierPositionInPixels( magnifierPosition );
end
%Move secondary axes to the bottom
if strcmp(currentModifier, 'control')
position = getSecondaryAxesPositionInPixels();
secondaryAxesPosition(1) = position(1);
secondaryAxesPosition(2) = position(2)-1;
secondaryAxesPosition(3) = position(3);
secondaryAxesPosition(4) = position(4);
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
%Compress secondary axes on the Y axis
if strcmp(currentModifier, 'alt')
position = getSecondaryAxesPositionInPixels();
if strcmpi( appDataStruct.globalZoomMode, 'off')
secondaryAxesPosition(3) = position(3);
else
%If 'freezeZoomAspectRatio' to 'on', be consistent
secondaryAxesPosition(3) = position(3)*(1 - 0.1);
end
secondaryAxesPosition(4) = position(4)*(1 - 0.1);
secondaryAxesPosition(1) = position(1)-(-position(3)+secondaryAxesPosition(3))/2;
secondaryAxesPosition(2) = position(2)-(-position(4)+secondaryAxesPosition(4))/2;
setSecondaryAxesPositionInPixels( secondaryAxesPosition );
end
case {'tab'} % Tabulator
%Switch focus to next magnifier instance on the current figure
toolArray = get( src, 'UserData' );
nTools = length(toolArray);
focusedTool = find([toolArray.focusOnThisTool] == 1);
if focusedTool ~= nTools
nextFocusedTool = focusedTool+1;
else
nextFocusedTool = 1;
end
appDataStruct = initializeToolStruct( toolArray(nextFocusedTool) );
appDataStruct.focusOnThisTool = 1;
toolArray(focusedTool).focusOnThisTool = 0;
toolArray(nextFocusedTool).focusOnThisTool = 1;
if not(isempty(toolArray(focusedTool).toolIdHandle))
set(toolArray(focusedTool).toolIdHandle,'BackgroundColor', 'black', 'Color', 'white');
set(toolArray(nextFocusedTool).toolIdHandle,'BackgroundColor', 'red', 'Color', 'white');
end
set( src, 'UserData', toolArray );
case {'d'} % 'd'
%Delete focused instance
if strcmp(currentModifier, 'control')
toolArray = get( src, 'UserData' );
nTools = length(toolArray);
focusedTool = find([toolArray.focusOnThisTool] == 1);
delete(toolArray(focusedTool).magnifierHandle);
delete(toolArray(focusedTool).linkHandle);
delete(toolArray(focusedTool).secondaryAxesHandle);
if nTools > 1
%Set focus to next instance
if focusedTool ~= nTools
nextFocusedTool = focusedTool+1;
else
nextFocusedTool = 1;
end
toolArray(nextFocusedTool).focusOnThisTool = 1;
appDataStruct = initializeToolStruct( toolArray(nextFocusedTool) );
toolArray(focusedTool) = [];
set( src, 'UserData', toolArray );
else
%No instance to set focus on
appDataStruct = [];
set( src, 'UserData', [] );
end
end
case {'a'} % 'a'
%Debug info
if strcmp(currentModifier, 'control')
magnifierPosition = getMagnifierPositionInPixels();
disp(sprintf('Magnifier position: [%g %g %g %g];', magnifierPosition(1), magnifierPosition(2), magnifierPosition(3), magnifierPosition(4) ));
secondaryAxesPosition = getSecondaryAxesPositionInPixels();
disp(sprintf('Secondary axes position: [%g %g %g %g];', secondaryAxesPosition(1), secondaryAxesPosition(2), secondaryAxesPosition(3), secondaryAxesPosition(4) ));
end