Skip to content

Commit d3ba4c4

Browse files
rhoeftwmr
authored andcommitted
qt4 subplottool is modal now + new layout
1 parent 432d9d8 commit d3ba4c4

File tree

2 files changed

+328
-95
lines changed

2 files changed

+328
-95
lines changed

lib/matplotlib/backends/backend_qt4.py

+98-95
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
figureoptions = None
4040

4141
from .qt4_compat import QtCore, QtGui, _getSaveFileName, __version__
42+
from matplotlib.backends.qt4_editor.formsubplottool import UiSubplotTool
4243

4344
backend_version = __version__
4445

@@ -558,6 +559,7 @@ class NavigationToolbar2QT(NavigationToolbar2, QtGui.QToolBar):
558559
def __init__(self, canvas, parent, coordinates=True):
559560
""" coordinates: should we show the coordinates on the right? """
560561
self.canvas = canvas
562+
self.parent = parent
561563
self.coordinates = coordinates
562564
self._actions = {}
563565
"""A mapping of toolitem method names to their QActions"""
@@ -628,9 +630,8 @@ def edit_parameters(self):
628630
titles.append(fmt % dict(title=title,
629631
ylabel=ylabel,
630632
axes_repr=repr(axes)))
631-
item, ok = QtGui.QInputDialog.getItem(self, 'Customize',
632-
'Select axes:', titles,
633-
0, False)
633+
item, ok = QtGui.QInputDialog.getItem(
634+
self.parent, 'Customize', 'Select axes:', titles, 0, False)
634635
if ok:
635636
axes = allaxes[titles.index(six.text_type(item))]
636637
else:
@@ -676,20 +677,11 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
676677
self.canvas.drawRectangle(rect)
677678

678679
def configure_subplots(self):
679-
self.adj_window = QtGui.QMainWindow()
680-
win = self.adj_window
681-
682-
win.setWindowTitle("Subplot Configuration Tool")
683680
image = os.path.join(matplotlib.rcParams['datapath'],
684681
'images', 'matplotlib.png')
685-
win.setWindowIcon(QtGui.QIcon(image))
686-
687-
tool = SubplotToolQt(self.canvas.figure, win)
688-
win.setCentralWidget(tool)
689-
win.setSizePolicy(QtGui.QSizePolicy.Preferred,
690-
QtGui.QSizePolicy.Preferred)
691-
692-
win.show()
682+
dia = SubplotToolQt(self.canvas.figure, self.parent)
683+
dia.setWindowIcon(QtGui.QIcon(image))
684+
dia.exec_()
693685

694686
def _get_canvas(self, fig):
695687
return FigureCanvasQT(fig)
@@ -712,8 +704,9 @@ def save_figure(self, *args):
712704
selectedFilter = filter
713705
filters.append(filter)
714706
filters = ';;'.join(filters)
715-
fname = _getSaveFileName(self, "Choose a filename to save to",
716-
start, filters, selectedFilter)
707+
708+
fname = _getSaveFileName(self.parent, "Choose a filename to save to",
709+
start, filters, selectedFilter)
717710
if fname:
718711
if startpath == '':
719712
# explicitly missing key or empty str signals to use cwd
@@ -730,132 +723,142 @@ def save_figure(self, *args):
730723
QtGui.QMessageBox.Ok, QtGui.QMessageBox.NoButton)
731724

732725

733-
class SubplotToolQt(SubplotTool, QtGui.QWidget):
726+
class SubplotToolQt(SubplotTool, UiSubplotTool):
734727
def __init__(self, targetfig, parent):
735-
QtGui.QWidget.__init__(self, None)
736-
728+
UiSubplotTool.__init__(self, None)
737729
self.targetfig = targetfig
738730
self.parent = parent
739-
740-
self.sliderleft = QtGui.QSlider(QtCore.Qt.Horizontal)
741-
self.sliderbottom = QtGui.QSlider(QtCore.Qt.Vertical)
742-
self.sliderright = QtGui.QSlider(QtCore.Qt.Horizontal)
743-
self.slidertop = QtGui.QSlider(QtCore.Qt.Vertical)
744-
self.sliderwspace = QtGui.QSlider(QtCore.Qt.Horizontal)
745-
self.sliderhspace = QtGui.QSlider(QtCore.Qt.Vertical)
746-
747-
# constraints
748-
self.sliderleft.valueChanged.connect(self.sliderright.setMinimum)
749-
self.sliderright.valueChanged.connect(self.sliderleft.setMaximum)
750-
self.sliderbottom.valueChanged.connect(self.slidertop.setMinimum)
751-
self.slidertop.valueChanged.connect(self.sliderbottom.setMaximum)
731+
self.connect(self.doneButton, QtCore.SIGNAL("clicked()"), self.close)
732+
self.connect(self.resetButton, QtCore.SIGNAL("clicked()"), self.reset)
733+
self.connect(self.tightLayout, QtCore.SIGNAL("clicked()"),
734+
self.functight)
752735

