Mercurial > libervia-desktop-kivy
annotate src/cagou/core/widgets_handler.py @ 34:02acbb297a61
handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 22 Aug 2016 20:58:12 +0200 |
parents | ba14b596b90e |
children | 9f45098289cc |
rev | line source |
---|---|
13 | 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__) | |
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 | 24 from kivy.uix.boxlayout import BoxLayout |
25 from kivy.uix.button import Button | |
26 from kivy import properties | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
27 from cagou import G |
13 | 28 |
29 | |
30 NEW_WIDGET_DIST = 10 | |
31 REMOVE_WIDGET_DIST = NEW_WIDGET_DIST | |
32 | |
33 | |
34 class WHSplitter(Button): | |
35 horizontal=properties.BooleanProperty(True) | |
36 thickness=properties.NumericProperty(15) | |
37 split_move = None # we handle one split at a time, so we use a class attribute | |
38 | |
39 def __init__(self, handler, **kwargs): | |
40 super(WHSplitter, self).__init__(**kwargs) | |
41 self.handler = handler | |
42 | |
43 def getPos(self, touch): | |
44 if self.horizontal: | |
45 relative_y = self.handler.to_local(*touch.pos, relative=True)[1] | |
46 return self.handler.height - relative_y | |
47 else: | |
48 return touch.x | |
49 | |
50 def on_touch_move(self, touch): | |
51 if self.split_move is None and self.collide_point(*touch.opos): | |
52 WHSplitter.split_move = self | |
53 | |
54 if self.split_move is self: | |
55 pos = self.getPos(touch) | |
56 if pos > NEW_WIDGET_DIST: | |
57 # we are above minimal distance, we resize the widget | |
58 self.handler.setWidgetSize(self.horizontal, pos) | |
59 | |
60 def on_touch_up(self, touch): | |
61 if self.split_move is self: | |
62 pos = self.getPos(touch) | |
63 if pos <= REMOVE_WIDGET_DIST: | |
64 # if we go under minimal distance, the widget is not wanted anymore | |
65 self.handler.removeWidget(self.horizontal) | |
66 WHSplitter.split_move=None | |
67 return super(WHSplitter, self).on_touch_up(touch) | |
68 | |
69 | |
70 class WidgetsHandler(BoxLayout): | |
71 | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
72 def __init__(self, wid=None, **kw): |
14 | 73 if wid is None: |
74 wid=self.default_widget | |
13 | 75 self.vert_wid = self.hor_wid = None |
76 BoxLayout.__init__(self, orientation="vertical", **kw) | |
77 self.blh = BoxLayout(orientation="horizontal") | |
78 self.blv = BoxLayout(orientation="vertical") | |
79 self.blv.add_widget(WHSplitter(self)) | |
80 self.blv.add_widget(wid) | |
81 self.blh.add_widget(WHSplitter(self, horizontal=False)) | |
82 self.blh.add_widget(self.blv) | |
83 self.add_widget(self.blh) | |
84 | |
14 | 85 @property |
86 def default_widget(self): | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
87 return G.host.default_wid['factory'](G.host.default_wid, None, None) |
14 | 88 |
13 | 89 def removeWidget(self, vertical): |
90 if vertical and self.vert_wid is not None: | |
91 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
|
92 self.vert_wid.onDelete() |
13 | 93 self.vert_wid = None |
94 elif self.hor_wid is not None: | |
95 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
|
96 self.hor_wid.onDelete() |
13 | 97 self.hor_wid = None |
98 | |
99 def setWidgetSize(self, vertical, size): | |
100 if vertical: | |
101 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
|
102 self.vert_wid = WidgetsHandler(self.default_widget, size_hint=(1, None)) |
13 | 103 self.add_widget(self.vert_wid, len(self.children)) |
104 self.vert_wid.height=size | |
105 else: | |
106 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
|
107 self.hor_wid = WidgetsHandler(self.default_widget, size_hint=(None, 1)) |
13 | 108 self.blh.add_widget(self.hor_wid, len(self.blh.children)) |
109 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
|
110 |
02acbb297a61
handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
111 def onDelete(self): |
02acbb297a61
handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
112 # when this handler is deleted, we need to delete the holded CagouWidget |
02acbb297a61
handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
113 cagou_widget = self.children[0].children[0].children[0] |
02acbb297a61
handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
114 if isinstance(cagou_widget, quick_widgets.QuickWidget): |
02acbb297a61
handler, widget: deleteWidget is now properly called when a QuickWidget is deleted
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
115 G.host.widgets.deleteWidget(cagou_widget) |