-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
use Qt window title as default savefig filename #908
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
Conversation
A very pertinent mailinglist post: http://old.nabble.com/How-to-give-a-name-to-a-figure--td19498850.html . I think this pull request should include the definition of an interface to get the window title (as suggested in the mailing list) in a general way in the |
@@ -431,8 +431,10 @@ def save_figure(self, *args): | |||
sorted_filetypes = filetypes.items() | |||
sorted_filetypes.sort() | |||
default_filetype = self.canvas.get_default_filetype() | |||
default_filename = self.canvas.window().windowTitle() or 'image' | |||
default_filename.replace(' ', '_') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Careful here. Replace is not done in place:
>>> a = 'foo bar'
>>> a.replace(' ', '_')
'foo_bar'
>>> a
'foo bar'
Whoops, I've made that string in-place error before. Strange thing was it didn't show up in testing. That was because it was actually a QString, for which replace() is in-place apparently :) I agree, there should be a canvas.get_window_title() method. Should be easy enough to add, at least for qt. I'll get to it shortly. |
Ouch. Thanks for the clarification. |
OK, FigureCanvasBase and FigureManagerBase now both have a My only concern is that perhaps |
Yes -- I think the base class should raise |
OK, done. Sure, if you (or anyone else) with access to the other backends could implement them, that would be great. |
@mspacek I have sent you a pull request, against this branch, which implements the title in the remaining backends. I have been able to test on a Linux (standard Ubuntu 12.04) machine. |
a default filename. | ||
""" | ||
default_filename = self.get_window_title() or 'image' | ||
default_filename = default_filename.replace(' ', '_') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you probably guessed, I would prefer the filename to be lower case for the same reason that the spaces have been removed. Having said that, I'm not overly fussed if you would prefer to keep mixed case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I agree with the lowercase thing. It's more of a gut feeling though -- and part of me feels like it should be lower case on Unix and mixed case on Windows, but that's probably a bad idea ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I disagree with going lowercase because it's a loss of information, but I'm outnumbered, so I'll change it back :)
This looks great. Just tested all GUI backends on Linux and seems to work as advertised. |
def set_window_title(self, title): | ||
""" | ||
Set the title text of the window containing the figure. Note that | ||
this has no effect if there is no window (eg, a PS backend). | ||
""" | ||
pass | ||
raise NotImplementedError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I am not able to test is the impact of this line. For instance, does the MacOS backend blow up now before any plot is produced? Are there other backends which we don't have sight of which will have this method called, with the obvious impact of having an unexpected exception raised?
@pelson, I guess the point is we want an exception to be raised, so that it doesn't fail silently. And then if reports of failure come back, they can be dealt with as needed? It seems there isn't a |
I think @pelson raises a good point about raising |
OK. What would be a reasonable default in the base class? A warning? Or should I just restore it to pass? |
I think |
Returning "image" on The other thing to consider is that we probably want to keep the previous behaviour of nothing happening when |
Good point about the warning. Let's just make it "pass". Anyone have OS-X and want to tackle implementing this in the mac osx backend? |
There's a (very lightly tested) attempt for OSX in jkseppan:default_filename: jkseppan@8046c39 |
@jkseppan: Great stuff! What setup have you been able to test it on? |
(I updated the commit: jkseppan@08dd29. I submitted it as a pull request to your branch, so you can pull it and this request should automatically update.) Mac OS X version 10.6.8, Python 2.7 installed from a python.org package: Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) As a test, I run:
and then click on the save icon. The save window now defaults to "hääyöaie.png" as the filename (though the extension is hidden by default). |
Great, thanks for that! I don't understand much in the .m file, but trust that it works. |
Great! As this now includes all of the major backends, I think this is probably ready to merge. @mspacek: Any remaining issues you're aware of? Can you do a fresh rebase on master? |
Not so far, because I had trouble installing the various backend requirements on a Mac - but I'm installing some of them via MacPorts now, so I should get to testing the backends soon. |
I was able to test with TkAgg and GtkAgg (Python 2.7.3 installed via MacPorts). Both work; in GtkAgg I can't set the title to a unicode string. MacPorts is still installing the dependencies for Qt, but I couldn't get WxPython to work via MacPorts. |
With the Qt4Agg backend, the save dialog simply does not appear (Python 2.7.3 via MacPorts). |
@jkseppan: Interesting. Is the Qt4Agg problem specific to this branch, or is that true on master as well? |
@mdboom, nope, no issues that I know of, at least on Qt4Agg in Linux. I hope I've rebased correctly. |
@mdboom: The same problem occurs on latest master without this change. Getting the dependencies right on OSX is notoriously difficult, so I suspect I have some problem with how I've installed Qt. |
@jkseppan: Thanks for all your work on this, its been really valuable to test at least one other OS. The Qt issue sounds like a separate issue to this PR, so unless we can get a windows tester on board, this change has my +1. |
I agree, go ahead and merge. |
Update: using homebrew, I have been able to test on OSX 10.6, Python 2.7.3, with the following backends: try_gtk.py:matplotlib.use('GtkAgg') Out of these, only Wx fails (so does WxAgg). I don't see a toolbar, and when I press the s key to save, I get a NotImplementedError. I wonder if I'm doing something wrong? In any case, I'm still in favor of merging.
|
@mspacek: Would you mind re-basing this PR, then I will merge. |
…ig filename revert prior changes to qt backend, since I can't test them
restore enforcing lowercase in get_default_filename
OK, done. Again, I hope I've rebased correctly. For my own reference, this is roughly what I did:
I noticed that there are two save figure buttons on the figure toolbar now. This must be something new from master. |
Thanks @mspacek. This is a nice feature. Thanks for all your work on it. Will merge in 24 hrs. |
You're welcome! |
use window title as default savefig filename
When in IPython, I tend to set my figure window title to the last command entered in IPython, ostensibly the command used to generate the figure. When I save the figure, I like to use the same string for the filename, but I've always had to punch that in manually. This patch automates this, but only for the Qt backend, since I'm not familiar with any others. For the typical figure where the user doesn't change the window title, the default filename is now the slightly more descriptive "Figure_x" instead of just "image".