753736
sliders = (self.sliderleft, self.sliderbottom, self.sliderright,
754737
self.slidertop, self.sliderwspace, self.sliderhspace,)
755-
adjustments = ('left:', 'bottom:', 'right:',
756-
'top:', 'wspace:', 'hspace:')
757738

758-
for slider, adjustment in zip(sliders, adjustments):
739+
for slider in sliders:
759740
slider.setMinimum(0)
760741
slider.setMaximum(1000)
761742
slider.setSingleStep(5)
762743

763-
layout = QtGui.QGridLayout()
764-
765-
leftlabel = QtGui.QLabel('left')
766-
layout.addWidget(leftlabel, 2, 0)
767-
layout.addWidget(self.sliderleft, 2, 1)
768-
769-
toplabel = QtGui.QLabel('top')
770-
layout.addWidget(toplabel, 0, 2)
771-
layout.addWidget(self.slidertop, 1, 2)
772-
layout.setAlignment(self.slidertop, QtCore.Qt.AlignHCenter)
773-
774-
bottomlabel = QtGui.QLabel('bottom') # this might not ever be used
775-
layout.addWidget(bottomlabel, 4, 2)
776-
layout.addWidget(self.sliderbottom, 3, 2)
777-
layout.setAlignment(self.sliderbottom, QtCore.Qt.AlignHCenter)
778-
779-
rightlabel = QtGui.QLabel('right')
780-
layout.addWidget(rightlabel, 2, 4)
781-
layout.addWidget(self.sliderright, 2, 3)
782-
783-
hspacelabel = QtGui.QLabel('hspace')
784-
layout.addWidget(hspacelabel, 0, 6)
785-
layout.setAlignment(hspacelabel, QtCore.Qt.AlignHCenter)
786-
layout.addWidget(self.sliderhspace, 1, 6)
787-
layout.setAlignment(self.sliderhspace, QtCore.Qt.AlignHCenter)
788-
789-
wspacelabel = QtGui.QLabel('wspace')
790-
layout.addWidget(wspacelabel, 4, 6)
791-
layout.setAlignment(wspacelabel, QtCore.Qt.AlignHCenter)
792-
layout.addWidget(self.sliderwspace, 3, 6)
793-
layout.setAlignment(self.sliderwspace, QtCore.Qt.AlignBottom)
794-
795-
layout.setRowStretch(1, 1)
796-
layout.setRowStretch(3, 1)
797-
layout.setColumnStretch(1, 1)
798-
layout.setColumnStretch(3, 1)
799-
layout.setColumnStretch(6, 1)
800-
801-
self.setLayout(layout)
802-
803-
self.sliderleft.setSliderPosition(int(targetfig.subplotpars.left*1000))
744+
# constraints
745+
self.connect(self.sliderleft,
746+
QtCore.SIGNAL("valueChanged(int)"),
747+
self.sliderright.setMinimum)
748+
self.connect(self.sliderright,
749+
QtCore.SIGNAL("valueChanged(int)"),
750+
self.sliderleft.setMaximum)
751+
self.connect(self.sliderbottom,
752+
QtCore.SIGNAL("valueChanged(int)"),
753+
self.slidertop.setMinimum)
754+
self.connect(self.slidertop,
755+
QtCore.SIGNAL("valueChanged(int)"),
756+
self.sliderbottom.setMaximum)
757+
758+
self._read_defaults()
759+
self._setSliderPositions()
760+
761+
self.connect(self.sliderleft,
762+
QtCore.SIGNAL("valueChanged(int)"),
763+
self.funcleft)
764+
self.connect(self.sliderbottom,
765+
QtCore.SIGNAL("valueChanged(int)"),
766+
self.funcbottom)
767+
self.connect(self.sliderright,
768+
QtCore.SIGNAL("valueChanged(int)"),
769+
self.funcright)
770+
self.connect(self.slidertop,
771+
QtCore.SIGNAL("valueChanged(int)"),
772+
self.functop)
773+
self.connect(self.sliderwspace,
774+
QtCore.SIGNAL("valueChanged(int)"),
775+
self.funcwspace)
776+
self.connect(self.sliderhspace,
777+
QtCore.SIGNAL("valueChanged(int)"),
778+
self.funchspace)
779+
780+
def _read_defaults(self):
781+
self.defaults = {'left': self.targetfig.subplotpars.left,
782+
'bottom': self.targetfig.subplotpars.bottom,
783+
'right': self.targetfig.subplotpars.right,
784+
'top': self.targetfig.subplotpars.top,
785+
'wspace': self.targetfig.subplotpars.wspace,
786+
'hspace': self.targetfig.subplotpars.hspace}
787+
788+
def _setSliderPositions(self):
789+
self.sliderleft.setSliderPosition(
790+
int(self.targetfig.subplotpars.left*1000))
804791
self.sliderbottom.setSliderPosition(
805-
int(targetfig.subplotpars.bottom*1000))
792+
int(self.targetfig.subplotpars.bottom*1000))
806793
self.sliderright.setSliderPosition(
807-
int(targetfig.subplotpars.right*1000))
808-
self.slidertop.setSliderPosition(int(targetfig.subplotpars.top*1000))
794+
int(self.targetfig.subplotpars.right*1000))
795+
self.slidertop.setSliderPosition(
796+
int(self.targetfig.subplotpars.top*1000))
809797
self.sliderwspace.setSliderPosition(
810-
int(targetfig.subplotpars.wspace*1000))
798+
int(self.targetfig.subplotpars.wspace*1000))
811799
self.sliderhspace.setSliderPosition(
812-
int(targetfig.subplotpars.hspace*1000))
813-
814-
self.sliderleft.valueChanged.connect(self.funcleft)
815-
self.sliderbottom.valueChanged.connect(self.funcbottom)
816-
self.sliderright.valueChanged.connect(self.funcright)
817-
self.slidertop.valueChanged.connect(self.functop)
818-
self.sliderwspace.valueChanged.connect(self.funcwspace)
819-
self.sliderhspace.valueChanged.connect(self.funchspace)
800+
int(self.targetfig.subplotpars.hspace*1000))
820801

