Skip to content
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

Fix file chooser dialog on recipe export #347

Merged
merged 4 commits into from
Jun 24, 2021
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
78 changes: 34 additions & 44 deletions src/gourmet/gtk_extras/dialog_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -889,6 +889,8 @@ def select_file(title,
buttons=None,
parent=None
):
if filters is None:
filters = []
sfd = FileSelectorDialog(title,
filename=filename,
filters=filters,
Expand Down Expand Up @@ -917,14 +919,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()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list here is explicitly unpacked to catch empty lists (if the export was cancelled) to correctly return Nones to the export manager, cleanly cancelling the action.

return filename, export_type
except TypeError:
return None, None


def get_type_for_filters(fname, filters):
Expand All @@ -941,10 +940,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:
Expand Down Expand Up @@ -1081,42 +1088,25 @@ 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) -> Optional[List[str]]:
"""Return a list of filenames.

def run(self) -> List[str]:
"""Run our dialog and return the filename(s)"""
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()

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 []
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

def quit(self, *args):
if hasattr(self, 'timeout'):
Expand Down
2 changes: 1 addition & 1 deletion src/gourmet/importers/importManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 3 additions & 5 deletions src/gourmet/reccard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down