Mercurial > libervia-backend
annotate frontends/primitivus/custom_widgets.py @ 125:8d611eb9ae48
primitivus: contact list enhancement
- primitivus: contact list now display groups and sexy name (instead of bare jid)
- primitivus: contact list now show an alert when somebody want to contact you, and its chat window is not on the screen
- primitivus: cursor is now visible when going to chat window (useful to go back in logs)
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 12 Jul 2010 17:50:00 +0800 |
parents | 961e0898271f |
children | 2240f34f6452 |
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 | |
120 | 23 from urwid.escape import utf8decode |
113 | 24 |
25 class Password(urwid.Edit): | |
121 | 26 """Edit box which doesn't show what is entered (show '*' or other char instead)""" |
113 | 27 |
28 def __init__(self, *args, **kwargs): | |
121 | 29 """Same args than Edit.__init__ with an additional keyword arg 'hidden_char' |
30 @param hidden_char: char to show instead of what is actually entered: default '*' | |
31 """ | |
113 | 32 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*' |
33 self.__real_text='' | |
34 super(Password, self).__init__(*args, **kwargs) | |
35 | |
36 def set_edit_text(self, text): | |
37 self.__real_text = text | |
38 hidden_txt = len(text)*'*' | |
39 super(Password, self).set_edit_text(hidden_txt) | |
40 | |
41 def get_edit_text(self): | |
42 return self.__real_text | |
43 | |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
44 class AdvancedEdit(urwid.Edit): |
121 | 45 """Edit box with some custom improvments |
46 new chars: | |
47 - C-a: like 'home' | |
48 - C-e: like 'end' | |
49 - C-k: remove everything on the right of the cursor | |
50 - C-w: remove the word on the back | |
51 new behaviour: emit a 'click' signal when enter is pressed""" | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
52 signals = urwid.Edit.signals + ['click'] |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
53 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
54 def keypress(self, size, key): |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
55 #TODO: insert mode is not managed yet |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
56 if key == 'ctrl a': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
57 key = 'home' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
58 elif key == 'ctrl e': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
59 key = 'end' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
60 elif key == 'ctrl k': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
61 self._delete_highlighted() |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
62 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
|
63 elif key == 'ctrl w': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
64 before = self.edit_text[:self.edit_pos] |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
65 pos = before.rstrip().rfind(" ")+1 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
66 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
|
67 self.set_edit_pos(pos) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
68 elif key == 'enter': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
69 self._emit('click') |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
70 return super(AdvancedEdit, self).keypress(size, key) |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
71 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
72 |
120 | 73 class SurroundedText(urwid.FlowWidget): |
121 | 74 """Text centered on a repeated character (like a Divider, but with a text in the center)""" |
120 | 75 |
76 def __init__(self,text,car=utf8decode('─')): | |
77 self.text=text | |
78 self.car=car | |
79 | |
80 def rows(self,size,focus=False): | |
81 return self.display_widget(size, focus).rows(size, focus) | |
82 | |
83 def render(self, size, focus=False): | |
84 return self.display_widget(size, focus).render(size, focus) | |
85 | |
86 def display_widget(self, size, focus): | |
87 (maxcol,) = size | |
88 middle = (maxcol-len(self.text))/2 | |
89 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car | |
90 return urwid.Text(render_text) | |
91 | |
92 | |
93 | |
113 | 94 class SelectableText(urwid.FlowWidget): |
120 | 95 """Text which can be selected with space""" |
113 | 96 signals = ['change'] |
97 | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
98 def __init__(self, text, align='left', header='', select_attr=None, default_attr=None, selected = False, data=None): |
113 | 99 self.text=unicode(text) |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
100 self.header=header |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
101 if data: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
102 self.data=data |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
103 if select_attr: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
104 self.selected = select_attr |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
105 if default_attr: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
106 self.default = default_attr |
113 | 107 self.align = align |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
108 self.__selected=selected |
113 | 109 |
110 def getValue(self): | |
111 return self.text | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
112 |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
113 def setAttribute(self, name, value): |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
114 """Change attribut used for rendering widget |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
115 @param name: one of |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
116 -default: when not selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
117 -selected: when selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
118 @param value: name of the attribute |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
119 /!\ the attribute name followed by _focus is used when widget has focus""" |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
120 assert name in ['default', 'selected'] |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
121 self.__setattr__(name,value) |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
122 self._invalidate() |
113 | 123 |
124 def setState(self, selected, invisible=False): | |
125 """Change state | |
126 @param selected: boolean state value | |
127 @param invisible: don't emit change signal if True""" | |
128 assert(type(selected)==bool) | |
129 self.__selected=selected | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
130 self._invalidate() |
113 | 131 if not invisible: |
132 self._emit("change", self.__selected) | |
133 | |
134 def getState(self): | |
135 return self.__selected | |
136 | |
137 def selectable(self): | |
138 return True | |
139 | |
140 def keypress(self, size, key): | |
141 if key==' ' or key=='enter': | |
142 self.setState(not self.__selected) | |
143 else: | |
144 return key | |
145 | |
146 def rows(self,size,focus=False): | |
147 return self.display_widget(size, focus).rows(size, focus) | |
148 | |
149 def render(self, size, focus=False): | |
150 return self.display_widget(size, focus).render(size, focus) | |
151 | |
152 def display_widget(self, size, focus): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
153 try: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
154 select_attr = self.selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
155 except AttributeError: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
156 select_attr = 'selected' |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
157 try: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
158 default_attr = self.default |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
159 except AttributeError: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
160 default_attr = 'default' |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
161 attr = select_attr if self.__selected else default_attr |
113 | 162 if focus: |
163 attr+="_focus" | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
164 return urwid.Text((attr,self.header+self.text), align=self.align) |
113 | 165 |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
166 class ClickableText(SelectableText): |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
167 signals = SelectableText.signals + ['click'] |
124 | 168 |
169 def setState(self, selected, invisible=False): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
170 self._emit('click') |
124 | 171 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
172 class GenericList(urwid.WidgetWrap): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
173 signals = ['click','change'] |
113 | 174 |
124 | 175 def __init__(self, options, style=[], align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
176 """ |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
177 Widget managing list of string and their selection |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
178 @param options: list of strings used for options |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
179 @param style: list of string: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
180 - 'single' if only one must be selected |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
181 - 'no_first_select' nothing selected when list is first displayed |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
182 - 'can_select_none' if we can select nothing |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
183 @param align: alignement of text inside the list |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
184 @param on_click: method called when click signal is emited |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
185 @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
|
186 """ |
113 | 187 self.single = 'single' in style |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
188 self.no_first_select = 'no_first_select' in style |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
189 self.can_select_none = 'can_select_none' in style |
113 | 190 self.align = align |
124 | 191 self.option_type = option_type |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
192 self.first_display = True |
113 | 193 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
194 if on_click: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
195 urwid.connect_signal(self, 'click', on_click, user_data) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
196 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
197 if on_change: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
198 urwid.connect_signal(self, 'change', on_change, user_data) |
113 | 199 |
114 | 200 self.content = urwid.SimpleListWalker([]) |
113 | 201 self.list_box = urwid.ListBox(self.content) |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
202 urwid.WidgetWrap.__init__(self, self.list_box) |
114 | 203 self.changeValues(options) |
113 | 204 |
205 def __onStateChange(self, widget, selected): | |
206 if self.single: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
207 if not selected and not self.can_select_none: |
113 | 208 #if in single mode, it's forbidden to unselect a value |
209 widget.setState(True, invisible=True) | |
210 return | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
211 if selected: |
113 | 212 self.unselectAll(invisible=True) |
213 widget.setState(True, invisible=True) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
214 self._emit("click") |
113 | 215 |
216 | |
217 def unselectAll(self, invisible=False): | |
218 for widget in self.content: | |
219 if widget.getState(): | |
220 widget.setState(False, invisible) | |
221 widget._invalidate() | |
222 | |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
223 def deleteValue(self, value): |
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
224 """Delete the first value equal to the param given""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
225 for widget in self.content: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
226 if widget.getValue() == value: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
227 self.content.remove(widget) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
228 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
229 return |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
230 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
|
231 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
232 def getSelectedValue(self): |
114 | 233 """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
|
234 values = self.getSelectedValues() |
114 | 235 return values[0] if values else None |
236 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
237 def getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
238 """Return values of all items""" |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
239 return [widget.getValue() for widget in self.content] |
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 values of selected items""" |
113 | 243 result = [] |
244 for widget in self.content: | |
245 if widget.getState(): | |
246 result.append(widget.getValue()) | |
247 return result | |
248 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
249 def getDisplayWidget(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
250 return self.list_box |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
251 |
113 | 252 def changeValues(self, new_values): |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
253 """Change all value in one shot""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
254 if not self.first_display: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
255 old_selected = self.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
256 widgets = [] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
257 for option in new_values: |
124 | 258 widget = self.option_type(option, self.align) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
259 if not self.first_display and option in old_selected: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
260 widget.setState(True) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
261 widgets.append(widget) |
124 | 262 try: |
263 urwid.connect_signal(widget, 'change', self.__onStateChange) | |
264 except NameError: | |
265 pass #the widget given doesn't support 'change' signal | |
113 | 266 self.content[:] = widgets |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
267 if self.first_display and self.single and new_values and not self.no_first_select: |
114 | 268 self.content[0].setState(True) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
269 display_widget = self.getDisplayWidget() |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
270 self._set_w(display_widget) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
271 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
272 self.first_display = False |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
273 |
113 | 274 def selectValue(self, value): |
275 self.unselectAll() | |
276 idx = 0 | |
277 for widget in self.content: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
278 if widget.getSelectedValue() == value: |
113 | 279 widget.setState(True) |
280 self.list_box.set_focus(idx) | |
281 return | |
282 idx+=1 | |
283 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
284 class List(urwid.FlowWidget): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
285 """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
|
286 signals = ['click','change'] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
287 |
124 | 288 def __init__(self, options, style=[], max_height=5, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): |
289 self.genericList = GenericList(options, style, align, option_type, on_click, on_change, user_data) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
290 self.max_height = max_height |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
291 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
292 def selectable(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
293 return True |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
294 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
295 def keypress(self, size, key): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
296 return self.displayWidget(size,True).keypress(size, key) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
297 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
298 def unselectAll(self, invisible=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
299 return self.genericList.unselectAll(invisible) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
300 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
301 def deleteValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
302 return self.genericList.deleteValue(value) |
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 def getSelectedValue(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
305 return self.genericList.getSelectedValue() |
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 getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
308 return self.genericList.getAllValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
309 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
310 def getSelectedValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
311 return self.genericList.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
312 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
313 def changeValues(self, new_values): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
314 return self.genericList.changeValues(new_values) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
315 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
316 def selectValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
317 return self.genericList.selectValue(value) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
318 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
319 def render(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
320 return self.displayWidget(size, focus).render(size, focus) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
321 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
322 def rows(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
323 return self.displayWidget(size, focus).rows(size, focus) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
324 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
325 def displayWidget(self, size, focus): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
326 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) |
120 | 327 height = min(list_size,self.max_height) or 1 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
328 return urwid.BoxAdapter(self.genericList, height) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
329 |
120 | 330 ## DIALOGS ## |
331 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
332 class GenericDialog(urwid.WidgetWrap): |
113 | 333 |
334 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
335 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
336 | |
337 buttons = None | |
338 | |
339 if "OK/CANCEL" in style: | |
340 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
341 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
342 elif "YES/NO" in style: | |
343 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
344 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
114 | 345 if "OK" in style: |
346 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
113 | 347 if buttons: |
348 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
349 widgets_lst.append(buttons_flow) | |
350 body_content = urwid.SimpleListWalker(widgets_lst) | |
351 frame_body = urwid.ListBox(body_content) | |
352 frame = urwid.Frame(frame_body, frame_header) | |
353 decorated_frame = urwid.LineBox(frame) | |
354 urwid.WidgetWrap.__init__(self, decorated_frame) | |
355 | |
356 | |
357 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
358 class InputDialog(GenericDialog): |
121 | 359 """Dialog with an edit box""" |
113 | 360 |
120 | 361 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs): |
113 | 362 instr_wid = urwid.Text(instrucions+':') |
120 | 363 edit_box = urwid.Edit(edit_text=default_txt) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
364 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) |
113 | 365 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
366 class ConfirmDialog(GenericDialog): |
121 | 367 """Dialog with buttons for confirm or cancel an action""" |
113 | 368 |
369 def __init__(self, title, style=['YES/NO'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
370 GenericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |
114 | 371 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
372 class Alert(GenericDialog): |
121 | 373 """Dialog with just a message and a OK button""" |
114 | 374 |
375 def __init__(self, title, message, style=['OK'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
376 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
|
377 |
120 | 378 ## CONTAINERS ## |
379 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
380 class FocusFrame(urwid.Frame): |
121 | 381 """Frame which manage 'tab' key""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
382 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
383 def keypress(self, size, key): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
384 if key == 'tab': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
385 focus_list = ('header','body','footer') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
386 focus_idx = focus_list.index(self.focus_part) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
387 for i in range(2): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
388 focus_idx = (focus_idx + 1) % len(focus_list) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
389 focus_name = focus_list[focus_idx] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
390 widget = getattr(self,'_'+focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
391 if widget!=None and widget.selectable(): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
392 self.set_focus(focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
393 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
394 return urwid.Frame.keypress(self, size, key) |
120 | 395 |
396 ## DECORATORS ## | |
397 class LabelLine(urwid.LineBox): | |
121 | 398 """Like LineBox, but with a Label centered in the top line""" |
120 | 399 |
400 def __init__(self, original_widget, label_widget): | |
401 urwid.LineBox.__init__(self, original_widget) | |
402 top_columns = self._w.widget_list[0] | |
403 top_columns.widget_list[1] = label_widget | |
124 | 404 |
405 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap): | |
406 def __init__(self, original_widget, left_char = utf8decode("│"), right_char = ''): | |
407 """Draw a separator on left and/or of original_widget.""" | |
120 | 408 |
124 | 409 widgets = [original_widget] |
410 if left_char: | |
411 widgets.insert(0, ('fixed', 1, urwid.SolidFill(left_char))) | |
412 if right_char: | |
413 widgets.append(('fixed', 1, urwid.SolidFill(right_char))) | |
414 columns = urwid.Columns(widgets, box_columns = [0,2], focus_column = 1) | |
415 urwid.WidgetDecoration.__init__(self, original_widget) | |
416 urwid.WidgetWrap.__init__(self, columns) | |
417 | |
418 |