8000 Inline and simplify SubplotToolQt. · matplotlib/matplotlib@ff703fe · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>

Commit ff703fe

Browse files
committed
Inline and simplify SubplotToolQt.
Inline the UI setup into the main SubplotToolQt class: it is short enough that having everything in a single class makes things easier to follow. Also rework the attributes a bit, replacing _widgets and _attrs by a single _spinboxes which only tracks spinboxes (and thus attrs), not buttons.
1 parent 75c425c commit ff703fe

File tree

2 files changed

+56
-78
lines changed

2 files changed

+56
-78
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 56 additions & 38 deletions
< 6D47 td data-grid-cell-id="diff-b0827e7f471b84f27f061df2949c5a99e9acc4d9ae33384dd48c480ace7ec34c-809-828-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">809
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1212
TimerBase, cursors, ToolContainerBase, MouseButton)
1313
import matplotlib.backends.qt_editor.figureoptions as figureoptions
14-
from matplotlib.backends.qt_editor._formsubplottool import UiSubplotTool
1514
from . import qt_compat
1615
from .qt_compat import (
1716
QtCore, QtGui, QtWidgets, __version__, QT_API,
@@ -759,27 +758,48 @@ def set_history_buttons(self):
759758
self._actions['forward'].setEnabled(can_forward)
760759

761760

762-
class SubplotToolQt(UiSubplotTool):
761+
class SubplotToolQt(QtWidgets.QDialog):
763762
def __init__(self, targetfig, parent):
764-
super().__init__(None)
765-
763+
super().__init__()
764+
self.setObjectName("SubplotTool")
765+
self._spinboxes = {}
766+
main_layout = QtWidgets.QHBoxLayout()
767+
self.setLayout(main_layout)
768+
for group, spinboxes, buttons in [
769+
("Borders",
770+
["top", "bottom", "left", "right"],
771+
[("Export values", self._export_values)]),
772+
("Spacings",
773+
["hspace", "wspace"],
774+
[("Tight layout", self._tight_layout),
775+
("Reset", self._reset),
776+
("Close", self.close)])]:
777+
layout = QtWidgets.QVBoxLayout()
778+
main_layout.addLayout(layout)
779+
box = QtWidgets.QGroupBox(group)
780+
layout.addWidget(box)
781+
inner = QtWidgets.QFormLayout(box)
782+
for name in spinboxes:
783+
self._spinboxes[name] = spinbox = QtWidgets.QDoubleSpinBox()
784+
spinbox.setValue(getattr(targetfig.subplotpars, name))
785+
spinbox.setRange(0, 1)
786+
spinbox.setDecimals(3)
787+
spinbox.setSingleStep(0.005)
788+
spinbox.setKeyboardTracking(False)
789+
spinbox.valueChanged.connect(self._on_value_changed)
790+
inner.addRow(name, spinbox)
791+
layout.addStretch(1)
792+
for name, method in buttons:
793+
button = QtWidgets.QPushButton(name)
794+
# Don't trigger on <enter>, which is used to input values.
795+
button.setAutoDefault(False)
796+
button.clicked.connect(method)
797+
layout.addWidget(button)
798+
if name == "Close":
799+
button.setFocus()
766800
self._figure = targetfig
767-
768-
self._attrs = ["top", "bottom", "left", "right", "hspace", "wspace"]
769-
self._defaults = {attr: vars(self._figure.subplotpars)[attr]
770-
for attr in self._attrs}
771-
772-
# Set values after setting the range callbacks, but before setting up
773-
# the redraw callbacks.
774-
self._reset()
775-
776-
for attr in self._attrs:
777-
self._widgets[attr].valueChanged.connect(self._on_value_changed)
778-
for action, method in [("Export values", self._export_values),
779-
("Tight layout", self._tight_layout),
780-
("Reset", self._reset),
781-
("Close", self.close)]:
782-
self._widgets[action].clicked.connect(method)
801+
self._defaults = {spinbox: vars(self._figure.subplotpars)[attr]
802+
for attr, spinbox in self._spinboxes.items()}
783803

784804
def _export_values(self):
785805
# Explicitly round to 3 decimals (which is also the spinbox precision)
@@ -791,8 +811,8 @@ def _export_values(self):
791811
text.setReadOnly(True)
792812
layout.addWidget(text)
793813
text.setPlainText(
794-
",\n".join("{}={:.3}".format(attr, self._widgets[attr].value())
795-
for attr in self._attrs))
814+
",\n".join(f"{attr}={spinbox.value():.3}"
815+
for attr, spinbox in self._spinboxes.items()))
796816
# Adjust the height of the text widget to fit the whole text, plus
797817
# some padding.
798818
size = text.maximumSize()
@@ -803,31 +823,29 @@ def _export_values(self):
803823
dialog.exec_()
804824

805825
def _on_value_changed(self):
806-
widgets = self._widgets
826+
spinboxes = self._spinboxes
807827
# Set all mins and maxes, so that this can also be used in _reset().
808828
for lower, higher in [("bottom", "top"), ("left", "right")]:
-
widgets[higher].setMinimum(widgets[lower].value() + .001)
810-
widgets[lower].setMaximum(widgets[higher].value() - .001)
811-
self._figure.subplots_adjust(**{attr: widgets[attr].value()
812-
for attr in self._attrs})
829+
spinboxes[higher].setMinimum(spinboxes[lower].value() + .001)
830+
spinboxes[lower].setMaximum(spinboxes[higher].value() - .001)
831+
self._figure.subplots_adjust(
832+
**{attr: spinbox.value() for attr, spinbox in spinboxes.items()})
813833
self._figure.canvas.draw_idle()
814834

815835
def _tight_layout(self):
816836
self._figure.tight_layout()
817-
for attr in self._attrs:
818-
widget = self._widgets[attr]
819-
widget.blockSignals(True)
820-
widget.setValue(vars(self._figure.subplotpars)[attr])
821-
widget.blockSignals(False)
837+
for attr, spinbox in self._spinboxes.items():
838+
spinbox.blockSignals(True)
839+
spinbox.setValue(vars(self._figure.subplotpars)[attr])
840+
spinbox.blockSignals(False)
822841
self._figure.canvas.draw_idle()
823842

824843
def _reset(self):
825-
for attr, value in self._defaults.items():
826-
widget = self._widgets[attr]
827-
widget.setRange(0, 1)
828-
widget.blockSignals(True)
829-
widget.setValue(value)
830-
widget.blockSignals(False)
844+
for spinbox, value in self._defaults.items():
845+
spinbox.setRange(0, 1)
846+
spinbox.blockSignals(True)
847+
spinbox.setValue(value)
848+
spinbox.blockSignals(False)
831849
self._on_value_changed()
832850

833851

lib/matplotlib/backends/qt_editor/_formsubplottool.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0