Skip to content

Use the builtin GTK3 FileChooser rather than our custom subclass. #12748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ per-file-ignores =
lib/matplotlib/backends/_backend_tk.py: E501
lib/matplotlib/backends/backend_agg.py: E302
lib/matplotlib/backends/backend_cairo.py: E203, E221, E402
lib/matplotlib/backends/backend_gtk3.py: E203, E221, E222, E225, E251, E501
lib/matplotlib/backends/backend_gtk3.py: E203, E221, E225, E251, E501
lib/matplotlib/backends/backend_pgf.py: E731
lib/matplotlib/backends/qt_editor/_formlayout.py: E501
lib/matplotlib/font_manager.py: E203, E221, E251, E501
Expand Down
3 changes: 3 additions & 0 deletions doc/api/next_api_changes/2018-08-17-AL-deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ API deprecations
The following API elements are deprecated:

- ``get_py2exe_datafiles``, ``tk_window_focus``,
- ``backend_gtk3.FileChooserDialog``,
``backend_gtk3.NavigationToolbar2GTK3.get_filechooser``,
``backend_gtk3.SaveFigureGTK3.get_filechooser``,
- ``backend_ps.PsBackendHelper``, ``backend_ps.ps_backend_helper``,
- ``cbook.iterable``,
- ``cbook.get_label``, ``cbook.iterable``,
Expand Down
81 changes: 53 additions & 28 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import functools
import logging
import os
from pathlib import Path
import sys

import matplotlib
Expand Down Expand Up @@ -521,6 +523,7 @@ def _init_toolbar(self):

self.show_all()

@cbook.deprecated("3.1")
def get_filechooser(self):
fc = FileChooserDialog(
title='Save the figure',
Expand All @@ -532,24 +535,54 @@ def get_filechooser(self):
return fc

def save_figure(self, *args):
chooser = self.get_filechooser()
fname, format = chooser.get_filename_from_user()
chooser.destroy()
if fname:
startpath = os.path.expanduser(rcParams['savefig.directory'])
# Save dir for next time, unless empty str (i.e., use cwd).
if startpath != "":
rcParams['savefig.directory'] = os.path.dirname(fname)
try:
self.canvas.figure.savefig(fname, format=format)
except Exception as e:
error_msg_gtk(str(e), parent=self)
dialog = Gtk.FileChooserDialog(
title="Save the figure",
parent=self.canvas.get_toplevel(),
action=Gtk.FileChooserAction.SAVE,
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.OK),
)
for name, fmts \
in self.canvas.get_supported_filetypes_grouped().items():
ff = Gtk.FileFilter()
ff.set_name(name)
for fmt in fmts:
ff.add_pattern("*." + fmt)
dialog.add_filter(ff)
if self.canvas.get_default_filetype() in fmts:
dialog.set_filter(ff)

@functools.partial(dialog.connect, "notify::filter")
def on_notify_filter(*args):
name = dialog.get_filter().get_name()
fmt = self.canvas.get_supported_filetypes_grouped()[name][0]
dialog.set_current_name(
str(Path(dialog.get_current_name()).with_suffix("." + fmt)))

dialog.set_current_folder(rcParams["savefig.directory"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current recommendations are to not even call this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but that'd mean not respecting the documented rcParam...

dialog.set_current_name(self.canvas.get_default_filename())
dialog.set_do_overwrite_confirmation(True)

response = dialog.run()
fname = dialog.get_filename()
ff = dialog.get_filter() # Doesn't autoadjust to filename :/
fmt = self.canvas.get_supported_filetypes_grouped()[ff.get_name()][0]
dialog.destroy()
if response == Gtk.ResponseType.CANCEL:
return
# Save dir for next time, unless empty str (which means use cwd).
if rcParams['savefig.directory']:
rcParams['savefig.directory'] = os.path.dirname(fname)
try:
self.canvas.figure.savefig(fname, format=fmt)
except Exception as e:
error_msg_gtk(str(e), parent=self)

def configure_subplots(self, button):
toolfig = Figure(figsize=(6, 3))
canvas = self._get_canvas(toolfig)
toolfig.subplots_adjust(top=0.9)
tool = SubplotTool(self.canvas.figure, toolfig)
tool = SubplotTool(self.canvas.figure, toolfig)

w = int(toolfig.bbox.width)
h = int(toolfig.bbox.height)
Expand Down Expand Up @@ -582,6 +615,7 @@ def set_history_buttons(self):
self._gtk_ids['Forward'].set_sensitive(can_forward)


@cbook.deprecated("3.1")
class FileChooserDialog(Gtk.FileChooserDialog):
"""GTK+ file selector which remembers the last file/directory
selected and presents the user with a menu of supported image formats
Expand Down Expand Up @@ -777,6 +811,7 @@ def set_message(self, s):

class SaveFigureGTK3(backend_tools.SaveFigureBase):

@cbook.deprecated("3.1")
def get_filechooser(self):
fc = FileChooserDialog(
title='Save the figure',
Expand All @@ -788,21 +823,11 @@ def get_filechooser(self):
return fc

def trigger(self, *args, **kwargs):
chooser = self.get_filechooser()
fname, format_ = chooser.get_filename_from_user()
chooser.destroy()
if fname:
startpath = os.path.expanduser(rcParams['savefig.directory'])
if startpath == '':
# explicitly missing key or empty str signals to use cwd
rcParams['savefig.directory'] = startpath
else:
# save dir for next time
rcParams['savefig.directory'] = os.path.dirname(fname)
try:
self.figure.canvas.print_figure(fname, format=format_)
except Exception as e:
error_msg_gtk(str(e), parent=self)

class PseudoToolbar:
canvas = self.figure.canvas

return NavigationToolbar2GTK3.save_figure(PseudoToolbar())


class SetCursorGTK3(backend_tools.SetCursorBase):
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/cbook/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ def deprecate(obj, message=message, name=name, alternative=alternative,
old_doc = obj.__doc__

def finalize(wrapper, new_doc):
obj.__doc__ = new_doc
try:
obj.__doc__ = new_doc
except AttributeError: # Can't set on some extension objects.
pass
obj.__init__ = wrapper
return obj

Expand Down