Skip to content

Commit 9da2b13

Browse files
OceanWolffariza
authored andcommitted
Rename ToolbarBase -> ToolContainerBase
1 parent 9f2ee2b commit 9da2b13

File tree

3 files changed

+68
-73
lines changed

3 files changed

+68
-73
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
user interaction (key press, toolbar clicks, ..) and the actions in
3131
response to the user inputs.
3232
33-
:class:`ToolbarBase`
33+
:class:`ToolContainerBase`
3434
The base class for the Toolbar class of each interactive backend.
3535
3636
"""
@@ -3399,18 +3399,16 @@ def remove_tool(self, name):
33993399
del self._tools[name]
34003400

34013401
def add_tools(self, tools):
3402-
""" Add multiple tools to `Navigation`
3402+
""" Add multiple tools to `NavigationBase`
34033403
34043404
Parameters
34053405
----------
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.
34113409
"""
34123410

3413-
for tool, name in tools:
3411+
for name, tool in six.iteritems(tools):
34143412
self.add_tool(name, tool)
34153413

34163414
def add_tool(self, name, tool, *args, **kwargs):
@@ -3424,14 +3422,18 @@ def add_tool(self, name, tool, *args, **kwargs):
34243422
34253423
Parameters
34263424
----------
3427-
name : string
3425+
name : str
34283426
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.
34313429
34323430
Notes
34333431
-----
34343432
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.
34353437
"""
34363438

34373439
tool_cls = self._get_cls_to_instantiate(tool)
@@ -3442,7 +3444,7 @@ def add_tool(self, name, tool, *args, **kwargs):
34423444
if name in self._tools:
34433445
warnings.warn('A tool_cls with the same name already exist, '
34443446
'not added')
3445-
return
3447+
return self._tools[name]
34463448

34473449
self._tools[name] = tool_cls(self, name, *args, **kwargs)
34483450

@@ -3573,30 +3575,32 @@ def tools(self):
35733575

35743576
return self._tools
35753577

3576-
def get_tool(self, name):
3578+
def get_tool(self, name, warn=True):
35773579
"""Return the tool object, also accepts the actual tool for convenience
35783580
35793581
Parameters
35803582
-----------
3581-
name : String, ToolBase
3583+
name : str, ToolBase
35823584
Name of the tool, or the tool itself
3585+
warn : bool
3586+
If this method should give warnings.
35833587
"""
3584-
if isinstance(name, tools.ToolBase) and tool.name in self._tools:
3588+
if isinstance(name, tools.ToolBase) and name.name in self._tools:
35853589
return name
35863590
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)
35883593
return None
35893594
return self._tools[name]
35903595

35913596

3592-
class ToolbarBase(object):
3593-
"""Base class for `Toolbar` implementation
3597+
class ToolContainerBase(object):
3598+
"""Base class for all tool containers, e.g. toolbars.
35943599
35953600
Attributes
35963601
----------
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.
36003604
"""
36013605

36023606
def __init__(self, navigation):
@@ -3618,51 +3622,38 @@ def _tool_toggled_cbk(self, event):
36183622
self.toggle_toolitem(event.tool.name, event.tool.toggled)
36193623

36203624
def add_tools(self, tools):
3621-
""" Add multiple tools to `Navigation`
3625+
""" Add multiple tools to the container.
36223626
36233627
Parameters
36243628
----------
3625-
tools : List
3629+
tools : list
36263630
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.
36313634
"""
36323635

36333636
for group, grouptools in tools:
36343637
for position, tool in enumerate(grouptools):
36353638
self.add_tool(tool, group, position)
36363639

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
36393642
36403643
Parameters
36413644
----------
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
36453648
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)
36623653
image = self._get_image_filename(tool.image)
36633654
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)
36663657
if toggle:
36673658
self.navigation.nav_connect('tool_trigger_%s' % tool.name,
36683659
self._tool_toggled_cbk)
@@ -3688,13 +3679,13 @@ def trigger_tool(self, name):
36883679
Parameters
36893680
----------
36903681
name : String
3691-
Name(id) of the tool triggered from within the toolbar
3682+
Name(id) of the tool triggered from within the container
36923683
36933684
"""
36943685
self.navigation.tool_trigger_event(name, sender=self)
36953686

