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

Skip to content

Commit c887139

Browse files
Phil Elsonpelson
Phil Elson
authored andcommitted
Backend factorisation for tooltip sharing.
1 parent ed4d338 commit c887139

File tree

6 files changed

+107
-93
lines changed

6 files changed

+107
-93
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,6 +2467,26 @@ class NavigationToolbar2(object):
24672467
24682468
That's it, we'll do the rest!
24692469
"""
2470+
2471+
# list of toolitems to add to the toolbar, format is:
2472+
# (
2473+
# text, # the text of the button (often not visible to users)
2474+
# tooltip_text, # the tooltip shown on hover (where possible)
2475+
# image_file, # name of the image for the button (without the extension)
2476+
# name_of_method, # name of the method in NavigationToolbar2 to call
2477+
# )
2478+
toolitems = (
2479+
('Home', 'Reset original view', 'home', 'home'),
2480+
('Back', 'Back to previous view', 'back', 'back'),
2481+
('Forward', 'Forward to next view', 'forward', 'forward'),
2482+
(None, None, None, None),
2483+
('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'),
2484+
('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),
2485+
(None, None, None, None),
2486+
('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'),
2487+
('Save', 'Save the figure', 'filesave', 'save_figure'),
2488+
)
2489+
24702490

24712491
def __init__(self, canvas):
24722492
self.canvas = canvas

lib/matplotlib/backends/backend_gtk.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -624,19 +624,6 @@ def resize(self, width, height):
624624

625625

626626
class NavigationToolbar2GTK(NavigationToolbar2, gtk.Toolbar):
627-
# list of toolitems to add to the toolbar, format is:
628-
# text, tooltip_text, image_file, callback(str)
629-
toolitems = (
630-
('Home', 'Reset original view', 'home.png', 'home'),
631-
('Back', 'Back to previous view','back.png', 'back'),
632-
('Forward', 'Forward to next view','forward.png', 'forward'),
633-
('Pan', 'Pan axes with left mouse, zoom with right', 'move.png','pan'),
634-
('Zoom', 'Zoom to rectangle','zoom_to_rect.png', 'zoom'),
635-
(None, None, None, None),
636-
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
637-
('Save', 'Save the figure','filesave.png', 'save_figure'),
638-
)
639-
640627
def __init__(self, canvas, window):
641628
self.win = window
642629
gtk.Toolbar.__init__(self)
@@ -704,7 +691,7 @@ def _init_toolbar2_4(self):
704691
if text is None:
705692
self.insert( gtk.SeparatorToolItem(), -1 )
706693
continue
707-
fname = os.path.join(basedir, image_file)
694+
fname = os.path.join(basedir, image_file + '.png')
708695
image = gtk.Image()
709696
image.set_from_file(fname)
710697
tbutton = gtk.ToolButton(image, text)

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,19 +453,6 @@ def resize(self, width, height):
453453

454454

455455
class NavigationToolbar2GTK3(NavigationToolbar2, Gtk.Toolbar):
456-
# list of toolitems to add to the toolbar, format is:
457-
# text, tooltip_text, image_file, callback(str)
458-
toolitems = (
459-
('Home', 'Reset original view', 'home.png', 'home'),
460-
('Back', 'Back to previous view','back.png', 'back'),
461-
('Forward', 'Forward to next view','forward.png', 'forward'),
462-
('Pan', 'Pan axes with left mouse, zoom with right', 'move.png','pan'),
463-
('Zoom', 'Zoom to rectangle','zoom_to_rect.png', 'zoom'),
464-
(None, None, None, None),
465-
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
466-
('Save', 'Save the figure','filesave.png', 'save_figure'),
467-
)
468-
469456
def __init__(self, canvas, window):
470457
self.win = window
471458
GObject.GObject.__init__(self)
@@ -516,7 +503,7 @@ def _init_toolbar(self):
516503
if text is None:
517504
self.insert( Gtk.SeparatorToolItem(), -1 )
518505
continue
519-
fname = os.path.join(basedir, image_file)
506+
fname = os.path.join(basedir, image_file + '.png')
520507
image = Gtk.Image()
521508
image.set_from_file(fname)
522509
tbutton = Gtk.ToolButton()

lib/matplotlib/backends/backend_qt.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,6 @@ def set_window_title(self, title):
295295
self.window.setCaption(title)
296296

297297
class NavigationToolbar2QT( NavigationToolbar2, qt.QWidget ):
298-
# list of toolitems to add to the toolbar, format is:
299-
# text, tooltip_text, image_file, callback(str)
300-
toolitems = (
301-
('Home', 'Reset original view', 'home.ppm', 'home'),
302-
('Back', 'Back to previous view','back.ppm', 'back'),
303-
('Forward', 'Forward to next view','forward.ppm', 'forward'),
304-
(None, None, None, None),
305-
('Pan', 'Pan axes with left mouse, zoom with right', 'move.ppm', 'pan'),
306-
('Zoom', 'Zoom to rectangle','zoom_to_rect.ppm', 'zoom'),
307-
(None, None, None, None),
308-
('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
309-
('Save', 'Save the figure','filesave.ppm', 'save_figure'),
310-
)
311-
312298
def __init__( self, canvas, parent ):
313299
self.canvas = canvas
314300
self.buttons = {}
@@ -329,8 +315,8 @@ def _init_toolbar( self ):
329315
self.layout.addSpacing( 8 )
330316
continue
331317

332-
fname = os.path.join( basedir, image_file )
333-
image = qt.QPixmap()
318+
fname = os.path.join(basedir, image_file + '.ppm')
319+
image = qt.QPixmap()
334320
image.load( fname )
335321

336322
button = qt.QPushButton( qt.QIconSet( image ), "", self )

lib/matplotlib/backends/backend_qt4.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -441,22 +441,15 @@ def _icon(self, name):
441441
def _init_toolbar(self):
442442
self.basedir = os.path.join(matplotlib.rcParams[ 'datapath' ],'images')
443443

444-
a = self.addAction(self._icon('home.png'), 'Home', self.home)
445-
a.setToolTip('Reset original view')
446-
a = self.addAction(self._icon('back.png'), 'Back', self.back)
447-
a.setToolTip('Back to previous view')
448-
a = self.addAction(self._icon('forward.png'), 'Forward', self.forward)
449-
a.setToolTip('Forward to next view')
450-
self.addSeparator()
451-
a = self.addAction(self._icon('move.png'), 'Pan', self.pan)
452-
a.setToolTip('Pan axes with left mouse, zoom with right')
453-
a = self.addAction(self._icon('zoom_to_rect.png'), 'Zoom', self.zoom)
454-
a.setToolTip('Zoom to rectangle')
455-
self.addSeparator()
456-
a = self.addAction(self._icon('subplots.png'), 'Subplots',
457-
self.configure_subplots)
458-
a.setToolTip('Configure subplots')
459-
444+
# XXX pelson: use NavigationToolbar2.toolitems
445+
for text, tooltip_text, image_file, callback in self.toolitems:
446+
if text is None:
447+
self.addSeparator()
448+
else:
449+
a = self.addAction(self._icon(image_file + '.png'), text, getattr(self, callback))
450+
if tooltip_text is not None:
451+
a.setToolTip(tooltip_text)
452+
460453
if figureoptions is not None:
461454
a = self.addAction(self._icon("qt4_editor_options.png"),
462455
'Customize', self.edit_parameters)

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ def select_all(self):
596596
a.set(1)
597597
self.set_active()
598598

599+
599600
class NavigationToolbar(Tk.Frame):
600601
"""
601602
Public attributes
@@ -626,39 +627,39 @@ def __init__(self, canvas, window):
626627
self.update() # Make axes menu
627628

