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

Skip to content

Commit 4f75af3

Browse files
committed
Merge pull request #908 from mspacek/default_filename
use window title as default savefig filename
2 parents 5b90a27 + 23deb00 commit 4f75af3

9 files changed

Lines changed: 131 additions & 14 deletions

File tree

lib/matplotlib/backend_bases.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,14 @@ def get_default_filetype(self):
20922092
"""
20932093
return rcParams['savefig.format']
20942094

2095+
def get_window_title(self):
2096+
"""
2097+
Get the title text of the window containing the figure.
2098+
Return None if there is no window (eg, a PS backend).
2099+
"""
2100+
if hasattr(self, "manager"):
2101+
return self.manager.get_window_title()
2102+
20952103
def set_window_title(self, title):
20962104
"""
20972105
Set the title text of the window containing the figure. Note that
@@ -2100,6 +2108,15 @@ def set_window_title(self, title):
21002108
if hasattr(self, "manager"):
21012109
self.manager.set_window_title(title)
21022110

2111+
def get_default_filename(self):
2112+
"""
2113+
Return a string, which includes extension, suitable for use as
2114+
a default filename.
2115+
"""
2116+
default_filename = self.get_window_title() or 'image'
2117+
default_filename = default_filename.lower().replace(' ', '_')
2118+
return default_filename + '.' + self.get_default_filetype()
2119+
21032120
def switch_backends(self, FigureCanvasClass):
21042121
"""
21052122
Instantiate an instance of FigureCanvasClass
@@ -2413,10 +2430,17 @@ def show_popup(self, msg):
24132430
"""
24142431
pass
24152432

2433+
def get_window_title(self):
2434+
"""
2435+
Get the title text of the window containing the figure.
2436+
Return None for non-GUI backends (eg, a PS backend).
2437+
"""
2438+
return 'image'
2439+
24162440
def set_window_title(self, title):
24172441
"""
24182442
Set the title text of the window containing the figure. Note that
2419-
this has no effect if there is no window (eg, a PS backend).
2443+
this has no effect for non-GUI backends (eg, a PS backend).
24202444
"""
24212445
pass
24222446

lib/matplotlib/backends/backend_gtk.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def __init__(self, canvas, num):
529529
FigureManagerBase.__init__(self, canvas, num)
530530

531531
self.window = gtk.Window()
532-
self.window.set_title("Figure %d" % num)
532+
self.set_window_title("Figure %d" % num)
533533
if (window_icon):
534534
try:
535535
self.window.set_icon_from_file(window_icon)
@@ -620,6 +620,9 @@ def _get_toolbar(self, canvas):
620620
toolbar = None
621621
return toolbar
622622

623+
def get_window_title(self):
624+
return self.window.get_title()
625+
623626
def set_window_title(self, title):
624627
self.window.set_title(title)
625628

@@ -725,11 +728,13 @@ def _init_toolbar2_4(self):
725728
self.show_all()
726729

