@@ -336,77 +336,84 @@ def trigger(self, sender, event, data=None):
336
336
ax .figure .canvas .draw ()
337
337
338
338
339
- class ViewsPositions (object ):
340
- """Auxiliary class to handle changes in views and positions"""
339
+ class ToolViewsPositions (ToolBase ):
340
+ """Auxiliary Tool to handle changes in views and positions
341
+
342
+ This tool is accessed by navigation.manipulate_tool
343
+ This tool is used by all the tools that need to access the record of
344
+ views and positions of the figure
345
+ - Zoom
346
+ - Pan
347
+ - Home
348
+ - Back
349
+ - Forward
350
+ """
341
351
342
- views = WeakKeyDictionary ()
343
- """Record of views with Figure objects as keys"""
352
+ def __init__ (self , * args , ** kwargs ):
353
+ self .views = WeakKeyDictionary ()
354
+ self .positions = WeakKeyDictionary ()
355
+ ToolBase .__init__ (self , * args , ** kwargs )
344
356
345
- positions = WeakKeyDictionary ()
346
- """Record of positions with Figure objects as keys"""
357
+ def set_figure ( self , figure ):
358
+ ToolBase . set_figure ( self , figure )
347
359
348
- @classmethod
349
- def add_figure (cls , figure ):
350
- """Add a figure to the list of figures handled by this class"""
351
- if figure not in cls .views :
352
- cls .views [figure ] = cbook .Stack ()
353
- cls .positions [figure ] = cbook .Stack ()
360
+ def add_figure (self ):
361
+ """Add the current figure to the stack of views and positions"""
362
+ if self .figure not in self .views :
363
+ self .views [self .figure ] = cbook .Stack ()
364
+ self .positions [self .figure ] = cbook .Stack ()
354
365
# Define Home
355
- cls .push_current (figure )
366
+ self .push_current ()
356
367
# Adding the clear method as axobserver, removes this burden from
357
368
# the backend
358
- figure .add_axobserver (cls .clear )
369
+ self . figure .add_axobserver (self .clear )
359
370
360
- @classmethod
361
- def clear (cls , figure ):
371
+ def clear (self , figure ):
362
372
"""Reset the axes stack"""
363
- if figure in cls .views :
364
- cls .views [figure ].clear ()
365
- cls .positions [figure ].clear ()
373
+ if figure in self .views :
374
+ self .views [figure ].clear ()
375
+ self .positions [figure ].clear ()
366
376
367
- @classmethod
368
- def update_view (cls , figure ):
377
+ def update_view (self ):
369
378
"""Update the viewlim and position from the view and
370
379
position stack for each axes
371
380
"""
372
381
373
- lims = cls .views [figure ]()
382
+ lims = self .views [self . figure ]()
374
383
if lims is None :
375
384
return
376
- pos = cls .positions [figure ]()
385
+ pos = self .positions [self . figure ]()
377
386
if pos is None :
378
387
return
379
- for i , a in enumerate (figure .get_axes ()):
388
+ for i , a in enumerate (self . figure .get_axes ()):
380
389
xmin , xmax , ymin , ymax = lims [i ]
381
390
a .set_xlim ((xmin , xmax ))
382
391
a .set_ylim ((ymin , ymax ))
383
392
# Restore both the original and modified positions
384
393
a .set_position (pos [i ][0 ], 'original' )
385
394
a .set_position (pos [i ][1 ], 'active' )
386
395
387
- figure .canvas .draw_idle ()
396
+ self . figure .canvas .draw_idle ()
388
397
389
- @classmethod
390
- def push_current (cls , figure ):
398
+ def push_current (self ):
391
399
"""push the current view limits and position onto the stack"""
392
400
393
401
lims = []
394
402
pos = []
395
- for a in figure .get_axes ():
403
+ for a in self . figure .get_axes ():
396
404
xmin , xmax = a .get_xlim ()
397
405
ymin , ymax = a .get_ylim ()
398
406
lims .append ((xmin , xmax , ymin , ymax ))
399
407
# Store both the original and modified positions
400
408
pos .append ((
401
409
a .get_position (True ).frozen (),
402
410
a .get_position ().frozen ()))
403
- cls .views [figure ].push (lims )
404
- cls .positions [figure ].push (pos )
411
+ self .views [self . figure ].push (lims )
412
+ self .positions [self . figure ].push (pos )
405
413
406
- @classmethod
407
- def refresh_locators (cls , figure ):
414
+ def refresh_locators (self ):
408
415
"""Redraw the canvases, update the locators"""
409
- for a in figure .get_axes ():
416
+ for a in self . figure .get_axes ():
410
417
xaxis = getattr (a , 'xaxis' , None )
411
418
yaxis = getattr (a , 'yaxis' , None )
412
419
zaxis = getattr (a , 'zaxis' , None )
@@ -423,37 +430,30 @@ def refresh_locators(cls, figure):
423
430
424
431
for loc in locators :
425
432
loc .refresh ()
426
- figure .canvas .draw_idle ()
433
+ self . figure .canvas .draw_idle ()
427
434
428
- @classmethod
429
- def home (cls , figure ):
430
- cls .views [figure ].home ()
431
- cls .positions [figure ].home ()
435
+ def home (self ):
436
+ self .views [self .figure ].home ()
437
+ self .positions [self .figure ].home ()
432
438
433
- @classmethod
434
- def back (cls , figure ):
435
- cls .views [figure ].back ()
436
- cls .positions [figure ].back ()
439
+ def back (self ):
440
+ self .views [self .figure ].back ()
441
+ self .positions [self .figure ].back ()
437
442
438
- @classmethod
439
- def forward (cls , figure ):
440
- cls .views [figure ].forward ()
441
- cls .positions [figure ].forward ()
443
+ def forward (self ):
444
+ self .views [self .figure ].forward ()
445
+ self .positions [self .figure ].forward ()
442
446
443
447
444
448
class ViewsPositionsBase (ToolBase ):
445
449
"""Base class for ToolHome, ToolBack and ToolForward"""
446
450
447
451
_on_trigger = None
448
452
449
- def __init__ (self , * args , ** kwargs ):
450
- ToolBase .__init__ (self , * args , ** kwargs )
451
- self .viewspos = ViewsPositions ()
452
-
453
453
def trigger (self , sender , event , data = None ):
454
- self .viewspos . add_figure ( self . figure )
455
- getattr (self .viewspos , self ._on_trigger )(self . figure )
456
- self .viewspos . update_view ( self . figure )
454
+ self .navigation . get_tool ( 'viewpos' ). add_figure ( )
455
+ getattr (self .navigation . get_tool ( 'viewpos' ) , self ._on_trigger )()
456
+ self .navigation . get_tool ( 'viewpos' ). update_view ( )
457
457
458
458
459
459
class ToolHome (ViewsPositionsBase ):
@@ -499,15 +499,13 @@ class SaveFigureBase(ToolBase):
499
499
500
500
501
501
class ZoomPanBase (ToolToggleBase ):
502
- # Base class to group common functionality between zoom and pan
503
- # Not of much use for other tools, so not documented
502
+ """Base class for Zoom and Pan tools"""
504
503
def __init__ (self , * args ):
505
504
ToolToggleBase .__init__ (self , * args )
506
505
self ._button_pressed = None
507
506
self ._xypress = None
508
507
self ._idPress = None
509
508
self ._idRelease = None
510
- self .viewspos = ViewsPositions ()
511
509
512
510
def enable (self , event ):
513
511
self .figure .canvas .widgetlock (self )
@@ -523,7 +521,7 @@ def disable(self, event):
523
521
self .figure .canvas .mpl_disconnect (self ._idRelease )
524
522
525
523
def trigger (self , sender , event , data = None ):
526
- self .viewspos . add_figure ( self . figure )
524
+ self .navigation . get_tool ( 'viewpos' ). add_figure ( )
527
525
ToolToggleBase .trigger (self , sender , event , data )
528
526
529
527
@@ -543,7 +541,7 @@ def _cancel_action(self):
543
541
for zoom_id in self ._ids_zoom :
544
542
self .figure .canvas .mpl_disconnect (zoom_id )
545
543
self .navigation .tool_trigger_event ('rubberband' , self )
546
- self .viewspos . refresh_locators ( self . figure )
544
+ self .navigation . get_tool ( 'viewpos' ). refresh_locators ( )
547
545
self ._xypress = None
548
546
self ._button_pressed = None
549
547
self ._ids_zoom = []
@@ -611,8 +609,6 @@ def _mouse_move(self, event):
611
609
x1 , y1 , x2 , y2 = a .bbox .extents
612
610
x , lastx = x1 , x2
613
611
614
- # self.navigation.draw_rubberband(event, self, x, y, lastx, lasty)
615
- # data = {'x': x, 'y': y, 'lastx': lastx, 'lasty': lasty}
616
612
self .navigation .tool_trigger_event ('rubberband' ,
617
613
self ,
618
614
data = (x , y , lastx , lasty ))
@@ -736,7 +732,7 @@ def _release(self, event):
736
732
a .set_ylim ((ry1 , ry2 ))
737
733
738
734
self ._zoom_mode = None
739
- self .viewspos . push_current ( self . figure )
735
+ self .navigation . get_tool ( 'viewpos' ). push_current ( )
740
736
self ._cancel_action ()
741
737
742
738
@@ -757,7 +753,7 @@ def _cancel_action(self):
757
753
self ._xypress = []
758
754
self .figure .canvas .mpl_disconnect (self ._idDrag )
759
755
self .navigation .messagelock .release (self )
760
- self .viewspos . refresh_locators ( self . figure )
756
+ self .navigation . get_tool ( 'viewpos' ). refresh_locators ( )
761
757
762
758
def _press (self , event ):
763
759
if event .button == 1 :
@@ -794,7 +790,7 @@ def _release(self, event):
794
790
self ._cancel_action ()
795
791
return
796
792
797
- self .viewspos . push_current ( self . figure )
793
+ self .navigation . get_tool ( 'viewpos' ). push_current ( )
798
794
self ._cancel_action ()
799
795
800
796
def _mouse_move (self , event ):
@@ -842,6 +838,7 @@ def _mouse_move(self, event):
842
838
(ToolXScale , 'xscale' ),
843
839
(ToolYScale , 'yscale' ),
844
840
(ToolCursorPosition , 'position' ),
841
+ (ToolViewsPositions , 'viewpos' ),
845
842
('ToolSetCursor' , 'cursor' ),
846
843
('ToolRubberband' , 'rubberband' )]]]
847
844
0 commit comments