Mercurial > libervia-backend
comparison sat_frontends/quick_frontend/quick_widgets.py @ 2853:6901a425d882
quick frontend (widgets): improved deletion of widget:
QuickWidget deletion has been improved a bit. A new "all_instances" kwarg can be used to completely delete all instances of a widget.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Mar 2019 18:03:41 +0100 |
parents | e2595c81eb6d |
children | ef65dbce313b |
comparison
equal
deleted
inserted
replaced
2852:e2595c81eb6d | 2853:6901a425d882 |
---|---|
264 "Unexpected on_existing_widget value ({})".format(on_existing_widget)) | 264 "Unexpected on_existing_widget value ({})".format(on_existing_widget)) |
265 | 265 |
266 return widget | 266 return widget |
267 | 267 |
268 def deleteWidget(self, widget_to_delete, *args, **kwargs): | 268 def deleteWidget(self, widget_to_delete, *args, **kwargs): |
269 """Delete a widget | 269 """Delete a widget instance |
270 | 270 |
271 this method must be called by frontends when a widget is deleted | 271 this method must be called by frontends when a widget is deleted |
272 widget's onDelete method will be called before deletion | 272 widget's onDelete method will be called before deletion, and deletion will be |
273 stopped if it returns False. | |
273 @param widget_to_delete(QuickWidget): widget which need to deleted | 274 @param widget_to_delete(QuickWidget): widget which need to deleted |
274 @param *args: extra arguments to pass to onDelete | 275 @param *args: extra arguments to pass to onDelete |
275 @param *kwargs: extra keywords arguments to pass to onDelete | 276 @param *kwargs: extra keywords arguments to pass to onDelete |
276 the extra arguments are not use by QuickFrontend, it's is up to | 277 the extra arguments are not use by QuickFrontend, it's is up to |
277 the frontend to use them or not | 278 the frontend to use them or not |
278 """ | 279 "all_instances" can be used as kwarg, if it evaluate to True, all |
279 if widget_to_delete.onDelete(*args, **kwargs) == False: | 280 instances of the widget will be deleted (if onDelete is not returning False |
280 return | 281 for any of the instance). This arguments is not sent to onDelete methods. |
282 """ | |
283 # TODO: all_instances must be independante kwargs, this is not possible with Python 2 | |
284 # but will be with Python 3 | |
285 all_instances = kwargs.get('all_instances', False) | |
286 | |
287 if all_instances: | |
288 for w in self.getWidgetInstances(widget_to_delete): | |
289 if w.onDelete(*args, **kwargs) == False: | |
290 return | |
291 else: | |
292 if widget_to_delete.onDelete(*args, **kwargs) == False: | |
293 return | |
281 | 294 |
282 if self.host.selected_widget == widget_to_delete: | 295 if self.host.selected_widget == widget_to_delete: |
283 self.host.selected_widget = None | 296 self.host.selected_widget = None |
284 | 297 |
285 for widget_map in self._widgets.itervalues(): | 298 class_ = self.getRealClass(widget_to_delete.__class__) |
286 to_delete = set() | 299 try: |
287 for hash_, widget_instances in widget_map.iteritems(): | 300 widgets_map = self._widgets[class_.__name__] |
288 if widget_to_delete in widget_instances: | 301 except KeyError: |
289 widget_instances.remove(widget_to_delete) | 302 log.error(u"no widgets_map found for class {cls}".format(cls=class_)) |
290 if not widget_instances: | 303 return |
291 to_delete.add(hash_) | 304 widget_hash = unicode(class_.getWidgetHash(widget_to_delete.target, |
292 for hash_ in to_delete: | 305 widget_to_delete.profiles)) |
293 del widget_map[hash_] | 306 widget_instances = widgets_map[widget_hash] |
307 if all_instances: | |
308 widget_instances.clear() | |
309 else: | |
310 try: | |
311 widget_instances.remove(widget_to_delete) | |
312 except ValueError: | |
313 log.error(u"widget_to_delete not found in widget instances") | |
314 return | |
315 | |
316 log.debug(u"widget {} deleted".format(widget_to_delete)) | |
317 | |
318 if not widget_instances: | |
319 # all instances with this hash have been deleted | |
320 # we remove the hash itself | |
321 del widgets_map[widget_hash] | |
322 log.debug(u"All instances of {cls} with hash {widget_hash} have been deleted" | |
323 .format(cls=class_, widget_hash=widget_hash)) | |
294 | 324 |
295 | 325 |
296 class QuickWidget(object): | 326 class QuickWidget(object): |
297 """generic widget base""" | 327 """generic widget base""" |
298 | 328 |
400 """Called when a widget is being deleted | 430 """Called when a widget is being deleted |
401 | 431 |
402 @return (boot, None): False to cancel deletion | 432 @return (boot, None): False to cancel deletion |
403 all other value continue deletion | 433 all other value continue deletion |
404 """ | 434 """ |
405 log.debug(u"widget {} deleted".format(self)) | |
406 return True | 435 return True |