727730
def get_filechooser(self):
728-
return FileChooserDialog(
731+
fc = FileChooserDialog(
729732
title='Save the figure',
730733
parent=self.win,
731734
filetypes=self.canvas.get_supported_filetypes(),
732735
default_filetype=self.canvas.get_default_filetype())
736+
fc.set_current_name(self.canvas.get_default_filename())
737+
return fc
733738

734739
def save_figure(self, *args):
735740
fname, format = self.get_filechooser().get_filename_from_user()

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def __init__(self, canvas, num):
358358
FigureManagerBase.__init__(self, canvas, num)
359359

360360
self.window = Gtk.Window()
361-
self.window.set_title("Figure %d" % num)
361+
self.set_window_title("Figure %d" % num)
362362
if (window_icon):
363363
try:
364364
self.window.set_icon_from_file(window_icon)
@@ -447,6 +447,9 @@ def _get_toolbar(self, canvas):
447447
toolbar = None
448448
return toolbar
449449

450+
def get_window_title(self):
451+
return self.window.get_title()
452+
450453
def set_window_title(self, title):
451454
self.window.set_title(title)
452455

@@ -532,11 +535,13 @@ def _init_toolbar(self):
532535
self.show_all()
533536

534537
def get_filechooser(self):
535-
return FileChooserDialog(
538+
fc = FileChooserDialog(
536539
title='Save the figure',
537540
parent=self.win,
538541
filetypes=self.canvas.get_supported_filetypes(),
539542
default_filetype=self.canvas.get_default_filetype())
543+
fc.set_current_name(self.canvas.get_default_filename())
544+
return fc
540545

541546
def save_figure(self, *args):
542547
fname, format = self.get_filechooser().get_filename_from_user()

lib/matplotlib/backends/backend_macosx.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ def zoomy(self, direction):
444444
self.canvas.invalidate()
445445

446446
def save_figure(self, *args):
447-
filename = _macosx.choose_save_file('Save the figure')
447+
filename = _macosx.choose_save_file('Save the figure',
448+
self.canvas.get_default_filename())
448449
if filename is None: # Cancel
449450
return
450451
self.canvas.print_figure(filename)
@@ -469,7 +470,8 @@ def set_cursor(self, cursor):
469470
_macosx.set_cursor(cursor)
470471

471472
def save_figure(self, *args):
472-
filename = _macosx.choose_save_file('Save the figure')
473+
filename = _macosx.choose_save_file('Save the figure',
474+
self.canvas.get_default_filename())
473475
if filename is None: # Cancel
474476
return
475477
self.canvas.print_figure(filename)

lib/matplotlib/backends/backend_qt.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def __init__( self, canvas, num ):
219219
# Give the keyboard focus to the figure instead of the manager
220220
self.canvas.setFocusPolicy( qt.QWidget.ClickFocus )
221221
self.canvas.setFocus()
222-
self.window.setCaption( "Figure %d" % num )
222+
self.set_window_title( "Figure %d" % num )
223223

224224
self.window._destroying = False
225225

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

296+
def get_window_title(self):
297+
return str(self.window.caption())
298+
296299
def set_window_title(self, title):
297300
self.window.setCaption(title)
298301

@@ -420,7 +423,7 @@ def save_figure(self, *args):
420423
sorted_filetypes.sort()
421424
default_filetype = self.canvas.get_default_filetype()
422425

423-
start = "image." + default_filetype
426+
start = self.canvas.get_default_filename()
424427
filters = []
425428
selectedFilter = None
426429
for name, exts in sorted_filetypes:

lib/matplotlib/backends/backend_qt4.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,9 @@ def destroy( self, *args ):
481481
if DEBUG: print("destroy figure manager")
482482
self.window.close()
483483

484+
def get_window_title(self):
485+
return str(self.window.windowTitle())
486+
484487
def set_window_title(self, title):
485488
self.window.setWindowTitle(title)
486489

@@ -615,7 +618,7 @@ def save_figure(self, *args):
615618
sorted_filetypes.sort()
616619
default_filetype = self.canvas.get_default_filetype()
617620

618-
start = "image." + default_filetype
621+
start = self.canvas.get_default_filename()
619622
filters = []
620623
selectedFilter = None
621624
for name, exts in sorted_filetypes:

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def __init__(self, canvas, num, window):
494494
FigureManagerBase.__init__(self, canvas, num)
495495
self.window = window
496496
self.window.withdraw()
497-
self.window.wm_title("Figure %d" % num)
497+
self.set_window_title("Figure %d" % num)
498498
self.canvas = canvas
499499
self._num = num
500500
_, _, w, h = canvas.figure.bbox.bounds
@@ -565,6 +565,9 @@ def destroy(self, *args):
565565
self.window.quit()
566566
self.window = None
567567

568+
def get_window_title(self):
569+
return self.window.wm_title()
570+
568571
def set_window_title(self, title):
569572
self.window.wm_title(title)
570573

@@ -874,7 +877,8 @@ def save_figure(self, *args):
874877
master=self.window,
875878
title='Save the figure',
876879
filetypes = tk_filetypes,
877-
defaultextension = defaultextension
880+
defaultextension = defaultextension,
881+
initialfile=self.canvas.get_default_filename(),
878882
)
879883

880884
if fname == "" or fname == ():

lib/matplotlib/backends/backend_wx.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,9 @@ def destroy(self, *args):
16031603
#wx.GetApp().ProcessIdle()
16041604
wx.WakeUpIdle()
16051605

1606+
def get_window_title(self):
1607+
return self.window.GetTitle()
1608+
16061609
def set_window_title(self, title):
16071610
self.window.SetTitle(title)
16081611

@@ -1840,7 +1843,7 @@ def configure_subplots(self, evt):
18401843
def save_figure(self, *args):
18411844
# Fetch the required filename and file type.
18421845
filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards()
1843-
default_file = "image." + self.canvas.get_default_filetype()
1846+
default_file = self.canvas.get_default_filename()
18441847
dlg = wx.FileDialog(self._parent, "Save to file", "", default_file,
18451848
filetypes,
18461849
wx.SAVE|wx.OVERWRITE_PROMPT)

src/_macosx.m

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3827,6 +3827,56 @@ static void _data_provider_release(void* info, const void* data, size_t size)
38273827
return Py_None;
38283828
}
38293829

