Skip to content

Commit

Permalink
merge Improve ImageBrowserDialog (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
cydanil authored Jun 27, 2021
2 parents dce85c8 + 40a888a commit ab2c4cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
41 changes: 31 additions & 10 deletions src/gourmet/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from enum import Enum
from pathlib import Path
from pkgutil import get_data
from threading import Event, Thread
from typing import Dict, List, Optional
from urllib.parse import unquote, urlparse

Expand Down Expand Up @@ -121,34 +122,54 @@ def image_to_pixbuf(image: Image.Image) -> Pixbuf:

class ImageBrowser(Gtk.Dialog):
def __init__(self, parent: Gtk.Window, uris: List[str]):
"""Retrieve the images from the uris and let user select one.
Image retrieval is done in another thread which is cancelled when the
dialog is destroyed, if not completed already.
"""
Gtk.Dialog.__init__(self, title="Choose an image",
transient_for=parent, flags=0)
self.set_default_size(600, 600)

self.image: Image.Image = None

self.image: Optional[Image.Image] = None
self.liststore = Gtk.ListStore(GdkPixbuf.Pixbuf)

iconview = Gtk.IconView.new()
iconview.set_model(self.liststore)
iconview.set_pixbuf_column(0)
iconview.connect('selection-changed', self.on_selection)

scrollable = Gtk.ScrolledWindow()
scrollable.set_vexpand(True)
scrollable.add(iconview)

box = self.get_content_area()
box.pack_end(scrollable, True, True, 0)
self.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK)
self.show_all()

self._stop_retrieval = Event()
self._image_retrieve_task = Thread(target=self._load_uris, args=[uris])
self._image_retrieve_task.start()

def _load_uris(self, uris: List[str]):
for uri in uris:
if self._stop_retrieval.is_set():
return # Cancel retrieval of image as user selection is done.
image = make_thumbnail(uri, ThumbnailSize.SMALL)
if image is None:
continue
pixbuf = bytes_to_pixbuf(image_to_bytes(image))
self.liststore.append([pixbuf])

iconview.connect('selection-changed', self.on_selection)

box = self.get_content_area()
box.pack_end(iconview, True, True, 0)
self.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK)
self.show_all()

def on_selection(self, iconview: Gtk.IconView):
item = iconview.get_selected_items()
if item:
itr = self.liststore.get_iter(item[0])
self.image = pixbuf_to_image(self.liststore.get_value(itr, 0))

def destroy(self):
self._stop_retrieval.set()
self._image_retrieve_task.join()
super().destroy()
2 changes: 1 addition & 1 deletion src/gourmet/importers/interactive_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,13 @@ def commit_changes(self):
for rec in self.added_recs:
browser = ImageBrowser(self.w, self.images)
response = browser.run()
browser.destroy()
if response == Gtk.ResponseType.OK:
thumb = browser.image.copy()
thumb.thumbnail((40, 40))
self.rd.modify_rec(rec,
{'image': image_to_bytes(browser.image),
'thumb': image_to_bytes(thumb)})
browser.destroy()

if self.modal:
self.w.hide()
Expand Down

0 comments on commit ab2c4cf

Please sign in to comment.