Mercurial > libervia-desktop-kivy
annotate cagou/core/widgets_handler.py @ 358:8efed1d47d9f
core: don't crash when a widget without profiles switch to default widget
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 18 Jan 2020 23:12:52 +0100 |
parents | 8b6621cc142c |
children | e1f56257cabc |
rev | line source |
---|---|
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
1 #!/usr/bin/env python3 |
13 | 2 |
3 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
282 | 4 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org) |
13 | 5 |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 | |
20 from sat.core import log as logging | |
154 | 21 from sat.core import exceptions |
34
02acbb297a61
handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
22 from sat_frontends.quick_frontend import quick_widgets |
154 | 23 from kivy.graphics import Color, Ellipse |
24 from kivy.uix.layout import Layout | |
13 | 25 from kivy.uix.boxlayout import BoxLayout |
155
a0e486074d91
widget handler: block carousel swiping when there is only one slide
Goffi <goffi@goffi.org>
parents:
154
diff
changeset
|
26 from kivy.uix.stencilview import StencilView |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
27 from kivy.uix.carousel import Carousel |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
28 from kivy.uix.screenmanager import ScreenManager, Screen |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
29 from kivy.uix.scrollview import ScrollView |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
30 from kivy.metrics import dp |
13 | 31 from kivy import properties |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
32 from cagou import G |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
33 from .constants import Const as C |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
34 from . import cagou_widget |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
35 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
36 log = logging.getLogger(__name__) |
13 | 37 |
163
ef09dce878c7
widgets handler: removed constants which are not used anymore
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
38 |
269
a5dfc789eeaf
widgets_handler: increased remove limit and min height/width:
Goffi <goffi@goffi.org>
parents:
263
diff
changeset
|
39 REMOVE_WID_LIMIT = dp(50) |
a5dfc789eeaf
widgets_handler: increased remove limit and min height/width:
Goffi <goffi@goffi.org>
parents:
263
diff
changeset
|
40 MIN_WIDTH = MIN_HEIGHT = dp(70) |
13 | 41 |
42 | |
154 | 43 class WHWrapper(BoxLayout): |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
44 main_container = properties.ObjectProperty(None) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
45 screen_manager = properties.ObjectProperty(None, allownone=True) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
46 carousel = properties.ObjectProperty(None, allownone=True) |
154 | 47 split_size = properties.NumericProperty(dp(1)) |
48 split_margin = properties.NumericProperty(dp(2)) | |
49 split_color = properties.ListProperty([0.8, 0.8, 0.8, 1]) | |
177
9835cafbd909
widgets handler: use dark secondary color for split
Goffi <goffi@goffi.org>
parents:
171
diff
changeset
|
50 split_color_move = C.COLOR_SEC_DARK |
154 | 51 split_color_del = properties.ListProperty([0.8, 0.0, 0.0, 1]) |
52 # sp stands for "split point" | |
53 sp_size = properties.NumericProperty(dp(1)) | |
54 sp_space = properties.NumericProperty(dp(4)) | |
55 sp_zone = properties.NumericProperty(dp(30)) | |
56 _split = properties.OptionProperty('None', options=['None', 'left', 'top']) | |
57 _split_del = properties.BooleanProperty(False) | |
58 | |
59 def __init__(self, **kwargs): | |
60 idx = kwargs.pop('_wid_idx') | |
61 self._wid_idx = idx | |
62 super(WHWrapper, self).__init__(**kwargs) | |
63 self._left_wids = set() | |
64 self._top_wids = set() | |
65 self._right_wids = set() | |
66 self._bottom_wids = set() | |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
67 self._clear_attributes() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
68 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
69 def _clear_attributes(self): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
70 self._former_slide = None |
154 | 71 |
72 def __repr__(self): | |
73 return "WHWrapper_{idx}".format(idx=self._wid_idx) | |
74 | |
75 def _main_wid(self, wid_list): | |
76 """return main widget of a side list | |
77 | |
263
fe540a6dc14d
widgets handler: call host._removeVisibleWidget when a widget is deleted (using split)
Goffi <goffi@goffi.org>
parents:
260
diff
changeset
|
78 main widget is either the widget currently splitted |
154 | 79 or any widget if none is split |
80 @return (WHWrapper, None): main widget or None | |
81 if there is not widget | |
82 """ | |
83 if not wid_list: | |
84 return None | |
85 for wid in wid_list: | |
86 if wid._split != 'None': | |
87 return wid | |
88 return next(iter(wid_list)) | |
89 | |
355
8b6621cc142c
core (widgets handler): clear widgets when WHWrapper is detached:
Goffi <goffi@goffi.org>
parents:
353
diff
changeset
|
90 def on_parent(self, __, new_parent): |
8b6621cc142c
core (widgets handler): clear widgets when WHWrapper is detached:
Goffi <goffi@goffi.org>
parents:
353
diff
changeset
|
91 if new_parent is None: |
8b6621cc142c
core (widgets handler): clear widgets when WHWrapper is detached:
Goffi <goffi@goffi.org>
parents:
353
diff
changeset
|
92 # we detach all children so CagouWidget.whwrapper won't link to this one |
8b6621cc142c
core (widgets handler): clear widgets when WHWrapper is detached:
Goffi <goffi@goffi.org>
parents:
353
diff
changeset
|
93 # anymore |
8b6621cc142c
core (widgets handler): clear widgets when WHWrapper is detached:
Goffi <goffi@goffi.org>
parents:
353
diff
changeset
|
94 self.clear_widgets() |
8b6621cc142c
core (widgets handler): clear widgets when WHWrapper is detached:
Goffi <goffi@goffi.org>
parents:
353
diff
changeset
|
95 |
154 | 96 @property |
97 def _left_wid(self): | |
98 return self._main_wid(self._left_wids) | |
99 | |
100 @property | |
101 def _top_wid(self): | |
102 return self._main_wid(self._top_wids) | |
103 | |
104 @property | |
105 def _right_wid(self): | |
106 return self._main_wid(self._right_wids) | |
107 | |
108 @property | |
109 def _bottom_wid(self): | |
110 return self._main_wid(self._bottom_wids) | |
13 | 111 |
154 | 112 @property |
113 def current_slide(self): | |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
114 if (self.carousel is not None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
115 and (self.screen_manager is None or self.screen_manager.current == '')): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
116 return self.carousel.current_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
117 elif self.screen_manager is not None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
118 # we should have exactly one children in current_screen, else there is a bug |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
119 return self.screen_manager.current_screen.children[0] |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
120 else: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
121 try: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
122 return self.main_container.children[0] |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
123 except IndexError: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
124 log.error("No child found, this should not happen") |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
125 return None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
126 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
127 @property |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
128 def carousel_active(self): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
129 """Return True if Carousel is used and active""" |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
130 if self.carousel is None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
131 return False |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
132 if self.screen_manager is not None and self.screen_manager.current != '': |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
133 return False |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
134 return True |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
135 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
136 @property |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
137 def former_screen_wid(self): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
138 """Return widget currently active for former screen""" |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
139 if self.screen_manager is None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
140 raise exceptions.InternalError( |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
141 "former_screen_wid can only be used if ScreenManager is used") |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
142 if self._former_screen_name is None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
143 return None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
144 return self.getScreenWidget(self._former_screen_name) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
145 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
146 def getScreenWidget(self, screen_name): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
147 """Return screen main widget, handling carousel if necessary""" |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
148 if self.carousel is not None and screen_name == '': |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
149 return self.carousel.current_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
150 try: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
151 return self.screen_manager.get_screen(screen_name).children[0] |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
152 except IndexError: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
153 return None |
154 | 154 |
155 def _draw_ellipse(self): | |
156 """draw split ellipse""" | |
157 color = self.split_color_del if self._split_del else self.split_color_move | |
158 try: | |
159 self.canvas.after.remove(self.ellipse) | |
160 except AttributeError: | |
161 pass | |
162 if self._split == "top": | |
163 with self.canvas.after: | |
164 Color(*color) | |
165 self.ellipse = Ellipse(angle_start=90, angle_end=270, | |
166 pos=(self.x + self.width/2 - self.sp_zone/2, | |
167 self.y + self.height - self.sp_zone/2), | |
168 size=(self.sp_zone, self.sp_zone)) | |
169 elif self._split == "left": | |
170 with self.canvas.after: | |
171 Color(*color) | |
172 self.ellipse = Ellipse(angle_end=180, | |
173 pos=(self.x + -self.sp_zone/2, | |
174 self.y + self.height/2 - self.sp_zone/2), | |
175 size = (self.sp_zone, self.sp_zone)) | |
176 else: | |
177 raise exceptions.InternalError('unexpected split value') | |
13 | 178 |
154 | 179 def on_touch_down(self, touch): |
180 """activate split if touch is on a split zone""" | |
181 if not self.collide_point(*touch.pos): | |
182 return | |
183 log.debug("WIDGET IDX: {} (left: {}, top: {}, right: {}, bottom: {}), pos: {}, size: {}".format( | |
184 self._wid_idx, | |
185 'None' if not self._left_wids else [w._wid_idx for w in self._left_wids], | |
186 'None' if not self._top_wids else [w._wid_idx for w in self._top_wids], | |
187 'None' if not self._right_wids else [w._wid_idx for w in self._right_wids], | |
188 'None' if not self._bottom_wids else [w._wid_idx for w in self._bottom_wids], | |
189 self.pos, | |
190 self.size, | |
191 )) | |
192 touch_rx, touch_ry = self.to_widget(*touch.pos, relative=True) | |
193 if (touch_ry <= self.height and | |
194 touch_ry >= self.height - self.split_size - self.split_margin or | |
195 touch_ry <= self.height and | |
196 touch_ry >= self.height - self.sp_zone and | |
300
efd3fbc85aaf
core (widgets_handler): use explicit floor division to avoid warnings.
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
197 touch_rx >= self.width//2 - self.sp_zone//2 and |
efd3fbc85aaf
core (widgets_handler): use explicit floor division to avoid warnings.
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
198 touch_rx <= self.width//2 + self.sp_zone//2): |
154 | 199 # split area is touched, we activate top split mode |
200 self._split = "top" | |
201 self._draw_ellipse() | |
202 elif (touch_rx >= 0 and | |
203 touch_rx <= self.split_size + self.split_margin or | |
204 touch_rx >= 0 and | |
205 touch_rx <= self.sp_zone and | |
300
efd3fbc85aaf
core (widgets_handler): use explicit floor division to avoid warnings.
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
206 touch_ry >= self.height//2 - self.sp_zone//2 and |
efd3fbc85aaf
core (widgets_handler): use explicit floor division to avoid warnings.
Goffi <goffi@goffi.org>
parents:
282
diff
changeset
|
207 touch_ry <= self.height//2 + self.sp_zone//2): |
154 | 208 # split area is touched, we activate left split mode |
209 self._split = "left" | |
210 touch.ud['ori_width'] = self.width | |
211 self._draw_ellipse() | |
13 | 212 else: |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
213 if self.carousel_active and len(self.carousel.slides) <= 1: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
214 # we don't want swipe of carousel if there is only one slide |
155
a0e486074d91
widget handler: block carousel swiping when there is only one slide
Goffi <goffi@goffi.org>
parents:
154
diff
changeset
|
215 return StencilView.on_touch_down(self.carousel, touch) |
a0e486074d91
widget handler: block carousel swiping when there is only one slide
Goffi <goffi@goffi.org>
parents:
154
diff
changeset
|
216 else: |
a0e486074d91
widget handler: block carousel swiping when there is only one slide
Goffi <goffi@goffi.org>
parents:
154
diff
changeset
|
217 return super(WHWrapper, self).on_touch_down(touch) |
13 | 218 |
219 def on_touch_move(self, touch): | |
154 | 220 """handle size change and widget creation on split""" |
221 if self._split == 'None': | |
222 return super(WHWrapper, self).on_touch_move(touch) | |
223 | |
224 elif self._split == 'top': | |
225 new_height = touch.y - self.y | |
226 | |
227 if new_height < MIN_HEIGHT: | |
228 return | |
229 | |
230 # we must not pass the top widget/border | |
231 if self._top_wids: | |
232 top = next(iter(self._top_wids)) | |
233 y_limit = top.y + top.height | |
234 | |
235 if top.height <= REMOVE_WID_LIMIT: | |
236 # we are in remove zone, we add visual hint for that | |
160
916af9c1cb9b
widget handler: don't set split delete mode if there is not top/left widget
Goffi <goffi@goffi.org>
parents:
155
diff
changeset
|
237 if not self._split_del and self._top_wids: |
154 | 238 self._split_del = True |
239 self._draw_ellipse() | |
240 else: | |
241 if self._split_del: | |
242 self._split_del = False | |
243 self._draw_ellipse() | |
244 else: | |
245 y_limit = self.y + self.height | |
246 | |
247 if touch.y >= y_limit: | |
248 return | |
249 | |
250 # all right, we can change size | |
251 self.height = new_height | |
252 self.ellipse.pos = (self.ellipse.pos[0], touch.y - self.sp_zone/2) | |
253 | |
254 if not self._top_wids: | |
255 # we are the last widget on the top | |
256 # so we create a new widget | |
257 new_wid = self.parent.add_widget() | |
258 self._top_wids.add(new_wid) | |
259 new_wid._bottom_wids.add(self) | |
260 for w in self._right_wids: | |
261 new_wid._right_wids.add(w) | |
262 w._left_wids.add(new_wid) | |
263 for w in self._left_wids: | |
264 new_wid._left_wids.add(w) | |
265 w._right_wids.add(new_wid) | |
13 | 266 |
154 | 267 elif self._split == 'left': |
268 ori_width = touch.ud['ori_width'] | |
269 new_x = touch.x | |
270 new_width = ori_width - (touch.x - touch.ox) | |
271 | |
272 if new_width < MIN_WIDTH: | |
273 return | |
274 | |
275 # we must not pass the left widget/border | |
276 if self._left_wids: | |
277 left = next(iter(self._left_wids)) | |
278 x_limit = left.x | |
279 | |
280 if left.width <= REMOVE_WID_LIMIT: | |
281 # we are in remove zone, we add visual hint for that | |
160
916af9c1cb9b
widget handler: don't set split delete mode if there is not top/left widget
Goffi <goffi@goffi.org>
parents:
155
diff
changeset
|
282 if not self._split_del and self._left_wids: |
154 | 283 self._split_del = True |
284 self._draw_ellipse() | |
285 else: | |
286 if self._split_del: | |
287 self._split_del = False | |
288 self._draw_ellipse() | |
289 else: | |
290 x_limit = self.x | |
291 | |
292 if new_x <= x_limit: | |
293 return | |
294 | |
295 # all right, we can change position/size | |
296 self.x = new_x | |
297 self.width = new_width | |
298 self.ellipse.pos = (touch.x - self.sp_zone/2, self.ellipse.pos[1]) | |
299 | |
300 if not self._left_wids: | |
301 # we are the last widget on the left | |
302 # so we create a new widget | |
303 new_wid = self.parent.add_widget() | |
304 self._left_wids.add(new_wid) | |
305 new_wid._right_wids.add(self) | |
306 for w in self._top_wids: | |
307 new_wid._top_wids.add(w) | |
308 w._bottom_wids.add(new_wid) | |
309 for w in self._bottom_wids: | |
310 new_wid._bottom_wids.add(w) | |
311 w._top_wids.add(new_wid) | |
312 | |
313 else: | |
314 raise Exception.InternalError('invalid _split value') | |
13 | 315 |
316 def on_touch_up(self, touch): | |
154 | 317 if self._split == 'None': |
318 return super(WHWrapper, self).on_touch_up(touch) | |
319 if self._split == 'top': | |
320 # we remove all top widgets in delete zone, | |
321 # and update there side widgets list | |
322 for top in self._top_wids.copy(): | |
323 if top.height <= REMOVE_WID_LIMIT: | |
263
fe540a6dc14d
widgets handler: call host._removeVisibleWidget when a widget is deleted (using split)
Goffi <goffi@goffi.org>
parents:
260
diff
changeset
|
324 G.host._removeVisibleWidget(top.current_slide) |
154 | 325 for w in top._top_wids: |
326 w._bottom_wids.remove(top) | |
327 w._bottom_wids.update(top._bottom_wids) | |
328 for w in top._bottom_wids: | |
329 w._top_wids.remove(top) | |
330 w._top_wids.update(top._top_wids) | |
331 for w in top._left_wids: | |
332 w._right_wids.remove(top) | |
333 for w in top._right_wids: | |
334 w._left_wids.remove(top) | |
335 self.parent.remove_widget(top) | |
336 elif self._split == 'left': | |
337 # we remove all left widgets in delete zone, | |
338 # and update there side widgets list | |
339 for left in self._left_wids.copy(): | |
340 if left.width <= REMOVE_WID_LIMIT: | |
263
fe540a6dc14d
widgets handler: call host._removeVisibleWidget when a widget is deleted (using split)
Goffi <goffi@goffi.org>
parents:
260
diff
changeset
|
341 G.host._removeVisibleWidget(left.current_slide) |
154 | 342 for w in left._left_wids: |
343 w._right_wids.remove(left) | |
344 w._right_wids.update(left._right_wids) | |
345 for w in left._right_wids: | |
346 w._left_wids.remove(left) | |
347 w._left_wids.update(left._left_wids) | |
348 for w in left._top_wids: | |
349 w._bottom_wids.remove(left) | |
350 for w in left._bottom_wids: | |
351 w._top_wids.remove(left) | |
352 self.parent.remove_widget(left) | |
353 self._split = 'None' | |
354 self.canvas.after.remove(self.ellipse) | |
355 del self.ellipse | |
13 | 356 |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
357 def clear_widgets(self): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
358 current_slide = self.current_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
359 if current_slide is not None: |
355
8b6621cc142c
core (widgets handler): clear widgets when WHWrapper is detached:
Goffi <goffi@goffi.org>
parents:
353
diff
changeset
|
360 G.host._removeVisibleWidget(current_slide, ignore_missing=True) |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
361 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
362 super().clear_widgets() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
363 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
364 self.screen_manager = None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
365 self.carousel = None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
366 self._clear_attributes() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
367 |
154 | 368 def set_widget(self, wid, index=0): |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
369 assert len(self.children) == 0 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
370 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
371 if wid.collection_carousel or wid.global_screen_manager: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
372 self.main_container = self |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
373 else: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
374 self.main_container = ScrollView() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
375 self.add_widget(self.main_container) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
376 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
377 if self.carousel is not None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
378 return self.carousel.add_widget(wid, index) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
379 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
380 if wid.global_screen_manager: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
381 if self.screen_manager is None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
382 self.screen_manager = ScreenManager() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
383 self.main_container.add_widget(self.screen_manager) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
384 parent = Screen() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
385 self.screen_manager.add_widget(parent) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
386 self._former_screen_name = '' |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
387 self.screen_manager.bind(current=self.onScreenChange) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
388 wid.screenManagerInit(self.screen_manager) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
389 else: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
390 parent = self.main_container |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
391 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
392 if wid.collection_carousel: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
393 # a Carousel is requested, and this is the first widget that we add |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
394 # so we need to create the carousel |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
395 self.carousel = Carousel( |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
396 direction = "right", |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
397 ignore_perpendicular_swipes = True, |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
398 loop = True, |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
399 ) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
400 self._slides_update_lock = 0 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
401 self.carousel.bind(current_slide=self.onSlideChange) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
402 parent.add_widget(self.carousel) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
403 self.carousel.add_widget(wid, index) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
404 else: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
405 # no Carousel requested, we add the widget as a direct child |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
406 parent.add_widget(wid) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
407 G.host._addVisibleWidget(wid) |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
408 |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
409 def changeWidget(self, new_widget): |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
410 """Change currently displayed widget |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
411 |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
412 slides widgets will be updated |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
413 """ |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
414 if (self.carousel is not None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
415 and self.carousel.current_slide.__class__ == new_widget.__class__): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
416 # we have the same class, we reuse carousel and screen manager setting |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
417 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
418 if self.carousel.current_slide != new_widget: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
419 # slides update need to be blocked to avoid the update in onSlideChange |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
420 # which would mess the removal of current widgets |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
421 self._slides_update_lock += 1 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
422 new_wid = None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
423 for w in self.carousel.slides[:]: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
424 if w.widget_hash == new_widget.widget_hash: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
425 new_wid = w |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
426 continue |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
427 self.carousel.remove_widget(w) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
428 if isinstance(w, quick_widgets.QuickWidget): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
429 G.host.widgets.deleteWidget(w) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
430 if new_wid is None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
431 new_wid = G.host.getOrClone(new_widget) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
432 self.carousel.add_widget(new_wid) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
433 self._updateHiddenSlides() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
434 self._slides_update_lock -= 1 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
435 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
436 if self.screen_manager is not None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
437 self.screen_manager.clear_widgets([ |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
438 s for s in self.screen_manager.screens if s.name != '']) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
439 new_wid.screenManagerInit(self.screen_manager) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
440 else: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
441 # else, we restart fresh |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
442 self.clear_widgets() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
443 self.set_widget(G.host.getOrClone(new_widget)) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
444 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
445 def onScreenChange(self, screen_manager, new_screen): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
446 try: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
447 new_screen_wid = self.current_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
448 except IndexError: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
449 new_screen_wid = None |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
450 log.warning("Switching to a screen without children") |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
451 if new_screen == '' and self.carousel is not None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
452 # carousel may have been changed in the background, so we update slides |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
453 self._updateHiddenSlides() |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
454 former_screen_wid = self.former_screen_wid |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
455 if isinstance(former_screen_wid, cagou_widget.CagouWidget): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
456 G.host._removeVisibleWidget(former_screen_wid) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
457 if isinstance(new_screen_wid, cagou_widget.CagouWidget): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
458 G.host._addVisibleWidget(new_screen_wid) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
459 self._former_screen_name = new_screen |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
460 G.host.selected_widget = new_screen_wid |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
461 |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
462 def onSlideChange(self, handler, new_slide): |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
463 if self._former_slide is new_slide: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
464 # FIXME: workaround for Kivy a95d67f (and above?), Carousel.current_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
465 # binding now calls onSlideChange twice with the same widget (here |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
466 # "new_slide"). To be checked with Kivy team. |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
467 return |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
468 log.debug(f"Slide change: new_slide = {new_slide}") |
248
b6e33b35538b
core, widgets handler: visible_widgets now keep all CagouWidgets, not only QuickWidgets. visible_quick_widgets can be used if only QuickWidgets are desired.
Goffi <goffi@goffi.org>
parents:
177
diff
changeset
|
469 if self._former_slide is not None: |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
470 G.host._removeVisibleWidget(self._former_slide, ignore_missing=True) |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
471 self._former_slide = new_slide |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
472 if self.carousel_active: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
473 G.host.selected_widget = new_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
474 if new_slide is not None: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
475 G.host._addVisibleWidget(new_slide) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
476 self._updateHiddenSlides() |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
477 |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
478 def hiddenList(self, visible_list, ignore=None): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
479 """return widgets of same class as carousel current one, if they are hidden |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
480 |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
481 @param visible_list(list[QuickWidget]): widgets visible |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
482 @param ignore(QuickWidget, None): do no return this widget |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
483 @return (iter[QuickWidget]): widgets hidden |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
484 """ |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
485 # we want to avoid recreated widgets |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
486 added = [w.widget_hash for w in visible_list] |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
487 current_slide = self.carousel.current_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
488 for w in G.host.widgets.getWidgets(current_slide.__class__, |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
489 profiles=current_slide.profiles): |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
490 wid_hash = w.widget_hash |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
491 if w in visible_list or wid_hash in added: |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
492 continue |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
493 if wid_hash == ignore.widget_hash: |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
494 continue |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
495 yield w |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
496 |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
497 def widgets_sort(self, widget): |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
498 """method used as key to sort the widgets |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
499 |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
500 order of the widgets when changing slide is affected |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
501 @param widget(QuickWidget): widget to sort |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
502 @return: a value which will be used for sorting |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
503 """ |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
504 try: |
312 | 505 return str(widget.target).lower() |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
506 except AttributeError: |
312 | 507 return str(list(widget.targets)[0]).lower() |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
508 |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
509 def _updateHiddenSlides(self): |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
510 """adjust carousel slides according to visible widgets""" |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
511 if self._slides_update_lock or not self.carousel_active: |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
512 return |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
513 current_slide = self.carousel.current_slide |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
514 if not isinstance(current_slide, quick_widgets.QuickWidget): |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
515 return |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
516 # lock must be used here to avoid recursions |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
517 self._slides_update_lock += 1 |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
518 visible_list = G.host.getVisibleList(current_slide.__class__) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
519 # we ignore current_slide as it may not be visible yet (e.g. if an other |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
520 # screen is shown |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
521 hidden = list(self.hiddenList(visible_list, ignore=current_slide)) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
522 slides_sorted = sorted(set(hidden + [current_slide]), key=self.widgets_sort) |
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
523 to_remove = set(self.carousel.slides).difference({current_slide}) |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
524 for w in to_remove: |
154 | 525 self.carousel.remove_widget(w) |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
526 if hidden: |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
527 # no need to add more than two widgets (next and previous), |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
528 # as the list will be updated on each new visible widget |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
529 current_idx = slides_sorted.index(current_slide) |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
530 try: |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
531 next_slide = slides_sorted[current_idx+1] |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
532 except IndexError: |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
533 next_slide = slides_sorted[0] |
154 | 534 self.carousel.add_widget(G.host.getOrClone(next_slide)) |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
535 if len(hidden)>1: |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
536 previous_slide = slides_sorted[current_idx-1] |
154 | 537 self.carousel.add_widget(G.host.getOrClone(previous_slide)) |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
538 |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
539 self._slides_update_lock -= 1 |
38
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
540 |
9f45098289cc
widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
541 |
154 | 542 class WidgetsHandlerLayout(Layout): |
543 count = 0 | |
13 | 544 |
154 | 545 def __init__(self, **kwargs): |
546 super(WidgetsHandlerLayout, self).__init__(**kwargs) | |
171
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
547 self._layout_size = None # size used for the last layout |
154 | 548 fbind = self.fbind |
549 update = self._trigger_layout | |
550 fbind('children', update) | |
551 fbind('parent', update) | |
171
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
552 fbind('size', self.adjust_prop) |
154 | 553 fbind('pos', update) |
13 | 554 |
14 | 555 @property |
556 def default_widget(self): | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
557 return G.host.default_wid['factory'](G.host.default_wid, None, None) |
14 | 558 |
171
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
559 def adjust_prop(self, handler, new_size): |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
560 """Adjust children proportion |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
561 |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
562 useful when this widget is resized (e.g. when going to fullscreen) |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
563 """ |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
564 if len(self.children) > 1: |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
565 old_width, old_height = self._layout_size |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
566 if not old_width or not old_height: |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
567 # we don't want division by zero |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
568 return self._trigger_layout(handler, new_size) |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
569 width_factor = float(self.width) / old_width |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
570 height_factor = float(self.height) / old_height |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
571 for child in self.children: |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
572 child.width *= width_factor |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
573 child.height *= height_factor |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
574 child.x *= width_factor |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
575 child.y *= height_factor |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
576 self._trigger_layout(handler, new_size) |
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
577 |
154 | 578 def do_layout(self, *args): |
171
27b4ceb977c7
widgets handler: keep proportion on resize
Goffi <goffi@goffi.org>
parents:
170
diff
changeset
|
579 self._layout_size = self.size[:] |
154 | 580 for child in self.children: |
581 # XXX: left must be calculated before right and bottom before top | |
582 # because they are the pos, and are used to caculate size (right and top) | |
583 # left | |
584 left = child._left_wid | |
170
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
585 left_end_x = self.x-1 if left is None else left.right |
154 | 586 if child.x != left_end_x + 1 and child._split == "None": |
587 child.x = left_end_x + 1 | |
588 # right | |
589 right = child._right_wid | |
170
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
590 right_x = self.right + 1 if right is None else right.x |
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
591 if child.right != right_x - 1: |
154 | 592 child.width = right_x - child.x - 1 |
593 # bottom | |
594 bottom = child._bottom_wid | |
595 if bottom is None: | |
170
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
596 if child.y != self.y: |
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
597 child.y = self.y |
154 | 598 else: |
170
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
599 if child.y != bottom.top + 1: |
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
600 child.y = bottom.top + 1 |
154 | 601 # top |
602 top = child._top_wid | |
170
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
603 top_y = self.top+1 if top is None else top.y |
f4522b7c3318
widgets handler: use widget's top and right
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
604 if child.top != top_y - 1: |
154 | 605 if child._split == "None": |
606 child.height = top_y - child.y - 1 | |
607 | |
608 def remove_widget(self, wid): | |
609 super(WidgetsHandlerLayout, self).remove_widget(wid) | |
610 log.debug("widget deleted ({})".format(wid._wid_idx)) | |
611 | |
612 def add_widget(self, wid=None, index=0): | |
613 WidgetsHandlerLayout.count += 1 | |
614 if wid is None: | |
615 wid = self.default_widget | |
616 wrapper = WHWrapper(_wid_idx=WidgetsHandlerLayout.count) | |
617 log.debug("WHWrapper created ({})".format(wrapper._wid_idx)) | |
618 wrapper.set_widget(wid) | |
619 super(WidgetsHandlerLayout, self).add_widget(wrapper, index) | |
620 return wrapper | |
621 | |
622 | |
623 class WidgetsHandler(WidgetsHandlerLayout): | |
624 | |
625 def __init__(self, **kw): | |
626 super(WidgetsHandler, self).__init__(**kw) | |
353
19422bbd9c8e
core (widgets handler): refactoring:
Goffi <goffi@goffi.org>
parents:
312
diff
changeset
|
627 self.add_widget() |