From bc5b546af38e675274ae1a04d988770bae78162f Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Mon, 5 Jul 2021 00:57:22 +0200 Subject: [PATCH 1/2] Do small ImageBox refactor Correct ImageBox.commit type hinting; reformat ImageBox.__init__ --- src/gourmet/reccard.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/gourmet/reccard.py b/src/gourmet/reccard.py index 18fb5c68..7eb75d33 100644 --- a/src/gourmet/reccard.py +++ b/src/gourmet/reccard.py @@ -3,7 +3,7 @@ import webbrowser import xml.sax.saxutils from pkgutil import get_data -from typing import Any, Callable, Dict, List, Optional, Tuple +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from gi.repository import Gdk, GdkPixbuf, GLib, GObject, Gtk, Pango from PIL import Image @@ -1459,13 +1459,15 @@ def save(self, recdic): self.emit('saved') return recdic -class ImageBox: # used in DescriptionEditor for recipe image. - def __init__ (self, RecCard): + +class ImageBox: + """A widget for handling images in the DescriptionEditor.""" + def __init__(self, rec_card): debug("__init__ (self, RecCard):",5) self.edited = False - self.rg = RecCard.rg - self.rc = RecCard - self.ui = self.rc.ui + self.rc = rec_card + self.rg = rec_card.rg + self.ui = rec_card.ui self.imageW = self.ui.get_object('recImage') self.addW = self.ui.get_object('addImage') self.delW = self.ui.get_object('delImageButton') @@ -1506,7 +1508,7 @@ def hide (self): self.addW.show() return True - def commit(self) -> Optional[Tuple[bytes, bytes]]: + def commit(self) -> Union[Tuple[bytes, bytes], Tuple[None, None]]: """Return image and thumbnail data for storage in the database.""" debug("commit (self):", 5) if self.image: From 387d313ced08d87f0fbfeef7049d81ceacb2454e Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Mon, 5 Jul 2021 00:58:07 +0200 Subject: [PATCH 2/2] Create ImageBox thumbnail when initializing self.image --- src/gourmet/reccard.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/gourmet/reccard.py b/src/gourmet/reccard.py index 7eb75d33..3ddf0010 100644 --- a/src/gourmet/reccard.py +++ b/src/gourmet/reccard.py @@ -1472,6 +1472,7 @@ def __init__(self, rec_card): self.addW = self.ui.get_object('addImage') self.delW = self.ui.get_object('delImageButton') self.image: Image.Image = None + self.thumbnail: Image.Image = None def get_image(self, rec: Optional['RowProxy'] = None): """Set image based on current recipe.""" @@ -1513,7 +1514,7 @@ def commit(self) -> Union[Tuple[bytes, bytes], Tuple[None, None]]: debug("commit (self):", 5) if self.image: self.imageW.show() - return iu.image_to_bytes(self.image), iu.image_to_bytes(self.thumb) + return iu.image_to_bytes(self.image), iu.image_to_bytes(self.thumbnail) else: self.imageW.hide() return None, None @@ -1533,8 +1534,6 @@ def draw_image(self): size = (100, 100) self.image.thumbnail(size) - self.thumb = self.image.copy() - self.thumb.thumbnail((40, 40)) self.set_from_bytes(iu.image_to_bytes(self.image)) def show_image (self): @@ -1552,6 +1551,8 @@ def set_from_bytes(self, bytes_: bytes): self.orig_pixbuf = pb self.image = iu.bytes_to_image(bytes_) + self.thumbnail = self.image.copy() + self.thumbnail.thumbnail((40, 40)) self.show_image() self.edited = True