quick frontend(widget): added with_duplicates argument to getWidgets:

when this argument is False, widgets with the same widget_hash are filtered to only return
first one. It is True by default to keep former behaviour.
This commit is contained in:
Goffi 2020-02-12 19:38:32 +01:00
commit e8aaa9fd43

View file

@ -95,7 +95,7 @@ class QuickWidgetsManager(object):
"""
return self.getWidgets(widget.__class__, widget.target, widget.profiles)
def getWidgets(self, class_, target=None, profiles=None):
def getWidgets(self, class_, target=None, profiles=None, with_duplicates=True):
"""Get all subclassed widgets instances
@param class_: subclass of QuickWidget, same parameter as used in
@ -105,6 +105,8 @@ class QuickWidgetsManager(object):
recreated widgets are handled
@param profiles(iterable, None): if not None, filter on instances linked to these
profiles
@param with_duplicates(bool): if False, only first widget with a given hash is
returned
@return: iterator on widgets
"""
class_ = self.getRealClass(class_)
@ -120,10 +122,16 @@ class QuickWidgetsManager(object):
if filter_hash is not None:
for widget in widgets_map.get(filter_hash, []):
yield widget
if not with_duplicates:
return
else:
for widget_instances in widgets_map.values():
for widget in widget_instances:
yield widget
if not with_duplicates:
# widgets are set by hashes, so if don't want duplicates
# we only return the first widget of the list
break
def getWidget(self, class_, target=None, profiles=None):
"""Get a widget without creating it if it doesn't exist.