Mercurial > libervia-desktop-kivy
comparison src/cagou/core/widgets_handler.py @ 15:56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 09 Jul 2016 17:24:01 +0200 |
parents | src/widgets_handler.py@21a432afd06d |
children | ba14b596b90e |
comparison
equal
deleted
inserted
replaced
14:21a432afd06d | 15:56838ad5c84b |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 from sat.core import log as logging | |
22 log = logging.getLogger(__name__) | |
23 from kivy.uix.boxlayout import BoxLayout | |
24 from kivy.uix.button import Button | |
25 from kivy import properties | |
26 | |
27 | |
28 NEW_WIDGET_DIST = 10 | |
29 REMOVE_WIDGET_DIST = NEW_WIDGET_DIST | |
30 | |
31 | |
32 class WHSplitter(Button): | |
33 horizontal=properties.BooleanProperty(True) | |
34 thickness=properties.NumericProperty(15) | |
35 split_move = None # we handle one split at a time, so we use a class attribute | |
36 | |
37 def __init__(self, handler, **kwargs): | |
38 super(WHSplitter, self).__init__(**kwargs) | |
39 self.handler = handler | |
40 | |
41 def getPos(self, touch): | |
42 if self.horizontal: | |
43 relative_y = self.handler.to_local(*touch.pos, relative=True)[1] | |
44 return self.handler.height - relative_y | |
45 else: | |
46 return touch.x | |
47 | |
48 def on_touch_move(self, touch): | |
49 if self.split_move is None and self.collide_point(*touch.opos): | |
50 WHSplitter.split_move = self | |
51 | |
52 if self.split_move is self: | |
53 pos = self.getPos(touch) | |
54 if pos > NEW_WIDGET_DIST: | |
55 # we are above minimal distance, we resize the widget | |
56 self.handler.setWidgetSize(self.horizontal, pos) | |
57 | |
58 def on_touch_up(self, touch): | |
59 if self.split_move is self: | |
60 pos = self.getPos(touch) | |
61 if pos <= REMOVE_WIDGET_DIST: | |
62 # if we go under minimal distance, the widget is not wanted anymore | |
63 self.handler.removeWidget(self.horizontal) | |
64 WHSplitter.split_move=None | |
65 return super(WHSplitter, self).on_touch_up(touch) | |
66 | |
67 | |
68 class WidgetsHandler(BoxLayout): | |
69 | |
70 def __init__(self, host, wid=None, **kw): | |
71 self.host = host | |
72 if wid is None: | |
73 wid=self.default_widget | |
74 self.vert_wid = self.hor_wid = None | |
75 BoxLayout.__init__(self, orientation="vertical", **kw) | |
76 self.blh = BoxLayout(orientation="horizontal") | |
77 self.blv = BoxLayout(orientation="vertical") | |
78 self.blv.add_widget(WHSplitter(self)) | |
79 self.blv.add_widget(wid) | |
80 self.blh.add_widget(WHSplitter(self, horizontal=False)) | |
81 self.blh.add_widget(self.blv) | |
82 self.add_widget(self.blh) | |
83 | |
84 @property | |
85 def default_widget(self): | |
86 return self.host.default_wid['factory'](self.host, self.host.default_wid, None, None) | |
87 | |
88 def removeWidget(self, vertical): | |
89 if vertical and self.vert_wid is not None: | |
90 self.remove_widget(self.vert_wid) | |
91 self.vert_wid = None | |
92 elif self.hor_wid is not None: | |
93 self.blh.remove_widget(self.hor_wid) | |
94 self.hor_wid = None | |
95 | |
96 def setWidgetSize(self, vertical, size): | |
97 if vertical: | |
98 if self.vert_wid is None: | |
99 self.vert_wid = WidgetsHandler(self.host, self.default_widget, size_hint=(1, None)) | |
100 self.add_widget(self.vert_wid, len(self.children)) | |
101 self.vert_wid.height=size | |
102 else: | |
103 if self.hor_wid is None: | |
104 self.hor_wid = WidgetsHandler(self.host, self.default_widget, size_hint=(None, 1)) | |
105 self.blh.add_widget(self.hor_wid, len(self.blh.children)) | |
106 self.hor_wid.width=size |