21
21
from matplotlib ._pylab_helpers import Gcf
22
22
from matplotlib .figure import Figure
23
23
24
- from matplotlib .widgets import SubplotTool
25
24
import matplotlib .backends .qt_editor .figureoptions as figureoptions
26
25
27
26
from .qt_compat import (QtCore , QtGui , QtWidgets , _getSaveFileName ,
@@ -778,7 +777,7 @@ def save_figure(self, *args):
778
777
QtWidgets .QMessageBox .Ok , QtWidgets .QMessageBox .NoButton )
779
778
780
779
781
- class SubplotToolQt (SubplotTool , UiSubplotTool ):
780
+ class SubplotToolQt (UiSubplotTool ):
782
781
def __init__ (self , targetfig , parent ):
783
782
UiSubplotTool .__init__ (self , None )
784
783
@@ -790,39 +789,60 @@ def __init__(self, targetfig, parent):
790
789
self ._widgets [higher ].valueChanged .connect (
791
790
lambda val : self ._widgets [lower ].setMaximum (val - .001 ))
792
791
793
- self .defaults = {
794
- attr : getattr (self ._figure .subplotpars , attr )
795
- for attr in ["left" , "bottom" , "right" , "top" , "wspace" , "hspace" ]}
792
+ self ._attrs = ["top" , "bottom" , "left" , "right" , "hspace" , "wspace" ]
793
+ self ._defaults = {attr : vars (self ._figure .subplotpars )[attr ]
794
+ for attr in self ._attrs }
795
+
796
796
# Set values after setting the range callbacks, but before setting up
797
797
# the redraw callbacks.
798
798
self ._reset ()
799
799
800
- for attr in self .defaults :
800
+ for attr in self ._defaults :
801
801
self ._widgets [attr ].valueChanged .connect (self ._on_value_changed )
802
- for action , method in [("Tight Layout" , self ._tight_layout ),
802
+ for action , method in [("Export values" , self ._export_values ),
803
+ ("Tight layout" , self ._tight_layout ),
803
804
("Reset" , self ._reset ),
804
805
("Close" , self .close )]:
805
806
self ._widgets [action ].clicked .connect (method )
806
807
808
+ def _export_values (self ):
809
+ # Explicitly round to 3 decimals (which is also the spinbox precision)
810
+ # to avoid numbers of the form 0.100...001.
811
+ dialog = QtWidgets .QDialog ()
812
+ layout = QtWidgets .QVBoxLayout ()
813
+ dialog .setLayout (layout )
814
+ text = QtWidgets .QPlainTextEdit ()
815
+ text .setReadOnly (True )
816
+ layout .addWidget (text )
817
+ text .setPlainText (
818
+ ",\n " .join ("{}={:.3}" .format (attr , self ._widgets [attr ].value ())
819
+ for attr in self ._attrs ))
820
+ # Adjust the height of the text widget to fit the whole text, plus
821
+ # some padding.
822
+ size = text .maximumSize ()
823
+ size .setHeight (
824
+ QtGui .QFontMetrics (text .document ().defaultFont ())
825
+ .size (0 , text .toPlainText ()).height () + 20 )
826
+ text .setMaximumSize (size )
827
+ dialog .exec_ ()
828
+
807
829
def _on_value_changed (self ):
808
- self ._figure .subplots_adjust (
809
- ** {attr : self ._widgets [attr ].value () for attr in self .defaults })
810
- if self .drawon :
811
- self ._figure .canvas .draw_idle ()
830
+ self ._figure .subplots_adjust (** {attr : self ._widgets [attr ].value ()
831
+ for attr in self ._attrs })
832
+ self ._figure .canvas .draw_idle ()
812
833
813
834
def _tight_layout (self ):
814
835
self ._figure .tight_layout ()
815
- for attr in self .defaults :
836
+ for attr in self ._attrs :
816
837
widget = self ._widgets [attr ]
817
838
widget .blockSignals (True )
818
- widget .setValue (getattr (self ._figure .subplotpars , attr ) )
839
+ widget .setValue (vars (self ._figure .subplotpars )[ attr ] )
819
840
widget .blockSignals (False )
820
- if self .drawon :
821
- self ._figure .canvas .draw_idle ()
841
+ self ._figure .canvas .draw_idle ()
822
842
823
843
def _reset (self ):
824
- for attr in self .defaults :
825
- self ._widgets [attr ].setValue (self .defaults [attr ])
844
+ for attr in self ._defaults :
845
+ self ._widgets [attr ].setValue (self ._defaults [attr ])
826
846
827
847
828
848
def error_msg_qt (msg , parent = None ):
0 commit comments