Mercurial > libervia-desktop-kivy
comparison cagou/core/simple_xhtml.py @ 422:efee0e0afb78
core (common): moved simple_xhtml's image code to a generic "SizedImage" widget
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 26 Feb 2020 16:41:27 +0100 |
parents | 1da3c379205b |
children | 2d277a3d9cec |
comparison
equal
deleted
inserted
replaced
421:ddca5a837029 | 422:efee0e0afb78 |
---|---|
20 | 20 |
21 from xml.etree import ElementTree as ET | 21 from xml.etree import ElementTree as ET |
22 from kivy.uix.stacklayout import StackLayout | 22 from kivy.uix.stacklayout import StackLayout |
23 from kivy.uix.label import Label | 23 from kivy.uix.label import Label |
24 from kivy.utils import escape_markup | 24 from kivy.utils import escape_markup |
25 from kivy.metrics import sp, dp | 25 from kivy.metrics import sp |
26 from kivy import properties | 26 from kivy import properties |
27 from sat.core import log as logging | 27 from sat.core import log as logging |
28 from sat_frontends.tools import css_color, strings as sat_strings | 28 from sat_frontends.tools import css_color, strings as sat_strings |
29 from cagou import G | 29 from cagou import G |
30 from cagou.core.image import AsyncImage | 30 from cagou.core.common import SizedImage |
31 from cagou.core.constants import Const as C | |
32 | 31 |
33 | 32 |
34 log = logging.getLogger(__name__) | 33 log = logging.getLogger(__name__) |
35 | 34 |
36 | 35 |
85 class SimpleXHTMLWidgetText(Label): | 84 class SimpleXHTMLWidgetText(Label): |
86 | 85 |
87 def on_parent(self, instance, parent): | 86 def on_parent(self, instance, parent): |
88 if parent is not None: | 87 if parent is not None: |
89 self.font_size = parent.font_size | 88 self.font_size = parent.font_size |
90 | |
91 | |
92 class SimpleXHTMLWidgetImage(AsyncImage): | |
93 # following properties are desired height/width | |
94 # i.e. the ones specified in height/width attributes of <img> | |
95 # (or wanted for whatever reason) | |
96 # set to None to ignore them | |
97 target_height = properties.NumericProperty(allownone=True) | |
98 target_width = properties.NumericProperty(allownone=True) | |
99 | |
100 def __init__(self, **kwargs): | |
101 # best calculated size | |
102 self._best_width = self._best_height = 100 | |
103 super().__init__(**kwargs) | |
104 | |
105 def on_texture(self, instance, texture): | |
106 """Adapt the size according to max size and target_*""" | |
107 if texture is None: | |
108 return | |
109 max_width, max_height = dp(C.IMG_MAX_WIDTH), dp(C.IMG_MAX_HEIGHT) | |
110 width, height = texture.size | |
111 if self.target_width: | |
112 width = min(width, self.target_width) | |
113 if width > max_width: | |
114 width = C.IMG_MAX_WIDTH | |
115 | |
116 height = width / self.image_ratio | |
117 | |
118 if self.target_height: | |
119 height = min(height, self.target_height) | |
120 | |
121 if height > max_height: | |
122 height = max_height | |
123 width = height * self.image_ratio | |
124 | |
125 self.width, self.height = self._best_width, self._best_height = width, height | |
126 | |
127 def on_parent(self, instance, parent): | |
128 if parent is not None: | |
129 parent.bind(width=self.on_parent_width) | |
130 | |
131 def on_parent_width(self, instance, width): | |
132 if self._best_width > width: | |
133 self.width = width | |
134 self.height = width / self.image_ratio | |
135 else: | |
136 self.width, self.height = self._best_width, self._best_height | |
137 | 89 |
138 | 90 |
139 class SimpleXHTMLWidget(StackLayout): | 91 class SimpleXHTMLWidget(StackLayout): |
140 """widget handling simple XHTML parsing""" | 92 """widget handling simple XHTML parsing""" |
141 xhtml = properties.StringProperty() | 93 xhtml = properties.StringProperty() |
433 target_width = int(elem.get('width', 0)) | 385 target_width = int(elem.get('width', 0)) |
434 except ValueError: | 386 except ValueError: |
435 log.warning(f"Can't parse image width: {elem.get('width')}") | 387 log.warning(f"Can't parse image width: {elem.get('width')}") |
436 target_width = None | 388 target_width = None |
437 | 389 |
438 img = SimpleXHTMLWidgetImage(source=src, target_height=target_height, target_width=target_width) | 390 img = SizedImage( |
391 source=src, target_height=target_height, target_width=target_width) | |
439 self.current_wid = img | 392 self.current_wid = img |
440 self.add_widget(img) | 393 self.add_widget(img) |
441 | 394 |
442 def xhtml_p(self, elem): | 395 def xhtml_p(self, elem): |
443 if isinstance(self.current_wid, Label): | 396 if isinstance(self.current_wid, Label): |