3830+
static PyObject*
3831+
FigureManager_set_window_title(FigureManager* self,
3832+
PyObject *args, PyObject *kwds)
3833+
{
3834+
char* title;
3835+
if(!PyArg_ParseTuple(args, "es", "UTF-8", &title))
3836+
return NULL;
3837+
3838+
Window* window = self->window;
3839+
if(window)
3840+
{
3841+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3842+
NSString* ns_title = [[NSString alloc]
3843+
initWithCString: title
3844+
encoding: NSUTF8StringEncoding];
3845+
[window setTitle: ns_title];
3846+
[pool release];
3847+
}
3848+
PyMem_Free(title);
3849+
Py_INCREF(Py_None);
3850+
return Py_None;
3851+
}
3852+
3853+
static PyObject*
3854+
FigureManager_get_window_title(FigureManager* self)
3855+
{
3856+
Window* window = self->window;
3857+
PyObject* result = NULL;
3858+
if(window)
3859+
{
3860+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3861+
NSString* title = [window title];
3862+
if (title) {
3863+
const char* cTitle = [title UTF8String];
3864+
#if PY3K || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
3865+
result = PyUnicode_FromString(cTitle);
3866+
#else
3867+
result = PyString_FromString(cTitle);
3868+
#endif
3869+
}
3870+
[pool release];
3871+
}
3872+
if (result) {
3873+
return result;
3874+
} else {
3875+
Py_INCREF(Py_None);
3876+
return Py_None;
3877+
}
3878+
}
3879+
38303880
static PyMethodDef FigureManager_methods[] = {
38313881
{"show",
38323882
(PyCFunction)FigureManager_show,
@@ -3838,6 +3888,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
38383888
METH_NOARGS,
38393889
"Closes the window associated with the figure manager."
38403890
},
3891+
{"set_window_title",
3892+
(PyCFunction)FigureManager_set_window_title,
3893+
METH_VARARGS,
3894+
"Sets the title of the window associated with the figure manager."
3895+
},
3896+
{"get_window_title",
3897+
(PyCFunction)FigureManager_get_window_title,
3898+
METH_NOARGS,
3899+
"Returns the title of the window associated with the figure manager."
3900+
},
38413901
{NULL} /* Sentinel */
38423902
};
38433903

@@ -4806,11 +4866,19 @@ -(void)save_figure:(id)sender
48064866
{
48074867
int result;
48084868
const char* title;
4809-
if(!PyArg_ParseTuple(args, "s", &title)) return NULL;
4869+
char* default_filename;
4870+
if(!PyArg_ParseTuple(args, "ses", &title, "UTF-8", &default_filename))
4871+
return NULL;
48104872

48114873
NSSavePanel* panel = [NSSavePanel savePanel];
48124874
[panel setTitle: [NSString stringWithCString: title
48134875
encoding: NSASCIIStringEncoding]];
4876+
NSString* ns_default_filename =
4877+
[[NSString alloc]
4878+
initWithCString: default_filename
4879+
encoding: NSUTF8StringEncoding];
4880+
PyMem_Free(default_filename);
4881+
[panel setNameFieldStringValue: ns_default_filename];
48144882
result = [panel runModal];
48154883
if (result == NSOKButton)
48164884
{

0 commit comments

Comments
 (0)