From 44fd78661eba8eedcb3f5b43166239ba5ea82a1d Mon Sep 17 00:00:00 2001 From: Nick Ward Date: Sat, 16 Mar 2013 11:33:41 +0000 Subject: [PATCH] Keyboard shortcuts work when toolbar not displayed --- lib/matplotlib/backend_bases.py | 10 ++++++++-- lib/matplotlib/backends/backend_gtk.py | 3 +++ lib/matplotlib/backends/backend_gtk3.py | 3 +++ lib/matplotlib/backends/backend_qt.py | 3 +++ lib/matplotlib/backends/backend_qt4.py | 3 ++- lib/matplotlib/pyplot.py | 14 ++++++++++++++ 6 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index abcf66c790ad..4a7588e44244 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2399,6 +2399,7 @@ def key_press_handler(event, canvas, toolbar=None): # saving current figure (default key 's') elif event.key in save_keys: toolbar.save_figure() + toolbar.display_cursor(event) if event.inaxes is None: return @@ -2676,7 +2677,7 @@ class implementation. """ raise NotImplementedError - def mouse_move(self, event): + def display_cursor(self, event): if not event.inaxes or not self._active: if self._lastCursor != cursors.POINTER: self.set_cursor(cursors.POINTER) @@ -2692,8 +2693,9 @@ def mouse_move(self, event): self._lastCursor = cursors.MOVE + def mouse_move(self, event): + self.display_cursor(event) if event.inaxes and event.inaxes.get_navigate(): - try: s = event.inaxes.format_coord(event.xdata, event.ydata) except (ValueError, OverflowError): @@ -3096,3 +3098,7 @@ def zoom(self, *args): def set_history_buttons(self): """Enable or disable back/forward button""" pass + + def set_hidden(self, is_hidden): + """Set the toolbars visibility.""" + pass diff --git a/lib/matplotlib/backends/backend_gtk.py b/lib/matplotlib/backends/backend_gtk.py index 3b30686b86a4..8a40dfaae213 100644 --- a/lib/matplotlib/backends/backend_gtk.py +++ b/lib/matplotlib/backends/backend_gtk.py @@ -783,6 +783,9 @@ def configure_subplots(self, button): def _get_canvas(self, fig): return FigureCanvasGTK(fig) + def set_hidden(self, hidden): + self.set_visible(hidden) + class NavigationToolbar(gtk.Toolbar): """ diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index be12f4e68c38..4d6bc7aeab70 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -592,6 +592,9 @@ def configure_subplots(self, button): def _get_canvas(self, fig): return self.canvas.__class__(fig) + def set_hidden(self, hidden): + self.set_visible(hidden) + class NavigationToolbar(Gtk.Toolbar): """ diff --git a/lib/matplotlib/backends/backend_qt.py b/lib/matplotlib/backends/backend_qt.py index 9b009cf5ac8d..f5544d8f86f0 100644 --- a/lib/matplotlib/backends/backend_qt.py +++ b/lib/matplotlib/backends/backend_qt.py @@ -464,6 +464,9 @@ def set_history_buttons( self ): self.buttons[ 'Back' ].setEnabled( canBackward ) self.buttons[ 'Forward' ].setEnabled( canForward ) + def set_hidden(self, hidden): + self.setVisible(hidden) + # set icon used when windows are minimized try: # TODO: This is badly broken diff --git a/lib/matplotlib/backends/backend_qt4.py b/lib/matplotlib/backends/backend_qt4.py index 3caad0bf90c1..4d7ef476be71 100644 --- a/lib/matplotlib/backends/backend_qt4.py +++ b/lib/matplotlib/backends/backend_qt4.py @@ -672,7 +672,8 @@ def save_figure(self, *args): self, "Error saving file", str(e), QtGui.QMessageBox.Ok, QtGui.QMessageBox.NoButton) - + def set_hidden(self, hidden): + self.setVisible(hidden) class SubplotToolQt( SubplotTool, QtGui.QWidget ): def __init__(self, targetfig, parent): diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 619e448d2983..f20475ef8ad7 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -339,6 +339,16 @@ class that will be passed on to :meth:`new_figure_manager` in the if figManager is None: if get_backend().lower() == 'ps': dpi = 72 + # If toolbar is set to 'None', it should still be instantiated if + # the backend supports setting it's visibility, so that keyboard + # shortcut logic is preserved + invisible_toolbar = False + if rcParams['toolbar'] not in ('toolbar2', 'classic'): + if get_backend().lower() in ('qt4agg', 'qtagg', 'gtk', 'gtkagg', + 'gtkcairo'): + rcParams['toolbar'] = 'toolbar2' + invisible_toolbar = True + figManager = new_figure_manager(num, figsize=figsize, dpi=dpi, facecolor=facecolor, @@ -347,6 +357,10 @@ class that will be passed on to :meth:`new_figure_manager` in the FigureClass=FigureClass, **kwargs) + if invisible_toolbar: + figManager.toolbar.set_hidden(False) + rcParams['toolbar'] = 'None' + if figLabel: figManager.set_window_title(figLabel) figManager.canvas.figure.set_label(figLabel)