821802
def funcleft(self, val):
822803
if val == self.sliderright.value():
823804
val -= 1
824-
self.targetfig.subplots_adjust(left=val/1000.)
805+
val /= 1000.
806+
self.targetfig.subplots_adjust(left=val)
807+
self.leftvalue.setText("%.2f" % val)
825808
if self.drawon:
826809
self.targetfig.canvas.draw()
827810

828811
def funcright(self, val):
829812
if val == self.sliderleft.value():
830813
val += 1
831-
self.targetfig.subplots_adjust(right=val/1000.)
814+
val /= 1000.
815+
self.targetfig.subplots_adjust(right=val)
816+
self.rightvalue.setText("%.2f" % val)
832817
if self.drawon:
833818
self.targetfig.canvas.draw()
834819

835820
def funcbottom(self, val):
836821
if val == self.slidertop.value():
837822
val -= 1
838-
self.targetfig.subplots_adjust(bottom=val/1000.)
823+
val /= 1000.
824+
self.targetfig.subplots_adjust(bottom=val)
825+
self.bottomvalue.setText("%.2f" % val)
839826
if self.drawon:
840827
self.targetfig.canvas.draw()
841828

842829
def functop(self, val):
843830
if val == self.sliderbottom.value():
844831
val += 1
845-
self.targetfig.subplots_adjust(top=val/1000.)
832+
val /= 1000.
833+
self.targetfig.subplots_adjust(top=val)
834+
self.topvalue.setText("%.2f" % val)
846835
if self.drawon:
847836
self.targetfig.canvas.draw()
848837

849838
def funcwspace(self, val):
850-
self.targetfig.subplots_adjust(wspace=val/1000.)
839+
val /= 1000.
840+
self.targetfig.subplots_adjust(wspace=val)
841+
self.wspacevalue.setText("%.2f" % val)
851842
if self.drawon:
852843
self.targetfig.canvas.draw()
853844

854845
def funchspace(self, val):
855-
self.targetfig.subplots_adjust(hspace=val/1000.)
846+
val /= 1000.
847+
self.targetfig.subplots_adjust(hspace=val)
848+
self.hspacevalue.setText("%.2f" % val)
856849
if self.drawon:
857850
self.targetfig.canvas.draw()
858851

852+
def functight(self):
853+
self.targetfig.tight_layout()
854+
self._setSliderPositions()
855+
self.targetfig.canvas.draw()
856+
857+
def reset(self):
858+
self.targetfig.subplots_adjust(**self.defaults)
859+
self._setSliderPositions()
860+
self.targetfig.canvas.draw()
861+
859862

860863
def error_msg_qt(msg, parent=None):
861864
if not is_string_like(msg):

0 commit comments

Comments
 (0)