From 8e975bd5c9eb8b01639caa101c27e42749090383 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Wed, 26 Jun 2013 15:27:41 -0300 Subject: [PATCH 1/2] Add a method to create a Pixbuf with the preview in the metadata This code was moved from sugar expandedentry.py to be available to use in other places. Some activities like Portfolio or JournalShare have this code copied. Signed-off-by: Gonzalo Odiard --- src/sugar3/graphics/objectchooser.py | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/sugar3/graphics/objectchooser.py b/src/sugar3/graphics/objectchooser.py index 3f74c5cd..54fd9653 100644 --- a/src/sugar3/graphics/objectchooser.py +++ b/src/sugar3/graphics/objectchooser.py @@ -20,6 +20,8 @@ STABLE. """ import logging +import StringIO +import cairo from gi.repository import GObject from gi.repository import Gtk @@ -27,6 +29,7 @@ from gi.repository import Gdk import dbus from sugar3.datastore import datastore +from sugar3.activity.activity import PREVIEW_SIZE J_DBUS_SERVICE = 'org.laptop.Journal' @@ -38,6 +41,63 @@ FILTER_TYPE_GENERIC_MIME = 'generic_mime' FILTER_TYPE_ACTIVITY = 'activity' +def get_preview_pixbuf(preview_data, width=-1, height=-1): + """Retrive a pixbuf with the content of the preview field + + Keyword arguments: + metadata -- the metadata dictionary. + Can't be None, use metadata.get('preview', '') + width -- the pixbuf width, if is not set, the default width will be used + height -- the pixbuf width, if is not set, the default height will be used + + Return: a Pixbuf or None if couldn't create it + + """ + if width == -1: + width = PREVIEW_SIZE[0] + + if height == -1: + height = PREVIEW_SIZE[1] + + pixbuf = None + + if len(preview_data) > 4: + if preview_data[1:4] != 'PNG': + # TODO: We are close to be able to drop this. + import base64 + preview_data = base64.b64decode(preview_data) + + png_file = StringIO.StringIO(preview_data) + try: + # Load image and scale to dimensions + surface = cairo.ImageSurface.create_from_png(png_file) + png_width = surface.get_width() + png_height = surface.get_height() + + preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, + width, height) + cr = cairo.Context(preview_surface) + + scale_w = width * 1.0 / png_width + scale_h = height * 1.0 / png_height + scale = min(scale_w, scale_h) + + cr.scale(scale, scale) + + cr.set_source_rgba(1, 1, 1, 0) + cr.set_operator(cairo.OPERATOR_SOURCE) + cr.paint() + cr.set_source_surface(surface) + cr.paint() + + pixbuf = Gdk.pixbuf_get_from_surface(preview_surface, 0, 0, + width, height) + except Exception: + logging.exception('Error while loading the preview') + + return pixbuf + + class ObjectChooser(object): def __init__(self, parent=None, what_filter=None, filter_type=None): From 3e6507af6e1e1d65ec2c78bbacd1ecd20bfd6df6 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Thu, 4 Jul 2013 12:31:34 -0300 Subject: [PATCH 2/2] Add a option to the objectchooser to show the object preview Signed-off-by: Gonzalo Odiard --- src/sugar3/graphics/objectchooser.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/sugar3/graphics/objectchooser.py b/src/sugar3/graphics/objectchooser.py index 54fd9653..edf4b00e 100644 --- a/src/sugar3/graphics/objectchooser.py +++ b/src/sugar3/graphics/objectchooser.py @@ -100,7 +100,8 @@ def get_preview_pixbuf(preview_data, width=-1, height=-1): class ObjectChooser(object): - def __init__(self, parent=None, what_filter=None, filter_type=None): + def __init__(self, parent=None, what_filter=None, filter_type=None, + show_preview=False): """Initialise the ObjectChoser parent -- the widget calling ObjectChooser @@ -134,6 +135,12 @@ class ObjectChooser(object): chooser will filter based on the 'mime_type' metadata field and the mime types defined by the activity in the activity.info file. + + show_preview -- boolean + + if True will show the preview image asociated with + the object in the Journal. This option is only available + if filter_type is selected. """ if parent is None: @@ -145,6 +152,7 @@ class ObjectChooser(object): parent_xid = parent self._parent_xid = parent_xid + self._show_preview = show_preview self._main_loop = None self._object_id = None self._bus = None @@ -190,7 +198,8 @@ class ObjectChooser(object): self._parent_xid, what_filter) else: self._chooser_id = journal.ChooseObjectWithFilter( - self._parent_xid, what_filter, self._filter_type) + self._parent_xid, what_filter, self._filter_type, + self._show_preview) Gdk.threads_leave() try: