comparison src/widgets_handler.py @ 13:12a189fbb9ba

widget handler first draft: This widget handle other widgets location and size. It is currently loosely inspired from Blender's UI, and the implementation is currenlty naïve. It should be updated in the future to have a behaviour more close to Blender one.
author Goffi <goffi@goffi.org>
date Fri, 08 Jul 2016 20:18:43 +0200
parents
children 21a432afd06d
comparison
equal deleted inserted replaced
12:30f6586f904b 13:12a189fbb9ba
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 from widget_selector import WidgetSelector
27
28
29 NEW_WIDGET_DIST = 10
30 REMOVE_WIDGET_DIST = NEW_WIDGET_DIST
31
32
33 class WHSplitter(Button):
34 horizontal=properties.BooleanProperty(True)
35 thickness=properties.NumericProperty(15)
36 split_move = None # we handle one split at a time, so we use a class attribute
37
38 def __init__(self, handler, **kwargs):
39 super(WHSplitter, self).__init__(**kwargs)
40 self.handler = handler
41
42 def getPos(self, touch):
43 if self.horizontal:
44 relative_y = self.handler.to_local(*touch.pos, relative=True)[1]
45 return self.handler.height - relative_y
46 else:
47 return touch.x
48
49 def on_touch_move(self, touch):
50 if self.split_move is None and self.collide_point(*touch.opos):
51 WHSplitter.split_move = self
52
53 if self.split_move is self:
54 pos = self.getPos(touch)
55 if pos > NEW_WIDGET_DIST:
56 # we are above minimal distance, we resize the widget
57 self.handler.setWidgetSize(self.horizontal, pos)
58
59 def on_touch_up(self, touch):
60 if self.split_move is self:
61 pos = self.getPos(touch)
62 if pos <= REMOVE_WIDGET_DIST:
63 # if we go under minimal distance, the widget is not wanted anymore
64 self.handler.removeWidget(self.horizontal)
65 WHSplitter.split_move=None
66 return super(WHSplitter, self).on_touch_up(touch)
67
68
69 class WidgetsHandler(BoxLayout):
70
71 def __init__(self, host, wid, **kw):
72 self.host = host
73 self.vert_wid = self.hor_wid = None
74 BoxLayout.__init__(self, orientation="vertical", **kw)
75 self.blh = BoxLayout(orientation="horizontal")
76 self.blv = BoxLayout(orientation="vertical")
77 self.blv.add_widget(WHSplitter(self))
78 self.blv.add_widget(wid)
79 self.blh.add_widget(WHSplitter(self, horizontal=False))
80 self.blh.add_widget(self.blv)
81 self.add_widget(self.blh)
82
83 def removeWidget(self, vertical):
84 if vertical and self.vert_wid is not None:
85 self.remove_widget(self.vert_wid)
86 self.vert_wid = None
87 elif self.hor_wid is not None:
88 self.blh.remove_widget(self.hor_wid)
89 self.hor_wid = None
90
91 def setWidgetSize(self, vertical, size):
92 if vertical:
93 if self.vert_wid is None:
94 self.vert_wid = WidgetsHandler(self.host, WidgetSelector(self.host), size_hint=(1, None))
95 self.add_widget(self.vert_wid, len(self.children))
96 self.vert_wid.height=size
97 else:
98 if self.hor_wid is None:
99 self.hor_wid = WidgetsHandler(self.host, WidgetSelector(self.host), size_hint=(None, 1))
100 self.blh.add_widget(self.hor_wid, len(self.blh.children))
101 self.hor_wid.width=size