From c7f5923a3abbd6287463396d3982f3593cd65a8e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 10 Apr 2020 13:19:22 +0200 Subject: [PATCH 1/3] Don't create a statusbar in Qt, wx backends. Display the coordinates text on the right of the toolbar, consistently with the GTK3, wx, and (I think?) macosx backends. This helps when embedding Matplotlib in larger GUIs, because Matplotlib may not necessarily control the statusbar (which is typically global for the whole window) but controls the toolbar if there's one. If we decide to go this route we could probably also kill the status bar for toolmanager. --- doc/api/api_changes_3.3/behaviour.rst | 6 ++++++ lib/matplotlib/backend_bases.py | 1 + lib/matplotlib/backends/backend_qt5.py | 14 ++++++-------- lib/matplotlib/backends/backend_wx.py | 16 ++++++++-------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/doc/api/api_changes_3.3/behaviour.rst b/doc/api/api_changes_3.3/behaviour.rst index b23b136f0d0e..ccdb72e2ec18 100644 --- a/doc/api/api_changes_3.3/behaviour.rst +++ b/doc/api/api_changes_3.3/behaviour.rst @@ -274,3 +274,9 @@ instead of :: # "bar", "barstacked" # "step", "stepfilled" + +Qt and wx backends no longer create a status bar by default +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The coordinates information is now displayed in the toolbar, consistently with +the other backends. This is intended to simplify embedding of Matplotlib in +larger GUIs, where Matplotlib may control the toolbar but not the status bar. diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 6502eadb7d49..3aad9bf8ae2b 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2893,6 +2893,7 @@ def mouse_move(self, event): if data_str is not None: s = s + ' ' + data_str + s = s.rstrip() if len(self.mode): self.set_message('%s, %s' % (self.mode, s)) else: diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index be18180f8839..8207b69b4566 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -550,14 +550,12 @@ def __init__(self, canvas, num): if self.toolbar: backend_tools.add_tools_to_container(self.toolbar) self.statusbar = StatusbarQt(self.window, self.toolmanager) + sbs_height = self.statusbar.sizeHint().height() + else: + sbs_height = 0 if self.toolbar is not None: self.window.addToolBar(self.toolbar) - if not self.toolmanager: - # add text label to status bar - statusbar_label = QtWidgets.QLabel() - self.window.statusBar().addWidget(statusbar_label) - self.toolbar.message.connect(statusbar_label.setText) tbs_height = self.toolbar.sizeHint().height() else: tbs_height = 0 @@ -565,8 +563,8 @@ def __init__(self, canvas, num): # resize the main window so it will display the canvas with the # requested size: cs = canvas.sizeHint() - sbs = self.window.statusBar().sizeHint() - height = cs.height() + tbs_height + sbs.height() + cs_height = cs.height() + height = cs_height + tbs_height + sbs_height self.window.resize(cs.width(), height) self.window.setCentralWidget(self.canvas) @@ -599,7 +597,7 @@ def _get_toolbar(self, canvas, parent): # must be inited after the window, drawingArea and figure # attrs are set if matplotlib.rcParams['toolbar'] == 'toolbar2': - toolbar = NavigationToolbar2QT(canvas, parent, False) + toolbar = NavigationToolbar2QT(canvas, parent, True) elif matplotlib.rcParams['toolbar'] == 'toolmanager': toolbar = ToolbarQt(self.toolmanager, self.window) else: diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index a5ab3ca50f97..f57979366130 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -928,13 +928,11 @@ def __init__(self, num, fig): self.figmgr = FigureManagerWx(self.canvas, num, self) - statusbar = (StatusbarWx(self, self.toolmanager) - if self.toolmanager else StatusBarWx(self)) - self.SetStatusBar(statusbar) self.toolbar = self._get_toolbar() - if self.toolmanager: - backend_tools.add_tools_to_manager(self.toolmanager) + if self.figmgr.toolmanager: + self.SetStatusBar(StatusbarWx(self, self.figmgr.toolmanager)) + backend_tools.add_tools_to_manager(self.figmgr.toolmanager) if self.toolbar: backend_tools.add_tools_to_container(self.toolbar) @@ -1122,6 +1120,10 @@ def __init__(self, canvas): self.Bind(wx.EVT_TOOL, getattr(self, callback), id=self.wx_ids[text]) + self.AddStretchableSpace() + self._label_text = wx.StaticText(self) + self.AddControl(self._label_text) + self.Realize() NavigationToolbar2.__init__(self, canvas) @@ -1300,9 +1302,7 @@ def statbar(self): return self.GetTopLevelParent().GetStatusBar() def set_message(self, s): - status_bar = self.GetTopLevelParent().GetStatusBar() - if status_bar is not None: - status_bar.set_function(s) + self._label_text.SetLabel(s) def set_history_buttons(self): can_backward = self._nav_stack._pos > 0 From 1857fcafd9cc9e4438e4f1942779c891276784be Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 11 Apr 2020 10:37:34 +0200 Subject: [PATCH 2/3] Move image value to second line; remove mode info. --- lib/matplotlib/backend_bases.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 3aad9bf8ae2b..1a957b212e96 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2881,23 +2881,18 @@ def mouse_move(self, event): except (ValueError, OverflowError): pass else: + s = s.rstrip() artists = [a for a in event.inaxes._mouseover_set if a.contains(event)[0] and a.get_visible()] - if artists: a = cbook._topmost_artist(artists) if a is not event.inaxes.patch: data = a.get_cursor_data(event) if data is not None: - data_str = a.format_cursor_data(data) - if data_str is not None: - s = s + ' ' + data_str - - s = s.rstrip() - if len(self.mode): - self.set_message('%s, %s' % (self.mode, s)) - else: - self.set_message(s) + data_str = a.format_cursor_data(data).rstrip() + if data_str: + s = s + '\n' + data_str + self.set_message(s) else: self.set_message(self.mode) From 21eb1e9820fbd384f991745dcd80454a4e9be74e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 11 Apr 2020 13:48:35 +0200 Subject: [PATCH 3/3] Change the Qt label vertical alignment to centered. --- lib/matplotlib/backends/backend_qt5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 8207b69b4566..c2a7cadf220e 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -677,7 +677,7 @@ def __init__(self, canvas, parent, coordinates=True): if self.coordinates: self.locLabel = QtWidgets.QLabel("", self) self.locLabel.setAlignment( - QtCore.Qt.AlignRight | QtCore.Qt.AlignTop) + QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter) self.locLabel.setSizePolicy( QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Ignored))