Skip to content

Commit

Permalink
Backend factorisation for tooltip sharing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Elson authored and pelson committed Jun 24, 2012
1 parent ed4d338 commit c887139
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 93 deletions.
20 changes: 20 additions & 0 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,26 @@ class NavigationToolbar2(object):
That's it, we'll do the rest!
"""

# list of toolitems to add to the toolbar, format is:
# (
# text, # the text of the button (often not visible to users)
# tooltip_text, # the tooltip shown on hover (where possible)
# image_file, # name of the image for the button (without the extension)
# name_of_method, # name of the method in NavigationToolbar2 to call
# )
toolitems = (
('Home', 'Reset original view', 'home', 'home'),
('Back', 'Back to previous view', 'back', 'back'),
('Forward', 'Forward to next view', 'forward', 'forward'),
(None, None, None, None),
('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'),
('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),
(None, None, None, None),
('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'),
('Save', 'Save the figure', 'filesave', 'save_figure'),
)


def __init__(self, canvas):
self.canvas = canvas
Expand Down
15 changes: 1 addition & 14 deletions lib/matplotlib/backends/backend_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,19 +624,6 @@ def resize(self, width, height):


class NavigationToolbar2GTK(NavigationToolbar2, gtk.Toolbar):
# list of toolitems to add to the toolbar, format is:
# text, tooltip_text, image_file, callback(str)
toolitems = (
('Home', 'Reset original view', 'home.png', 'home'),
('Back', 'Back to previous view','back.png', 'back'),
('Forward', 'Forward to next view','forward.png', 'forward'),
('Pan', 'Pan axes with left mouse, zoom with right', 'move.png','pan'),
('Zoom', 'Zoom to rectangle','zoom_to_rect.png', 'zoom'),
(None, None, None, None),
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
('Save', 'Save the figure','filesave.png', 'save_figure'),
)

def __init__(self, canvas, window):
self.win = window
gtk.Toolbar.__init__(self)
Expand Down Expand Up @@ -704,7 +691,7 @@ def _init_toolbar2_4(self):
if text is None:
self.insert( gtk.SeparatorToolItem(), -1 )
continue
fname = os.path.join(basedir, image_file)
fname = os.path.join(basedir, image_file + '.png')
image = gtk.Image()
image.set_from_file(fname)
tbutton = gtk.ToolButton(image, text)
Expand Down
15 changes: 1 addition & 14 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,19 +453,6 @@ def resize(self, width, height):


class NavigationToolbar2GTK3(NavigationToolbar2, Gtk.Toolbar):
# list of toolitems to add to the toolbar, format is:
# text, tooltip_text, image_file, callback(str)
toolitems = (
('Home', 'Reset original view', 'home.png', 'home'),
('Back', 'Back to previous view','back.png', 'back'),
('Forward', 'Forward to next view','forward.png', 'forward'),
('Pan', 'Pan axes with left mouse, zoom with right', 'move.png','pan'),
('Zoom', 'Zoom to rectangle','zoom_to_rect.png', 'zoom'),
(None, None, None, None),
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
('Save', 'Save the figure','filesave.png', 'save_figure'),
)

def __init__(self, canvas, window):
self.win = window
GObject.GObject.__init__(self)
Expand Down Expand Up @@ -516,7 +503,7 @@ def _init_toolbar(self):
if text is None:
self.insert( Gtk.SeparatorToolItem(), -1 )
continue
fname = os.path.join(basedir, image_file)
fname = os.path.join(basedir, image_file + '.png')
image = Gtk.Image()
image.set_from_file(fname)
tbutton = Gtk.ToolButton()
Expand Down
18 changes: 2 additions & 16 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,20 +295,6 @@ def set_window_title(self, title):
self.window.setCaption(title)

class NavigationToolbar2QT( NavigationToolbar2, qt.QWidget ):
# list of toolitems to add to the toolbar, format is:
# text, tooltip_text, image_file, callback(str)
toolitems = (
('Home', 'Reset original view', 'home.ppm', 'home'),
('Back', 'Back to previous view','back.ppm', 'back'),
('Forward', 'Forward to next view','forward.ppm', 'forward'),
(None, None, None, None),
('Pan', 'Pan axes with left mouse, zoom with right', 'move.ppm', 'pan'),
('Zoom', 'Zoom to rectangle','zoom_to_rect.ppm', 'zoom'),
(None, None, None, None),
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
('Save', 'Save the figure','filesave.ppm', 'save_figure'),
)

def __init__( self, canvas, parent ):
self.canvas = canvas
self.buttons = {}
Expand All @@ -329,8 +315,8 @@ def _init_toolbar( self ):
self.layout.addSpacing( 8 )
continue

fname = os.path.join( basedir, image_file )
image = qt.QPixmap()
fname = os.path.join(basedir, image_file + '.ppm')
image = qt.QPixmap()
image.load( fname )

button = qt.QPushButton( qt.QIconSet( image ), "", self )
Expand Down
25 changes: 9 additions & 16 deletions lib/matplotlib/backends/backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,15 @@ def _icon(self, name):
def _init_toolbar(self):
self.basedir = os.path.join(matplotlib.rcParams[ 'datapath' ],'images')

a = self.addAction(self._icon('home.png'), 'Home', self.home)
a.setToolTip('Reset original view')
a = self.addAction(self._icon('back.png'), 'Back', self.back)
a.setToolTip('Back to previous view')
a = self.addAction(self._icon('forward.png'), 'Forward', self.forward)
a.setToolTip('Forward to next view')
self.addSeparator()
a = self.addAction(self._icon('move.png'), 'Pan', self.pan)
a.setToolTip('Pan axes with left mouse, zoom with right')
a = self.addAction(self._icon('zoom_to_rect.png'), 'Zoom', self.zoom)
a.setToolTip('Zoom to rectangle')
self.addSeparator()
a = self.addAction(self._icon('subplots.png'), 'Subplots',
self.configure_subplots)
a.setToolTip('Configure subplots')

# XXX pelson: use NavigationToolbar2.toolitems
for text, tooltip_text, image_file, callback in self.toolitems:
if text is None:
self.addSeparator()
else:
a = self.addAction(self._icon(image_file + '.png'), text, getattr(self, callback))
if tooltip_text is not None:
a.setToolTip(tooltip_text)

if figureoptions is not None:
a = self.addAction(self._icon("qt4_editor_options.png"),
'Customize', self.edit_parameters)
Expand Down
107 changes: 74 additions & 33 deletions lib/matplotlib/backends/backend_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ def select_all(self):
a.set(1)
self.set_active()


class NavigationToolbar(Tk.Frame):
"""
Public attributes
Expand Down Expand Up @@ -626,39 +627,39 @@ def __init__(self, canvas, window):
self.update() # Make axes menu

