Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1213086

Browse files
OceanWolffariza
authored andcommitted
Refactored Toolbar out of NavigationBase
1 parent e415d8d commit 1213086

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,17 +3405,15 @@ def add_tools(self, tools):
34053405
----------
34063406
tools : List
34073407
List in the form
3408-
[[group1, [(Tool1, name1), (Tool2, name2) ...]][group2...]]
3409-
where group1 is the name of the group where the
3410-
Tool1, Tool2... are going to be added, and name1, name2... are the
3411-
names of the tools
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.
34123411
"""
34133412

3414-
for group, grouptools in tools:
3415-
for position, tool in enumerate(grouptools):
3416-
self.add_tool(tool[1], tool[0], group, position)
3413+
for tool, name in tools:
3414+
self.add_tool(name, tool)
34173415

3418-
def add_tool(self, name, tool, group=None, position=None):
3416+
def add_tool(self, name, tool):
34193417
"""Add tool to `NavigationBase`
34203418
34213419
Add a tool to the tools controlled by Navigation
@@ -3430,10 +3428,6 @@ def add_tool(self, name, tool, group=None, position=None):
34303428
Name of the tool, treated as the ID, has to be unique
34313429
tool : string or `matplotlib.backend_tools.ToolBase` derived class
34323430
Reference to find the class of the Tool to be added
3433-
group: String
3434-
Group to position the tool in
3435-
position : int or None (default)
3436-
Position within its group in the toolbar, if None, it goes at the end
34373431
"""
34383432

34393433
tool_cls = self._get_cls_to_instantiate(tool)
@@ -3459,14 +3453,11 @@ def add_tool(self, name, tool, group=None, position=None):
34593453
else:
34603454
self._toggled.setdefault(tool_cls.radio_group, None)
34613455

3462-
self._tool_added_event(self._tools[name], group, position)
3456+
self._tool_added_event(self._tools[name])
34633457

3464-
def _tool_added_event(self, tool, group, position):
3458+
def _tool_added_event(self, tool):
34653459
s = 'tool_added_event'
3466-
event = ToolEvent(s,
3467-
self,
3468-
tool,
3469-
data={'group': group, 'position': position})
3460+
event = ToolEvent(s, self, tool)
34703461
self._callbacks.process(s, event)
34713462

34723463
def _handle_toggle(self, tool, sender, canvasevent, data):
@@ -3606,7 +3597,6 @@ def __init__(self, navigation):
36063597
self.navigation = navigation
36073598

36083599
self.navigation.nav_connect('tool_message_event', self._message_cbk)
3609-
self.navigation.nav_connect('tool_added_event', self._add_tool_cbk)
36103600
self.navigation.nav_connect('tool_removed_event',
36113601
self._remove_tool_cbk)
36123602

@@ -3624,18 +3614,31 @@ def _tool_triggered_cbk(self, event):
36243614

36253615
self.toggle_toolitem(event.tool.name)
36263616

3627-
def _add_tool_cbk(self, event):
3628-
"""Captures 'tool_added_event' and adds the tool to the toolbar"""
3629-
image = self._get_image_filename(event.tool.image)
3630-
toggle = getattr(event.tool, 'toggled', None) is not None
3631-
self.add_toolitem(event.tool.name,
3632-
event.data['group'],
3633-
event.data['position'],
3634-
image,
3635-
event.tool.description,
3636-
toggle)
3617+
def add_tools(self, tools):
3618+
""" Add multiple tools to `Navigation`
3619+
3620+
Parameters
3621+
----------
3622+
tools : List
3623+
List in the form
3624+
[[group1, [name1, name2 ...]][group2...]]
3625+
where group1 is the name of the group where the
3626+
Tool1, Tool2... are going to be added, and name1, name2... are the
3627+
names of the tools
3628+
"""
3629+
3630+
for group, grouptools in tools:
3631+
for position, tool in enumerate(grouptools):
3632+
self.add_tool(self.navigation.get_tool(tool), group, position)
3633+
3634+
def add_tool(self, tool, group, position):
3635+
"""Adds a tool to the toolbar"""
3636+
image = self._get_image_filename(tool.image)
3637+
toggle = getattr(tool, 'toggled', None) is not None
3638+
self.add_toolitem(tool.name, group, position, image,
3639+
tool.description, toggle)
36373640
if toggle:
3638-
self.navigation.nav_connect('tool_trigger_%s' % event.tool.name,
3641+
self.navigation.nav_connect('tool_trigger_%s' % tool.name,
36393642
self._tool_triggered_cbk)
36403643

36413644
def _remove_tool_cbk(self, event):

lib/matplotlib/backend_tools.py

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

905905

906-
tools = [['navigation', [(ToolHome, 'home'),
907-
(ToolBack, 'back'),
908-
(ToolForward, 'forward')]],
909-
910-
['zoompan', [(ToolZoom, 'zoom'),
911-
(ToolPan, 'pan')]],
912-
913-
['layout', [('ToolConfigureSubplots', 'subplots'), ]],
914-
915-
['io', [('ToolSaveFigure', 'save'), ]],
916-
917-
[None, [(ToolGrid, 'grid'),
918-
(ToolFullScreen, 'fullscreen'),
919-
(ToolQuit, 'quit'),
920-
(ToolEnableAllNavigation, 'allnav'),
921-
(ToolEnableNavigation, 'nav'),
922-
(ToolXScale, 'xscale'),
923-
(ToolYScale, 'yscale'),
924-
(ToolCursorPosition, 'position'),
925-
(ToolViewsPositions, 'viewpos'),
926-
('ToolSetCursor', 'cursor'),
927-
('ToolRubberband', 'rubberband')]]]
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')]
921+
toolbar_tools = [['navigation', ['home', 'back', 'forward']],
922+
['zoompan', ['zoom', 'pan']],
923+
['layout', ['subplots']],
924+
['io', ['save']]]
928925
"""Default tools"""

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3232
FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors, TimerBase
3333
from matplotlib.backend_bases import ShowBase, ToolbarBase, NavigationBase
3434
from matplotlib.backend_tools import SaveFigureBase, ConfigureSubplotsBase, \
35-
tools, SetCursorBase, RubberbandBase
35+
tools, toolbar_tools, SetCursorBase, RubberbandBase
3636

3737
from matplotlib.cbook import is_string_like, is_writable_file_like
3838
from matplotlib.colors import colorConverter
@@ -421,6 +421,7 @@ def __init__(self, canvas, num):
421421
self.toolbar = self._get_toolbar()
422422
if matplotlib.rcParams['toolbar'] == 'navigation':
423423
self.navigation.add_tools(tools)
424+
self.toolbar.add_tools(toolbar_tools)
424425

425426
# calculate size for window
426427
w = int (self.canvas.figure.bbox.width)

0 commit comments

Comments
 (0)