changeset 3165:7699a08ba8fb

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.
author Goffi <goffi@goffi.org>
date Wed, 12 Feb 2020 19:38:32 +0100
parents 9dc170635bee
children 122075ceaa53
files sat_frontends/quick_frontend/quick_widgets.py
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/sat_frontends/quick_frontend/quick_widgets.py	Mon Feb 10 22:02:31 2020 +0100
+++ b/sat_frontends/quick_frontend/quick_widgets.py	Wed Feb 12 19:38:32 2020 +0100
@@ -95,7 +95,7 @@
         """
         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 @@
             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 @@
             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.