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

Skip to content

Default filename backend extensions #1

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 2 commits into from
May 31, 2012
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
14 changes: 13 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2065,8 +2065,11 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',




def get_default_filetype(self):
"""
Return the default file extension for this canvas,
excluding the leading period.
"""
raise NotImplementedError

def get_window_title(self):
Expand All @@ -2085,6 +2088,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.replace(' ', '_')
return default_filename + '.' + self.get_default_filetype()

def switch_backends(self, FigureCanvasClass):
"""
instantiate an instance of FigureCanvasClass
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 @@ -523,7 +523,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 @@ -614,6 +614,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 @@ -732,11 +735,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 @@ -354,7 +354,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 @@ -443,6 +443,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 @@ -541,11 +544,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
7 changes: 5 additions & 2 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,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 @@ -291,6 +291,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 @@ -432,7 +435,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 @@ -454,7 +454,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
t1,t2,w,h = canvas.figure.bbox.bounds
Expand Down Expand Up @@ -530,6 +530,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 @@ -844,7 +847,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 @@ -1596,6 +1596,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 @@ -1859,7 +1862,7 @@ def configure_subplot(self, evt):
def save(self, evt):
# 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