@@ -3234,9 +3234,9 @@ def __init__(self, name, sender):
3234
3234
3235
3235
class ToolTriggerEvent (ToolEvent ):
3236
3236
"""Event to inform that a tool has been triggered"""
3237
- def __init__ (self , name , toolname , sender , canvasevent = None , data = None ):
3237
+ def __init__ (self , name , tool , sender , canvasevent = None , data = None ):
3238
3238
ToolEvent .__init__ (self , name , sender )
3239
- self .toolname = toolname
3239
+ self .tool = tool
3240
3240
self .canvasevent = canvasevent
3241
3241
self .data = data
3242
3242
@@ -3254,10 +3254,10 @@ class NavigationBase(object):
3254
3254
3255
3255
Attributes
3256
3256
----------
3257
- manager : `FigureManager` instance
3258
- keypresslock : `LockDraw` to know if the `canvas` key_press_event is
3259
- locked
3260
- messagelock : `LockDraw` to know if the message is available to write
3257
+ manager: `FigureManager` instance
3258
+ keypresslock: `LockDraw` to know if the `canvas` key_press_event is
3259
+ locked
3260
+ messagelock: `LockDraw` to know if the message is available to write
3261
3261
"""
3262
3262
3263
3263
def __init__ (self , manager ):
@@ -3270,7 +3270,7 @@ def __init__(self, manager):
3270
3270
self ._tools = {}
3271
3271
self ._keys = {}
3272
3272
self ._toggled = None
3273
- self .callbacks = cbook .CallbackRegistry ()
3273
+ self ._callbacks = cbook .CallbackRegistry ()
3274
3274
3275
3275
# to process keypress event
3276
3276
self .keypresslock = widgets .LockDraw ()
@@ -3279,22 +3279,24 @@ def __init__(self, manager):
3279
3279
def mpl_connect (self , s , func ):
3280
3280
"""Connect event with string *s* to *func*.
3281
3281
3282
- Parameters:
3282
+ Parameters
3283
3283
-----------
3284
- s: String
3284
+ s : String
3285
3285
Name of the event
3286
+
3286
3287
The following events are recognized
3287
- - 'tool_message_even'
3288
+
3289
+ - 'tool_message_event'
3288
3290
- 'tool_removed_event'
3289
3291
- 'tool_added_event'
3290
3292
For every tool added a new event is created
3291
3293
- 'tool_trigger_TOOLNAME
3292
3294
Where TOOLNAME is the id of the tool.
3293
- func: function
3295
+ func : function
3294
3296
Function to be called with signature
3295
3297
def func(event)
3296
3298
"""
3297
- return self .callbacks .connect (s , func )
3299
+ return self ._callbacks .connect (s , func )
3298
3300
3299
3301
def mpl_disconnect (self , cid ):
3300
3302
"""Disconnect callback id cid
@@ -3305,16 +3307,16 @@ def mpl_disconnect(self, cid):
3305
3307
#...later
3306
3308
navigation.mpl_disconnect(cid)
3307
3309
"""
3308
- return self .callbacks .disconnect (cid )
3310
+ return self ._callbacks .disconnect (cid )
3309
3311
3310
3312
def message_event (self , message , sender = None ):
3311
- """ Send a tool_message_event event"""
3313
+ """ Emit a tool_message_event event"""
3312
3314
if sender is None :
3313
3315
sender = self
3314
3316
3315
3317
s = 'tool_message_event'
3316
3318
event = NavigationEvent (s , sender , message = message )
3317
- self .callbacks .process (s , event )
3319
+ self ._callbacks .process (s , event )
3318
3320
3319
3321
@property
3320
3322
def active_toggle (self ):
@@ -3369,7 +3371,7 @@ def set_tool_keymap(self, name, *keys):
3369
3371
self ._keys [k ] = name
3370
3372
3371
3373
def remove_tool (self , name ):
3372
- """Remove tool from the `Navigation`
3374
+ """Remove tool from `Navigation`
3373
3375
3374
3376
Parameters
3375
3377
----------
@@ -3387,7 +3389,7 @@ def remove_tool(self, name):
3387
3389
3388
3390
s = 'tool_removed_event'
3389
3391
event = NavigationEvent (s , self , tool = tool )
3390
- self .callbacks .process (s , event )
3392
+ self ._callbacks .process (s , event )
3391
3393
3392
3394
del self ._tools [name ]
3393
3395
@@ -3396,33 +3398,37 @@ def add_tools(self, tools):
3396
3398
3397
3399
Parameters
3398
3400
----------
3399
- tools : a list of tuples which contains the id of the tool and
3400
- a either a reference to the tool Tool class itself, or None to
3401
- insert a spacer. See :func:`add_tool`.
3401
+ tools : List
3402
+ List in the form
3403
+ [[group1, [(Tool1, name1), (Tool2, name2) ...]][group2...]]
3404
+ where group1 is the name of the group where the
3405
+ Tool1, Tool2... are going to be added, and name1, name2... are the
3406
+ names of the tools
3402
3407
"""
3403
3408
3404
3409
for group , grouptools in tools :
3405
3410
for position , tool in enumerate (grouptools ):
3406
3411
self .add_tool (tool [1 ], tool [0 ], group , position )
3407
3412
3408
3413
def add_tool (self , name , tool , group = None , position = None ):
3409
- """Add tool to `Navigation `
3414
+ """Add tool to `NavigationBase `
3410
3415
3411
3416
Add a tool to the tools controlled by Navigation
3412
- If successful adds a new event `tool_trigger_name` where name is
3413
- the name of the tool, this event is fired everytime
3417
+
3418
+ If successful adds a new event `tool_trigger_name` where **name** is
3419
+ the **name** of the tool, this event is fired everytime
3414
3420
the tool is triggered.
3415
3421
3416
3422
Parameters
3417
3423
----------
3418
3424
name : string
3419
3425
Name of the tool, treated as the ID, has to be unique
3420
- tool : string or `Tool` class
3426
+ tool : string or `matplotlib.backend_tools.ToolBase` derived class
3421
3427
Reference to find the class of the Tool to be added
3422
3428
group: String
3423
3429
Group to position the tool in
3424
3430
position : int or None (default)
3425
- Position in the toolbar, if None, is positioned at the end
3431
+ Position within its group in the toolbar, if None, is positioned at the end
3426
3432
"""
3427
3433
3428
3434
tool_cls = self ._get_cls_to_instantiate (tool )
@@ -3447,7 +3453,7 @@ def _tool_added_event(self, tool, group, position):
3447
3453
tool = tool ,
3448
3454
group = group ,
3449
3455
position = position )
3450
- self .callbacks .process (s , event )
3456
+ self ._callbacks .process (s , event )
3451
3457
3452
3458
def _handle_toggle (self , name , sender , canvasevent , data ):
3453
3459
# Toggle tools, need to be untoggled before other Toggle tool is used
@@ -3466,8 +3472,6 @@ def _handle_toggle(self, name, sender, canvasevent, data):
3466
3472
for a in self .canvas .figure .get_axes ():
3467
3473
a .set_navigate_mode (self ._toggled )
3468
3474
3469
- self ._set_cursor (canvasevent )
3470
-
3471
3475
def _get_cls_to_instantiate (self , callback_class ):
3472
3476
# Find the class that corresponds to the tool
3473
3477
if isinstance (callback_class , six .string_types ):
@@ -3485,17 +3489,17 @@ def _get_cls_to_instantiate(self, callback_class):
3485
3489
3486
3490
def tool_trigger_event (self , name , sender = None , canvasevent = None ,
3487
3491
data = None ):
3488
- """Trigger a tool and fire the tool-trigger-[name] event
3492
+ """Trigger a tool and emit the tool-trigger-[name] event
3489
3493
3490
3494
Parameters
3491
3495
----------
3492
3496
name : string
3493
3497
Name of the tool
3494
3498
sender: object
3495
3499
Object that wish to trigger the tool
3496
- canvasevent: Event
3500
+ canvasevent : Event
3497
3501
Original Canvas event or None
3498
- data: Object
3502
+ data : Object
3499
3503
Extra data to pass to the tool when triggering
3500
3504
"""
3501
3505
if name not in self ._tools :
@@ -3508,8 +3512,8 @@ def tool_trigger_event(self, name, sender=None, canvasevent=None,
3508
3512
self ._trigger_tool (name , sender , canvasevent , data )
3509
3513
3510
3514
s = 'tool-trigger-%s' % name
3511
- event = ToolTriggerEvent (s , name , sender , canvasevent , data )
3512
- self .callbacks .process (s , event )
3515
+ event = ToolTriggerEvent (s , self . _tools [ name ] , sender , canvasevent , data )
3516
+ self ._callbacks .process (s , event )
3513
3517
3514
3518
def _trigger_tool (self , name , sender = None , canvasevent = None , data = None ):
3515
3519
"""Trigger on a tool
@@ -3534,45 +3538,31 @@ def _key_press(self, event):
3534
3538
return
3535
3539
self .tool_trigger_event (name , canvasevent = event )
3536
3540
3537
- def get_tools (self ):
3541
+ @property
3542
+ def tools (self ):
3538
3543
"""Return the tools controlled by `Navigation`"""
3539
3544
3540
- d = {}
3541
- for name in sorted (self ._tools .keys ()):
3542
- tool = self ._tools [name ]
3543
- keys = [k for k , i in six .iteritems (self ._keys ) if i == name ]
3544
- d [name ] = {'obj' : tool ,
3545
- 'description' : tool .description ,
3546
- 'keymap' : keys }
3547
- return d
3545
+ return self ._tools
3548
3546
3549
3547
def get_tool (self , name ):
3550
3548
"""Return the tool object
3551
3549
3552
- Parameters:
3550
+ Parameters
3553
3551
-----------
3554
- name: String
3552
+ name : String
3555
3553
Name of the tool
3556
3554
"""
3557
3555
return self ._tools [name ]
3558
3556
3559
- def _set_cursor (self , canvasevent ):
3560
- """Sets the current cursor in ToolSetCursor"""
3561
-
3562
- if self ._toggled :
3563
- cursor = self ._tools [self ._toggled ].cursor
3564
- else :
3565
- cursor = None
3566
-
3567
- self .tool_trigger_event ('cursor' , self , canvasevent , data = cursor )
3568
-
3569
3557
3570
3558
class ToolbarBase (object ):
3571
3559
"""Base class for `Toolbar` implementation
3572
3560
3573
3561
Attributes
3574
3562
----------
3575
- manager : `FigureManager` instance that integrates this `Toolbar`
3563
+ manager : `FigureManager` object that integrates this `Toolbar`
3564
+ navigation : `NavigationBase` object that hold the tools that
3565
+ this `Toolbar` wants to communicate with
3576
3566
"""
3577
3567
3578
3568
def __init__ (self , manager ):
@@ -3596,12 +3586,12 @@ def _tool_triggered_cbk(self, event):
3596
3586
if event .sender is self :
3597
3587
return
3598
3588
3599
- self .toggle_toolitem (event .toolname )
3589
+ self .toggle_toolitem (event .tool . name )
3600
3590
3601
3591
def _add_tool_cbk (self , event ):
3602
3592
"""Captures 'tool_added_event' and add the tool to the toolbar"""
3603
3593
image = self ._get_image_filename (event .tool .image )
3604
- toggle = isinstance (event .tool , tools . ToolToggleBase )
3594
+ toggle = getattr (event .tool , 'toggled' , None ) is not None
3605
3595
self .add_toolitem (event .tool .name ,
3606
3596
event .group ,
3607
3597
event .position ,
@@ -3641,6 +3631,8 @@ def trigger_tool(self, name):
3641
3631
def add_toolitem (self , name , group , position , image , description , toggle ):
3642
3632
"""Add a toolitem to the toolbar
3643
3633
3634
+ This method has to be implemented per backend
3635
+
3644
3636
The callback associated with the button click event,
3645
3637
must be **EXACTLY** `self.trigger_tool(name)`
3646
3638
@@ -3653,31 +3645,47 @@ def add_toolitem(self, name, group, position, image, description, toggle):
3653
3645
Name of the group that the tool belongs to
3654
3646
position : Int
3655
3647
Position of the tool whthin its group if -1 at the End
3656
- image_file : string
3648
+ image_file : String
3657
3649
Filename of the image for the button or `None`
3658
- description : string
3650
+ description : String
3659
3651
Description of the tool, used for the tooltips
3660
- toggle : bool
3652
+ toggle : Bool
3661
3653
* `True` : The button is a toggle (change the pressed/unpressed
3662
- state between consecutive clicks)
3654
+ state between consecutive clicks)
3663
3655
* `False` : The button is a normal button (returns to unpressed
3664
- state after release)
3656
+ state after release)
3665
3657
"""
3666
3658
3667
3659
raise NotImplementedError
3668
3660
3669
3661
def set_message (self , s ):
3670
- """Display a message on toolbar or in status bar"""
3662
+ """Display a message on toolbar or in status bar
3663
+
3664
+ Parameters
3665
+ ----------
3666
+ s : String
3667
+ Message text
3668
+ """
3671
3669
3672
3670
pass
3673
3671
3674
3672
def toggle_toolitem (self , name ):
3675
- """Toggle the toolitem without firing event"""
3673
+ """Toggle the toolitem without firing event
3674
+
3675
+ Parameters
3676
+ ----------
3677
+ name : String
3678
+ Id of the tool to toggle
3679
+ """
3676
3680
raise NotImplementedError
3677
3681
3678
3682
def remove_toolitem (self , name ):
3679
3683
"""Remove a toolitem from the `Toolbar`
3680
3684
3685
+ This method has to be implemented per backend
3686
+
3687
+ Called when `tool_removed_event` is emited by `NavigationBase`
3688
+
3681
3689
Parameters
3682
3690
----------
3683
3691
name : string
0 commit comments