Mercurial > libervia-desktop-kivy
annotate libervia/desktop_kivy/core/image.py @ 508:d87b9a6b0b69
doc (calls): updated documentation to describe the new UI features:
fix 425
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Oct 2023 15:29:33 +0200 |
parents | b3cedbee561d |
children |
rev | line source |
---|---|
379 | 1 #!/usr/bin/env python3 |
2 | |
8
d9095d1dd7ae
added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
493
b3cedbee561d
refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
Goffi <goffi@goffi.org>
parents:
491
diff
changeset
|
4 #Libervia Desktop-Kivy |
461 | 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 |
493
b3cedbee561d
refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
Goffi <goffi@goffi.org>
parents:
491
diff
changeset
|
24 from libervia.backend.core import log as logging |
b3cedbee561d
refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
Goffi <goffi@goffi.org>
parents:
491
diff
changeset
|
25 from libervia.backend.tools.common import data_format |
b3cedbee561d
refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
Goffi <goffi@goffi.org>
parents:
491
diff
changeset
|
26 from libervia.desktop_kivy import G |
460
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 |
491
203755bbe0fe
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
461
diff
changeset
|
39 def _image_convert_cb(self, path): |
460
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") |
491
203755bbe0fe
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
461
diff
changeset
|
46 G.host.bridge.image_convert( |
460
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 "", |
491
203755bbe0fe
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
461
diff
changeset
|
51 callback=self._image_convert_cb, |
460
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) |