changeset 414:72a6b06728ab

core: file dropping: on compatible OSes, one or more file(s) can now be dropped on the main window, it will then be transmitted to the selected widget if it has a "on_drop_file" method. This is used in chat widget to add an attachment
author Goffi <goffi@goffi.org>
date Sun, 23 Feb 2020 15:39:06 +0100
parents c466678c57b2
children 5761b5f03c0c
files cagou/core/cagou_main.py cagou/plugins/plugin_wid_chat.py
diffstat 2 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Sun Feb 23 15:39:06 2020 +0100
+++ b/cagou/core/cagou_main.py	Sun Feb 23 15:39:06 2020 +0100
@@ -20,6 +20,7 @@
 import os.path
 import glob
 import sys
+from pathlib import Path
 from functools import partial
 from sat.core.i18n import _
 from . import kivy_hack
@@ -304,6 +305,7 @@
 
     def build(self):
         Window.bind(on_keyboard=self.key_input)
+        Window.bind(on_dropfile=self.on_dropfile)
         wid = CagouRootWidget(Label(text=_("Loading please wait")))
         local_platform.on_app_build(wid)
         return wid
@@ -371,6 +373,17 @@
         else:
             return False
 
+    def on_dropfile(self, __, file_path):
+        if self.host.selected_widget is not None:
+            try:
+                on_drop_file = self.host.selected_widget.on_drop_file
+            except AttributeError:
+                log.info(
+                    f"Select widget {self.host.selected_widget} doesn't handle file "
+                    f"dropping")
+            else:
+                on_drop_file(Path(file_path.decode()))
+
 
 class Cagou(QuickApp):
     MB_HANDLE = False
--- a/cagou/plugins/plugin_wid_chat.py	Sun Feb 23 15:39:06 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sun Feb 23 15:39:06 2020 +0100
@@ -530,6 +530,10 @@
             screen_manager.current = 'chat_selector'
             return True
 
+    ## drop ##
+
+    def on_drop_file(self, path):
+        self.addAttachment(path)
 
     ## header ##