self.bLeft = self._Button(
text="Left", file="stock_left.ppm",
text="Left", file="stock_left",
command=lambda x=-1: self.panx(x))

self.bRight = self._Button(
text="Right", file="stock_right.ppm",
text="Right", file="stock_right",
command=lambda x=1: self.panx(x))

self.bZoomInX = self._Button(
text="ZoomInX",file="stock_zoom-in.ppm",
text="ZoomInX",file="stock_zoom-in",
command=lambda x=1: self.zoomx(x))

self.bZoomOutX = self._Button(
text="ZoomOutX", file="stock_zoom-out.ppm",
text="ZoomOutX", file="stock_zoom-out",
command=lambda x=-1: self.zoomx(x))

self.bUp = self._Button(
text="Up", file="stock_up.ppm",
text="Up", file="stock_up",
command=lambda y=1: self.pany(y))

self.bDown = self._Button(
text="Down", file="stock_down.ppm",
text="Down", file="stock_down",
command=lambda y=-1: self.pany(y))

self.bZoomInY = self._Button(
text="ZoomInY", file="stock_zoom-in.ppm",
text="ZoomInY", file="stock_zoom-in",
command=lambda y=1: self.zoomy(y))

self.bZoomOutY = self._Button(
text="ZoomOutY",file="stock_zoom-out.ppm",
text="ZoomOutY",file="stock_zoom-out",
command=lambda y=-1: self.zoomy(y))

self.bSave = self._Button(
text="Save", file="stock_save_as.ppm",
text="Save", file="stock_save_as",
command=self.save_figure)

self.pack(side=Tk.BOTTOM, fill=Tk.X)
Expand Down Expand Up @@ -763,9 +764,9 @@ def release(self, event):
def set_cursor(self, cursor):
self.window.configure(cursor=cursord[cursor])

def _Button(self, text, file, command):
file = os.path.join(rcParams['datapath'], 'images', file)
im = Tk.PhotoImage(master=self, file=file)
def _Button(self, text, file, command, extension='.ppm'):
img_file = os.path.join(rcParams['datapath'], 'images', file + extension)
im = Tk.PhotoImage(master=self, file=img_file)
b = Tk.Button(
master=self, text=text, padx=2, pady=2, image=im, command=command)
b._ntimage = im
Expand All @@ -781,27 +782,16 @@ def _init_toolbar(self):

self.update() # Make axes menu

self.bHome = self._Button( text="Home", file="home.ppm",
command=self.home)

self.bBack = self._Button( text="Back", file="back.ppm",
command = self.back)

self.bForward = self._Button(text="Forward", file="forward.ppm",
command = self.forward)

self.bPan = self._Button( text="Pan", file="move.ppm",
command = self.pan)

self.bZoom = self._Button( text="Zoom",
file="zoom_to_rect.ppm",
command = self.zoom)

self.bsubplot = self._Button( text="Configure Subplots", file="subplots.ppm",
command = self.configure_subplots)

self.bsave = self._Button( text="Save", file="filesave.ppm",
command = self.save_figure)
for text, tooltip_text, image_file, callback in self.toolitems:
if text is None:
# spacer, unhandled in Tk
pass
else:
button = self._Button(text=text, file=image_file,
command=getattr(self, callback))
if tooltip_text is not None:
ToolTip.createToolTip(button, tooltip_text)

self.message = Tk.StringVar(master=self)
self._message_label = Tk.Label(master=self, textvariable=self.message)
self._message_label.pack(side=Tk.RIGHT)
Expand Down Expand Up @@ -879,3 +869,54 @@ def dynamic_update(self):


FigureManager = FigureManagerTkAgg


class ToolTip(object):
"""
Tooltip recipe from
http://www.voidspace.org.uk/python/weblog/arch_d7_2006_07_01.shtml#e387
"""
@staticmethod
def createToolTip(widget, text):
toolTip = ToolTip(widget)
def enter(event):
toolTip.showtip(text)
def leave(event):
toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)

def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0

def showtip(self, text):
"Display text in tooltip window"
self.text = text
if self.tipwindow or not self.text:
return
x, y, _, _ = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 27
y = y + self.widget.winfo_rooty()
self.tipwindow = tw = Tk.Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" % (x, y))
try:
# For Mac OS
tw.tk.call("::tk::unsupported::MacWindowStyle",
"style", tw._w,
"help", "noActivates")
except Tk.TclError:
pass
label = Tk.Label(tw, text=self.text, justify=Tk.LEFT,
background="#ffffe0", relief=Tk.SOLID, borderwidth=1,
)
label.pack(ipadx=1)

def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()

0 comments on commit c887139

Please sign in to comment.