annotate src/cagou/core/image.py @ 35:20b04c58868f

chat: header_input text is reset when widget is changed due to input
author Goffi <goffi@goffi.org>
date Sun, 28 Aug 2016 14:59:49 +0200
parents 56838ad5c84b
children d5ede9281e4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
3
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
6
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
11
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
16
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
19
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
20
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import log as logging
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = logging.getLogger(__name__)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from kivy.uix import image as kivy_img
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from kivy.core.image import Image as CoreImage
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from kivy.resources import resource_find
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import io
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import PIL
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
28
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
29
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class Image(kivy_img.Image):
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
31 """Image widget which accept source without extension"""
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
32
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def texture_update(self, *largs):
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
34 if not self.source:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self.texture = None
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
36 else:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
37 filename = resource_find(self.source)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self._loops = 0
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if filename is None:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
40 return log.error('Image: Error reading file {filename}'.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
41 format(filename=self.source))
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
42 mipmap = self.mipmap
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
43 if self._coreimage is not None:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self._coreimage.unbind(on_texture=self._on_tex_change)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
45 try:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self._coreimage = ci = CoreImage(filename, mipmap=mipmap,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
47 anim_delay=self.anim_delay,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
48 keep_data=self.keep_data,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
49 nocache=self.nocache)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
50 except Exception as e:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
51 # loading failed probably because of unmanaged extention,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
52 # we try our luck with with PIL
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
53 try:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
54 im = PIL.Image.open(filename)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
55 ext = im.format.lower()
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
56 del im
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
57 # we can't use im.tobytes as it would use the
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
58 # internal decompressed representation from pillow
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
59 # and im.save would need processing to handle format
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
60 data = io.BytesIO(open(filename, "rb").read())
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
61 cache_filename = u"{}.{}".format(filename,ext) # needed for kivy's Image to use cache
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self._coreimage = ci = CoreImage(data, ext=ext,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
63 filename=cache_filename, mipmap=mipmap,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
64 anim_delay=self.anim_delay,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
65 keep_data=self.keep_data,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
66 nocache=self.nocache)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
67 except Exception as e:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
68 log.warning(u"Can't load image: {}".format(e))
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self._coreimage = ci = None
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
70
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if ci:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
72 ci.bind(on_texture=self._on_tex_change)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.texture = ci.texture