39
39
figureoptions = None
40
40
41
41
from .qt4_compat import QtCore , QtGui , _getSaveFileName , __version__
42
+ from matplotlib .backends .qt4_editor .formsubplottool import UiSubplotTool
42
43
43
44
backend_version = __version__
44
45
@@ -558,6 +559,7 @@ class NavigationToolbar2QT(NavigationToolbar2, QtGui.QToolBar):
558
559
def __init__ (self , canvas , parent , coordinates = True ):
559
560
""" coordinates: should we show the coordinates on the right? """
560
561
self .canvas = canvas
562
+ self .parent = parent
561
563
self .coordinates = coordinates
562
564
self ._actions = {}
563
565
"""A mapping of toolitem method names to their QActions"""
@@ -628,9 +630,8 @@ def edit_parameters(self):
628
630
titles .append (fmt % dict (title = title ,
629
631
ylabel = ylabel ,
630
632
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 )
634
635
if ok :
635
636
axes = allaxes [titles .index (six .text_type (item ))]
636
637
else :
@@ -676,20 +677,11 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
676
677
self .canvas .drawRectangle (rect )
677
678
678
679
def configure_subplots (self ):
679
- self .adj_window = QtGui .QMainWindow ()
680
- win = self .adj_window
681
-
682
- win .setWindowTitle ("Subplot Configuration Tool" )
683
680
image = os .path .join (matplotlib .rcParams ['datapath' ],
684
681
'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_ ()
693
685
694
686
def _get_canvas (self , fig ):
695
687
return FigureCanvasQT (fig )
@@ -712,8 +704,9 @@ def save_figure(self, *args):
712
704
selectedFilter = filter
713
705
filters .append (filter )
714
706
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 )
717
710
if fname :
718
711
if startpath == '' :
719
712
# explicitly missing key or empty str signals to use cwd
@@ -730,132 +723,142 @@ def save_figure(self, *args):
730
723
QtGui .QMessageBox .Ok , QtGui .QMessageBox .NoButton )
731
724
732
725
733
- class SubplotToolQt (SubplotTool , QtGui . QWidget ):
726
+ class SubplotToolQt (SubplotTool , UiSubplotTool ):
734
727
def __init__ (self , targetfig , parent ):
735
- QtGui .QWidget .__init__ (self , None )
736
-
728
+ UiSubplotTool .__init__ (self , None )
737
729
self .targetfig = targetfig
738
730
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 )
752
735
753
736
sliders = (self .sliderleft , self .sliderbottom , self .sliderright ,
754
737
self .slidertop , self .sliderwspace , self .sliderhspace ,)
755
- adjustments = ('left:' , 'bottom:' , 'right:' ,
756
- 'top:' , 'wspace:' , 'hspace:' )
757
738
758
- for slider , adjustment in zip ( sliders , adjustments ) :
739
+ for slider in sliders :
759
740
slider .setMinimum (0 )
760
741
slider .setMaximum (1000 )
761
742
slider .setSingleStep (5 )
762
743
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 ))
804
791
self .sliderbottom .setSliderPosition (
805
- int (targetfig .subplotpars .bottom * 1000 ))
792
+ int (self . targetfig .subplotpars .bottom * 1000 ))
806
793
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 ))
809
797
self .sliderwspace .setSliderPosition (
810
- int (targetfig .subplotpars .wspace * 1000 ))
798
+ int (self . targetfig .subplotpars .wspace * 1000 ))
811
799
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 ))
820
801
821
802
def funcleft (self , val ):
822
803
if val == self .sliderright .value ():
823
804
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 )
825
808
if self .drawon :
826
809
self .targetfig .canvas .draw ()
827
810
828
811
def funcright (self , val ):
829
812
if val == self .sliderleft .value ():
830
813
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 )
832
817
if self .drawon :
833
818
self .targetfig .canvas .draw ()
834
819
835
820
def funcbottom (self , val ):
836
821
if val == self .slidertop .value ():
837
822
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 )
839
826
if self .drawon :
840
827
self .targetfig .canvas .draw ()
841
828
842
829
def functop (self , val ):
843
830
if val == self .sliderbottom .value ():
844
831
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 )
846
835
if self .drawon :
847
836
self .targetfig .canvas .draw ()
848
837
849
838
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 )
851
842
if self .drawon :
852
843
self .targetfig .canvas .draw ()
853
844
854
845
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 )
856
849
if self .drawon :
857
850
self .targetfig .canvas .draw ()
858
851
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
+
859
862
860
863
def error_msg_qt (msg , parent = None ):
861
864
if not is_string_like (msg ):
0 commit comments