comparison cagou/core/cagou_main.py @ 353:19422bbd9c8e

core (widgets handler): refactoring: - CagouWidget now has class properties (to be overridden when needed) which indicate how if the widget handle must add a wrapping ScreenManager (global_screen_manager) or show all instances of the class in a Carousel (collection_carousel). If none of those options is used, a ScrollView will be wrapping the widget, to be sure that the widget will be resized correctly when necessary (without it, the widget could still be drawn in the backround when the size is too small and overflow on the WidgetWrapper, this would be the case with WidgetSelector) - some helper methods/properties have been added to CagouWidget. Check docstrings for details - better handling of (in)visible widget in WidgetsHandler - thanks to the new wrapping ScrollView, WidgetSelect will show scroll bars if the available space is too small. - bugs fixes
author Goffi <goffi@goffi.org>
date Fri, 17 Jan 2020 18:44:35 +0100
parents 434f770fe55b
children 307c2501d8b2
comparison
equal deleted inserted replaced
352:434f770fe55b 353:19422bbd9c8e
751 751
752 if to_change is None: 752 if to_change is None:
753 raise exceptions.InternalError("no CagouWidget found when " 753 raise exceptions.InternalError("no CagouWidget found when "
754 "trying to switch widget") 754 "trying to switch widget")
755 755
756 wrapper = to_change.parent 756 to_change.whwrapper.changeWidget(new)
757 while wrapper is not None and not(isinstance(wrapper, widgets_handler.WHWrapper)):
758 wrapper = wrapper.parent
759
760 if wrapper is None:
761 raise exceptions.InternalError("no wrapper found")
762
763 wrapper.changeWidget(new)
764 self.selected_widget = new 757 self.selected_widget = new
765 758
766 def _addVisibleWidget(self, widget): 759 def _addVisibleWidget(self, widget):
767 """declare a widget visible 760 """declare a widget visible
768 761
809 return self._visible_widgets[cls] 802 return self._visible_widgets[cls]
810 except KeyError: 803 except KeyError:
811 return set() 804 return set()
812 805
813 def deleteUnusedWidgetInstances(self, widget): 806 def deleteUnusedWidgetInstances(self, widget):
814 """Delete instance of this widget without parent 807 """Delete instance of this widget which are not attached to a WHWrapper
815 808
816 @param widget(quick_widgets.QuickWidget): reference widget 809 @param widget(quick_widgets.QuickWidget): reference widget
817 other instance of this widget will be deleted if they have no parent 810 other instance of this widget will be deleted if they have no parent
818 """ 811 """
819 # FIXME: unused for now
820 to_delete = [] 812 to_delete = []
821 if isinstance(widget, quick_widgets.QuickWidget): 813 if isinstance(widget, quick_widgets.QuickWidget):
822 for w in self.widgets.getWidgetInstances(widget): 814 for w in self.widgets.getWidgetInstances(widget):
823 if w.parent is None and w != widget: 815 if w.whwrapper is None and w != widget:
824 to_delete.append(w) 816 to_delete.append(w)
825 for w in to_delete: 817 for w in to_delete:
826 log.debug("cleaning widget: {wid}".format(wid=w)) 818 log.debug("cleaning widget: {wid}".format(wid=w))
827 self.widgets.deleteWidget(w) 819 self.widgets.deleteWidget(w)
828 820
829 def getOrClone(self, widget): 821 def getOrClone(self, widget, **kwargs):
830 """Get a QuickWidget if it has not parent set else clone it 822 """Get a QuickWidget if it is not in a WHWrapper, else clone it
831 823
832 if an other instance of this widget exist without parent, it will be used. 824 if an other instance of this widget exist without being in a WHWrapper
833 """ 825 (i.e. if it is not already in use) it will be used.
834 if widget.parent is None: 826 """
827 if widget.whwrapper is None:
828 if widget.parent is not None:
829 widget.parent.remove_widget(widget)
835 self.deleteUnusedWidgetInstances(widget) 830 self.deleteUnusedWidgetInstances(widget)
836 return widget 831 return widget
837 for w in self.widgets.getWidgetInstances(widget): 832 for w in self.widgets.getWidgetInstances(widget):
838 if w.parent is None: 833 if w.whwrapper is None:
834 if w.parent is not None:
835 w.parent.remove_widget(w)
839 self.deleteUnusedWidgetInstances(w) 836 self.deleteUnusedWidgetInstances(w)
840 return w 837 return w
841 targets = list(widget.targets) 838 targets = list(widget.targets)
842 w = self.widgets.getOrCreateWidget(widget.__class__, 839 w = self.widgets.getOrCreateWidget(widget.__class__,
843 targets[0], 840 targets[0],
844 on_new_widget=None, 841 on_new_widget=None,
845 on_existing_widget=C.WIDGET_RECREATE, 842 on_existing_widget=C.WIDGET_RECREATE,
846 profiles=widget.profiles) 843 profiles=widget.profiles,
844 **kwargs)
847 for t in targets[1:]: 845 for t in targets[1:]:
848 w.addTarget(t) 846 w.addTarget(t)
849 return w 847 return w
850 848
851 def getWidgetToSwitch(self): 849 def getWidgetToSwitch(self):
925 self.app.connected = False 923 self.app.connected = False
926 924
927 ## misc ## 925 ## misc ##
928 926
929 def plugging_profiles(self): 927 def plugging_profiles(self):
930 self.app.root.changeWidget(widgets_handler.WidgetsHandler()) 928 self._widgets_handler = widgets_handler.WidgetsHandler()
929 self.app.root.changeWidget(self._widgets_handler)
931 self.bridge.menusGet("", C.NO_SECURITY_LIMIT, callback=self._menusGetCb) 930 self.bridge.menusGet("", C.NO_SECURITY_LIMIT, callback=self._menusGetCb)
932 931
933 def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE): 932 def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
934 log.info("Profile presence status set to {show}/{status}".format(show=show, 933 log.info("Profile presence status set to {show}/{status}".format(show=show,
935 status=status)) 934 status=status))
1021 except Exception as e: 1020 except Exception as e:
1022 log.warning(_("Can't use notifications, disabling: {msg}").format( 1021 log.warning(_("Can't use notifications, disabling: {msg}").format(
1023 msg = e)) 1022 msg = e))
1024 notification = None 1023 notification = None
1025 1024
1025 def getParentWHWrapper(self, wid):
1026 """Retrieve parent WHWrapper instance managing a widget
1027
1028 @param wid(Widget): widget to check
1029 @return (WHWrapper, None): found instance if any, else None
1030 """
1031 wh = self.getAncestorWidget(wid, widgets_handler.WHWrapper)
1032 if wh is None:
1033 # we may have a screen
1034 try:
1035 sm = wid.screen_manager
1036 except (exceptions.InternalError, exceptions.NotFound):
1037 return None
1038 else:
1039 wh = self.getAncestorWidget(sm, widgets_handler.WHWrapper)
1040 return wh
1041
1026 def getAncestorWidget(self, wid, cls): 1042 def getAncestorWidget(self, wid, cls):
1027 """Retrieve an ancestor of given class 1043 """Retrieve an ancestor of given class
1028 1044
1029 @param wid(Widget): current widget 1045 @param wid(Widget): current widget
1030 @param cls(type): class of the ancestor to retrieve 1046 @param cls(type): class of the ancestor to retrieve