annotate src/cagou/core/widgets_handler.py @ 38:9f45098289cc

widgets handler, core: hidden widgets can now be shown with swipes: - a couple of methods have been added to handle visible and hidden widgets - a new getOrClone method allow to recreate a widget if it already has a parent (can happen even if the widget is not shown, e.g. in a carousel) - handler now display hidden widgets of the same class as the displayed one when swiping. For instance, if a chat widget is displayed, and header input is used to show an other one, it's now possible to go back to the former by swiping. QuickWidget.onDelete method can be used to handle if a widget must be really deleted (return True) or just hidden (any other value). - handler use a subclass of Carousel for this new feature, with some adjustement so event can be passed to children without too much delay (and frustration). This may need to be adjusted again in the future. - handler.cagou_widget now give the main displayed widget in the handler - handler.changeWidget must be used when widget need to be changed (it's better to use host.switchWidget which will call it itself)
author Goffi <goffi@goffi.org>
date Sun, 28 Aug 2016 15:27:48 +0200
parents 02acbb297a61
children 1922506846be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import log as logging
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = logging.getLogger(__name__)
34
02acbb297a61 handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents: 16
diff changeset
23 from sat_frontends.quick_frontend import quick_widgets
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from kivy.uix.boxlayout import BoxLayout
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from kivy.uix.button import Button
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
26 from kivy.uix.carousel import Carousel
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
27 from kivy.metrics import dp
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from kivy import properties
16
ba14b596b90e host can now be get as a global value:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
29 from cagou import G
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
32 CAROUSEL_SCROLL_DISTANCE = dp(50)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
33 CAROUSEL_SCROLL_TIMEOUT = 80
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 NEW_WIDGET_DIST = 10
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 REMOVE_WIDGET_DIST = NEW_WIDGET_DIST
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 class WHSplitter(Button):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 horizontal=properties.BooleanProperty(True)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 thickness=properties.NumericProperty(15)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 split_move = None # we handle one split at a time, so we use a class attribute
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def __init__(self, handler, **kwargs):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 super(WHSplitter, self).__init__(**kwargs)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.handler = handler
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def getPos(self, touch):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 if self.horizontal:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 relative_y = self.handler.to_local(*touch.pos, relative=True)[1]
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 return self.handler.height - relative_y
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 else:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return touch.x
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def on_touch_move(self, touch):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 if self.split_move is None and self.collide_point(*touch.opos):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 WHSplitter.split_move = self
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 if self.split_move is self:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 pos = self.getPos(touch)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 if pos > NEW_WIDGET_DIST:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 # we are above minimal distance, we resize the widget
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.handler.setWidgetSize(self.horizontal, pos)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def on_touch_up(self, touch):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if self.split_move is self:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 pos = self.getPos(touch)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 if pos <= REMOVE_WIDGET_DIST:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 # if we go under minimal distance, the widget is not wanted anymore
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.handler.removeWidget(self.horizontal)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 WHSplitter.split_move=None
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return super(WHSplitter, self).on_touch_up(touch)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
74 class HandlerCarousel(Carousel):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
75
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
76 def __init__(self, *args, **kwargs):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
77 super(HandlerCarousel, self).__init__(
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
78 *args,
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
79 direction='right',
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
80 loop=True,
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
81 **kwargs)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
82 self._former_slide = None
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
83 self.bind(current_slide=self.onSlideChange)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
84 self._slides_update_lock = False
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
85
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
86 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
87 """Change currently displayed widget
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
88
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
89 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
90 """
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
91 # slides update need to be blocked to avoid the update in onSlideChange
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
92 # which would mess the removal of current widgets
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
93 self._slides_update_lock = True
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
94 current = self.current_slide
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
95 for w in self.slides:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
96 if w == current or w == new_widget:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
97 continue
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
98 if isinstance(w, quick_widgets.QuickWidget):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
99 G.host.widgets.deleteWidget(w)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
100 self.clear_widgets()
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
101 self.add_widget(new_widget)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
102 self._slides_update_lock = False
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
103 self.updateHiddenSlides()
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
104
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
105 def onSlideChange(self, handler, new_slide):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
106 if isinstance(self._former_slide, quick_widgets.QuickWidget):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
107 G.host.removeVisibleWidget(self._former_slide)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
108 self._former_slide = new_slide
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
109 if isinstance(new_slide, quick_widgets.QuickWidget):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
110 G.host.addVisibleWidget(new_slide)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
111 self.updateHiddenSlides()
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
112
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
113 def hiddenList(self, visible_list):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
114 """return widgets of same class as holded one which are hidden
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
115
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
116 @param visible_list(list[QuickWidget]): widgets visible
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
117 @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
118 """
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
119 added = [(w.targets, w.profiles) for w in visible_list] # we want to avoid recreated widgets
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
120 for w in G.host.widgets.getWidgets(self.current_slide.__class__, profiles=self.current_slide.profiles):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
121 if w in visible_list or (w.targets, w.profiles) in added:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
122 continue
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
123 yield w
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
124
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
125 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
126 """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
127
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
128 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
129 @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
130 @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
131 """
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
132 try:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
133 return unicode(widget.target).lower()
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
134 except AttributeError:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
135 return unicode(list(widget.targets)[0]).lower()
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
136
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
137 def updateHiddenSlides(self):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
138 """adjust carousel slides according to visible widgets"""
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
139 if self._slides_update_lock:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
140 return
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
141 if not isinstance(self.current_slide, quick_widgets.QuickWidget):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
142 return
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
143 # lock must be used here to avoid recursions
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
144 self._slides_update_lock = True
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
145 visible_list = G.host.getVisibleList(self.current_slide.__class__)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
146 hidden = list(self.hiddenList(visible_list))
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
147 slides_sorted = sorted(hidden + [self.current_slide], key=self.widgets_sort)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
148 to_remove = set(self.slides).difference({self.current_slide})
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
149 for w in to_remove:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
150 self.remove_widget(w)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
151 if hidden:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
152 # 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
153 # as the list will be updated on each new visible widget
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
154 current_idx = slides_sorted.index(self.current_slide)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
155 try:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
156 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
157 except IndexError:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
158 next_slide = slides_sorted[0]
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
159 self.add_widget(G.host.getOrClone(next_slide))
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
160 if len(hidden)>1:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
161 previous_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
162 self.add_widget(G.host.getOrClone(previous_slide))
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
163
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
164 if len(self.slides) == 1:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
165 # we block carousel with high scroll_distance to avoid swiping
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
166 # when the is not other instance of the widget
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
167 self.scroll_distance=2**32
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
168 self.scroll_timeout=0
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
169 else:
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
170 self.scroll_distance = CAROUSEL_SCROLL_DISTANCE
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
171 self.scroll_timeout=CAROUSEL_SCROLL_TIMEOUT
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
172 self._slides_update_lock = False
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
173
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
174
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 class WidgetsHandler(BoxLayout):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
16
ba14b596b90e host can now be get as a global value:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
177 def __init__(self, wid=None, **kw):
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
178 if wid is None:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
179 wid=self.default_widget
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 self.vert_wid = self.hor_wid = None
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 BoxLayout.__init__(self, orientation="vertical", **kw)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 self.blh = BoxLayout(orientation="horizontal")
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 self.blv = BoxLayout(orientation="vertical")
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 self.blv.add_widget(WHSplitter(self))
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
185 self.carousel = HandlerCarousel()
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
186 self.blv.add_widget(self.carousel)
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 self.blh.add_widget(WHSplitter(self, horizontal=False))
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 self.blh.add_widget(self.blv)
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 self.add_widget(self.blh)
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
190 self.changeWidget(wid)
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
192 @property
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
193 def default_widget(self):
16
ba14b596b90e host can now be get as a global value:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
194 return G.host.default_wid['factory'](G.host.default_wid, None, None)
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
195
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
196 @property
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
197 def cagou_widget(self):
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
198 """get holded CagouWidget"""
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
199 return self.carousel.current_slide
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
200
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
201 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
202 self.carousel.changeWidget(new_widget)
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
203
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 def removeWidget(self, vertical):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 if vertical and self.vert_wid is not None:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 self.remove_widget(self.vert_wid)
34
02acbb297a61 handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents: 16
diff changeset
207 self.vert_wid.onDelete()
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 self.vert_wid = None
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 elif self.hor_wid is not None:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 self.blh.remove_widget(self.hor_wid)
34
02acbb297a61 handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents: 16
diff changeset
211 self.hor_wid.onDelete()
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 self.hor_wid = None
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 def setWidgetSize(self, vertical, size):
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 if vertical:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 if self.vert_wid is None:
16
ba14b596b90e host can now be get as a global value:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
217 self.vert_wid = WidgetsHandler(self.default_widget, size_hint=(1, None))
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 self.add_widget(self.vert_wid, len(self.children))
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 self.vert_wid.height=size
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 else:
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 if self.hor_wid is None:
16
ba14b596b90e host can now be get as a global value:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
222 self.hor_wid = WidgetsHandler(self.default_widget, size_hint=(None, 1))
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 self.blh.add_widget(self.hor_wid, len(self.blh.children))
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 self.hor_wid.width=size
34
02acbb297a61 handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents: 16
diff changeset
225
02acbb297a61 handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents: 16
diff changeset
226 def onDelete(self):
02acbb297a61 handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents: 16
diff changeset
227 # when this handler is deleted, we need to delete the holded CagouWidget
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
228 cagou_widget = self.cagou_widget
34
02acbb297a61 handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents: 16
diff changeset
229 if isinstance(cagou_widget, quick_widgets.QuickWidget):
38
9f45098289cc widgets handler, core: hidden widgets can now be shown with swipes:
Goffi <goffi@goffi.org>
parents: 34
diff changeset
230 G.host.removeVisibleWidget(cagou_widget)