[Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-24 Thread godiard
From: Gonzalo Odiard 

generates a preview image with a fixed size and proportions.
if the original image is bigger scale down and center the image, if is smaller 
center the image
the supported format files are checked with pixbuf_get_formats
---
 downloadmanager.py |   45 +
 1 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/downloadmanager.py b/downloadmanager.py
index 3eec649..f4bb2a5 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -35,6 +35,7 @@ from sugar.datastore import datastore
 from sugar import profile
 from sugar import mime
 from sugar.graphics.alert import Alert, TimeoutAlert
+from sugar.graphics import style
 from sugar.graphics.icon import Icon
 from sugar.activity import activity
 
@@ -192,12 +193,56 @@ class Download:
 sniffed_mime_type = mime.get_for_file(self._target_file.path)
 self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
 
+if self._check_image_mime_type():
+self.dl_jobject.metadata['preview'] = self._get_preview_image()
+
 datastore.write(self.dl_jobject,
 transfer_ownership=True,
 reply_handler=self._internal_save_cb,
 error_handler=self._internal_save_error_cb,
 timeout=360 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
 
+def _check_image_mime_type(self):
+for pixbuf_format in gtk.gdk.pixbuf_get_formats():
+if self._mime_type in pixbuf_format['mime_types']:
+return True
+return False
+
+def _get_preview_image(self):
+preview_width, preview_height = style.zoom(300), style.zoom(225)
+
+pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
+width, height = pixbuf.get_width(), pixbuf.get_height()
+
+scale = 1
+if (width > preview_width) or (height > preview_height):
+scale_x = preview_width / float(width)
+scale_y = preview_height / float(height)
+scale = min(scale_x, scale_y)
+
+pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, \
+pixbuf.get_has_alpha(), \
+pixbuf.get_bits_per_sample(), \
+preview_width, preview_height)
+pixbuf2.fill(style.COLOR_WHITE.get_int())
+
+margin_x = int((preview_width - (width * scale)) / 2)
+margin_y = int((preview_height - (height * scale)) / 2)
+
+pixbuf.scale(pixbuf2, margin_x, margin_y, \
+preview_width - (margin_x * 2), \
+preview_height - (margin_y * 2), \
+margin_x, margin_y, scale, scale, \
+gtk.gdk.INTERP_BILINEAR)
+
+preview_data = []
+def save_func(buf, data):
+data.append(buf)
+
+pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
+preview_data = ''.join(preview_data)
+return dbus.ByteArray(preview_data)
+
 def __start_response_cb(self, alert, response_id):
 global _active_downloads
 if response_id is gtk.RESPONSE_CANCEL:
-- 
1.7.2.3

___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


Re: [Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-24 Thread Gonzalo Odiard
ignore this patch.
I am doing anything wrong with git

Gonzalo

On Fri, Sep 24, 2010 at 10:44 AM,  wrote:

> From: Gonzalo Odiard 
>
> generates a preview image with a fixed size and proportions.
> if the original image is bigger scale down and center the image, if is
> smaller center the image
> the supported format files are checked with pixbuf_get_formats
>
> this patch adressed comments from silbe about segregate the check of a
> image mime type and use of sugar.style constant colors
> now opens the image in the original size because we don't want scale up
> small images
> ---
>  downloadmanager.py |   46 ++
>  1 files changed, 46 insertions(+), 0 deletions(-)
>
> diff --git a/downloadmanager.py b/downloadmanager.py
> index 3eec649..ed68d82 100644
> --- a/downloadmanager.py
> +++ b/downloadmanager.py
> @@ -35,6 +35,7 @@ from sugar.datastore import datastore
>  from sugar import profile
>  from sugar import mime
>  from sugar.graphics.alert import Alert, TimeoutAlert
> +from sugar.graphics import style
>  from sugar.graphics.icon import Icon
>  from sugar.activity import activity
>
> @@ -192,12 +193,57 @@ class Download:
> sniffed_mime_type =
> mime.get_for_file(self._target_file.path)
> self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
>
> +if self._check_image_mime_type():
> +self.dl_jobject.metadata['preview'] =
> self._get_preview_image()
> +
> datastore.write(self.dl_jobject,
> transfer_ownership=True,
> reply_handler=self._internal_save_cb,
> error_handler=self._internal_save_error_cb,
> timeout=360 *
> DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
>
> +
> +def _check_image_mime_type(self):
> +pixbuf_mime_types = []
> +for pixbuf_format in gtk.gdk.pixbuf_get_formats():
> +pixbuf_mime_types.extend(pixbuf_format['mime_types'])
> +
> +return self._mime_type in pixbuf_mime_types
> +
> +def _get_preview_image(self):
> +preview_width, preview_height = style.zoom(300), style.zoom(225)
> +
> +pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
> +width, height = pixbuf.get_width(), pixbuf.get_height()
> +
> +
> +if (width > preview_width) or (height > preview_height):
> +scale_x = float(width) / preview_width
> +scale_y = float(height) / preview_height
> +scale = max(scale_x,scale_y)
> +
> +pixbuf = pixbuf.scale_simple(float(width) / scale, height /
> scale,
> + gtk.gdk.INTERP_BILINEAR)
> +
> +width, height = pixbuf.get_width(), pixbuf.get_height()
> +
> +pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,
> pixbuf.get_has_alpha(),
> +pixbuf.get_bits_per_sample(), preview_width, preview_height)
> +
> +pixbuf2.fill(style.COLOR_WHITE.get_int())
> +
> +margin_x, margin_y = (preview_width - width) / 2, (preview_height
> - height) / 2
> +
> +pixbuf.copy_area(0, 0, width, height, pixbuf2, margin_x, margin_y)
> +
> +preview_data = []
> +def save_func(buf, data):
> +data.append(buf)
> +
> +pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
> +preview_data = ''.join(preview_data)
> +return dbus.ByteArray(preview_data)
> +
> def __start_response_cb(self, alert, response_id):
> global _active_downloads
> if response_id is gtk.RESPONSE_CANCEL:
> --
> 1.7.2.3
>
>


-- 
Gonzalo Odiard
SugarLabs Argentina
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


[Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-24 Thread godiard
From: Gonzalo Odiard 

generates a preview image with a fixed size and proportions.
if the original image is bigger scale down and center the image, if is smaller 
center the image
the supported format files are checked with pixbuf_get_formats

this patch adressed comments from silbe about segregate the check of a image 
mime type and use of sugar.style constant colors
now opens the image in the original size because we don't want scale up small 
images
---
 downloadmanager.py |   46 ++
 1 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/downloadmanager.py b/downloadmanager.py
index 3eec649..ed68d82 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -35,6 +35,7 @@ from sugar.datastore import datastore
 from sugar import profile
 from sugar import mime
 from sugar.graphics.alert import Alert, TimeoutAlert
+from sugar.graphics import style
 from sugar.graphics.icon import Icon
 from sugar.activity import activity
 
@@ -192,12 +193,57 @@ class Download:
 sniffed_mime_type = mime.get_for_file(self._target_file.path)
 self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
 
+if self._check_image_mime_type():
+self.dl_jobject.metadata['preview'] = self._get_preview_image()
+
 datastore.write(self.dl_jobject,
 transfer_ownership=True,
 reply_handler=self._internal_save_cb,
 error_handler=self._internal_save_error_cb,
 timeout=360 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
 
+
+def _check_image_mime_type(self):
+pixbuf_mime_types = []
+for pixbuf_format in gtk.gdk.pixbuf_get_formats():
+pixbuf_mime_types.extend(pixbuf_format['mime_types'])
+
+return self._mime_type in pixbuf_mime_types
+
+def _get_preview_image(self):
+preview_width, preview_height = style.zoom(300), style.zoom(225)
+
+pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
+width, height = pixbuf.get_width(), pixbuf.get_height()
+
+
+if (width > preview_width) or (height > preview_height):
+scale_x = float(width) / preview_width
+scale_y = float(height) / preview_height
+scale = max(scale_x,scale_y)
+
+pixbuf = pixbuf.scale_simple(float(width) / scale, height / scale,
+ gtk.gdk.INTERP_BILINEAR)
+
+width, height = pixbuf.get_width(), pixbuf.get_height()
+
+pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 
pixbuf.get_has_alpha(),
+pixbuf.get_bits_per_sample(), preview_width, preview_height)
+
+pixbuf2.fill(style.COLOR_WHITE.get_int())
+
+margin_x, margin_y = (preview_width - width) / 2, (preview_height - 
height) / 2
+
+pixbuf.copy_area(0, 0, width, height, pixbuf2, margin_x, margin_y)
+
+preview_data = []
+def save_func(buf, data):
+data.append(buf)
+
+pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
+preview_data = ''.join(preview_data)
+return dbus.ByteArray(preview_data)
+
 def __start_response_cb(self, alert, response_id):
 global _active_downloads
 if response_id is gtk.RESPONSE_CANCEL:
-- 
1.7.2.3

___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


Re: [Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-23 Thread Gonzalo Odiard
> > +if (width > preview_width) or (height > preview_height):
> > +scale_x = float(width) / preview_width
> > +scale_y = float(height) / preview_height
> > +scale = max(scale_x, scale_y)
> > +
> > +pixbuf = pixbuf.scale_simple(float(width) / scale, height /
> scale,
> > + gtk.gdk.INTERP_BILINEAR)
>
>
I guess you meant "width / scale"? scale_simple() expects integers.
>

Yes


> If you use scale(), you can get rid of one pixbuf (currently you use up
> to three).
>
> Ok.



> > +width, height = pixbuf.get_width(), pixbuf.get_height()
>
> > +pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,
> pixbuf.get_has_alpha(),
> > +pixbuf.get_bits_per_sample(), preview_width, preview_height)
>
> This creates a border around an image that's smaller than the preview
> size, thus wasting disk space. Please use width and height instead.
>
>
That isn't true. If the image doesn't have the right proportions, it's stretch
when displayed.


-- 
Gonzalo Odiard
SugarLabs Argentina
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


Re: [Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-23 Thread Sascha Silbe
Excerpts from godiard's message of Thu Sep 23 15:42:15 +0200 2010:

> this patch adressed comments from silbe about segregate the check of a image 
> mime type and use of sugar.style constant colors
> now opens the image in the original size because we don't want scale up small 
> images
> also i did changes sugested by simon

Please adjust summary and description (and add a changelog) as explained
by Tomeu and Simon.

>  
> +

Methods should only be separated by one blank line, not two. Normally I
would recommend running pep8, but I haven't had time to clean up Browse
yet so it's hard to find new complaints among all the previously
existing ones, sorry. You might try redirecting the output to a file
and using comm:

$ git stash save
$ ~/sugar-jhbuild/sugar-jhbuild run pep8 --repeat *.py > pep8-old.log
$ git stash pop
$ ~/sugar-jhbuild/sugar-jhbuild run pep8 --repeat *.py > pep8-new.log
$ comm -3 pep8-old.log pep8-new.log |less

This will report a lot of line number changes, but the first few lines
of the output might be interesting.


> +def _get_preview_image(self):
> +preview_width, preview_height = style.zoom(300), style.zoom(225)

I still think using zoom(300) here is a bad idea, but will stop arguing
about it for now as sugar-toolkit does the same. We'll fix it up later.

> +pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
> +width, height = pixbuf.get_width(), pixbuf.get_height()

> +
> +

Blocks inside methods should be separated by a single blank line only.

> +if (width > preview_width) or (height > preview_height):
> +scale_x = float(width) / preview_width
> +scale_y = float(height) / preview_height
> +scale = max(scale_x, scale_y)
> +
> +pixbuf = pixbuf.scale_simple(float(width) / scale, height / 
> scale,
> + gtk.gdk.INTERP_BILINEAR)

I guess you meant "width / scale"? scale_simple() expects integers.
If you use scale(), you can get rid of one pixbuf (currently you use up
to three).

> +width, height = pixbuf.get_width(), pixbuf.get_height()

> +pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 
> pixbuf.get_has_alpha(),
> +pixbuf.get_bits_per_sample(), preview_width, preview_height)

This creates a border around an image that's smaller than the preview
size, thus wasting disk space. Please use width and height instead.

Thanks again for working on this!

Sascha

--
http://sascha.silbe.org/
http://www.infra-silbe.de/


signature.asc
Description: PGP signature
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


Re: [Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-23 Thread Tomeu Vizoso
On Wed, Sep 22, 2010 at 17:44,   wrote:
> From: Gonzalo Odiard 
>
> this patch adressed comments from silbe about segregate the check of a image 
> mime type and use of sugar.style constant colors
> now opens the image in the original size because we don't want scale up small 
> images

Just two small remarks:

- the commit subject line should be short but tell enough of the
commit to spot most things easily. Just appending #1234 at the end may
be enough.

- the rest of the commit message should be addressed to someone
reading git log in the future. So it should be about the commit as a
whole and not about a particular revision of it.

If there's anything specific to the proposed patch you want to note
when sending it, you can use the --compose arg to git-send-email.

Regards,

Tomeu

> ---
>  downloadmanager.py |   46 ++
>  1 files changed, 46 insertions(+), 0 deletions(-)
>
> diff --git a/downloadmanager.py b/downloadmanager.py
> index 3eec649..ed68d82 100644
> --- a/downloadmanager.py
> +++ b/downloadmanager.py
> @@ -35,6 +35,7 @@ from sugar.datastore import datastore
>  from sugar import profile
>  from sugar import mime
>  from sugar.graphics.alert import Alert, TimeoutAlert
> +from sugar.graphics import style
>  from sugar.graphics.icon import Icon
>  from sugar.activity import activity
>
> @@ -192,12 +193,57 @@ class Download:
>                 sniffed_mime_type = mime.get_for_file(self._target_file.path)
>                 self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
>
> +            if self._check_image_mime_type():
> +                self.dl_jobject.metadata['preview'] = 
> self._get_preview_image()
> +
>             datastore.write(self.dl_jobject,
>                             transfer_ownership=True,
>                             reply_handler=self._internal_save_cb,
>                             error_handler=self._internal_save_error_cb,
>                             timeout=360 * 
> DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
>
> +
> +    def _check_image_mime_type(self):
> +        pixbuf_mime_types = []
> +        for pixbuf_format in gtk.gdk.pixbuf_get_formats():
> +            pixbuf_mime_types.extend(pixbuf_format['mime_types'])
> +
> +        return self._mime_type in pixbuf_mime_types
> +
> +    def _get_preview_image(self):
> +        preview_width, preview_height = style.zoom(300), style.zoom(225)
> +
> +        pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
> +        width, height = pixbuf.get_width(), pixbuf.get_height()
> +
> +
> +        if (width > preview_width) or (height > preview_height):
> +            scale_x = float(width) / preview_width
> +            scale_y = float(height) / preview_height
> +            scale = max(scale_x,scale_y)
> +
> +            pixbuf = pixbuf.scale_simple(float(width) / scale, height / 
> scale,
> +                                     gtk.gdk.INTERP_BILINEAR)
> +
> +        width, height = pixbuf.get_width(), pixbuf.get_height()
> +
> +        pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 
> pixbuf.get_has_alpha(),
> +            pixbuf.get_bits_per_sample(), preview_width, preview_height)
> +
> +        pixbuf2.fill(style.COLOR_WHITE.get_int())
> +
> +        margin_x, margin_y = (preview_width - width) / 2, (preview_height - 
> height) / 2
> +
> +        pixbuf.copy_area(0, 0, width, height, pixbuf2, margin_x, margin_y)
> +
> +        preview_data = []
> +        def save_func(buf, data):
> +            data.append(buf)
> +
> +        pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
> +        preview_data = ''.join(preview_data)
> +        return dbus.ByteArray(preview_data)
> +
>     def __start_response_cb(self, alert, response_id):
>         global _active_downloads
>         if response_id is gtk.RESPONSE_CANCEL:
> --
> 1.7.2.3
>
> ___
> Sugar-devel mailing list
> Sugar-devel@lists.sugarlabs.org
> http://lists.sugarlabs.org/listinfo/sugar-devel
>
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


[Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-23 Thread godiard
From: Gonzalo Odiard 

this patch adressed comments from silbe about segregate the check of a image 
mime type and use of sugar.style constant colors
now opens the image in the original size because we don't want scale up small 
images
also i did changes sugested by simon
---
 downloadmanager.py |   46 ++
 1 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/downloadmanager.py b/downloadmanager.py
index 3eec649..ed68d82 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -35,6 +35,7 @@ from sugar.datastore import datastore
 from sugar import profile
 from sugar import mime
 from sugar.graphics.alert import Alert, TimeoutAlert
+from sugar.graphics import style
 from sugar.graphics.icon import Icon
 from sugar.activity import activity
 
@@ -192,12 +193,57 @@ class Download:
 sniffed_mime_type = mime.get_for_file(self._target_file.path)
 self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
 
+if self._check_image_mime_type():
+self.dl_jobject.metadata['preview'] = self._get_preview_image()
+
 datastore.write(self.dl_jobject,
 transfer_ownership=True,
 reply_handler=self._internal_save_cb,
 error_handler=self._internal_save_error_cb,
 timeout=360 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
 
+
+def _check_image_mime_type(self):
+for pixbuf_format in gtk.gdk.pixbuf_get_formats():
+if self._mime_type in pixbuf_format['mime_types']:
+return True
+return False
+
+def _get_preview_image(self):
+preview_width, preview_height = style.zoom(300), style.zoom(225)
+
+pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
+width, height = pixbuf.get_width(), pixbuf.get_height()
+
+
+if (width > preview_width) or (height > preview_height):
+scale_x = float(width) / preview_width
+scale_y = float(height) / preview_height
+scale = max(scale_x, scale_y)
+
+pixbuf = pixbuf.scale_simple(float(width) / scale, height / scale,
+ gtk.gdk.INTERP_BILINEAR)
+
+width, height = pixbuf.get_width(), pixbuf.get_height()
+
+pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 
pixbuf.get_has_alpha(),
+pixbuf.get_bits_per_sample(), preview_width, preview_height)
+
+pixbuf2.fill(style.COLOR_WHITE.get_int())
+
+margin_x, margin_y = (preview_width - width) / 2, (preview_height - 
height) / 2
+
+pixbuf.copy_area(0, 0, width, height, pixbuf2, margin_x, margin_y)
+
+preview_data = []
+def save_func(buf, data):
+data.append(buf)
+
+pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
+preview_data = ''.join(preview_data)
+return dbus.ByteArray(preview_data)
+
 def __start_response_cb(self, alert, response_id):
 global _active_downloads
 if response_id is gtk.RESPONSE_CANCEL:
-- 
1.7.2.3

___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


Re: [Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-23 Thread Simon Schampijer
On 09/22/2010 05:44 PM, godi...@sugarlabs.org wrote:
> From: Gonzalo Odiard
>
> this patch adressed comments from silbe about segregate the check of a image 
> mime type and use of sugar.style constant colors
> now opens the image in the original size because we don't want scale up small 
> images
> ---
>   downloadmanager.py |   46 ++
>   1 files changed, 46 insertions(+), 0 deletions(-)
>
> diff --git a/downloadmanager.py b/downloadmanager.py
> index 3eec649..ed68d82 100644
> --- a/downloadmanager.py
> +++ b/downloadmanager.py
> @@ -35,6 +35,7 @@ from sugar.datastore import datastore
>   from sugar import profile
>   from sugar import mime
>   from sugar.graphics.alert import Alert, TimeoutAlert
> +from sugar.graphics import style
>   from sugar.graphics.icon import Icon
>   from sugar.activity import activity
>
> @@ -192,12 +193,57 @@ class Download:
>   sniffed_mime_type = 
> mime.get_for_file(self._target_file.path)
>   self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
>
> +if self._check_image_mime_type():
> +self.dl_jobject.metadata['preview'] = 
> self._get_preview_image()
> +
>   datastore.write(self.dl_jobject,
>   transfer_ownership=True,
>   reply_handler=self._internal_save_cb,
>   error_handler=self._internal_save_error_cb,
>   timeout=360 * 
> DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
>
> +
> +def _check_image_mime_type(self):
> +pixbuf_mime_types = []
> +for pixbuf_format in gtk.gdk.pixbuf_get_formats():
> +pixbuf_mime_types.extend(pixbuf_format['mime_types'])
> +
> +return self._mime_type in pixbuf_mime_types

I guess you could do here as well (I think Sascha suggested so):

for pixbuf_format in gtk.gdk.pixbuf_get_formats():
 if self._mime_type in pixbuf_format['mime_types']:
 return True

return False

> +def _get_preview_image(self):
> +preview_width, preview_height = style.zoom(300), style.zoom(225)
> +
> +pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
> +width, height = pixbuf.get_width(), pixbuf.get_height()
> +
> +
> +if (width>  preview_width) or (height>  preview_height):
> +scale_x = float(width) / preview_width
> +scale_y = float(height) / preview_height
> +scale = max(scale_x,scale_y)

Please leave some space "max(scale_x, scale_y)".

> +pixbuf = pixbuf.scale_simple(float(width) / scale, height / 
> scale,
> + gtk.gdk.INTERP_BILINEAR)
> +
> +width, height = pixbuf.get_width(), pixbuf.get_height()
> +
> +pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 
> pixbuf.get_has_alpha(),
> +pixbuf.get_bits_per_sample(), preview_width, preview_height)
> +
> +pixbuf2.fill(style.COLOR_WHITE.get_int())
> +
> +margin_x, margin_y = (preview_width - width) / 2, (preview_height - 
> height) / 2
> +
> +pixbuf.copy_area(0, 0, width, height, pixbuf2, margin_x, margin_y)
> +
> +preview_data = []
> +def save_func(buf, data):
> +data.append(buf)
> +
> +pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
> +preview_data = ''.join(preview_data)
> +return dbus.ByteArray(preview_data)
> +
>   def __start_response_cb(self, alert, response_id):
>   global _active_downloads
>   if response_id is gtk.RESPONSE_CANCEL:

Regards,
Simon
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


[Sugar-devel] [PATCH] implements http://bugs.sugarlabs.org/ticket/1106 - Browse: No preview in Journal for downloaded image

2010-09-22 Thread godiard
From: Gonzalo Odiard 

this patch adressed comments from silbe about segregate the check of a image 
mime type and use of sugar.style constant colors
now opens the image in the original size because we don't want scale up small 
images
---
 downloadmanager.py |   46 ++
 1 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/downloadmanager.py b/downloadmanager.py
index 3eec649..ed68d82 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -35,6 +35,7 @@ from sugar.datastore import datastore
 from sugar import profile
 from sugar import mime
 from sugar.graphics.alert import Alert, TimeoutAlert
+from sugar.graphics import style
 from sugar.graphics.icon import Icon
 from sugar.activity import activity
 
@@ -192,12 +193,57 @@ class Download:
 sniffed_mime_type = mime.get_for_file(self._target_file.path)
 self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
 
+if self._check_image_mime_type():
+self.dl_jobject.metadata['preview'] = self._get_preview_image()
+
 datastore.write(self.dl_jobject,
 transfer_ownership=True,
 reply_handler=self._internal_save_cb,
 error_handler=self._internal_save_error_cb,
 timeout=360 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
 
+
+def _check_image_mime_type(self):
+pixbuf_mime_types = []
+for pixbuf_format in gtk.gdk.pixbuf_get_formats():
+pixbuf_mime_types.extend(pixbuf_format['mime_types'])
+
+return self._mime_type in pixbuf_mime_types
+
+def _get_preview_image(self):
+preview_width, preview_height = style.zoom(300), style.zoom(225)
+
+pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
+width, height = pixbuf.get_width(), pixbuf.get_height()
+
+
+if (width > preview_width) or (height > preview_height):
+scale_x = float(width) / preview_width
+scale_y = float(height) / preview_height
+scale = max(scale_x,scale_y)
+
+pixbuf = pixbuf.scale_simple(float(width) / scale, height / scale,
+ gtk.gdk.INTERP_BILINEAR)
+
+width, height = pixbuf.get_width(), pixbuf.get_height()
+
+pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 
pixbuf.get_has_alpha(),
+pixbuf.get_bits_per_sample(), preview_width, preview_height)
+
+pixbuf2.fill(style.COLOR_WHITE.get_int())
+
+margin_x, margin_y = (preview_width - width) / 2, (preview_height - 
height) / 2
+
+pixbuf.copy_area(0, 0, width, height, pixbuf2, margin_x, margin_y)
+
+preview_data = []
+def save_func(buf, data):
+data.append(buf)
+
+pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
+preview_data = ''.join(preview_data)
+return dbus.ByteArray(preview_data)
+
 def __start_response_cb(self, alert, response_id):
 global _active_downloads
 if response_id is gtk.RESPONSE_CANCEL:
-- 
1.7.2.3

___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel