diff cagou/core/cagou_widget.py @ 353:19422bbd9c8e

core (widgets handler): refactoring: - CagouWidget now has class properties (to be overridden when needed) which indicate how if the widget handle must add a wrapping ScreenManager (global_screen_manager) or show all instances of the class in a Carousel (collection_carousel). If none of those options is used, a ScrollView will be wrapping the widget, to be sure that the widget will be resized correctly when necessary (without it, the widget could still be drawn in the backround when the size is too small and overflow on the WidgetWrapper, this would be the case with WidgetSelector) - some helper methods/properties have been added to CagouWidget. Check docstrings for details - better handling of (in)visible widget in WidgetsHandler - thanks to the new wrapping ScrollView, WidgetSelect will show scroll bars if the available space is too small. - bugs fixes
author Goffi <goffi@goffi.org>
date Fri, 17 Jan 2020 18:44:35 +0100
parents 772c170b47a9
children 4d3a0c4f2430
line wrap: on
line diff
--- a/cagou/core/cagou_widget.py	Fri Jan 17 18:44:35 2020 +0100
+++ b/cagou/core/cagou_widget.py	Fri Jan 17 18:44:35 2020 +0100
@@ -19,13 +19,17 @@
 
 
 from sat.core import log as logging
-log = logging.getLogger(__name__)
+from sat.core import exceptions
 from kivy.uix.behaviors import ButtonBehavior
 from kivy.uix.boxlayout import BoxLayout
 from kivy.uix.dropdown import DropDown
+from kivy.uix.screenmanager import Screen
 from kivy import properties
 from cagou import G
-from cagou.core.common import ActionIcon
+from .common import ActionIcon
+
+
+log = logging.getLogger(__name__)
 
 
 class HeaderWidgetChoice(ButtonBehavior, BoxLayout):
@@ -44,7 +48,8 @@
 
     def __init__(self, cagou_widget):
         super(HeaderWidgetSelector, self).__init__()
-        for plugin_info in G.host.getPluggedWidgets(except_cls=cagou_widget.__class__):
+        plg_info_cls = cagou_widget.plugin_info_class or cagou_widget.__class__
+        for plugin_info in G.host.getPluggedWidgets(except_cls=plg_info_cls):
             choice = HeaderWidgetChoice(cagou_widget, plugin_info)
             self.add_widget(choice)
 
@@ -58,17 +63,60 @@
 
 
 class CagouWidget(BoxLayout):
+    main_container = properties.ObjectProperty(None)
     header_input = properties.ObjectProperty(None)
     header_box = properties.ObjectProperty(None)
+    # set to True if you want to be able to switch between visible widgets of this
+    # class using a carousel
+    collection_carousel = False
+    # set to True if you a global ScreenManager global to all widgets of this class.
+    # The screen manager is created in WHWrapper
+    global_screen_manager = False
+    # override this if a specific class (i.e. not self.__class__) must be used for
+    # plugin info. Useful when a CagouWidget is used with global_screen_manager.
+    plugin_info_class = None
 
-    def __init__(self):
+    def __init__(self, **kwargs):
+        plg_info_cls = self.plugin_info_class or self.__class__
         for p in G.host.getPluggedWidgets():
-            if p['main'] == self.__class__:
+            if p['main'] == plg_info_cls:
                 self.plugin_info = p
                 break
-        BoxLayout.__init__(self)
+        super().__init__(**kwargs)
         self.selector = HeaderWidgetSelector(self)
 
+    @property
+    def screen_manager(self):
+        if ((not self.global_screen_manager
+             and not (self.plugin_info_class is not None
+                      and self.plugin_info_class.global_screen_manager))):
+            raise exceptions.InternalError(
+                "screen_manager property can't be used if global_screen_manager is not "
+                "set")
+        screen = self.getAncestor(Screen)
+        if screen is None:
+            raise exceptions.NotFound("Can't find parent Screen")
+        if screen.manager is None:
+            raise exceptions.NotFound("Can't find parent ScreenManager")
+        return screen.manager
+
+    @property
+    def whwrapper(self):
+        """Retrieve parent widget handler"""
+        return G.host.getParentWHWrapper(self)
+
+    def screenManagerInit(self, screen_manager):
+        """Override this method to do init when ScreenManager is instantiated
+
+        This is only called once even if collection_carousel is used.
+        """
+        if not self.global_screen_manager:
+            raise exceptions.InternalError("screenManagerInit should not be called")
+
+    def getAncestor(self, cls):
+        """Helper method to use host.getAncestorWidget with self"""
+        return G.host.getAncestorWidget(self, cls)
+
     def switchWidget(self, plugin_info):
         self.selector.dismiss()
         factory = plugin_info["factory"]