628629
self.bLeft = self._Button(
629-
text="Left", file="stock_left.ppm",
630+
text="Left", file="stock_left",
630631
command=lambda x=-1: self.panx(x))
631632

632633
self.bRight = self._Button(
633-
text="Right", file="stock_right.ppm",
634+
text="Right", file="stock_right",
634635
command=lambda x=1: self.panx(x))
635636

636637
self.bZoomInX = self._Button(
637-
text="ZoomInX",file="stock_zoom-in.ppm",
638+
text="ZoomInX",file="stock_zoom-in",
638639
command=lambda x=1: self.zoomx(x))
639640

640641
self.bZoomOutX = self._Button(
641-
text="ZoomOutX", file="stock_zoom-out.ppm",
642+
text="ZoomOutX", file="stock_zoom-out",
642643
command=lambda x=-1: self.zoomx(x))
643644

644645
self.bUp = self._Button(
645-
text="Up", file="stock_up.ppm",
646+
text="Up", file="stock_up",
646647
command=lambda y=1: self.pany(y))
647648

648649
self.bDown = self._Button(
649-
text="Down", file="stock_down.ppm",
650+
text="Down", file="stock_down",
650651
command=lambda y=-1: self.pany(y))
651652

652653
self.bZoomInY = self._Button(
653-
text="ZoomInY", file="stock_zoom-in.ppm",
654+
text="ZoomInY", file="stock_zoom-in",
654655
command=lambda y=1: self.zoomy(y))
655656

