changeset 433:aa204c813f07

chat: attachment preview: - if there is not already one, a preview is generated for images collection thumbnails, instead of using the full size image. - for images attachment, local path is now used if present, before URL.
author Goffi <goffi@goffi.org>
date Sun, 01 Mar 2020 22:11:25 +0100
parents 36c3f1c02d33
children 977158b56ce6
files cagou/core/image.py cagou/plugins/plugin_wid_chat.kv cagou/plugins/plugin_wid_chat.py
diffstat 3 files changed, 18 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/image.py	Sun Mar 01 22:11:25 2020 +0100
+++ b/cagou/core/image.py	Sun Mar 01 22:11:25 2020 +0100
@@ -47,7 +47,7 @@
                                                  anim_delay=self.anim_delay,
                                                  keep_data=self.keep_data,
                                                  nocache=self.nocache)
-            except Exception as e:
+            except Exception:
                 # loading failed probably because of unmanaged extention,
                 # we try our luck with with PIL
                 try:
--- a/cagou/plugins/plugin_wid_chat.kv	Sun Mar 01 22:11:25 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.kv	Sun Mar 01 22:11:25 2020 +0100
@@ -63,7 +63,7 @@
     orientation: "vertical"
     SizedImage:
         id: image
-        source: root.data.get('url', root.data.get('path'))
+        source: root.data.get('preview') or root.data.get('path') or root.data.get('url')
         anim_delay: -1
 
 
--- a/cagou/plugins/plugin_wid_chat.py	Sun Mar 01 22:11:25 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sun Mar 01 22:11:25 2020 +0100
@@ -128,7 +128,8 @@
     image = properties.ObjectProperty()
 
     def on_press(self):
-        gallery = ImagesGallery(sources=[self.image.source])
+        full_size_source = self.data.get('path', self.data.get('url'))
+        gallery = ImagesGallery(sources=[full_size_source])
         G.host.showExtraUI(gallery)
 
 
@@ -137,10 +138,22 @@
     chat = properties.ObjectProperty()
     mess_data = properties.ObjectProperty()
 
+    def _setPreview(self, attachment, wid, preview_path):
+        attachment['preview'] = preview_path
+        wid.source = preview_path
+
     def _setDecryptedPath(self, attachment, wid, path):
         attachment['path'] = path
         if wid is not None:
-            wid.source = path
+            # we also need a preview for the widget
+            if 'preview' in attachment:
+                wid.source = attachment['preview']
+            else:
+                G.host.bridge.imageGeneratePreview(
+                    path,
+                    self.chat.profile,
+                    callback=partial(self._setPreview, attachment, wid),
+                )
 
     def on_kv_post(self, __):
         attachments = self.attachments
@@ -187,7 +200,7 @@
     def on_press(self):
         sources = []
         for attachment in self.attachments:
-            source = attachment.get('url', attachment.get('path'))
+            source = attachment.get('path', attachment.get('url'))
             if not source:
                 log.warning(f"no source for {attachment}")
             else: