comparison libervia/desktop_kivy/core/image.py @ 493:b3cedbee561d

refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 18:26:16 +0200
parents cagou/core/image.py@203755bbe0fe
children
comparison
equal deleted inserted replaced
492:5114bbb5daa3 493:b3cedbee561d
1 #!/usr/bin/env python3
2
3
4 #Libervia Desktop-Kivy
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 import mimetypes
22 from functools import partial
23 from kivy.uix import image as kivy_img
24 from libervia.backend.core import log as logging
25 from libervia.backend.tools.common import data_format
26 from libervia.desktop_kivy import G
27
28 log = logging.getLogger(__name__)
29
30
31 class Image(kivy_img.Image):
32 """Image widget which accept source without extension"""
33 SVG_CONVERT_EXTRA = {'width': 128, 'height': 128}
34
35 def __init__(self, **kwargs):
36 self.register_event_type('on_error')
37 super().__init__(**kwargs)
38
39 def _image_convert_cb(self, path):
40 self.source = path
41
42 def texture_update(self, *largs):
43 if self.source:
44 if mimetypes.guess_type(self.source, strict=False)[0] == 'image/svg+xml':
45 log.debug(f"Converting SVG image at {self.source} to PNG")
46 G.host.bridge.image_convert(
47 self.source,
48 "",
49 data_format.serialise(self.SVG_CONVERT_EXTRA),
50 "",
51 callback=self._image_convert_cb,
52 errback=partial(
53 G.host.errback,
54 message=f"Can't load image at {self.source}: {{msg}}"
55 )
56 )
57 return
58
59 super().texture_update(*largs)
60 if self.source and self.texture is None:
61 log.warning(
62 f"Image {self.source} has not been imported correctly, replacing by "
63 f"empty one")
64 # FIXME: temporary image, to be replaced by something showing that something
65 # went wrong
66 self.source = G.host.app.expand(
67 "{media}/misc/borders/border_hollow_black.png")
68 self.dispatch('on_error', Exception(f"Can't load source {self.source}"))
69
70 def on_error(self, err):
71 pass
72
73
74 class AsyncImage(kivy_img.AsyncImage):
75 """AsyncImage which accept file:// schema"""
76
77 def _load_source(self, *args):
78 if self.source.startswith('file://'):
79 self.source = self.source[7:]
80 else:
81 super(AsyncImage, self)._load_source(*args)