-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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', | ||
|
@@ -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"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current recommendations are to not even call this. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
QuLogic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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) | ||
|
@@ -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 | ||
|
@@ -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', | ||
|
@@ -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): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.