comparison 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
comparison
equal deleted inserted replaced
259:4601793b0dee 260:145c29b5f2b5
749 try: 749 try:
750 return self._visible_widgets[cls] 750 return self._visible_widgets[cls]
751 except KeyError: 751 except KeyError:
752 return [] 752 return []
753 753
754 def deleteUnusedWidgetInstances(self, widget):
755 """Delete instance of this widget without parent
756
757 @param widget(quick_widgets.QuickWidget): reference widget
758 other instance of this widget will be deleted if they have no parent
759 """
760 # FIXME: unused for now
761 to_delete = []
762 for w in self.widgets.getWidgetInstances(widget):
763 if w.parent is None and w != widget:
764 to_delete.append(w)
765 for w in to_delete:
766 log.debug(u"cleaning widget: {wid}".format(wid=w))
767 self.widgets.deleteWidget(w)
768
754 def getOrClone(self, widget): 769 def getOrClone(self, widget):
755 """Get a QuickWidget if it has not parent set else clone it""" 770 """Get a QuickWidget if it has not parent set else clone it
771
772 if an other instance of this widget exist without parent, it will be used.
773 """
756 if widget.parent is None: 774 if widget.parent is None:
775 self.deleteUnusedWidgetInstances(widget)
757 return widget 776 return widget
777 for w in self.widgets.getWidgetInstances(widget):
778 if w.parent is None:
779 self.deleteUnusedWidgetInstances(w)
780 return w
758 targets = list(widget.targets) 781 targets = list(widget.targets)
759 w = self.widgets.getOrCreateWidget(widget.__class__, 782 w = self.widgets.getOrCreateWidget(widget.__class__,
760 targets[0], 783 targets[0],
761 on_new_widget=None, 784 on_new_widget=None,
762 on_existing_widget=C.WIDGET_RECREATE, 785 on_existing_widget=C.WIDGET_RECREATE,