3
3
import six
4
4
5
5
import functools
6
+ import operator
6
7
import os
7
8
import re
8
9
import signal
@@ -612,6 +613,13 @@ def _init_toolbar(self):
612
613
self ._actions [callback ] = a
613
614
if callback in ['zoom' , 'pan' ]:
614
615
a .setCheckable (True )
616
+ if callback == 'save_figure' :
617
+ btn = self .widgetForAction (a )
618
+ btn .setPopupMode (QtWidgets .QToolButton .MenuButtonPopup )
619
+ menu = a .__menu = QtWidgets .QMenu ()
620
+ a .setMenu (menu )
621
+ menu .addAction (
622
+ 'Options for saving' , self ._ui_get_save_options )
615
623
if tooltip_text is not None :
616
624
a .setToolTip (tooltip_text )
617
625
if text == 'Subplots' :
@@ -743,32 +751,29 @@ def save_figure(self, *args):
743
751
if startpath != "" :
744
752
matplotlib .rcParams ['savefig.directory' ] = (
745
753
os .path .dirname (six .text_type (fname )))
746
- options = _default_savefig_options
747
- try :
748
- options = (options
749
- + [None ]
750
- + _extra_savefig_options [
751
- os .path .splitext (fname )[1 ].lower ()])
752
- except KeyError :
753
- pass
754
- fedit_arg = []
755
- for option in options :
756
- if option is None :
757
- fedit_arg .append ((None , None ))
758
- else :
759
- fedit_arg .append (option .make_fedit_entry ())
760
- fedit_res = formlayout .fedit (fedit_arg , "Options" )
761
- if not fedit_res :
762
- return
763
- for option , res in zip (filter (None , options ), fedit_res ):
764
- option .setter (res )
765
754
try :
766
755
self .canvas .figure .savefig (six .text_type (fname ))
767
756
except Exception as e :
768
757
QtWidgets .QMessageBox .critical (
769
758
self , "Error saving file" , six .text_type (e ),
770
759
QtWidgets .QMessageBox .Ok , QtWidgets .QMessageBox .NoButton )
771
760
761
+ def _ui_get_save_options (self ):
762
+ fedit_arg = []
763
+ for tab_name , options in _savefig_options :
764
+ fedit_arg .append (([], tab_name , "" ))
765
+ for option in options :
766
+ if option is None :
767
+ fedit_arg [- 1 ][0 ].append ((None , None ))
768
+ else :
769
+ fedit_arg [- 1 ][0 ].append (option .make_fedit_entry ())
770
+ fedit_res = formlayout .fedit (fedit_arg , "Options" )
771
+ if not fedit_res :
772
+ return
773
+ for (tab_name , options ), results in zip (_savefig_options , fedit_res ):
774
+ for option , result in zip (options , results ):
775
+ option .setter (result )
776
+
772
777
773
778
class _Option (object ):
774
779
@@ -792,55 +797,50 @@ def make_fedit_entry():
792
797
self .setter = setter
793
798
794
799
795
- _default_savefig_options = [
796
- _Option ( "DPI" , "savefig.dpi" ),
797
- _Option ( "Face color" , "savefig.facecolor" ),
798
- _Option ( "Edge color" , "savefig.edgecolor" ),
799
- _Option ( "Tight bounding box" ,
800
- getter = lambda : matplotlib . rcParams [ "savefig.bbox" ] == "tight" ,
801
- setter = lambda val : matplotlib . rcParams . __setitem__ (
802
- "savefig.bbox" , "tight" if val else "standard" ) ),
803
- _Option ("Tight bounding box padding " , "savefig.pad_inches " ),
804
- _Option ("Transparent background" , "savefig.transparent" )]
805
-
806
-
807
- _extra_savefig_options = {
808
- ".jpg" : [
809
- _Option ("JPEG quality " , "savefig.jpeg_quality " )],
810
- ".jpeg" : [
811
- _Option ("JPEG quality " , "savefig.jpeg_quality" )],
812
- ".pdf" : [
813
- _Option ("PDF compression " , "pdf.compression" ),
814
- _Option ("PDF font type" ,
800
+ def _make_rc_setter ( key ):
801
+ return functools . partial ( operator . setitem , matplotlib . rcParams , key )
802
+
803
+
804
+ _savefig_options = [
805
+ ( "Common" , [
806
+ _Option ( "DPI" , "savefig.dpi" ),
807
+ _Option ( "Face color" , "savefig.facecolor" ),
808
+ _Option ("Edge color " , "savefig.edgecolor " ),
809
+ _Option ("Tight bounding box" ,
810
+ getter = lambda : matplotlib . rcParams [ "savefig.bbox" ] == "tight" ,
811
+ setter = lambda val : matplotlib . rcParams . __setitem__ (
812
+ "savefig.bbox" , "tight" if val else "standard" )),
813
+ _Option ( "Tight bounding box padding" , "savefig.pad_inches" ),
814
+ _Option ("Transparent background " , "savefig.transparent " )]) ,
815
+ ( "JPEG" , [
816
+ _Option ("Quality " , "savefig.jpeg_quality" )]) ,
817
+ ( "PDF" , [
818
+ _Option ("Compression " , "pdf.compression" ),
819
+ _Option ("Font type" ,
815
820
getter = lambda :
816
821
[str (matplotlib .rcParams ["pdf.fonttype" ]),
817
822
"3" , "42" ],
818
- setter = lambda val :
819
- matplotlib .rcParams .__setitem__ ("pdf.fonttype" , val ))],
820
- ".ps" : [
821
- _Option ("PS paper size" ,
823
+ setter = _make_rc_setter ("pdf.fonttype" ))]),
824
+ ("PS" , [
825
+ _Option ("Paper size" ,
822
826
getter = lambda :
823
827
[matplotlib .rcParams ["ps.papersize" ]]
824
828
+ ["auto" , "letter" , "legal" , "ledger" ]
825
829
+ ["A{}" .format (i ) for i in range (11 )]
826
830
+ ["B{}" .format (i ) for i in range (11 )],
827
- setter = lambda val :
828
- matplotlib .rcParams .__setitem__ ("ps.papersize" , val )),
829
- _Option ("PS use AFM font" , "ps.useafm" ),
830
- _Option ("PS distiller" ,
831
+ setter = _make_rc_setter ("ps.papersize" )),
832
+ _Option ("Use AFM font?" , "ps.useafm" ),
833
+ _Option ("Distiller" ,
831
834
getter = lambda :
832
835
[str (matplotlib .rcParams ["ps.usedistiller" ])]
833
836
+ ["None" , "ghostscript" , "xpdf" ],
834
- setter = lambda val :
835
- matplotlib .rcParams .__setitem__ ("ps.usedistiller" , val )),
836
- _Option ("PS distiller resolution" , "ps.distiller.res" ),
837
- _Option ("PS font type" ,
837
+ setter = _make_rc_setter ("ps.usedistiller" )),
838
+ _Option ("Distiller resolution" , "ps.distiller.res" ),
839
+ _Option ("Font type" ,
838
840
getter = lambda :
839
841
[str (matplotlib .rcParams ["ps.fonttype" ]),
840
842
"3" , "42" ],
841
- setter = lambda val :
842
- matplotlib .rcParams .__setitem__ ("ps.fonttype" , val ))]
843
- }
843
+ setter = _make_rc_setter ("ps.fonttype" ))])]
844
844
845
845
846
846
class SubplotToolQt (UiSubplotTool ):
0 commit comments