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

Skip to content

Commit e2804ea

Browse files
committed
tool group position
1 parent 110253f commit e2804ea

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

examples/user_interfaces/navigation.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
'''This example demonstrates how the `matplotlib.backend_bases.NavigationBase`
2+
class allows to:
3+
* Modify the Toolbar
4+
* Add tools
5+
* Remove tools
6+
'''
7+
8+
9+
from __future__ import print_function
110
import matplotlib
211
matplotlib.use('GTK3Cairo')
3-
# matplotlib.use('TkAGG')
412
matplotlib.rcParams['toolbar'] = 'navigation'
513
import matplotlib.pyplot as plt
614
from matplotlib.backend_tools import ToolBase
715

816

9-
# Create a simple tool to list all the tools
1017
class ListTools(ToolBase):
18+
'''List all the tools controlled by `Navigation`'''
1119
# keyboard shortcut
1220
keymap = 'm'
1321
description = 'List Tools'
@@ -34,9 +42,9 @@ def trigger(self, *args, **kwargs):
3442
print("{0:12} {1:45}").format(group, active)
3543

3644

37-
# A simple example of copy canvas
3845
# ref: at https://github.com/matplotlib/matplotlib/issues/1987
3946
class CopyToolGTK3(ToolBase):
47+
'''Copy canvas to clipboard'''
4048
keymap = 'ctrl+c'
4149
description = 'Copy canvas'
4250

@@ -54,10 +62,16 @@ def trigger(self, *args, **kwargs):
5462

5563
# Add the custom tools that we created
5664
fig.canvas.manager.navigation.add_tool('List', ListTools)
57-
if matplotlib.rcParams['backend'] == 'GTK3Cairo':
58-
fig.canvas.manager.navigation.add_tool('copy', CopyToolGTK3)
65+
fig.canvas.manager.navigation.add_tool('copy', CopyToolGTK3)
66+
67+
# Add an existing tool to new group `foo`.
68+
# It can be added as many times as we want
5969
fig.canvas.manager.toolbar.add_tool('zoom', 'foo')
60-
# Uncomment to remove the forward button
61-
# fig.canvas.manager.navigation.remove_tool('forward')
70+
71+
# Remove the forward button
72+
fig.canvas.manager.navigation.remove_tool('forward')
73+
74+
# To add a custom tool to the toolbar at specific location
75+
fig.canvas.manager.toolbar.add_tool('List', 'navigation', 1)
6276

6377
plt.show()

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -771,14 +771,16 @@ def __init__(self, navigation):
771771
Gtk.Box.__init__(self)
772772
self.set_property("orientation", Gtk.Orientation.VERTICAL)
773773

774-
self._toolbar = Gtk.Toolbar()
775-
self._toolbar.set_style(Gtk.ToolbarStyle.ICONS)
776-
self.pack_start(self._toolbar, False, False, 0)
777-
self._toolbar.show_all()
774+
self._toolarea = Gtk.Box()
775+
self._toolarea.set_property('orientation', Gtk.Orientation.HORIZONTAL)
776+
self.pack_start(self._toolarea, False, False, 0)
777+
self._toolarea.show_all()
778+
self._groups = {}
778779
self._toolitems = {}
779780

780781
def add_toolitem(self, name, group, position, image_file, description,
781782
toggle):
783+
782784
if toggle:
783785
tbutton = Gtk.ToggleToolButton()
784786
else:
@@ -792,14 +794,25 @@ def add_toolitem(self, name, group, position, image_file, description,
792794

793795
if position is None:
794796
position = -1
795-
# TODO implement groups positions
796-
self._toolbar.insert(tbutton, -1)
797+
798+
self._add_button(tbutton, group, position)
797799
signal = tbutton.connect('clicked', self._call_tool, name)
798800
tbutton.set_tooltip_text(description)
799801
tbutton.show_all()
800802
self._toolitems.setdefault(name, [])
801803
self._toolitems[name].append((tbutton, signal))
802804

805+
def _add_button(self, button, group, position):
806+
if group not in self._groups:
807+
if self._groups:
808+
self._add_separator()
809+
toolbar = Gtk.Toolbar()
810+
toolbar.set_style(Gtk.ToolbarStyle.ICONS)
811+
self._toolarea.pack_start(toolbar, False, False, 0)
812+
toolbar.show_all()
813+
self._groups[group] = toolbar
814+
self._groups[group].insert(button, position)
815+
803816
def _call_tool(self, btn, name):
804817
self.trigger_tool(name)
805818

@@ -815,15 +828,18 @@ def remove_toolitem(self, name):
815828
if name not in self._toolitems:
816829
self.navigation.message_event('%s Not in toolbar' % name, self)
817830
return
818-
for toolitem, _signal in self._toolitems[name]:
819-
self._toolbar.remove(toolitem)
831+
832+
for group in self._groups:
833+
for toolitem, _signal in self._toolitems[name]:
834+
if toolitem in self._groups[group]:
835+
self._groups[group].remove(toolitem)
820836
del self._toolitems[name]
821837

822-
def add_separator(self, pos=-1):
823-
toolitem = Gtk.SeparatorToolItem()
824-
self._toolbar.insert(toolitem, pos)
825-
toolitem.show()
826-
return toolitem
838+
def _add_separator(self):
839+
sep = Gtk.Separator()
840+
sep.set_property("orientation", Gtk.Orientation.VERTICAL)
841+
self._toolarea.pack_start(sep, False, True, 0)
842+
sep.show_all()
827843

828844

829845
class StatusbarGTK3(StatusbarBase, Gtk.Statusbar):

0 commit comments

Comments
 (0)