annotate src/cagou/core/xmlui.py @ 44:7819e9efa250

chat: avatar and nick are now displayed, need further aesthetic improvments
author Goffi <goffi@goffi.org>
date Mon, 29 Aug 2016 01:23:49 +0200
parents c21d1be2e54c
children 65775152aac1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: a SàT frontend
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat_frontends.constants import Const as C
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat_frontends.tools import xmlui
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from kivy.uix.boxlayout import BoxLayout
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from kivy.uix.textinput import TextInput
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from kivy.uix.label import Label
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from kivy.uix.button import Button
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from kivy.uix.widget import Widget
29
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
30 from cagou import G
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
31
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
32
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
33 ## Widgets ##
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class TextWidget(xmlui.TextWidget, Label):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def __init__(self, xmlui_parent, value):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 Label.__init__(self, text=value)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 class PasswordWidget(xmlui.PasswordWidget, TextInput):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def __init__(self, _xmlui_parent, value, read_only=False):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 TextInput.__init__(self, password=True, multiline=False,
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 text=value, readonly=read_only, size=(100,25), size_hint=(1,None))
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def _xmluiSetValue(self, value):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.text = value
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def _xmluiGetValue(self):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return self.text
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
29
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
55 ## Containers ##
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
56
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
57
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 class VerticalContainer(xmlui.VerticalContainer, BoxLayout):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def __init__(self, xmlui_parent):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 BoxLayout.__init__(self, orientation='vertical')
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def _xmluiAppend(self, widget):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.add_widget(widget)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
29
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
67 ## Dialogs ##
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
68
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
69
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
70 class NoteDialog(xmlui.NoteDialog):
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
71
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
72 def __init__(self, _xmlui_parent, title, message, level):
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
73 xmlui.NoteDialog.__init__(self, _xmlui_parent)
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
74 self.title, self.message, self.level = title, message, level
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
75
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
76 def _xmluiShow(self):
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
77 G.host.addNote(self.title, self.message, self.level)
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
78
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
79
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
80 ## Factory ##
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
81
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
82
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 class WidgetFactory(object):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def __getattr__(self, attr):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if attr.startswith("create"):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 cls = globals()[attr[6:]]
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return cls
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
29
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
91 ## Core ##
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
92
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
93
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 class Title(Label):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def __init__(self, *args, **kwargs):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 kwargs['size'] = (100, 25)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 kwargs['size_hint'] = (1,None)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 super(Title, self).__init__(*args, **kwargs)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 class XMLUIPanel(xmlui.XMLUIPanel, BoxLayout):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 widget_factory = WidgetFactory()
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 def __init__(self, host, parsed_xml, title=None, flags=None, callback=None, profile=C.PROF_KEY_NONE):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.close_cb = None
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 BoxLayout.__init__(self, orientation='vertical')
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 xmlui.XMLUIPanel.__init__(self, host, parsed_xml, title, flags, callback, profile)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def setCloseCb(self, close_cb):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 self.close_cb = close_cb
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def _xmluiClose(self):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if self.close_cb is not None:
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.close_cb(self)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 else:
33
c21d1be2e54c core: XMLUI notifications coming from backend are handled:
Goffi <goffi@goffi.org>
parents: 29
diff changeset
117 G.host.closeUI()
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def constructUI(self, parsed_dom):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 xmlui.XMLUIPanel.constructUI(self, parsed_dom)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if self.xmlui_title:
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 self.add_widget(Title(text=self.xmlui_title))
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 self.add_widget(self.main_cont)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 if self.type == 'form':
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 submit_btn = Button(text=_(u"Submit"), size_hint=(1,0.2))
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 submit_btn.bind(on_press=self.onFormSubmitted)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 self.add_widget(submit_btn)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 if not 'NO_CANCEL' in self.flags:
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 cancel_btn = Button(text=_(u"Cancel"), size_hint=(1,0.2))
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 cancel_btn.bind(on_press=self.onFormCancelled)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 self.add_widget(cancel_btn)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 self.add_widget(Widget()) # to have elements on the top
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
33
c21d1be2e54c core: XMLUI notifications coming from backend are handled:
Goffi <goffi@goffi.org>
parents: 29
diff changeset
134 def show(self, *args, **kwargs):
c21d1be2e54c core: XMLUI notifications coming from backend are handled:
Goffi <goffi@goffi.org>
parents: 29
diff changeset
135 if not self.user_action and not kwargs.get("force", False):
c21d1be2e54c core: XMLUI notifications coming from backend are handled:
Goffi <goffi@goffi.org>
parents: 29
diff changeset
136 G.host.addNotifUI(self)
c21d1be2e54c core: XMLUI notifications coming from backend are handled:
Goffi <goffi@goffi.org>
parents: 29
diff changeset
137 else:
c21d1be2e54c core: XMLUI notifications coming from backend are handled:
Goffi <goffi@goffi.org>
parents: 29
diff changeset
138 G.host.showUI(self)
c21d1be2e54c core: XMLUI notifications coming from backend are handled:
Goffi <goffi@goffi.org>
parents: 29
diff changeset
139
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
29
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
141 class XMLUIDialog(xmlui.XMLUIDialog):
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
142 dialog_factory = WidgetFactory()
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
143
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
144
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel)
29
8b5827c43155 notes first draft:
Goffi <goffi@goffi.org>
parents: 15
diff changeset
146 xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog)
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 create = xmlui.create