30
30
user interaction (key press, toolbar clicks, ..) and the actions in
31
31
response to the user inputs.
32
32
33
- :class:`ToolbarBase `
33
+ :class:`ToolContainerBase `
34
34
The base class for the Toolbar class of each interactive backend.
35
35
36
36
"""
@@ -3399,18 +3399,16 @@ def remove_tool(self, name):
3399
3399
del self ._tools [name ]
3400
3400
3401
3401
def add_tools (self , tools ):
3402
- """ Add multiple tools to `Navigation `
3402
+ """ Add multiple tools to `NavigationBase `
3403
3403
3404
3404
Parameters
3405
3405
----------
3406
- tools : List
3407
- List in the form
3408
- [(Tool1, name1), (Tool2, name2) ...]
3409
- where Tool1, name1 represent the tool, and the respective name
3410
- of the tool which gets used as an id.
3406
+ tools : {str: class_like}
3407
+ The tools to add in a {name: tool} dict, see `add_tool` for more
3408
+ info.
3411
3409
"""
3412
3410
3413
- for tool , name in tools :
3411
+ for name , tool in six . iteritems ( tools ) :
3414
3412
self .add_tool (name , tool )
3415
3413
3416
3414
def add_tool (self , name , tool , * args , ** kwargs ):
@@ -3424,14 +3422,18 @@ def add_tool(self, name, tool, *args, **kwargs):
3424
3422
3425
3423
Parameters
3426
3424
----------
3427
- name : string
3425
+ name : str
3428
3426
Name of the tool, treated as the ID, has to be unique
3429
- tool : string or `matplotlib.backend_tools.ToolBase` derived class
3430
- Reference to find the class of the Tool to be added
3427
+ tool : class_like, i.e. str or type
3428
+ Reference to find the class of the Tool to added.
3431
3429
3432
3430
Notes
3433
3431
-----
3434
3432
args and kwargs get passed directly to the tools constructor.
3433
+
3434
+ See Also
3435
+ --------
3436
+ matplotlib.backend_tools.ToolBase : The base class for tools.
3435
3437
"""
3436
3438
3437
3439
tool_cls = self ._get_cls_to_instantiate (tool )
@@ -3442,7 +3444,7 @@ def add_tool(self, name, tool, *args, **kwargs):
3442
3444
if name in self ._tools :
3443
3445
warnings .warn ('A tool_cls with the same name already exist, '
3444
3446
'not added' )
3445
- return
3447
+ return self . _tools [ name ]
3446
3448
3447
3449
self ._tools [name ] = tool_cls (self , name , * args , ** kwargs )
3448
3450
@@ -3573,30 +3575,32 @@ def tools(self):
3573
3575
3574
3576
return self ._tools
3575
3577
3576
- def get_tool (self , name ):
3578
+ def get_tool (self , name , warn = True ):
3577
3579
"""Return the tool object, also accepts the actual tool for convenience
3578
3580
3579
3581
Parameters
3580
3582
-----------
3581
- name : String , ToolBase
3583
+ name : str , ToolBase
3582
3584
Name of the tool, or the tool itself
3585
+ warn : bool
3586
+ If this method should give warnings.
3583
3587
"""
3584
- if isinstance (name , tools .ToolBase ) and tool .name in self ._tools :
3588
+ if isinstance (name , tools .ToolBase ) and name .name in self ._tools :
3585
3589
return name
3586
3590
if name not in self ._tools :
3587
- warnings .warn ("%s is not a tool controlled by Navigation" % name )
3591
+ if warn :
3592
+ warnings .warn ("Navigation does not control tool %s" % name )
3588
3593
return None
3589
3594
return self ._tools [name ]
3590
3595
3591
3596
3592
- class ToolbarBase (object ):
3593
- """Base class for `Toolbar` implementation
3597
+ class ToolContainerBase (object ):
3598
+ """Base class for all tool containers, e.g. toolbars.
3594
3599
3595
3600
Attributes
3596
3601
----------
3597
- manager : `FigureManager` object that integrates this `Toolbar`
3598
- navigation : `NavigationBase` object that hold the tools that
3599
- this `Toolbar` wants to communicate with
3602
+ navigation : `NavigationBase` object that holds the tools that
3603
+ this `ToolContainer` wants to communicate with.
3600
3604
"""
3601
3605
3602
3606
def __init__ (self , navigation ):
@@ -3618,51 +3622,38 @@ def _tool_toggled_cbk(self, event):
3618
3622
self .toggle_toolitem (event .tool .name , event .tool .toggled )
3619
3623
3620
3624
def add_tools (self , tools ):
3621
- """ Add multiple tools to `Navigation`
3625
+ """ Add multiple tools to the container.
3622
3626
3623
3627
Parameters
3624
3628
----------
3625
- tools : List
3629
+ tools : list
3626
3630
List in the form
3627
- [[group1, [name1, name2 ...]][group2...]]
3628
- where group1 is the name of the group where the
3629
- Tool1, Tool2... are going to be added, and name1, name2... are the
3630
- names of the tools
3631
+ [[group1, [tool1, tool2 ...]], [group2, [...]]]
3632
+ Where the tools given by tool1, and tool2 will display in group1.
3633
+ See `add_tool` for details.
3631
3634
"""
3632
3635
3633
3636
for group , grouptools in tools :
3634
3637
for position , tool in enumerate (grouptools ):
3635
3638
self .add_tool (tool , group , position )
3636
3639
3637
- def add_tool (self , tool , group , position = - 1 , name = None , ** kwargs ):
3638
- """Adds a tool to the toolbar
3640
+ def add_tool (self , tool , group , position = - 1 ):
3641
+ """Adds a tool to this container
3639
3642
3640
3643
Parameters
3641
3644
----------
3642
- tool : string, tool
3643
- The name or the type of tool to add.
3644
- group : string
3645
+ tool : tool_like
3646
+ The tool to add, see `NavigationBase.get_tool` .
3647
+ group : str
3645
3648
The name of the group to add this tool to.
3646
- position : int
3647
- the relative position within the group to place this tool.
3648
- name : string (optional)
3649
- If given, and the above fails, we use this to create a new tool of
3650
- type given by tool, and use this as the name of the tool.
3651
- """
3652
- t = self .navigation .get_tool (tool )
3653
- if t is None :
3654
- if isinstance (tool , type ):
3655
- tool = tool .__class__
3656
- if name is not None :
3657
- t = self .navigation .add_tool (name , tool , ** kwargs )
3658
- if t is None :
3659
- warning .warn ('Cannot add tool %s' % tool )
3660
- return
3661
- tool = t
3649
+ position : int (optional)
3650
+ The position within the group to place this tool. Defaults to end.
3651
+ """
3652
+ tool = self .navigation .get_tool (tool )
3662
3653
image = self ._get_image_filename (tool .image )
3663
3654
toggle = getattr (tool , 'toggled' , None ) is not None
3664
- self .add_toolitem (tool .name , group , position , image ,
3665
- tool .description , toggle )
3655
+ self .add_toolitem (tool .name , group , position ,
3656
+ image , tool .description , toggle )
3666
3657
if toggle :
3667
3658
self .navigation .nav_connect ('tool_trigger_%s' % tool .name ,
3668
3659
self ._tool_toggled_cbk )
@@ -3688,13 +3679,13 @@ def trigger_tool(self, name):
3688
3679
Parameters
3689
3680
----------
3690
3681
name : String
3691
- Name(id) of the tool triggered from within the toolbar
3682
+ Name(id) of the tool triggered from within the container
3692
3683
3693
3684
"""
3694
3685
self .navigation .tool_trigger_event (name , sender = self )
3695
3686
3696
3687
def add_toolitem (self , name , group , position , image , description , toggle ):
3697
- """Add a toolitem to the toolbar
3688
+ """Add a toolitem to the container
3698
3689
3699
3690
This method must get implemented per backend
3700
3691
@@ -3734,18 +3725,20 @@ def set_message(self, s):
3734
3725
3735
3726
pass
3736
3727
3737
- def toggle_toolitem (self , name ):
3728
+ def toggle_toolitem (self , name , toggled ):
3738
3729
"""Toggle the toolitem without firing event
3739
3730
3740
3731
Parameters
3741
3732
----------
3742
3733
name : String
3743
3734
Id of the tool to toggle
3735
+ toggled : bool
3736
+ Whether to set this tool as toggled or not.
3744
3737
"""
3745
3738
raise NotImplementedError
3746
3739
3747
3740
def remove_toolitem (self , name ):
3748
- """Remove a toolitem from the `Toolbar `
3741
+ """Remove a toolitem from the `ToolContainer `
3749
3742
3750
3743
This method must get implemented per backend
3751
3744
0 commit comments