comparison sat_frontends/quick_frontend/quick_widgets.py @ 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 30e08d904208
children 1cb232c9e845
comparison
equal deleted inserted replaced
3164:9dc170635bee 3165:7699a08ba8fb
93 @param widget(QuickWidget): retrieve instances of this widget 93 @param widget(QuickWidget): retrieve instances of this widget
94 @return: iterator on widgets 94 @return: iterator on widgets
95 """ 95 """
96 return self.getWidgets(widget.__class__, widget.target, widget.profiles) 96 return self.getWidgets(widget.__class__, widget.target, widget.profiles)
97 97
98 def getWidgets(self, class_, target=None, profiles=None): 98 def getWidgets(self, class_, target=None, profiles=None, with_duplicates=True):
99 """Get all subclassed widgets instances 99 """Get all subclassed widgets instances
100 100
101 @param class_: subclass of QuickWidget, same parameter as used in 101 @param class_: subclass of QuickWidget, same parameter as used in
102 [getOrCreateWidget] 102 [getOrCreateWidget]
103 @param target: if not None, construct a hash with this target and filter 103 @param target: if not None, construct a hash with this target and filter
104 corresponding widgets 104 corresponding widgets
105 recreated widgets are handled 105 recreated widgets are handled
106 @param profiles(iterable, None): if not None, filter on instances linked to these 106 @param profiles(iterable, None): if not None, filter on instances linked to these
107 profiles 107 profiles
108 @param with_duplicates(bool): if False, only first widget with a given hash is
109 returned
108 @return: iterator on widgets 110 @return: iterator on widgets
109 """ 111 """
110 class_ = self.getRealClass(class_) 112 class_ = self.getRealClass(class_)
111 try: 113 try:
112 widgets_map = self._widgets[class_.__name__] 114 widgets_map = self._widgets[class_.__name__]
118 else: 120 else:
119 filter_hash = None 121 filter_hash = None
120 if filter_hash is not None: 122 if filter_hash is not None:
121 for widget in widgets_map.get(filter_hash, []): 123 for widget in widgets_map.get(filter_hash, []):
122 yield widget 124 yield widget
125 if not with_duplicates:
126 return
123 else: 127 else:
124 for widget_instances in widgets_map.values(): 128 for widget_instances in widgets_map.values():
125 for widget in widget_instances: 129 for widget in widget_instances:
126 yield widget 130 yield widget
131 if not with_duplicates:
132 # widgets are set by hashes, so if don't want duplicates
133 # we only return the first widget of the list
134 break
127 135
128 def getWidget(self, class_, target=None, profiles=None): 136 def getWidget(self, class_, target=None, profiles=None):
129 """Get a widget without creating it if it doesn't exist. 137 """Get a widget without creating it if it doesn't exist.
130 138
131 if several instances of widgets with this hash exist, the first one is returned 139 if several instances of widgets with this hash exist, the first one is returned