Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2129eb0

Browse files
committed
Alternative UI, using a toolbar menu.
1 parent 7d72aa6 commit 2129eb0

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,13 @@ def _init_toolbar(self):
724724
self._actions[callback] = a
725725
if callback in ['zoom', 'pan']:
726726
a.setCheckable(True)
727+
if callback == 'save_figure':
728+
btn = self.widgetForAction(a)
729+
btn.setPopupMode(QtWidgets.QToolButton.MenuButtonPopup)
730+
menu = a.__menu = QtWidgets.QMenu()
731+
a.setMenu(menu)
732+
menu.addAction(
733+
'Options for saving', self._ui_get_save_options)
727734
if tooltip_text is not None:
728735
a.setToolTip(tooltip_text)
729736
if text == 'Subplots':
@@ -854,32 +861,32 @@ def save_figure(self, *args):
854861
if startpath != "":
855862
matplotlib.rcParams['savefig.directory'] = (
856863
os.path.dirname(six.text_type(fname)))
857-
options = _default_savefig_options
858-
try:
859-
options = (options
860-
+ [None]
861-
+ _extra_savefig_options[
862-
os.path.splitext(fname)[1].lower()])
863-
except KeyError:
864-
pass
865-
fedit_arg = []
866-
for option in options:
867-
if option is None:
868-
fedit_arg.append((None, None))
869-
else:
870-
fedit_arg.append(option.make_fedit_entry())
871-
fedit_res = formlayout.fedit(fedit_arg, "Options")
872-
if not fedit_res:
873-
return
874-
for option, res in zip(filter(None, options), fedit_res):
875-
option.setter(res)
876864
try:
877865
self.canvas.figure.savefig(six.text_type(fname))
878866
except Exception as e:
879867
QtWidgets.QMessageBox.critical(
880868
self, "Error saving file", six.text_type(e),
881869
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)
882870

871+
def _ui_get_save_options(self):
872+
fedit_arg = []
873+
for tab_name, options in _savefig_options:
874+
fedit_arg.append(([], tab_name, ""))
875+
for option in options:
876+
if option is None:
877+
fedit_arg[-1][0].append((None, None))
878+
else:
879+
fedit_arg[-1][0].append(option.make_fedit_entry())
880+
fedit_res = formlayout.fedit(fedit_arg, "Options")
881+
if not fedit_res:
882+
return
883+
for option, res in zip(
884+
[option
885+
for tab_name, options in _savefig_options
886+
for option in options],
887+
[res for tab in fedit_res for res in tab]):
888+
option.setter(res)
889+
883890

884891
class _Option(object):
885892

@@ -903,55 +910,50 @@ def make_fedit_entry():
903910
self.setter = setter
904911

905912

906-
_default_savefig_options = [
907-
_Option("DPI", "savefig.dpi"),
908-
_Option("Face color", "savefig.facecolor"),
909-
_Option("Edge color", "savefig.edgecolor"),
910-
_Option("Tight bounding box",
911-
getter=lambda: matplotlib.rcParams["savefig.bbox"] == "tight",
912-
setter=lambda val: matplotlib.rcParams.__setitem__(
913-
"savefig.bbox", "tight" if val else "standard")),
914-
_Option("Tight bounding box padding", "savefig.pad_inches"),
915-
_Option("Transparent background", "savefig.transparent")]
916-
917-
918-
_extra_savefig_options = {
919-
".jpg": [
920-
_Option("JPEG quality", "savefig.jpeg_quality")],
921-
".jpeg": [
922-
_Option("JPEG quality", "savefig.jpeg_quality")],
923-
".pdf": [
924-
_Option("PDF compression", "pdf.compression"),
925-
_Option("PDF font type",
913+
_savefig_options = [
914+
("Common", [
915+
_Option("DPI", "savefig.dpi"),
916+
_Option("Face color", "savefig.facecolor"),
917+
_Option("Edge color", "savefig.edgecolor"),
918+
_Option("Tight bounding box",
919+
getter=lambda: matplotlib.rcParams["savefig.bbox"] == "tight",
920+
setter=lambda val: matplotlib.rcParams.__setitem__(
921+
"savefig.bbox", "tight" if val else "standard")),
922+
_Option("Tight bounding box padding", "savefig.pad_inches"),
923+
_Option("Transparent background", "savefig.transparent")]),
924+
("JPEG", [
925+
_Option("Quality", "savefig.jpeg_quality")]),
926+
("PDF", [
927+
_Option("Compression", "pdf.compression"),
928+
_Option("Font type",
926929
getter=lambda:
927930
[str(matplotlib.rcParams["pdf.fonttype"]),
928931
"3", "42"],
929932
setter=lambda val:
930-
matplotlib.rcParams.__setitem__("pdf.fonttype", val))],
931-
".ps": [
932-
_Option("PS paper size",
933+
matplotlib.rcParams.__setitem__("pdf.fonttype", val))]),
934+
("PS", [
935+
_Option("Paper size",
933936
getter=lambda:
934937
[matplotlib.rcParams["ps.papersize"]]
935938
+ ["auto", "letter", "legal", "ledger"]
936939
+ ["A{}".format(i) for i in range(11)]
937940
+ ["B{}".format(i) for i in range(11)],
938941
setter=lambda val:
939942
matplotlib.rcParams.__setitem__("ps.papersize", val)),
940-
_Option("PS use AFM font", "ps.useafm"),
941-
_Option("PS distiller",
943+
_Option("Use AFM font?", "ps.useafm"),
944+
_Option("Distiller",
942945
getter=lambda:
943946
[str(matplotlib.rcParams["ps.usedistiller"])]
944947
+ ["None", "ghostscript", "xpdf"],
945948
setter=lambda val:
946949
matplotlib.rcParams.__setitem__("ps.usedistiller", val)),
947-
_Option("PS distiller resolution", "ps.distiller.res"),
948-
_Option("PS font type",
950+
_Option("Distiller resolution", "ps.distiller.res"),
951+
_Option("Font type",
949952
getter=lambda:
950953
[str(matplotlib.rcParams["ps.fonttype"]),
951954
"3", "42"],
952955
setter=lambda val:
953-
matplotlib.rcParams.__setitem__("ps.fonttype", val))]
954-
}
956+
matplotlib.rcParams.__setitem__("ps.fonttype", val))])]
955957

956958

957959
class SubplotToolQt(UiSubplotTool):

0 commit comments

Comments
 (0)