From 98fae3cc0371b68a6ac8a5a03d8a8e8722f28125 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Thu, 24 Jun 2021 12:19:14 +0200 Subject: [PATCH 1/4] Fix file chooser dialog on recipe export --- src/gourmet/gtk_extras/dialog_extras.py | 49 +++++++++---------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/src/gourmet/gtk_extras/dialog_extras.py b/src/gourmet/gtk_extras/dialog_extras.py index 0eb70e01..30eaf090 100644 --- a/src/gourmet/gtk_extras/dialog_extras.py +++ b/src/gourmet/gtk_extras/dialog_extras.py @@ -917,14 +917,11 @@ def saveas_file(title: str, buttons=buttons, show_filetype=show_filetype, parent=parent) - # Get the filename, and the extension of the chosen filetype menu - filename, *extension = sfd.run() - extension = ''.join(extension) - - # TODO: it would be cleaner to return the extension, rather than the - # output file type description - export_type = sfd.ext_to_filter[extension].get_name() - return filename, export_type + try: + filename, export_type = sfd.run() + return filename, export_type + except ValueError: + return None, None def get_type_for_filters(fname, filters): @@ -1091,32 +1088,20 @@ def is_extension_legal(self, filenames: List[str]) -> bool: return False def run(self) -> List[str]: - """Run our dialog and return the filename(s)""" + """Return a list of filenames. + + If saving files, the file type is also returned as the last entry in + the list. + In that case, it is assumed that all file are of the same type. + """ response = self.fsd.run() + filenames: List[str] = self.fsd.get_filenames() if response == Gtk.ResponseType.OK: - if self.multiple: - fn = self.fsd.get_filenames() - else: - fn = [self.fsd.get_filename()] - if not fn: - show_message(label=_('No file selected'), - sublabel=_( - 'No file was selected, so the action has been cancelled') - ) - return [] - if self.action == Gtk.FileChooserAction.SAVE: - # add the extension if need be... - if self.do_saveas and not self.is_extension_legal(fn): - if self.fsd.get_filter().get_name() in self.name_to_ext: - add_ext = self.name_to_ext[self.fsd.get_filter( - ).get_name()] - if add_ext: - fn += add_ext - self.quit() - return fn - else: - self.quit() - return [] + if self.action == Gtk.FileChooserAction.SAVE and self.do_saveas: + export_type = self.fsd.get_filter().get_name() + filenames.append(export_type) + self.quit() + return filenames def quit(self, *args): if hasattr(self, 'timeout'): From fc8518f97b65a573727f619fca634785c3a9a705 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Thu, 24 Jun 2021 14:17:36 +0200 Subject: [PATCH 2/4] Adapt FileSelectorDialog to work with images --- src/gourmet/gtk_extras/dialog_extras.py | 20 ++++++++++++++++---- src/gourmet/reccard.py | 8 +++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/gourmet/gtk_extras/dialog_extras.py b/src/gourmet/gtk_extras/dialog_extras.py index 30eaf090..b156a179 100644 --- a/src/gourmet/gtk_extras/dialog_extras.py +++ b/src/gourmet/gtk_extras/dialog_extras.py @@ -920,7 +920,7 @@ def saveas_file(title: str, try: filename, export_type = sfd.run() return filename, export_type - except ValueError: + except TypeError: return None, None @@ -938,10 +938,18 @@ def get_type_for_filters(fname, filters): def select_image(title, filename=None, action=Gtk.FileChooserAction.OPEN, - buttons=None): + buttons=None) -> Optional[Path]: sfd = ImageSelectorDialog(title, filename=filename, action=action, buttons=buttons) - return sfd.run() + filename = sfd.run() + if not filename: + return + + filename = Path(filename[-1]) + if not filename.is_file(): + return + + return filename class FileSelectorDialog: @@ -1095,11 +1103,15 @@ def run(self) -> List[str]: In that case, it is assumed that all file are of the same type. """ response = self.fsd.run() - filenames: List[str] = self.fsd.get_filenames() + if response == Gtk.ResponseType.OK: + filenames = self.fsd.get_filenames() if self.action == Gtk.FileChooserAction.SAVE and self.do_saveas: export_type = self.fsd.get_filter().get_name() filenames.append(export_type) + else: # response == Gtk.ResponseType.CANCEL: + filenames = None + self.quit() return filenames diff --git a/src/gourmet/reccard.py b/src/gourmet/reccard.py index 0a12babc..628bca68 100644 --- a/src/gourmet/reccard.py +++ b/src/gourmet/reccard.py @@ -1550,12 +1550,10 @@ def set_from_file (self, filename: str): self.draw_image() def set_from_fileCB(self, widget: Gtk.Button): - filenames = de.select_image("Select Image", + filename = de.select_image("Select Image", action=Gtk.FileChooserAction.OPEN) - if filenames: - fname, *_ = filenames - fname = Path(fname) - Undo.UndoableObject(lambda *args: self.set_from_file(fname), + if filename is not None: + Undo.UndoableObject(lambda *args: self.set_from_file(filename), lambda *args: self.remove_image(), self.rc.history, widget=self.imageW).perform() From 3ecf55e14981a5a1399e1a13b765d772c7af4bae Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Thu, 24 Jun 2021 14:18:02 +0200 Subject: [PATCH 3/4] Remove unused is_extension_legal function --- src/gourmet/gtk_extras/dialog_extras.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/gourmet/gtk_extras/dialog_extras.py b/src/gourmet/gtk_extras/dialog_extras.py index b156a179..66224ece 100644 --- a/src/gourmet/gtk_extras/dialog_extras.py +++ b/src/gourmet/gtk_extras/dialog_extras.py @@ -1086,16 +1086,7 @@ def change_file_extension(self, fsd, data: GObject.GParamSpec): else: self.fsd.set_current_name(stem) - def is_extension_legal(self, filenames: List[str]) -> bool: - if filenames: - for extension in self.extensions: - if not extension: - extension = "" - if fnmatch.fnmatch(filenames[0], extension): - return True - return False - - def run(self) -> List[str]: + def run(self) -> Optional[List[str]]: """Return a list of filenames. If saving files, the file type is also returned as the last entry in From fcb65db05a1cb225d9e1a24d2dc36dcd4729ea76 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Thu, 24 Jun 2021 14:24:23 +0200 Subject: [PATCH 4/4] Adapt FileSelectorDialog to work with file imports --- src/gourmet/gtk_extras/dialog_extras.py | 4 +++- src/gourmet/importers/importManager.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/gourmet/gtk_extras/dialog_extras.py b/src/gourmet/gtk_extras/dialog_extras.py index 66224ece..36075cff 100644 --- a/src/gourmet/gtk_extras/dialog_extras.py +++ b/src/gourmet/gtk_extras/dialog_extras.py @@ -880,7 +880,7 @@ def show_message(*args, **kwargs): def select_file(title, filename=None, - filters=[], + filters=None, # filters are lists of a name, a list of mime types and a list of # patterns ['Plain Text', ['text/plain'], '*txt'] action=Gtk.FileChooserAction.OPEN, @@ -889,6 +889,8 @@ def select_file(title, buttons=None, parent=None ): + if filters is None: + filters = [] sfd = FileSelectorDialog(title, filename=filename, filters=filters, diff --git a/src/gourmet/importers/importManager.py b/src/gourmet/importers/importManager.py index 6b722d94..5704775d 100644 --- a/src/gourmet/importers/importManager.py +++ b/src/gourmet/importers/importManager.py @@ -127,7 +127,7 @@ def offer_import(self, parent: Optional[Gtk.Window] = None): filters=self.get_filters(), parent=parent, select_multiple=True) - if not filenames: + if filenames is None: return self.import_filenames(filenames)