diff cagou/core/cagou_main.py @ 260:145c29b5f2b5

core: improved getOrClone + use in chat and widgets_handler: - getOrClone is now trying to re-use others instances of a widget if they have no parent, and will create a new widget only if nothing is found - it is now used in widgets_handler's changeWidget - it is now used in Chat factory, on existing widget - new host method deleteUnusedWidgetInstances remove instances without parent. It is used by getOrClone.
author Goffi <goffi@goffi.org>
date Mon, 11 Mar 2019 08:39:43 +0100
parents 4601793b0dee
children 825e8c91b703
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Mon Mar 11 08:39:43 2019 +0100
+++ b/cagou/core/cagou_main.py	Mon Mar 11 08:39:43 2019 +0100
@@ -751,10 +751,33 @@
         except KeyError:
             return []
 
+    def deleteUnusedWidgetInstances(self, widget):
+        """Delete instance of this widget without parent
+
+        @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 = []
+        for w in self.widgets.getWidgetInstances(widget):
+            if w.parent is None and w != widget:
+                to_delete.append(w)
+        for w in to_delete:
+            log.debug(u"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"""
+        """Get a QuickWidget if it has not parent set else clone it
+
+        if an other instance of this widget exist without parent, it will be used.
+        """
         if widget.parent is None:
+            self.deleteUnusedWidgetInstances(widget)
             return widget
+        for w in self.widgets.getWidgetInstances(widget):
+            if w.parent is None:
+                self.deleteUnusedWidgetInstances(w)
+                return w
         targets = list(widget.targets)
         w = self.widgets.getOrCreateWidget(widget.__class__,
                                            targets[0],