@@ -3230,41 +3230,27 @@ class NavigationBase(object):
3230
3230
3231
3231
Attributes
3232
3232
----------
3233
- canvas : `FigureCanvas ` instance
3233
+ manager : `FigureManager ` instance
3234
3234
toolbar : `Toolbar` instance that is controlled by this `Navigation`
3235
3235
keypresslock : `LockDraw` to know if the `canvas` key_press_event is
3236
3236
locked
3237
3237
messagelock : `LockDraw` to know if the message is available to write
3238
3238
"""
3239
3239
3240
3240
_default_cursor = cursors .POINTER
3241
- _default_tools = [tools .ToolToggleGrid ,
3242
- tools .ToolToggleFullScreen ,
3243
- tools .ToolQuit ,
3244
- tools .ToolEnableAllNavigation ,
3245
- tools .ToolEnableNavigation ,
3246
- tools .ToolToggleXScale ,
3247
- tools .ToolToggleYScale ,
3248
- tools .ToolHome , tools .ToolBack ,
3249
- tools .ToolForward ,
3250
- None ,
3251
- tools .ToolZoom ,
3252
- tools .ToolPan ,
3253
- None ,
3254
- 'ConfigureSubplots' ,
3255
- 'SaveFigure' ]
3256
-
3257
- def __init__ (self , canvas , toolbar = None ):
3241
+
3242
+ def __init__ (self , manager ):
3258
3243
""".. automethod:: _toolbar_callback"""
3259
3244
3260
- self .canvas = canvas
3261
- self .toolbar = self ._get_toolbar (toolbar , canvas )
3245
+ self .manager = manager
3246
+ self .canvas = manager .canvas
3247
+ self .toolbar = manager .toolbar
3262
3248
3263
- self ._key_press_handler_id = self .canvas .mpl_connect ('key_press_event' ,
3264
- self ._key_press )
3249
+ self ._key_press_handler_id = self .canvas .mpl_connect (
3250
+ 'key_press_event' , self ._key_press )
3265
3251
3266
- self ._idDrag = self .canvas .mpl_connect ('motion_notify_event' ,
3267
- self ._mouse_move )
3252
+ self ._idDrag = self .canvas .mpl_connect (
3253
+ 'motion_notify_event' , self ._mouse_move )
3268
3254
3269
3255
# a dict from axes index to a list of view limits
3270
3256
self .views = cbook .Stack ()
@@ -3280,36 +3266,15 @@ def __init__(self, canvas, toolbar=None):
3280
3266
# to write into toolbar message
3281
3267
self .messagelock = widgets .LockDraw ()
3282
3268
3283
- for tool in self . _default_tools :
3269
+ for name , tool in tools . tools :
3284
3270
if tool is None :
3285
3271
if self .toolbar is not None :
3286
3272
self .toolbar .add_separator (- 1 )
3287
3273
else :
3288
- self .add_tool (tool )
3274
+ self .add_tool (name , tool , None )
3289
3275
3290
3276
self ._last_cursor = self ._default_cursor
3291
3277
3292
- @classmethod
3293
- def get_default_tools (cls ):
3294
- """Get the default tools"""
3295
-
3296
- return cls ._default_tools
3297
-
3298
- @classmethod
3299
- def set_default_tools (cls , tools ):
3300
- """Set default tools"""
3301
-
3302
- cls ._default_tools = tools
3303
-
3304
- def _get_toolbar (self , toolbar , canvas ):
3305
- # must be inited after the window, drawingArea and figure
3306
- # attrs are set
3307
- if rcParams ['toolbar' ] == 'navigation' and toolbar is not None :
3308
- toolbar = toolbar (canvas .manager )
3309
- else :
3310
- toolbar = None
3311
- return toolbar
3312
-
3313
3278
@property
3314
3279
def active_toggle (self ):
3315
3280
"""Toggled Tool
@@ -3381,8 +3346,7 @@ def unregister(self, name):
3381
3346
This method is used by `PersistentTools` to remove the reference kept
3382
3347
by `Navigation`.
3383
3348
3384
- It is usually called by the `deactivate` method or during
3385
- destroy if it is a graphical Tool.
3349
+ It is usually called by the `unregister` method
3386
3350
3387
3351
If called, next time the `Tool` is used it will be reinstantiated
3388
3352
instead of using the existing instance.
@@ -3411,29 +3375,27 @@ def remove_tool(self, name):
3411
3375
if self .toolbar :
3412
3376
self .toolbar ._remove_toolitem (name )
3413
3377
3414
- def add_tool (self , tool ):
3378
+ def add_tool (self , name , tool , position = None ):
3415
3379
"""Add tool to `Navigation`
3416
3380
3417
3381
Parameters
3418
3382
----------
3383
+ name : string
3384
+ Name of the tool, treated as the ID, has to be unique
3419
3385
tool : string or `Tool` class
3420
3386
Reference to find the class of the Tool to be added
3387
+ position : int or None (default)
3388
+ Position in the toolbar, if None, is positioned at the end
3421
3389
"""
3422
3390
3423
3391
tool_cls = self ._get_cls_to_instantiate (tool )
3424
3392
if tool_cls is False :
3425
3393
warnings .warn ('Impossible to find class for %s' % str (tool ))
3426
3394
return
3427
- name = tool_cls .name
3428
3395
3429
- if name is None :
3430
- warnings .warn ('tool_clss need a name to be added, it is used '
3431
- 'as ID' )
3432
- return
3433
3396
if name in self ._tools :
3434
3397
warnings .warn ('A tool_cls with the same name already exist, '
3435
3398
'not added' )
3436
-
3437
3399
return
3438
3400
3439
3401
self ._tools [name ] = tool_cls
@@ -3444,7 +3406,7 @@ def add_tool(self, tool):
3444
3406
(k , self ._keys [k ], name ))
3445
3407
self ._keys [k ] = name
3446
3408
3447
- if self .toolbar and tool_cls .position is not None :
3409
+ if self .toolbar and tool_cls .intoolbar :
3448
3410
# TODO: better search for images, they are not always in the
3449
3411
# datapath
3450
3412
basedir = os .path .join (rcParams ['datapath' ], 'images' )
@@ -3453,10 +3415,11 @@ def add_tool(self, tool):
3453
3415
else :
3454
3416
fname = None
3455
3417
toggle = issubclass (tool_cls , tools .ToolToggleBase )
3456
- self .toolbar ._add_toolitem (name , tool_cls .description ,
3457
- fname ,
3458
- tool_cls .position ,
3459
- toggle )
3418
+ self .toolbar ._add_toolitem (name ,
3419
+ tool_cls .description ,
3420
+ fname ,
3421
+ position ,
3422
+ toggle )
3460
3423
3461
3424
def _get_cls_to_instantiate (self , callback_class ):
3462
3425
if isinstance (callback_class , six .string_types ):
@@ -3505,7 +3468,7 @@ def _key_press(self, event):
3505
3468
3506
3469
def _get_instance (self , name ):
3507
3470
if name not in self ._instances :
3508
- instance = self ._tools [name ](self .canvas .figure )
3471
+ instance = self ._tools [name ](self .canvas .figure , name )
3509
3472
# register instance
3510
3473
self ._instances [name ] = instance
3511
3474
@@ -3551,26 +3514,23 @@ def _handle_toggle(self, name, event=None, from_toolbar=False):
3551
3514
for a in self .canvas .figure .get_axes ():
3552
3515
a .set_navigate_mode (self ._toggled )
3553
3516
3554
- def list_tools (self ):
3555
- """Print the list the tools controlled by `Navigation`"""
3517
+ def get_tools (self ):
3518
+ """Return the tools controlled by `Navigation`"""
3556
3519
3557
- print ('_' * 80 )
3558
- print ("{0:20} {1:50} {2}" .format ('Name (id)' , 'Tool description' ,
3559
- 'Keymap' ))
3560
- print ('_' * 80 )
3520
+ d = {}
3561
3521
for name in sorted (self ._tools .keys ()):
3562
3522
tool = self ._tools [name ]
3563
3523
keys = [k for k , i in six .iteritems (self ._keys ) if i == name ]
3564
- print ("{0:20} {1:50} {2}" .format (tool .name , tool .description ,
3565
- ', ' .join (keys )))
3566
- print ('_' * 80 , '\n ' )
3524
+ d [name ] = {'cls' : tool ,
3525
+ 'description' : tool .description ,
3526
+ 'keymap' : keys }
3527
+ return d
3567
3528
3568
3529
def update (self ):
3569
3530
"""Reset the axes stack"""
3570
3531
3571
3532
self .views .clear ()
3572
3533
self .positions .clear ()
3573
- # self.set_history_buttons()
3574
3534
3575
3535
def _mouse_move (self , event ):
3576
3536
if not event .inaxes or not self ._toggled :
@@ -3667,7 +3627,6 @@ def push_current(self):
3667
3627
a .get_position ().frozen ()))
3668
3628
self .views .push (lims )
3669
3629
self .positions .push (pos )
3670
- # self.set_history_buttons()
3671
3630
3672
3631
def draw_rubberband (self , event , caller , x0 , y0 , x1 , y1 ):
3673
3632
"""Draw a rectangle rubberband to indicate zoom limits
@@ -3719,7 +3678,7 @@ def __init__(self, manager):
3719
3678
self .manager = manager
3720
3679
3721
3680
def _add_toolitem (self , name , description , image_file , position ,
3722
- toggle ):
3681
+ toggle ):
3723
3682
"""Add a toolitem to the toolbar
3724
3683
3725
3684
The callback associated with the button click event,
@@ -3776,7 +3735,8 @@ def _toggle(self, name, callback=False):
3776
3735
3777
3736
"""
3778
3737
3779
- # carefull, callback means to perform or not the callback while toggling
3738
+ # carefull, callback means to perform or not the callback while
3739
+ # toggling
3780
3740
raise NotImplementedError
3781
3741
3782
3742
def _remove_toolitem (self , name ):
0 commit comments