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

Skip to content

Commit 244a3e0

Browse files
committed
Simplify and improve Qt borders/spacing tool.
Replaced sliders in Qt borders/spacing tool by spinboxes, which 1. should be easier to set to an exact value, and 2. do not continuously trigger redraws unless the user presses enter or uses the arrows to step the values (the redraws can be quite slow when working with a complex plot). The spinbox step size of 0.005 was chosen for consistency with the earlier choice of 5/1000. Greatly simplified the implementation. Attributes on the SubplotToolQt instance are not kept because it is impossible to keep back-compatibility (the sliders simply don't exist anymore). New attributes are all private; only `.default` (which has the same meaning) is kept as is. Tested with PyQt 4.12, PySide 1.2.4, PyQt 5.8.
1 parent dc6441f commit 244a3e0

File tree

3 files changed

+81
-312
lines changed

3 files changed

+81
-312
lines changed

lib/matplotlib/backends/backend_qt4.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from matplotlib.widgets import SubplotTool
2525

2626
from .qt_compat import QtCore, QtWidgets, _getSaveFileName, __version__
27-
from matplotlib.backends.qt_editor.formsubplottool import UiSubplotTool
2827

2928
from .backend_qt5 import (backend_version, SPECIAL_KEYS, SUPER, ALT, CTRL,
3029
SHIFT, MODIFIER_KEYS, fn_name, cursord,

lib/matplotlib/backends/backend_qt5.py

Lines changed: 38 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -782,97 +782,47 @@ class SubplotToolQt(SubplotTool, UiSubplotTool):
782782
def __init__(self, targetfig, parent):
783783
UiSubplotTool.__init__(self, None)
784784

785-
self.targetfig = targetfig
786-
self.parent = parent
787-
self.donebutton.clicked.connect(self.close)
788-
self.resetbutton.clicked.connect(self.reset)
789-
self.tightlayout.clicked.connect(self.functight)
790-
791-
# constraints
792-
self.sliderleft.valueChanged.connect(self.sliderright.setMinimum)
793-
self.sliderright.valueChanged.connect(self.sliderleft.setMaximum)
794-
self.sliderbottom.valueChanged.connect(self.slidertop.setMinimum)
795-
self.slidertop.valueChanged.connect(self.sliderbottom.setMaximum)
796-
797-
self.defaults = {}
798-
for attr in ('left', 'bottom', 'right', 'top', 'wspace', 'hspace', ):
799-
val = getattr(self.targetfig.subplotpars, attr)
800-
self.defaults[attr] = val
801-
slider = getattr(self, 'slider' + attr)
802-
txt = getattr(self, attr + 'value')
803-
slider.setMinimum(0)
804-
slider.setMaximum(1000)
805-
slider.setSingleStep(5)
806-
# do this before hooking up the callbacks
807-
slider.setSliderPosition(int(val * 1000))
808-
txt.setText("%.2f" % val)
809-
slider.valueChanged.connect(getattr(self, 'func' + attr))
810-
self._setSliderPositions()
811-
812-
def _setSliderPositions(self):
813-
for attr in ('left', 'bottom', 'right', 'top', 'wspace', 'hspace', ):
814-
slider = getattr(self, 'slider' + attr)
815-
slider.setSliderPosition(int(self.defaults[attr] * 1000))
816-
817-
def funcleft(self, val):
818-
if val == self.sliderright.value():
819-
val -= 1
820-
val /= 1000.
821-
self.targetfig.subplots_adjust(left=val)
822-
self.leftvalue.setText("%.2f" % val)
823-
if self.drawon:
824-
self.targetfig.canvas.draw_idle()
825-
826-
def funcright(self, val):
827-
if val == self.sliderleft.value():
828-
val += 1
829-
val /= 1000.
830-
self.targetfig.subplots_adjust(right=val)
831-
self.rightvalue.setText("%.2f" % val)
832-
if self.drawon:
833-
self.targetfig.canvas.draw_idle()
834-
835-
def funcbottom(self, val):
836-
if val == self.slidertop.value():
837-
val -= 1
838-
val /= 1000.
839-
self.targetfig.subplots_adjust(bottom=val)
840-
self.bottomvalue.setText("%.2f" % val)
841-
if self.drawon:
842-
self.targetfig.canvas.draw_idle()
843-
844-
def functop(self, val):
845-
if val == self.sliderbottom.value():
846-
val += 1
847-
val /= 1000.
848-
self.targetfig.subplots_adjust(top=val)
849-
self.topvalue.setText("%.2f" % val)
785+
self._figure = targetfig
786+
787+
for lower, higher in [("bottom", "top"), ("left", "right")]:
788+
self._widgets[lower].valueChanged.connect(
789+
lambda val: self._widgets[higher].setMinimum(val + .001))
790+
self._widgets[higher].valueChanged.connect(
791+
lambda val: self._widgets[lower].setMaximum(val - .001))
792+
793+
self.defaults = {
794+
attr: getattr(self._figure.subplotpars, attr)
795+
for attr in ["left", "bottom", "right", "top", "wspace", "hspace"]}
796+
# Set values after setting the range callbacks, but before setting up
797+
# the redraw callbacks.
798+
self._reset()
799+
800+
for attr in self.defaults:
801+
self._widgets[attr].valueChanged.connect(self._on_value_changed)
802+
for action, method in [("Tight Layout", self._tight_layout),
803+
("Reset", self._reset),
804+
("Close", self.close)]:
805+
self._widgets[action].clicked.connect(method)
806+
807+
def _on_value_changed(self):
808+
self._figure.subplots_adjust(
809+
**{attr: self._widgets[attr].value() for attr in self.defaults})
850810
if self.drawon:
851-
self.targetfig.canvas.draw_idle()
852-
853-
def funcwspace(self, val):
854-
val /= 1000.
855-
self.targetfig.subplots_adjust(wspace=val)
856-
self.wspacevalue.setText("%.2f" % val)
857-
if self.drawon:
858-
self.targetfig.canvas.draw_idle()
859-
860-
def funchspace(self, val):
861-
val /= 1000.
862-
self.targetfig.subplots_adjust(hspace=val)
863-
self.hspacevalue.setText("%.2f" % val)
811+
self._figure.canvas.draw_idle()
812+
813+
def _tight_layout(self):
814+
self._figure.tight_layout()
815+
for attr in self.defaults:
816+
widget = self._widgets[attr]
817+
widget.blockSignals(True)
818+
widget.setValue(getattr(self._figure.subplotpars, attr))
819+
widget.blockSignals(False)
864820
if self.drawon:
865-
self.targetfig.canvas.draw_idle()
866-
867-
def functight(self):
868-
self.targetfig.tight_layout()
869-
self._setSliderPositions()
870-
self.targetfig.canvas.draw_idle()
821+
self._figure.canvas.draw_idle()
871822

872-
def reset(self):
873-
self.targetfig.subplots_adjust(**self.defaults)
874-
self._setSliderPositions()
875-
self.targetfig.canvas.draw_idle()
823+
def _reset(self):
824+
for attr in self.defaults:
825+
self._widgets[attr].setValue(self.defaults[attr])
876826

877827

878828
def error_msg_qt(msg, parent=None):

0 commit comments

Comments
 (0)