Skip to content

Commit

Permalink
OWSave: Allow only formats that support sparse when data is sparse; a…
Browse files Browse the repository at this point in the history
…dd and refactor tests
  • Loading branch information
janezd committed Mar 1, 2019
1 parent 546b398 commit 02d49df
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 143 deletions.
29 changes: 23 additions & 6 deletions Orange/widgets/data/owsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ def _replace_extension(filename, extension):
def _extension_from_filter(selected_filter):
return re.search(r".*\(\*?(\..*)\)$", selected_filter).group(1)

def _valid_filters(self):
if self.data is None or not self.data.is_sparse():
return self.filters
else:
return {filt: writer for filt, writer in self.filters.items()
if writer.SUPPORT_SPARSE_DATA}

def _default_valid_filter(self):
if self.data is None or not self.data.is_sparse() \
or self.filters[self.filter].SUPPORT_SPARSE_DATA:
return self.filter
for filt, writer in self.filters.items():
if writer.SUPPORT_SPARSE_DATA:
return filt
# This shouldn't happen and it will trigger an error in tests
return None # pragma: no cover

# As of Qt 5.9, QFileDialog.setDefaultSuffix does not support double
# suffixes, not even in non-native dialogs. We handle each OS separately.
if sys.platform == "darwin":
Expand All @@ -210,13 +227,13 @@ def get_save_filename(self): # pragma: no cover
def no_suffix(filt):
return filt.split("(")[0] + "(*.*)"

mac_filters = {no_suffix(f): f for f in self.filters}
mac_filters = {no_suffix(f): f for f in self._valid_filters()}
filename = self._initial_start_dir()
while True:
dlg = QFileDialog(
None, "Save File", filename, ";;".join(mac_filters))
dlg.setAcceptMode(dlg.AcceptSave)
dlg.selectNameFilter(no_suffix(self.filter))
dlg.selectNameFilter(no_suffix(self._default_valid_filter()))
dlg.setOption(QFileDialog.HideNameFilterDetails)
dlg.setOption(QFileDialog.DontConfirmOverwrite)
if dlg.exec() == QFileDialog.Rejected:
Expand All @@ -232,13 +249,13 @@ def no_suffix(filt):
return filename, selected_filter

elif sys.platform == "win32":
# TODO: This is not tested!!!
# TODO: Behaviour of getSaveFileName on Windows is not tested!!!
# Windows native dialog may work correctly; if not, we may do the same
# as for macOS?
def get_save_filename(self): # pragma: no cover
return QFileDialog.getSaveFileName(
self, "Save File", self._initial_start_dir(),
";;".join(self.filters), self.filter)
";;".join(self._valid_filters()), self._default_valid_filter())

else: # Linux and any unknown platforms
# Qt does not use a native dialog on Linux, so we can connect to
Expand Down Expand Up @@ -271,8 +288,8 @@ def selectFile(self, filename):
def get_save_filename(self):
dlg = self.SaveFileDialog(
None, "Save File", self._initial_start_dir(),
";;".join(self.filters))
dlg.selectNameFilter(self.filter)
";;".join(self._valid_filters()))
dlg.selectNameFilter(self._default_valid_filter())
if dlg.exec() == QFileDialog.Rejected:
return "", ""
else:
Expand Down
Loading

0 comments on commit 02d49df

Please sign in to comment.