Skip to content

Commit

Permalink
PR: Make the saving of the hydrograph figure more robust (#179)
Browse files Browse the repository at this point in the history
* Encose saving figure in a try

* Some code style changes

* Add a safeguard when wldset is None

* Re-word the docstring

* Improve the dialog options
  • Loading branch information
jnsebgosselin authored Apr 12, 2018
1 parent b21bd0d commit 0e1fcb3
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions gwhat/HydroPrint2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import sys
import os
import os.path as osp

# ---- Third party imports

Expand All @@ -36,6 +37,7 @@
from gwhat.common import QToolButtonNormal, QToolButtonSmall
from gwhat.common import icons
import gwhat.common.widgets as myqt
from gwhat.common.utils import find_unique_filename
from gwhat.projet.reader_waterlvl import load_waterlvl_measures


Expand Down Expand Up @@ -708,21 +710,29 @@ def draw_hydrograph(self):

def select_save_path(self):
"""
Opens a dialog which allows to select a file path where the hydrograph
figure can be saved.
Open a dialog where the user can select a file name to save the
hydrograph.
"""
dialog_dir = os.path.join(self.save_fig_dir,
'hydrograph_%s' % self.wldset['Well'])
if self.wldset is None:
return

dialog = QFileDialog()
fname, ftype = dialog.getSaveFileName(
self, "Save Figure", dialog_dir, '*.pdf;;*.svg')
ftype = ftype.replace('*', '')
ffmat = "*.pdf;;*.svg;;*.png"
fname = find_unique_filename(osp.join(
self.save_fig_dir, 'hydrograph_%s.pdf' % self.wldset['Well']))

fname, ftype = QFileDialog.getSaveFileName(
self, "Save Figure", fname, ffmat)
if fname:
if not fname.endswith(ftype):
fname = fname + ftype
ftype = ftype.replace('*', '')
fname = fname if fname.endswith(ftype) else fname + ftype
self.save_fig_dir = os.path.dirname(fname)
self.save_figure(fname)

try:
self.save_figure(fname)
except PermissionError:
msg = "The file is in use by another application or user."
QMessageBox.warning(self, 'Warning', msg, QMessageBox.Ok)
self.select_save_path()

def save_figure(self, fname):
"""Save the hydrograph figure in a file."""
Expand Down

0 comments on commit 0e1fcb3

Please sign in to comment.