656657
self.bZoomOutY = self._Button(
657-
text="ZoomOutY",file="stock_zoom-out.ppm",
658+
text="ZoomOutY",file="stock_zoom-out",
658659
command=lambda y=-1: self.zoomy(y))
659660

660661
self.bSave = self._Button(
661-
text="Save", file="stock_save_as.ppm",
662+
text="Save", file="stock_save_as",
662663
command=self.save_figure)
663664

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

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

782783
self.update() # Make axes menu
783784

784-
self.bHome = self._Button( text="Home", file="home.ppm",
785-
command=self.home)
786-
787-
self.bBack = self._Button( text="Back", file="back.ppm",
788-
command = self.back)
789-
790-
self.bForward = self._Button(text="Forward", file="forward.ppm",
791-
command = self.forward)
792-
793-
self.bPan = self._Button( text="Pan", file="move.ppm",
794-
command = self.pan)
795-
796-
self.bZoom = self._Button( text="Zoom",
797-
file="zoom_to_rect.ppm",
798-
command = self.zoom)
799-
800-
self.bsubplot = self._Button( text="Configure Subplots", file="subplots.ppm",
801-
command = self.configure_subplots)
802-
803-
self.bsave = self._Button( text="Save", file="filesave.ppm",
804-
command = self.save_figure)
785+
for text, tooltip_text, image_file, callback in self.toolitems:
786+
if text is None:
787+
# spacer, unhandled in Tk
788+
pass
789+
else:
790+
button = self._Button(text=text, file=image_file,
791+
command=getattr(self, callback))
792+
if tooltip_text is not None:
793+
ToolTip.createToolTip(button, tooltip_text)
794+
805795
self.message = Tk.StringVar(master=self)
806796
self._message_label = Tk.Label(master=self, textvariable=self.message)
807797
self._message_label.pack(side=Tk.RIGHT)
@@ -879,3 +869,54 @@ def dynamic_update(self):
879869

880870

881871
FigureManager = FigureManagerTkAgg
872+
873+
874+
class ToolTip(object):
875+
"""
876+
Tooltip recipe from
877+
http://www.voidspace.org.uk/python/weblog/arch_d7_2006_07_01.shtml#e387
878+
"""
879+
@staticmethod
880+
def createToolTip(widget, text):
881+
toolTip = ToolTip(widget)
882+
def enter(event):
883+
toolTip.showtip(text)
884+
def leave(event):
885+
toolTip.hidetip()
886+
widget.bind('<Enter>', enter)
887+
widget.bind('<Leave>', leave)
888+
889+
def __init__(self, widget):
890+
self.widget = widget
891+
self.tipwindow = None
892+
self.id = None
893+
self.x = self.y = 0
894+
895+
def showtip(self, text):
896+
"Display text in tooltip window"
897+
self.text = text
898+
if self.tipwindow or not self.text:
899+
return
900+
x, y, _, _ = self.widget.bbox("insert")
901+
x = x + self.widget.winfo_rootx() + 27
902+
y = y + self.widget.winfo_rooty()
903+
self.tipwindow = tw = Tk.Toplevel(self.widget)
904+
tw.wm_overrideredirect(1)
905+
tw.wm_geometry("+%d+%d" % (x, y))
906+
try:
907+
# For Mac OS
908+
tw.tk.call("::tk::unsupported::MacWindowStyle",
909+
"style", tw._w,
910+
"help", "noActivates")
911+
except Tk.TclError:
912+
pass
913+
label = Tk.Label(tw, text=self.text, justify=Tk.LEFT,
914+
background="#ffffe0", relief=Tk.SOLID, borderwidth=1,
915+
)
916+
label.pack(ipadx=1)
917+
918+
def hidetip(self):
919+
tw = self.tipwindow
920+
self.tipwindow = None
921+
if tw:
922+
tw.destroy()

0 commit comments

Comments
 (0)