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

Skip to content

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

Merged
merged 9 commits into from
Jul 9, 2012
26 changes: 25 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,14 @@ def get_default_filetype(self):
"""
return rcParams['savefig.format']

def get_window_title(self):
"""
Get the title text of the window containing the figure.
Return None if there is no window (eg, a PS backend).
"""
if hasattr(self, "manager"):
return self.manager.get_window_title()

def set_window_title(self, title):
"""
Set the title text of the window containing the figure. Note that
Expand All @@ -2100,6 +2108,15 @@ def set_window_title(self, title):
if hasattr(self, "manager"):
self.manager.set_window_title(title)

def get_default_filename(self):
"""
Return a string, which includes extension, suitable for use as
a default filename.
"""
default_filename = self.get_window_title() or 'image'
default_filename = default_filename.lower().replace(' ', '_')
return default_filename + '.' + self.get_default_filetype()

def switch_backends(self, FigureCanvasClass):
"""
Instantiate an instance of FigureCanvasClass
Expand Down Expand Up @@ -2413,10 +2430,17 @@ def show_popup(self, msg):
"""
pass

def get_window_title(self):
"""
Get the title text of the window containing the figure.
Return None for non-GUI backends (eg, a PS backend).
"""
return 'image'

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).
this has no effect for non-GUI backends (eg, a PS backend).
"""
pass

Expand Down
9 changes: 7 additions & 2 deletions lib/matplotlib/backends/backend_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def __init__(self, canvas, num):
FigureManagerBase.__init__(self, canvas, num)

self.window = gtk.Window()
self.window.set_title("Figure %d" % num)
self.set_window_title("Figure %d" % num)
if (window_icon):
try:
self.window.set_icon_from_file(window_icon)
Expand Down Expand Up @@ -620,6 +620,9 @@ def _get_toolbar(self, canvas):
toolbar = None
return toolbar

def get_window_title(self):
return self.window.get_title()

def set_window_title(self, title):
self.window.set_title(title)

Expand Down Expand Up @@ -725,11 +728,13 @@ def _init_toolbar2_4(self):
self.show_all()

def get_filechooser(self):
return FileChooserDialog(
fc = FileChooserDialog(
title='Save the figure',
parent=self.win,
filetypes=self.canvas.get_supported_filetypes(),
default_filetype=self.canvas.get_default_filetype())
fc.set_current_name(self.canvas.get_default_filename())
return fc

def save_figure(self, *args):
fname, format = self.get_filechooser().get_filename_from_user()
Expand Down
9 changes: 7 additions & 2 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def __init__(self, canvas, num):
FigureManagerBase.__init__(self, canvas, num)

self.window = Gtk.Window()
self.window.set_title("Figure %d" % num)
self.set_window_title("Figure %d" % num)
if (window_icon):
try:
self.window.set_icon_from_file(window_icon)
Expand Down Expand Up @@ -447,6 +447,9 @@ def _get_toolbar(self, canvas):
toolbar = None
return toolbar

def get_window_title(self):
return self.window.get_title()

def set_window_title(self, title):
self.window.set_title(title)

Expand Down Expand Up @@ -532,11 +535,13 @@ def _init_toolbar(self):
self.show_all()

def get_filechooser(self):
return FileChooserDialog(
fc = FileChooserDialog(
title='Save the figure',
parent=self.win,
filetypes=self.canvas.get_supported_filetypes(),
default_filetype=self.canvas.get_default_filetype())
fc.set_current_name(self.canvas.get_default_filename())
return fc

def save_figure(self, *args):
fname, format = self.get_filechooser().get_filename_from_user()
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ def zoomy(self, direction):
self.canvas.invalidate()

def save_figure(self, *args):
filename = _macosx.choose_save_file('Save the figure')
filename = _macosx.choose_save_file('Save the figure',
self.canvas.get_default_filename())
if filename is None: # Cancel
return
self.canvas.print_figure(filename)
Expand All @@ -469,7 +470,8 @@ def set_cursor(self, cursor):
_macosx.set_cursor(cursor)

def save_figure(self, *args):
filename = _macosx.choose_save_file('Save the figure')
filename = _macosx.choose_save_file('Save the figure',
self.canvas.get_default_filename())
if filename is None: # Cancel
return
self.canvas.print_figure(filename)
Expand Down
7 changes: 5 additions & 2 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def __init__( self, canvas, num ):
# Give the keyboard focus to the figure instead of the manager
self.canvas.setFocusPolicy( qt.QWidget.ClickFocus )
self.canvas.setFocus()
self.window.setCaption( "Figure %d" % num )
self.set_window_title( "Figure %d" % num )

self.window._destroying = False

Expand Down Expand Up @@ -293,6 +293,9 @@ def destroy( self, *args ):
if DEBUG: print("destroy figure manager")
self.window.close(True)

def get_window_title(self):
return str(self.window.caption())

def set_window_title(self, title):
self.window.setCaption(title)

Expand Down Expand Up @@ -420,7 +423,7 @@ def save_figure(self, *args):
sorted_filetypes.sort()
default_filetype = self.canvas.get_default_filetype()

start = "image." + default_filetype
start = self.canvas.get_default_filename()
filters = []
selectedFilter = None
for name, exts in sorted_filetypes:
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/backends/backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ def destroy( self, *args ):
if DEBUG: print("destroy figure manager")
self.window.close()

def get_window_title(self):
return str(self.window.windowTitle())

def set_window_title(self, title):
self.window.setWindowTitle(title)

Expand Down Expand Up @@ -615,7 +618,7 @@ def save_figure(self, *args):
sorted_filetypes.sort()
default_filetype = self.canvas.get_default_filetype()

start = "image." + default_filetype
start = self.canvas.get_default_filename()
filters = []
selectedFilter = None
for name, exts in sorted_filetypes:
Expand Down
8 changes: 6 additions & 2 deletions lib/matplotlib/backends/backend_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def __init__(self, canvas, num, window):
FigureManagerBase.__init__(self, canvas, num)
self.window = window
self.window.withdraw()
self.window.wm_title("Figure %d" % num)
self.set_window_title("Figure %d" % num)
self.canvas = canvas
self._num = num
_, _, w, h = canvas.figure.bbox.bounds
Expand Down Expand Up @@ -565,6 +565,9 @@ def destroy(self, *args):
self.window.quit()
self.window = None

def get_window_title(self):
return self.window.wm_title()

def set_window_title(self, title):
self.window.wm_title(title)

Expand Down Expand Up @@ -874,7 +877,8 @@ def save_figure(self, *args):
master=self.window,
title='Save the figure',
filetypes = tk_filetypes,
defaultextension = defaultextension
defaultextension = defaultextension,
initialfile=self.canvas.get_default_filename(),
)

if fname == "" or fname == ():
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,9 @@ def destroy(self, *args):
#wx.GetApp().ProcessIdle()
wx.WakeUpIdle()

def get_window_title(self):
return self.window.GetTitle()

def set_window_title(self, title):
self.window.SetTitle(title)

Expand Down Expand Up @@ -1840,7 +1843,7 @@ def configure_subplots(self, evt):
def save_figure(self, *args):
# Fetch the required filename and file type.
filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards()
default_file = "image." + self.canvas.get_default_filetype()
default_file = self.canvas.get_default_filename()
dlg = wx.FileDialog(self._parent, "Save to file", "", default_file,
filetypes,
wx.SAVE|wx.OVERWRITE_PROMPT)
Expand Down
70 changes: 69 additions & 1 deletion src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -3827,6 +3827,56 @@ static void _data_provider_release(void* info, const void* data, size_t size)
return Py_None;
}

static PyObject*
FigureManager_set_window_title(FigureManager* self,
PyObject *args, PyObject *kwds)
{
char* title;
if(!PyArg_ParseTuple(args, "es", "UTF-8", &title))
return NULL;

Window* window = self->window;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* ns_title = [[NSString alloc]
initWithCString: title
encoding: NSUTF8StringEncoding];
[window setTitle: ns_title];
[pool release];
}
PyMem_Free(title);
Py_INCREF(Py_None);
return Py_None;
}

static PyObject*
FigureManager_get_window_title(FigureManager* self)
{
Window* window = self->window;
PyObject* result = NULL;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* title = [window title];
if (title) {
const char* cTitle = [title UTF8String];
#if PY3K || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
result = PyUnicode_FromString(cTitle);
#else
result = PyString_FromString(cTitle);
#endif
}
[pool release];
}
if (result) {
return result;
} else {
Py_INCREF(Py_None);
return Py_None;
}
}

static PyMethodDef FigureManager_methods[] = {
{"show",
(PyCFunction)FigureManager_show,
Expand All @@ -3838,6 +3888,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
METH_NOARGS,
"Closes the window associated with the figure manager."
},
{"set_window_title",
(PyCFunction)FigureManager_set_window_title,
METH_VARARGS,
"Sets the title of the window associated with the figure manager."
},
{"get_window_title",
(PyCFunction)FigureManager_get_window_title,
METH_NOARGS,
"Returns the title of the window associated with the figure manager."
},
{NULL} /* Sentinel */
};

Expand Down Expand Up @@ -4806,11 +4866,19 @@ -(void)save_figure:(id)sender
{
int result;
const char* title;
if(!PyArg_ParseTuple(args, "s", &title)) return NULL;
char* default_filename;
if(!PyArg_ParseTuple(args, "ses", &title, "UTF-8", &default_filename))
return NULL;

NSSavePanel* panel = [NSSavePanel savePanel];
[panel setTitle: [NSString stringWithCString: title
encoding: NSASCIIStringEncoding]];
NSString* ns_default_filename =
[[NSString alloc]
initWithCString: default_filename
encoding: NSUTF8StringEncoding];
PyMem_Free(default_filename);
[panel setNameFieldStringValue: ns_default_filename];
result = [panel runModal];
if (result == NSOKButton)
{
Expand Down