annotate cagou/core/image.py @ 475:c35aa683fa2d

/!\ package is being renamed to libervia-desktop following global name change /!\
author Goffi <goffi@goffi.org>
date Fri, 18 Jun 2021 16:36:39 +0200
parents 3c9ba4a694ef
children 203755bbe0fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
379
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
1 #!/usr/bin/env python3
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
2
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
461
3c9ba4a694ef dates update
Goffi <goffi@goffi.org>
parents: 460
diff changeset
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
8
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
460
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
21 import mimetypes
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
22 from functools import partial
8
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
460
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
24 from sat.core import log as logging
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
25 from sat.tools.common import data_format
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
26 from cagou import G
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
27
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
28 log = logging.getLogger(__name__)
8
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
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
31 class Image(kivy_img.Image):
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
32 """Image widget which accept source without extension"""
460
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
33 SVG_CONVERT_EXTRA = {'width': 128, 'height': 128}
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
34
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
35 def __init__(self, **kwargs):
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
36 self.register_event_type('on_error')
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
37 super().__init__(**kwargs)
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
38
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
39 def _imageConvertCb(self, path):
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
40 self.source = path
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
41
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
42 def texture_update(self, *largs):
460
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
43 if self.source:
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
44 if mimetypes.guess_type(self.source, strict=False)[0] == 'image/svg+xml':
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
45 log.debug(f"Converting SVG image at {self.source} to PNG")
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
46 G.host.bridge.imageConvert(
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
47 self.source,
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
48 "",
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
49 data_format.serialise(self.SVG_CONVERT_EXTRA),
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
50 "",
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
51 callback=self._imageConvertCb,
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
52 errback=partial(
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
53 G.host.errback,
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
54 message=f"Can't load image at {self.source}: {{msg}}"
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
55 )
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
56 )
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
57 return
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
58
460
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
59 super().texture_update(*largs)
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
60 if self.source and self.texture is None:
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
61 log.warning(
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
62 f"Image {self.source} has not been imported correctly, replacing by "
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
63 f"empty one")
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
64 # FIXME: temporary image, to be replaced by something showing that something
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
65 # went wrong
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
66 self.source = G.host.app.expand(
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
67 "{media}/misc/borders/border_hollow_black.png")
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
68 self.dispatch('on_error', Exception(f"Can't load source {self.source}"))
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
69
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
70 def on_error(self, err):
b5e8e470f7f7 core (image): convert SVG images + better error handling:
Goffi <goffi@goffi.org>
parents: 433
diff changeset
71 pass
101
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
72
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
73
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
74 class AsyncImage(kivy_img.AsyncImage):
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
75 """AsyncImage which accept file:// schema"""
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
76
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
77 def _load_source(self, *args):
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
78 if self.source.startswith('file://'):
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
79 self.source = self.source[7:]
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
80 else:
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
81 super(AsyncImage, self)._load_source(*args)