comparison cagou/core/cagou_widget.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 772c170b47a9
children 4d3a0c4f2430
comparison
equal deleted inserted replaced
352:434f770fe55b 353:19422bbd9c8e
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 from sat.core import log as logging 21 from sat.core import log as logging
22 log = logging.getLogger(__name__) 22 from sat.core import exceptions
23 from kivy.uix.behaviors import ButtonBehavior 23 from kivy.uix.behaviors import ButtonBehavior
24 from kivy.uix.boxlayout import BoxLayout 24 from kivy.uix.boxlayout import BoxLayout
25 from kivy.uix.dropdown import DropDown 25 from kivy.uix.dropdown import DropDown
26 from kivy.uix.screenmanager import Screen
26 from kivy import properties 27 from kivy import properties
27 from cagou import G 28 from cagou import G
28 from cagou.core.common import ActionIcon 29 from .common import ActionIcon
30
31
32 log = logging.getLogger(__name__)
29 33
30 34
31 class HeaderWidgetChoice(ButtonBehavior, BoxLayout): 35 class HeaderWidgetChoice(ButtonBehavior, BoxLayout):
32 36
33 def __init__(self, cagou_widget, plugin_info): 37 def __init__(self, cagou_widget, plugin_info):
42 46
43 class HeaderWidgetSelector(DropDown): 47 class HeaderWidgetSelector(DropDown):
44 48
45 def __init__(self, cagou_widget): 49 def __init__(self, cagou_widget):
46 super(HeaderWidgetSelector, self).__init__() 50 super(HeaderWidgetSelector, self).__init__()
47 for plugin_info in G.host.getPluggedWidgets(except_cls=cagou_widget.__class__): 51 plg_info_cls = cagou_widget.plugin_info_class or cagou_widget.__class__
52 for plugin_info in G.host.getPluggedWidgets(except_cls=plg_info_cls):
48 choice = HeaderWidgetChoice(cagou_widget, plugin_info) 53 choice = HeaderWidgetChoice(cagou_widget, plugin_info)
49 self.add_widget(choice) 54 self.add_widget(choice)
50 55
51 def add_widget(self, *args): 56 def add_widget(self, *args):
52 widget = args[0] 57 widget = args[0]
56 def set_width(self, choice, minimum_width): 61 def set_width(self, choice, minimum_width):
57 self.width = max([c.minimum_width for c in self.container.children]) 62 self.width = max([c.minimum_width for c in self.container.children])
58 63
59 64
60 class CagouWidget(BoxLayout): 65 class CagouWidget(BoxLayout):
66 main_container = properties.ObjectProperty(None)
61 header_input = properties.ObjectProperty(None) 67 header_input = properties.ObjectProperty(None)
62 header_box = properties.ObjectProperty(None) 68 header_box = properties.ObjectProperty(None)
69 # set to True if you want to be able to switch between visible widgets of this
70 # class using a carousel
71 collection_carousel = False
72 # set to True if you a global ScreenManager global to all widgets of this class.
73 # The screen manager is created in WHWrapper
74 global_screen_manager = False
75 # override this if a specific class (i.e. not self.__class__) must be used for
76 # plugin info. Useful when a CagouWidget is used with global_screen_manager.
77 plugin_info_class = None
63 78
64 def __init__(self): 79 def __init__(self, **kwargs):
80 plg_info_cls = self.plugin_info_class or self.__class__
65 for p in G.host.getPluggedWidgets(): 81 for p in G.host.getPluggedWidgets():
66 if p['main'] == self.__class__: 82 if p['main'] == plg_info_cls:
67 self.plugin_info = p 83 self.plugin_info = p
68 break 84 break
69 BoxLayout.__init__(self) 85 super().__init__(**kwargs)
70 self.selector = HeaderWidgetSelector(self) 86 self.selector = HeaderWidgetSelector(self)
87
88 @property
89 def screen_manager(self):
90 if ((not self.global_screen_manager
91 and not (self.plugin_info_class is not None
92 and self.plugin_info_class.global_screen_manager))):
93 raise exceptions.InternalError(
94 "screen_manager property can't be used if global_screen_manager is not "
95 "set")
96 screen = self.getAncestor(Screen)
97 if screen is None:
98 raise exceptions.NotFound("Can't find parent Screen")
99 if screen.manager is None:
100 raise exceptions.NotFound("Can't find parent ScreenManager")
101 return screen.manager
102
103 @property
104 def whwrapper(self):
105 """Retrieve parent widget handler"""
106 return G.host.getParentWHWrapper(self)
107
108 def screenManagerInit(self, screen_manager):
109 """Override this method to do init when ScreenManager is instantiated
110
111 This is only called once even if collection_carousel is used.
112 """
113 if not self.global_screen_manager:
114 raise exceptions.InternalError("screenManagerInit should not be called")
115
116 def getAncestor(self, cls):
117 """Helper method to use host.getAncestorWidget with self"""
118 return G.host.getAncestorWidget(self, cls)
71 119
72 def switchWidget(self, plugin_info): 120 def switchWidget(self, plugin_info):
73 self.selector.dismiss() 121 self.selector.dismiss()
74 factory = plugin_info["factory"] 122 factory = plugin_info["factory"]
75 new_widget = factory(plugin_info, None, iter(G.host.profiles)) 123 new_widget = factory(plugin_info, None, iter(G.host.profiles))