diff cagou/core/cagou_main.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 434f770fe55b
children 307c2501d8b2
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Fri Jan 17 18:44:35 2020 +0100
+++ b/cagou/core/cagou_main.py	Fri Jan 17 18:44:35 2020 +0100
@@ -753,14 +753,7 @@
             raise exceptions.InternalError("no CagouWidget found when "
                                            "trying to switch widget")
 
-        wrapper = to_change.parent
-        while wrapper is not None and not(isinstance(wrapper, widgets_handler.WHWrapper)):
-            wrapper = wrapper.parent
-
-        if wrapper is None:
-            raise exceptions.InternalError("no wrapper found")
-
-        wrapper.changeWidget(new)
+        to_change.whwrapper.changeWidget(new)
         self.selected_widget = new
 
     def _addVisibleWidget(self, widget):
@@ -811,31 +804,35 @@
             return set()
 
     def deleteUnusedWidgetInstances(self, widget):
-        """Delete instance of this widget without parent
+        """Delete instance of this widget which are not attached to a WHWrapper
 
         @param widget(quick_widgets.QuickWidget): reference widget
             other instance of this widget will be deleted if they have no parent
         """
-        # FIXME: unused for now
         to_delete = []
         if isinstance(widget, quick_widgets.QuickWidget):
             for w in self.widgets.getWidgetInstances(widget):
-                if w.parent is None and w != widget:
+                if w.whwrapper is None and w != widget:
                     to_delete.append(w)
             for w in to_delete:
                 log.debug("cleaning widget: {wid}".format(wid=w))
                 self.widgets.deleteWidget(w)
 
-    def getOrClone(self, widget):
-        """Get a QuickWidget if it has not parent set else clone it
+    def getOrClone(self, widget, **kwargs):
+        """Get a QuickWidget if it is not in a WHWrapper, else clone it
 
-        if an other instance of this widget exist without parent, it will be used.
+        if an other instance of this widget exist without being in a WHWrapper
+        (i.e. if it is not already in use) it will be used.
         """
-        if widget.parent is None:
+        if widget.whwrapper is None:
+            if widget.parent is not None:
+                widget.parent.remove_widget(widget)
             self.deleteUnusedWidgetInstances(widget)
             return widget
         for w in self.widgets.getWidgetInstances(widget):
-            if w.parent is None:
+            if w.whwrapper is None:
+                if w.parent is not None:
+                    w.parent.remove_widget(w)
                 self.deleteUnusedWidgetInstances(w)
                 return w
         targets = list(widget.targets)
@@ -843,7 +840,8 @@
                                            targets[0],
                                            on_new_widget=None,
                                            on_existing_widget=C.WIDGET_RECREATE,
-                                           profiles=widget.profiles)
+                                           profiles=widget.profiles,
+                                           **kwargs)
         for t in targets[1:]:
             w.addTarget(t)
         return w
@@ -927,7 +925,8 @@
     ## misc ##
 
     def plugging_profiles(self):
-        self.app.root.changeWidget(widgets_handler.WidgetsHandler())
+        self._widgets_handler = widgets_handler.WidgetsHandler()
+        self.app.root.changeWidget(self._widgets_handler)
         self.bridge.menusGet("", C.NO_SECURITY_LIMIT, callback=self._menusGetCb)
 
     def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
@@ -1023,6 +1022,23 @@
                     msg = e))
                 notification = None
 
+    def getParentWHWrapper(self, wid):
+        """Retrieve parent WHWrapper instance managing a widget
+
+        @param wid(Widget): widget to check
+        @return (WHWrapper, None): found instance if any, else None
+        """
+        wh = self.getAncestorWidget(wid, widgets_handler.WHWrapper)
+        if wh is None:
+            # we may have a screen
+            try:
+                sm = wid.screen_manager
+            except (exceptions.InternalError, exceptions.NotFound):
+                return None
+            else:
+                wh = self.getAncestorWidget(sm, widgets_handler.WHWrapper)
+        return wh
+
     def getAncestorWidget(self, wid, cls):
         """Retrieve an ancestor of given class