changeset 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 ddca5a837029
children f8ba934ea462
files cagou/core/common.py cagou/core/constants.py cagou/core/simple_xhtml.py cagou/kv/common.kv cagou/kv/simple_xhtml.kv
diffstat 5 files changed, 58 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/common.py	Wed Feb 26 16:39:17 2020 +0100
+++ b/cagou/core/common.py	Wed Feb 26 16:41:27 2020 +0100
@@ -23,7 +23,7 @@
 from sat.core.i18n import _
 from sat.core import log as logging
 from kivy.uix.widget import Widget
-from kivy.uix.image import Image
+from kivy.uix.image import Image, AsyncImage
 from kivy.uix.label import Label
 from kivy.uix.behaviors import ButtonBehavior
 from kivy.uix.behaviors import ToggleButtonBehavior
@@ -196,6 +196,54 @@
             self.add_widget(icon_wid)
 
 
+class SizedImage(AsyncImage):
+    """AsyncImage sized according to C.IMG_MAX_WIDTH and C.IMG_MAX_HEIGHT"""
+    # following properties are desired height/width
+    # i.e. the ones specified in height/width attributes of <img>
+    # (or wanted for whatever reason)
+    # set to None to ignore them
+    target_height = properties.NumericProperty(allownone=True)
+    target_width = properties.NumericProperty(allownone=True)
+
+    def __init__(self, **kwargs):
+        # best calculated size
+        self._best_width = self._best_height = 100
+        super().__init__(**kwargs)
+
+    def on_texture(self, instance, texture):
+        """Adapt the size according to max size and target_*"""
+        if texture is None:
+            return
+        max_width, max_height = dp(C.IMG_MAX_WIDTH), dp(C.IMG_MAX_HEIGHT)
+        width, height = texture.size
+        if self.target_width:
+            width = min(width, self.target_width)
+        if width > max_width:
+            width = C.IMG_MAX_WIDTH
+
+        height = width / self.image_ratio
+
+        if self.target_height:
+            height = min(height, self.target_height)
+
+        if height > max_height:
+            height = max_height
+            width = height * self.image_ratio
+
+        self.width, self.height = self._best_width, self._best_height = width, height
+
+    def on_parent(self, instance, parent):
+        if parent is not None:
+            parent.bind(width=self.on_parent_width)
+
+    def on_parent_width(self, instance, width):
+        if self._best_width > width:
+            self.width = width
+            self.height = width / self.image_ratio
+        else:
+            self.width, self.height = self._best_width, self._best_height
+
+
 class JidSelectorCategoryLayout(StackLayout):
     pass
 
--- a/cagou/core/constants.py	Wed Feb 26 16:39:17 2020 +0100
+++ b/cagou/core/constants.py	Wed Feb 26 16:41:27 2020 +0100
@@ -53,8 +53,8 @@
     COLOR_BTN_LIGHT = (0.4, 0.4, 0.4, 1)
 
     # values are in dp
-    IMG_MAX_WIDTH = 500
-    IMG_MAX_HEIGHT = 500
+    IMG_MAX_WIDTH = 400
+    IMG_MAX_HEIGHT = 400
 
     # files
     FILE_DEST_DOWNLOAD = "DOWNLOAD"
--- a/cagou/core/simple_xhtml.py	Wed Feb 26 16:39:17 2020 +0100
+++ b/cagou/core/simple_xhtml.py	Wed Feb 26 16:41:27 2020 +0100
@@ -22,13 +22,12 @@
 from kivy.uix.stacklayout import StackLayout
 from kivy.uix.label import Label
 from kivy.utils import escape_markup
-from kivy.metrics import sp, dp
+from kivy.metrics import sp
 from kivy import properties
 from sat.core import log as logging
 from sat_frontends.tools import css_color, strings as sat_strings
 from cagou import G
-from cagou.core.image import AsyncImage
-from cagou.core.constants import Const as C
+from cagou.core.common  import SizedImage
 
 
 log = logging.getLogger(__name__)
@@ -89,53 +88,6 @@
             self.font_size = parent.font_size
 
 
-class SimpleXHTMLWidgetImage(AsyncImage):
-    # following properties are desired height/width
-    # i.e. the ones specified in height/width attributes of <img>
-    # (or wanted for whatever reason)
-    # set to None to ignore them
-    target_height = properties.NumericProperty(allownone=True)
-    target_width = properties.NumericProperty(allownone=True)
-
-    def __init__(self, **kwargs):
-        # best calculated size
-        self._best_width = self._best_height = 100
-        super().__init__(**kwargs)
-
-    def on_texture(self, instance, texture):
-        """Adapt the size according to max size and target_*"""
-        if texture is None:
-            return
-        max_width, max_height = dp(C.IMG_MAX_WIDTH), dp(C.IMG_MAX_HEIGHT)
-        width, height = texture.size
-        if self.target_width:
-            width = min(width, self.target_width)
-        if width > max_width:
-            width = C.IMG_MAX_WIDTH
-
-        height = width / self.image_ratio
-
-        if self.target_height:
-            height = min(height, self.target_height)
-
-        if height > max_height:
-            height = max_height
-            width = height * self.image_ratio
-
-        self.width, self.height = self._best_width, self._best_height = width, height
-
-    def on_parent(self, instance, parent):
-        if parent is not None:
-            parent.bind(width=self.on_parent_width)
-
-    def on_parent_width(self, instance, width):
-        if self._best_width > width:
-            self.width = width
-            self.height = width / self.image_ratio
-        else:
-            self.width, self.height = self._best_width, self._best_height
-
-
 class SimpleXHTMLWidget(StackLayout):
     """widget handling simple XHTML parsing"""
     xhtml = properties.StringProperty()
@@ -435,7 +387,8 @@
             log.warning(f"Can't parse image width: {elem.get('width')}")
             target_width = None
 
-        img = SimpleXHTMLWidgetImage(source=src, target_height=target_height, target_width=target_width)
+        img = SizedImage(
+            source=src, target_height=target_height, target_width=target_width)
         self.current_wid = img
         self.add_widget(img)
 
--- a/cagou/kv/common.kv	Wed Feb 26 16:39:17 2020 +0100
+++ b/cagou/kv/common.kv	Wed Feb 26 16:41:27 2020 +0100
@@ -146,6 +146,9 @@
     bg_color: 0, 0, 0, 0
     color: app.c_sec_light
 
+<SizedImage>:
+    size_hint: None, None
+
 
 <JidSelectorCategoryLayout>:
     size_hint: 1, None
--- a/cagou/kv/simple_xhtml.kv	Wed Feb 26 16:39:17 2020 +0100
+++ b/cagou/kv/simple_xhtml.kv	Wed Feb 26 16:41:27 2020 +0100
@@ -29,6 +29,3 @@
 <SimpleXHTMLWidgetText>:
     size_hint: None, None
     size: self.texture_size
-
-<SimpleXHTMLWidgetImage>:
-    size_hint: None, None