Skip to content

Commit

Permalink
Create ImageBox.thumbnail when ImageBox.image is being set (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
cydanil authored Jul 4, 2021
2 parents 38307bd + 387d313 commit e8110e3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/gourmet/reccard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1459,17 +1459,20 @@ 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')
self.image: Image.Image = None
self.thumbnail: Image.Image = None

def get_image(self, rec: Optional['RowProxy'] = None):
"""Set image based on current recipe."""
Expand Down Expand Up @@ -1506,12 +1509,12 @@ 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:
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
Expand All @@ -1531,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):
Expand All @@ -1550,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
Expand Down

0 comments on commit e8110e3

Please sign in to comment.