2121from matplotlib ._pylab_helpers import Gcf
2222from matplotlib .figure import Figure
2323
24- from matplotlib .widgets import SubplotTool
2524import matplotlib .backends .qt_editor .figureoptions as figureoptions
2625
2726from .qt_compat import (QtCore , QtGui , QtWidgets , _getSaveFileName ,
@@ -778,7 +777,7 @@ def save_figure(self, *args):
778777 QtWidgets .QMessageBox .Ok , QtWidgets .QMessageBox .NoButton )
779778
780779
781- class SubplotToolQt (SubplotTool , UiSubplotTool ):
780+ class SubplotToolQt (UiSubplotTool ):
782781 def __init__ (self , targetfig , parent ):
783782 UiSubplotTool .__init__ (self , None )
784783
@@ -790,39 +789,60 @@ def __init__(self, targetfig, parent):
790789 self ._widgets [higher ].valueChanged .connect (
791790 lambda val : self ._widgets [lower ].setMaximum (val - .001 ))
792791
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+
796796 # Set values after setting the range callbacks, but before setting up
797797 # the redraw callbacks.
798798 self ._reset ()
799799
800- for attr in self .defaults :
800+ for attr in self ._attrs :
801801 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 ),
803804 ("Reset" , self ._reset ),
804805 ("Close" , self .close )]:
805806 self ._widgets [action ].clicked .connect (method )
806807
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+
807829 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 ()
812833
813834 def _tight_layout (self ):
814835 self ._figure .tight_layout ()
815- for attr in self .defaults :
836+ for attr in self ._attrs :
816837 widget = self ._widgets [attr ]
817838 widget .blockSignals (True )
818- widget .setValue (getattr (self ._figure .subplotpars , attr ) )
839+ widget .setValue (vars (self ._figure .subplotpars )[ attr ] )
819840 widget .blockSignals (False )
820- if self .drawon :
821- self ._figure .canvas .draw_idle ()
841+ self ._figure .canvas .draw_idle ()
822842
823843 def _reset (self ):
824- for attr in self .defaults :
825- self ._widgets [attr ].setValue (self . defaults [ attr ] )
844+ for attr , value in self ._defaults . items () :
845+ self ._widgets [attr ].setValue (value )
826846
827847
828848def error_msg_qt (msg , parent = None ):
0 commit comments