Mercurial > libervia-backend
annotate frontends/primitivus/custom_widgets.py @ 119:ded2431cea5a
Primitivus: chat window / text sending.
Primitivus has now the most basics features \o/
- core: new getVersion method
- primitivus: new debug key (C-d), only work if SàT is in dev version (D in version)
- quick_app: new post_init method, used for automatique task like auto-plug
- primitivus: lists now use genericList (Box) or List (Flow)
- primitivus: List now manage correctly its size
- primitivus: new FocusFrame widget which manage focus changing with 'tab'
- primitivus: advancedEdit now manage 'click' signal
- primitivus: contactList now manager 'change' and 'click' signals
- primitivus: Chat window now working
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 05 Jul 2010 19:13:36 +0800 |
parents | 76055a209ed9 |
children | 1ca5f254ce41 |
rev | line source |
---|---|
113 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import urwid | |
23 | |
24 class Password(urwid.Edit): | |
25 | |
26 def __init__(self, *args, **kwargs): | |
27 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*' | |
28 self.__real_text='' | |
29 super(Password, self).__init__(*args, **kwargs) | |
30 | |
31 def set_edit_text(self, text): | |
32 self.__real_text = text | |
33 hidden_txt = len(text)*'*' | |
34 super(Password, self).set_edit_text(hidden_txt) | |
35 | |
36 def get_edit_text(self): | |
37 return self.__real_text | |
38 | |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
39 class AdvancedEdit(urwid.Edit): |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
40 """Edit box with some custom improvments""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
41 signals = urwid.Edit.signals + ['click'] |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
42 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
43 def keypress(self, size, key): |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
44 #TODO: insert mode is not managed yet |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
45 if key == 'ctrl a': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
46 key = 'home' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
47 elif key == 'ctrl e': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
48 key = 'end' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
49 elif key == 'ctrl k': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
50 self._delete_highlighted() |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
51 self.set_edit_text(self.edit_text[:self.edit_pos]) |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
52 elif key == 'ctrl w': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
53 before = self.edit_text[:self.edit_pos] |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
54 pos = before.rstrip().rfind(" ")+1 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
55 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:]) |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
56 self.set_edit_pos(pos) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
57 elif key == 'enter': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
58 self._emit('click') |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
59 return super(AdvancedEdit, self).keypress(size, key) |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
60 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
61 |
113 | 62 class SelectableText(urwid.FlowWidget): |
63 signals = ['change'] | |
64 | |
65 def __init__(self, text, align='left'): | |
66 self.text=unicode(text) | |
67 self.align = align | |
68 self.__selected=False | |
69 | |
70 def getValue(self): | |
71 return self.text | |
72 | |
73 def setState(self, selected, invisible=False): | |
74 """Change state | |
75 @param selected: boolean state value | |
76 @param invisible: don't emit change signal if True""" | |
77 assert(type(selected)==bool) | |
78 self.__selected=selected | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
79 self._invalidate() |
113 | 80 if not invisible: |
81 self._emit("change", self.__selected) | |
82 | |
83 def getState(self): | |
84 return self.__selected | |
85 | |
86 def selectable(self): | |
87 return True | |
88 | |
89 def keypress(self, size, key): | |
90 if key==' ' or key=='enter': | |
91 self.setState(not self.__selected) | |
92 else: | |
93 return key | |
94 | |
95 def rows(self,size,focus=False): | |
96 return self.display_widget(size, focus).rows(size, focus) | |
97 | |
98 def render(self, size, focus=False): | |
99 return self.display_widget(size, focus).render(size, focus) | |
100 | |
101 def display_widget(self, size, focus): | |
102 attr = 'selected' if self.__selected else 'default' | |
103 if focus: | |
104 attr+="_focus" | |
105 return urwid.Text((attr,self.text), align=self.align) | |
106 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
107 class GenericList(urwid.WidgetWrap): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
108 signals = ['click','change'] |
113 | 109 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
110 def __init__(self, options, style=[], align='left', on_click=None, on_change=None, user_data=None): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
111 """ |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
112 Widget managing list of string and their selection |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
113 @param options: list of strings used for options |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
114 @param style: list of string: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
115 - 'single' if only one must be selected |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
116 - 'no_first_select' nothing selected when list is first displayed |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
117 - 'can_select_none' if we can select nothing |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
118 @param align: alignement of text inside the list |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
119 @param on_click: method called when click signal is emited |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
120 @param user_data: data sent to the callback for click signal |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
121 """ |
113 | 122 self.single = 'single' in style |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
123 self.no_first_select = 'no_first_select' in style |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
124 self.can_select_none = 'can_select_none' in style |
113 | 125 self.align = align |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
126 self.first_display = True |
113 | 127 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
128 if on_click: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
129 urwid.connect_signal(self, 'click', on_click, user_data) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
130 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
131 if on_change: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
132 urwid.connect_signal(self, 'change', on_change, user_data) |
113 | 133 |
114 | 134 self.content = urwid.SimpleListWalker([]) |
113 | 135 self.list_box = urwid.ListBox(self.content) |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
136 urwid.WidgetWrap.__init__(self, self.list_box) |
114 | 137 self.changeValues(options) |
113 | 138 |
139 def __onStateChange(self, widget, selected): | |
140 if self.single: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
141 if not selected and not self.can_select_none: |
113 | 142 #if in single mode, it's forbidden to unselect a value |
143 widget.setState(True, invisible=True) | |
144 return | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
145 if selected: |
113 | 146 self.unselectAll(invisible=True) |
147 widget.setState(True, invisible=True) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
148 self._emit("click") |
113 | 149 |
150 | |
151 def unselectAll(self, invisible=False): | |
152 for widget in self.content: | |
153 if widget.getState(): | |
154 widget.setState(False, invisible) | |
155 widget._invalidate() | |
156 | |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
157 def deleteValue(self, value): |
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
158 """Delete the first value equal to the param given""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
159 for widget in self.content: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
160 if widget.getValue() == value: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
161 self.content.remove(widget) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
162 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
163 return |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
164 raise ValueError("%s ==> %s" % (str(value),str(self.content))) |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
165 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
166 def getSelectedValue(self): |
114 | 167 """Convenience method to get the value selected as a string in single mode, or None""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
168 values = self.getSelectedValues() |
114 | 169 return values[0] if values else None |
170 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
171 def getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
172 """Return values of all items""" |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
173 return [widget.getValue() for widget in self.content] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
174 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
175 def getSelectedValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
176 """Return values of selected items""" |
113 | 177 result = [] |
178 for widget in self.content: | |
179 if widget.getState(): | |
180 result.append(widget.getValue()) | |
181 return result | |
182 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
183 def getDisplayWidget(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
184 return self.list_box |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
185 |
113 | 186 def changeValues(self, new_values): |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
187 """Change all value in one shot""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
188 if not self.first_display: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
189 old_selected = self.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
190 widgets = [] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
191 for option in new_values: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
192 widget = SelectableText(option, self.align) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
193 if not self.first_display and option in old_selected: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
194 widget.setState(True) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
195 widgets.append(widget) |
114 | 196 urwid.connect_signal(widget, 'change', self.__onStateChange) |
113 | 197 self.content[:] = widgets |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
198 if self.first_display and self.single and new_values and not self.no_first_select: |
114 | 199 self.content[0].setState(True) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
200 display_widget = self.getDisplayWidget() |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
201 self._set_w(display_widget) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
202 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
203 self.first_display = False |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
204 |
113 | 205 def selectValue(self, value): |
206 self.unselectAll() | |
207 idx = 0 | |
208 for widget in self.content: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
209 if widget.getSelectedValue() == value: |
113 | 210 widget.setState(True) |
211 self.list_box.set_focus(idx) | |
212 return | |
213 idx+=1 | |
214 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
215 class List(urwid.FlowWidget): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
216 """FlowWidget list, same arguments as GenericList, with an additional one 'max_height'""" |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
217 signals = ['click','change'] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
218 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
219 def __init__(self, options, style=[], max_height=5, align='left', on_click=None, on_change=None, user_data=None): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
220 self.genericList = GenericList(options, style, align, on_click, on_change, user_data) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
221 self.max_height = max_height |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
222 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
223 def selectable(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
224 return True |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
225 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
226 def keypress(self, size, key): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
227 return self.displayWidget(size,True).keypress(size, key) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
228 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
229 def unselectAll(self, invisible=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
230 return self.genericList.unselectAll(invisible) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
231 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
232 def deleteValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
233 return self.genericList.deleteValue(value) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
234 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
235 def getSelectedValue(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
236 return self.genericList.getSelectedValue() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
237 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
238 def getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
239 return self.genericList.getAllValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
240 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
241 def getSelectedValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
242 return self.genericList.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
243 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
244 def changeValues(self, new_values): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
245 return self.genericList.changeValues(new_values) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
246 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
247 def selectValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
248 return self.genericList.selectValue(value) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
249 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
250 def render(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
251 return self.displayWidget(size, focus).render(size, focus) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
252 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
253 def rows(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
254 return self.displayWidget(size, focus).rows(size, focus) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
255 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
256 def displayWidget(self, size, focus): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
257 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
258 height = min(list_size,self.max_height) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
259 return urwid.BoxAdapter(self.genericList, height) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
260 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
261 class GenericDialog(urwid.WidgetWrap): |
113 | 262 |
263 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
264 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
265 | |
266 buttons = None | |
267 | |
268 if "OK/CANCEL" in style: | |
269 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
270 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
271 elif "YES/NO" in style: | |
272 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
273 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
114 | 274 if "OK" in style: |
275 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
113 | 276 if buttons: |
277 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
278 widgets_lst.append(buttons_flow) | |
279 body_content = urwid.SimpleListWalker(widgets_lst) | |
280 frame_body = urwid.ListBox(body_content) | |
281 frame = urwid.Frame(frame_body, frame_header) | |
282 decorated_frame = urwid.LineBox(frame) | |
283 urwid.WidgetWrap.__init__(self, decorated_frame) | |
284 | |
285 | |
286 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
287 class InputDialog(GenericDialog): |
113 | 288 |
289 def __init__(self, title, instrucions, style=['OK/CANCEL'], **kwargs): | |
290 instr_wid = urwid.Text(instrucions+':') | |
291 edit_box = urwid.Edit() | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
292 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) |
113 | 293 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
294 class ConfirmDialog(GenericDialog): |
113 | 295 |
296 def __init__(self, title, style=['YES/NO'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
297 GenericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |
114 | 298 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
299 class Alert(GenericDialog): |
114 | 300 |
301 def __init__(self, title, message, style=['OK'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
302 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
303 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
304 class FocusFrame(urwid.Frame): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
305 """Frame which manage "tab" key""" |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
306 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
307 def keypress(self, size, key): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
308 if key == 'tab': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
309 focus_list = ('header','body','footer') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
310 focus_idx = focus_list.index(self.focus_part) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
311 for i in range(2): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
312 focus_idx = (focus_idx + 1) % len(focus_list) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
313 focus_name = focus_list[focus_idx] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
314 widget = getattr(self,'_'+focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
315 if widget!=None and widget.selectable(): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
316 self.set_focus(focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
317 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
318 return urwid.Frame.keypress(self, size, key) |