36963687
def add_toolitem(self, name, group, position, image, description, toggle):
3697-
"""Add a toolitem to the toolbar
3688+
"""Add a toolitem to the container
36983689
36993690
This method must get implemented per backend
37003691
@@ -3734,18 +3725,20 @@ def set_message(self, s):
37343725

37353726
pass
37363727

3737-
def toggle_toolitem(self, name):
3728+
def toggle_toolitem(self, name, toggled):
37383729
"""Toggle the toolitem without firing event
37393730
37403731
Parameters
37413732
----------
37423733
name : String
37433734
Id of the tool to toggle
3735+
toggled : bool
3736+
Whether to set this tool as toggled or not.
37443737
"""
37453738
raise NotImplementedError
37463739

37473740
def remove_toolitem(self, name):
3748-
"""Remove a toolitem from the `Toolbar`
3741+
"""Remove a toolitem from the `ToolContainer`
37493742
37503743
This method must get implemented per backend
37513744

lib/matplotlib/backend_tools.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -903,23 +903,25 @@ def _mouse_move(self, event):
903903
self.navigation.canvas.draw_idle()
904904

905905

906-
tools = [(ToolHome, 'home'), (ToolBack, 'back'), (ToolForward, 'forward'),
907-
(ToolZoom, 'zoom'), (ToolPan, 'pan'),
908-
('ToolConfigureSubplots', 'subplots'),
909-
('ToolSaveFigure', 'save'),
910-
(ToolGrid, 'grid'),
911-
(ToolFullScreen, 'fullscreen'),
912-
(ToolQuit, 'quit'),
913-
(ToolEnableAllNavigation, 'allnav'),
914-
(ToolEnableNavigation, 'nav'),
915-
(ToolXScale, 'xscale'),
916-
(ToolYScale, 'yscale'),
917-
(ToolCursorPosition, 'position'),
918-
(ToolViewsPositions, 'viewpos'),
919-
('ToolSetCursor', 'cursor'),
920-
('ToolRubberband', 'rubberband')]
906+
tools = {'home': ToolHome, 'back': ToolBack, 'forward': ToolForward,
907+
'zoom': ToolZoom, 'pan': ToolPan,
908+
'subplots': 'ToolConfigureSubplots',
909+
'save': 'ToolSaveFigure',
910+
'grid': ToolGrid,
911+
'fullscreen': ToolFullScreen,
912+
'quit': ToolQuit,
913+
'allnav': ToolEnableAllNavigation,
914+
'nav': ToolEnableNavigation,
915+
'xscale': ToolXScale,
916+
'yscale': ToolYScale,
917+
'position': ToolCursorPosition,
918+
'viewpos': ToolViewsPositions,
919+
'cursor': 'ToolSetCursor',
920+
'rubberband': 'ToolRubberband'}
921+
"""Default tools"""
922+
921923
toolbar_tools = [['navigation', ['home', 'back', 'forward']],
922924
['zoompan', ['zoom', 'pan']],
923925
['layout', ['subplots']],
924926
['io', ['save']]]
925-
"""Default tools"""
927+
"""Default tools in the toolbar"""

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3030
from matplotlib._pylab_helpers import Gcf
3131
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
3232
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors, TimerBase
33-
from matplotlib.backend_bases import ShowBase, ToolbarBase, NavigationBase
33+
from matplotlib.backend_bases import ShowBase, ToolContainerBase, NavigationBase
3434
from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase, \
3535
tools, toolbar_tools, SetCursorBase, RubberbandBase
3636

@@ -756,9 +756,9 @@ def draw_rubberband(self, x0, y0, x1, y1):
756756
ToolRubberband = RubberbandGTK3
757757

758758

759-
class ToolbarGTK3(ToolbarBase, Gtk.Box):
759+
class ToolbarGTK3(ToolContainerBase, Gtk.Box):
760760
def __init__(self, navigation):
761-
ToolbarBase.__init__(self, navigation)
761+
ToolContainerBase.__init__(self, navigation)
762762
Gtk.Box.__init__(self)
763763
self.set_property("orientation", Gtk.Orientation.VERTICAL)
764764

0 commit comments

Comments
 (0)