|
9 | 9 |
|
10 | 10 | import logging
|
11 | 11 | import math
|
12 |
| -import os.path |
| 12 | +import pathlib |
13 | 13 | import sys
|
14 | 14 | import weakref
|
15 | 15 |
|
@@ -1374,29 +1374,28 @@ def save_figure(self, *args):
|
1374 | 1374 | # Fetch the required filename and file type.
|
1375 | 1375 | filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards()
|
1376 | 1376 | default_file = self.canvas.get_default_filename()
|
1377 |
| - dlg = wx.FileDialog(self.canvas.GetParent(), |
1378 |
| - "Save to file", "", default_file, filetypes, |
1379 |
| - wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) |
| 1377 | + dlg = wx.FileDialog( |
| 1378 | + self.canvas.GetParent(), "Save to file", |
| 1379 | + rcParams["savefig.directory"], default_file, filetypes, |
| 1380 | + wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) |
1380 | 1381 | dlg.SetFilterIndex(filter_index)
|
1381 | 1382 | if dlg.ShowModal() == wx.ID_OK:
|
1382 |
| - dirname = dlg.GetDirectory() |
1383 |
| - filename = dlg.GetFilename() |
1384 |
| - _log.debug('%s - Save file dir:%s name:%s', |
1385 |
| - self, dirname, filename) |
1386 |
| - format = exts[dlg.GetFilterIndex()] |
1387 |
| - basename, ext = os.path.splitext(filename) |
1388 |
| - if ext.startswith('.'): |
1389 |
| - ext = ext[1:] |
1390 |
| - if ext in ('svg', 'pdf', 'ps', 'eps', 'png') and format != ext: |
| 1383 | + path = pathlib.Path(dlg.GetPath()) |
| 1384 | + _log.debug('%s - Save file path: %s', type(self), path) |
| 1385 | + fmt = exts[dlg.GetFilterIndex()] |
| 1386 | + ext = path.suffix[1:] |
| 1387 | + if ext in self.canvas.get_supported_filetypes() and fmt != ext: |
1391 | 1388 | # looks like they forgot to set the image type drop
|
1392 | 1389 | # down, going with the extension.
|
1393 | 1390 | _log.warning('extension %s did not match the selected '
|
1394 | 1391 | 'image type %s; going with %s',
|
1395 |
| - ext, format, ext) |
1396 |
| - format = ext |
| 1392 | + ext, fmt, ext) |
| 1393 | + fmt = ext |
| 1394 | + # Save dir for next time, unless empty str (which means use cwd). |
| 1395 | + if rcParams["savefig.directory"]: |
| 1396 | + rcParams["savefig.directory"] = str(path.parent) |
1397 | 1397 | try:
|
1398 |
| - self.canvas.figure.savefig( |
1399 |
| - os.path.join(dirname, filename), format=format) |
| 1398 | + self.canvas.figure.savefig(str(path), format=fmt) |
1400 | 1399 | except Exception as e:
|
1401 | 1400 | error_msg_wx(str(e))
|
1402 | 1401 |
|
|
0 commit comments