Mercurial > libervia-backend
annotate frontends/primitivus/custom_widgets.py @ 177:8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
- AdvancedEdit now support a completion callback
- Notification bar now show progressions
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 16 Aug 2010 21:06:03 +0800 |
parents | a50953ac6191 |
children | 556c2bd7c344 |
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 | |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
44 def insert_text(self, text): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
45 self._edit_text = self.__real_text |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
46 super(Password,self).insert_text(text) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
47 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
48 def render(self, size, focus=False): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
49 return super(Password, self).render(size, focus) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
50 |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
51 class AdvancedEdit(urwid.Edit): |
121 | 52 """Edit box with some custom improvments |
53 new chars: | |
54 - C-a: like 'home' | |
55 - C-e: like 'end' | |
56 - C-k: remove everything on the right of the cursor | |
57 - C-w: remove the word on the back | |
58 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
|
59 signals = urwid.Edit.signals + ['click'] |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
60 |
177
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
61 def setCompletionMethod(self, callback): |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
62 """Define method called when completion is asked |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
63 @callback: method with 2 arguments: |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
64 - the text to complete |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
65 - if there was already a completion, a dict with |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
66 - 'completed':last completion |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
67 - 'completion_pos': cursor position where the completion starts |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
68 - 'position': last completion cursor position |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
69 this dict must be used (and can be filled) to find next completion) |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
70 and which return the full text completed""" |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
71 self.completion_cb = callback |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
72 self.completion_data = {} |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
73 |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
74 def keypress(self, size, key): |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
75 #TODO: insert mode is not managed yet |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
76 if key == 'ctrl a': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
77 key = 'home' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
78 elif key == 'ctrl e': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
79 key = 'end' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
80 elif key == 'ctrl k': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
81 self._delete_highlighted() |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
82 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
|
83 elif key == 'ctrl w': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
84 before = self.edit_text[:self.edit_pos] |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
85 pos = before.rstrip().rfind(" ")+1 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
86 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
|
87 self.set_edit_pos(pos) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
88 elif key == 'enter': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
89 self._emit('click') |
177
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
90 elif key == 'shift tab': |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
91 try: |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
92 before = self.edit_text[:self.edit_pos] |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
93 if self.completion_data: |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
94 if (not self.completion_data['completed'] |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
95 or self.completion_data['position'] != self.edit_pos |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
96 or not before.endswith(self.completion_data['completed'])): |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
97 self.completion_data.clear() |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
98 else: |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
99 before = before[:-len(self.completion_data['completed'])] |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
100 complet = self.completion_cb(before, self.completion_data) |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
101 self.completion_data['completed'] = complet[len(before):] |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
102 self.set_edit_text(complet+self.edit_text[self.edit_pos:]) |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
103 self.set_edit_pos(len(complet)) |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
104 self.completion_data['position'] = self.edit_pos |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
105 return |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
106 except AttributeError: |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
107 #No completion method defined |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
108 pass |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
109 return super(AdvancedEdit, self).keypress(size, key) |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
110 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
111 |
120 | 112 class SurroundedText(urwid.FlowWidget): |
121 | 113 """Text centered on a repeated character (like a Divider, but with a text in the center)""" |
120 | 114 |
115 def __init__(self,text,car=utf8decode('─')): | |
116 self.text=text | |
117 self.car=car | |
118 | |
119 def rows(self,size,focus=False): | |
120 return self.display_widget(size, focus).rows(size, focus) | |
121 | |
122 def render(self, size, focus=False): | |
123 return self.display_widget(size, focus).render(size, focus) | |
124 | |
125 def display_widget(self, size, focus): | |
126 (maxcol,) = size | |
127 middle = (maxcol-len(self.text))/2 | |
128 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car | |
129 return urwid.Text(render_text) | |
130 | |
113 | 131 class SelectableText(urwid.FlowWidget): |
120 | 132 """Text which can be selected with space""" |
113 | 133 signals = ['change'] |
134 | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
135 def __init__(self, text, align='left', header='', select_attr=None, default_attr=None, selected = False, data=None): |
113 | 136 self.text=unicode(text) |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
137 self.header=header |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
138 if data: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
139 self.data=data |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
140 if select_attr: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
141 self.selected = select_attr |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
142 if default_attr: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
143 self.default = default_attr |
113 | 144 self.align = align |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
145 self.__selected=selected |
113 | 146 |
147 def getValue(self): | |
148 return self.text | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
149 |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
150 def get_text(self): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
151 """for compatibility with urwid.Text""" |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
152 return self.getValue() |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
153 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
154 def set_text(self, text): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
155 self.text=unicode(text) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
156 self._invalidate() |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
157 |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
158 def setAttribute(self, name, value): |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
159 """Change attribut used for rendering widget |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
160 @param name: one of |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
161 -default: when not selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
162 -selected: when selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
163 @param value: name of the attribute |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
164 /!\ 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
|
165 assert name in ['default', 'selected'] |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
166 self.__setattr__(name,value) |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
167 self._invalidate() |
113 | 168 |
169 def setState(self, selected, invisible=False): | |
170 """Change state | |
171 @param selected: boolean state value | |
172 @param invisible: don't emit change signal if True""" | |
173 assert(type(selected)==bool) | |
174 self.__selected=selected | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
175 self._invalidate() |
113 | 176 if not invisible: |
177 self._emit("change", self.__selected) | |
178 | |
179 def getState(self): | |
180 return self.__selected | |
181 | |
182 def selectable(self): | |
183 return True | |
184 | |
185 def keypress(self, size, key): | |
186 if key==' ' or key=='enter': | |
187 self.setState(not self.__selected) | |
188 else: | |
189 return key | |
190 | |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
191 def mouse_event(self, size, event, button, x, y, focus): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
192 if urwid.is_mouse_press(event) and button == 1: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
193 self.setState(not self.__selected) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
194 return True |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
195 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
196 return False |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
197 |
113 | 198 def rows(self,size,focus=False): |
199 return self.display_widget(size, focus).rows(size, focus) | |
200 | |
201 def render(self, size, focus=False): | |
202 return self.display_widget(size, focus).render(size, focus) | |
203 | |
204 def display_widget(self, size, focus): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
205 try: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
206 select_attr = self.selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
207 except AttributeError: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
208 select_attr = 'selected' |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
209 try: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
210 default_attr = self.default |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
211 except AttributeError: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
212 default_attr = 'default' |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
213 attr = select_attr if self.__selected else default_attr |
113 | 214 if focus: |
215 attr+="_focus" | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
216 return urwid.Text((attr,self.header+self.text), align=self.align) |
113 | 217 |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
218 class ClickableText(SelectableText): |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
219 signals = SelectableText.signals + ['click'] |
124 | 220 |
221 def setState(self, selected, invisible=False): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
222 self._emit('click') |
124 | 223 |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
224 class CustomButton(ClickableText): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
225 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
226 def __init__(self, label, on_press=None, user_data=None, left_border = "[ ", right_border = " ]"): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
227 self.label = label |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
228 render_txt = "%s%s%s" % (left_border, label, right_border) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
229 self.size = len(render_txt) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
230 super(CustomButton, self).__init__(render_txt) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
231 if on_press: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
232 urwid.connect_signal(self, 'click', on_press, user_data) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
233 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
234 def getSize(self): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
235 """Return representation size of the button""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
236 return self.size |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
237 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
238 def get_label(self): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
239 return self.label |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
240 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
241 class GenericList(urwid.WidgetWrap): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
242 signals = ['click','change'] |
113 | 243 |
124 | 244 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
|
245 """ |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
246 Widget managing list of string and their selection |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
247 @param options: list of strings used for options |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
248 @param style: list of string: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
249 - 'single' if only one must be selected |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
250 - 'no_first_select' nothing selected when list is first displayed |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
251 - 'can_select_none' if we can select nothing |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
252 @param align: alignement of text inside the list |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
253 @param on_click: method called when click signal is emited |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
254 @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
|
255 """ |
113 | 256 self.single = 'single' in style |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
257 self.no_first_select = 'no_first_select' in style |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
258 self.can_select_none = 'can_select_none' in style |
113 | 259 self.align = align |
124 | 260 self.option_type = option_type |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
261 self.first_display = True |
113 | 262 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
263 if on_click: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
264 urwid.connect_signal(self, 'click', on_click, user_data) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
265 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
266 if on_change: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
267 urwid.connect_signal(self, 'change', on_change, user_data) |
113 | 268 |
114 | 269 self.content = urwid.SimpleListWalker([]) |
113 | 270 self.list_box = urwid.ListBox(self.content) |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
271 urwid.WidgetWrap.__init__(self, self.list_box) |
114 | 272 self.changeValues(options) |
113 | 273 |
274 def __onStateChange(self, widget, selected): | |
275 if self.single: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
276 if not selected and not self.can_select_none: |
113 | 277 #if in single mode, it's forbidden to unselect a value |
278 widget.setState(True, invisible=True) | |
279 return | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
280 if selected: |
113 | 281 self.unselectAll(invisible=True) |
282 widget.setState(True, invisible=True) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
283 self._emit("click") |
113 | 284 |
285 | |
286 def unselectAll(self, invisible=False): | |
287 for widget in self.content: | |
288 if widget.getState(): | |
289 widget.setState(False, invisible) | |
290 widget._invalidate() | |
291 | |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
292 def deleteValue(self, value): |
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
293 """Delete the first value equal to the param given""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
294 for widget in self.content: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
295 if widget.getValue() == value: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
296 self.content.remove(widget) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
297 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
298 return |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
299 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
|
300 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
301 def getSelectedValue(self): |
114 | 302 """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
|
303 values = self.getSelectedValues() |
114 | 304 return values[0] if values else None |
305 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
306 def getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
307 """Return values of all items""" |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
308 return [widget.getValue() for widget in self.content] |
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 values of selected items""" |
113 | 312 result = [] |
313 for widget in self.content: | |
314 if widget.getState(): | |
315 result.append(widget.getValue()) | |
316 return result | |
317 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
318 def getDisplayWidget(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
319 return self.list_box |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
320 |
113 | 321 def changeValues(self, new_values): |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
322 """Change all value in one shot""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
323 if not self.first_display: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
324 old_selected = self.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
325 widgets = [] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
326 for option in new_values: |
124 | 327 widget = self.option_type(option, self.align) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
328 if not self.first_display and option in old_selected: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
329 widget.setState(True) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
330 widgets.append(widget) |
124 | 331 try: |
332 urwid.connect_signal(widget, 'change', self.__onStateChange) | |
333 except NameError: | |
334 pass #the widget given doesn't support 'change' signal | |
113 | 335 self.content[:] = widgets |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
336 if self.first_display and self.single and new_values and not self.no_first_select: |
114 | 337 self.content[0].setState(True) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
338 display_widget = self.getDisplayWidget() |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
339 self._set_w(display_widget) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
340 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
341 self.first_display = False |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
342 |
113 | 343 def selectValue(self, value): |
344 self.unselectAll() | |
345 idx = 0 | |
346 for widget in self.content: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
347 if widget.getSelectedValue() == value: |
113 | 348 widget.setState(True) |
349 self.list_box.set_focus(idx) | |
350 return | |
351 idx+=1 | |
352 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
353 class List(urwid.FlowWidget): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
354 """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
|
355 signals = ['click','change'] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
356 |
124 | 357 def __init__(self, options, style=[], max_height=5, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): |
358 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
|
359 self.max_height = max_height |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
360 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
361 def selectable(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
362 return True |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
363 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
364 def keypress(self, size, key): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
365 return self.displayWidget(size,True).keypress(size, key) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
366 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
367 def unselectAll(self, invisible=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
368 return self.genericList.unselectAll(invisible) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
369 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
370 def deleteValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
371 return self.genericList.deleteValue(value) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
372 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
373 def getSelectedValue(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
374 return self.genericList.getSelectedValue() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
375 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
376 def getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
377 return self.genericList.getAllValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
378 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
379 def getSelectedValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
380 return self.genericList.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
381 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
382 def changeValues(self, new_values): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
383 return self.genericList.changeValues(new_values) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
384 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
385 def selectValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
386 return self.genericList.selectValue(value) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
387 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
388 def render(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
389 return self.displayWidget(size, focus).render(size, focus) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
390 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
391 def rows(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
392 return self.displayWidget(size, focus).rows(size, focus) |
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 def displayWidget(self, size, focus): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
395 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) |
120 | 396 height = min(list_size,self.max_height) or 1 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
397 return urwid.BoxAdapter(self.genericList, height) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
398 |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
399 ## MISC ## |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
400 |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
401 class NotificationBar(urwid.WidgetWrap): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
402 """Bar used to show misc information to user""" |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
403 signals = ['change'] |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
404 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
405 def __init__(self): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
406 self.waitNotifs = urwid.Text('') |
177
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
407 self.message = ClickableText('', default_attr='notifs') |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
408 urwid.connect_signal(self.message, 'click', lambda wid: self.showNext()) |
177
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
409 self.progress = ClickableText('', default_attr='notifs') |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
410 self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message,('fixed',4,self.progress)]) |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
411 urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs')) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
412 self.notifs = [] |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
413 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
414 def __modQueue(self): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
415 """must be called each time the notifications queue is changed""" |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
416 self.waitNotifs.set_text("(%i)" % len(self.notifs) if self.notifs else '') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
417 self._emit('change') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
418 |
177
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
419 def setProgress(self,percentage): |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
420 """Define the progression to show on the right side of the bar""" |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
421 if percentage == None: |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
422 self.progress.set_text('') |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
423 else: |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
424 self.progress.set_text('%02i%%' % percentage) |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
425 self._emit('change') |
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
426 |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
427 def addPopUp(self, pop_up_widget): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
428 """Add a popup to the waiting queue""" |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
429 self.notifs.append(('popup',pop_up_widget)) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
430 self.__modQueue() |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
431 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
432 def addMessage(self, message): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
433 "Add a message to the notificatio bar" |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
434 if not self.message.get_text(): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
435 self.message.set_text(message) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
436 self._invalidate() |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
437 self._emit('change') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
438 else: |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
439 self.notifs.append(('message',message)) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
440 self.__modQueue() |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
441 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
442 def showNext(self): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
443 """Show next message if any, else delete current message""" |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
444 found = None |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
445 for notif in self.notifs: |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
446 if notif[0] == "message": |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
447 found = notif |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
448 break |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
449 if found: |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
450 self.notifs.remove(found) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
451 self.message.set_text(found[1]) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
452 self.__modQueue() |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
453 else: |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
454 self.message.set_text('') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
455 self._emit('change') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
456 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
457 def getNextPopup(self): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
458 """Return next pop-up and remove it from the queue |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
459 @return: pop-up or None if there is no more in the queue""" |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
460 ret = None |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
461 for notif in self.notifs: |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
462 if notif[0] == 'popup': |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
463 ret = notif[1] |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
464 break |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
465 if ret: |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
466 self.notifs.remove(notif) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
467 self.__modQueue() |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
468 return ret |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
469 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
470 def isQueueEmpty(self): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
471 return not bool(self.notifs) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
472 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
473 def canHide(self): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
474 """Return True if there is now important information to show""" |
177
8f56238309d9
Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents:
176
diff
changeset
|
475 return self.isQueueEmpty() and not self.message.get_text() and not self.progress.get_text() |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
476 |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
151
diff
changeset
|
477 |
131 | 478 class MenuBox(urwid.WidgetWrap): |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
479 """Show menu items of a category in a box""" |
131 | 480 signals = ['click'] |
481 | |
482 def __init__(self,parent,items): | |
483 self.parent = parent | |
484 self.selected = None | |
485 content = urwid.SimpleListWalker([ClickableText(text,default_attr='menuitem') for text in items]) | |
486 for wid in content: | |
487 urwid.connect_signal(wid, 'click', self.onClick) | |
488 | |
489 self.listBox = urwid.ListBox(content) | |
490 menubox = urwid.LineBox(urwid.BoxAdapter(self.listBox,len(items))) | |
491 urwid.WidgetWrap.__init__(self,menubox) | |
492 | |
493 def getValue(self): | |
494 return self.selected | |
495 | |
496 def keypress(self, size, key): | |
497 if key=='up': | |
498 if self.listBox.get_focus()[1] == 0: | |
499 self.parent.keypress(size, key) | |
500 elif key=='left' or key=='right': | |
501 self.parent.keypress(size,'up') | |
502 self.parent.keypress(size,key) | |
503 return super(MenuBox,self).keypress(size,key) | |
504 | |
505 def onClick(self, wid): | |
506 self.selected = wid.getValue() | |
507 self._emit('click') | |
508 | |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
509 class Menu(urwid.FlowWidget): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
510 |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
511 def __init__(self,loop, x_orig=0): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
512 """Menu widget |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
513 @param loop: main loop of urwid |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
514 @param x_orig: absolute start of the abscissa |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
515 """ |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
516 super(Menu, self).__init__() |
131 | 517 self.loop = loop |
518 self.menu_keys = [] | |
519 self.menu = {} | |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
520 self.x_orig = x_orig |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
521 self.shortcuts = {} #keyboard shortcuts |
131 | 522 self.focus_menu = 0 |
523 self.save_bottom = None | |
524 | |
525 def selectable(self): | |
526 return True | |
139
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
527 |
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
528 def getMenuSize(self): |
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
529 """return the current number of categories in this menu""" |
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
530 return len(self.menu_keys) |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
531 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
532 def setOrigX(self, orig_x): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
533 self.x_orig = orig_x |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
534 |
131 | 535 def __buildOverlay(self,menu_key,columns): |
536 """Build the overlay menu which show menuitems | |
537 @param menu_key: name of the category | |
538 @colums: column number where the menubox must be displayed""" | |
539 max_len = 0 | |
540 for item in self.menu[menu_key]: | |
541 if len(item[0]) > max_len: | |
542 max_len = len(item[0]) | |
543 | |
544 self.save_bottom = self.loop.widget | |
545 menu_box = MenuBox(self,[item[0] for item in self.menu[menu_key]]) | |
546 urwid.connect_signal(menu_box, 'click', self.onClick) | |
547 | |
548 self.loop.widget = urwid.Overlay(urwid.AttrMap(menu_box,'menubar'),self.save_bottom,('fixed left', columns),max_len+2,('fixed top',1),None) | |
549 | |
550 def keypress(self, size, key): | |
551 if key == 'right' and self.focus_menu < len(self.menu)-1: | |
552 self.focus_menu += 1 | |
553 self._invalidate() | |
554 elif key == 'left' and self.focus_menu > 0: | |
555 self.focus_menu -= 1 | |
556 self._invalidate() | |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
557 return |
131 | 558 elif key == 'down': |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
559 if self.menu_keys and not self.save_bottom: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
560 column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+self.x_orig) |
131 | 561 self.__buildOverlay(self.menu_keys[self.focus_menu],column) |
562 elif key == 'up': | |
563 if self.save_bottom: | |
564 self.loop.widget = self.save_bottom | |
565 self.save_bottom = None | |
566 | |
567 return key | |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
568 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
569 def checkShortcuts(self, key): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
570 for shortcut in self.shortcuts.keys(): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
571 if key == shortcut: |
131 | 572 category, item, callback = self.shortcuts[shortcut] |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
573 callback((category, item)) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
574 return key |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
575 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
576 def addMenu(self, category, item, callback, shortcut=None): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
577 """Add a menu item, create the category if new |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
578 @param category: category of the menu (e.g. File/Edit) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
579 @param item: menu item (e.g. new/close/about) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
580 @callback: method to call when item is selected""" |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
581 if not category in self.menu.keys(): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
582 self.menu_keys.append(category) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
583 self.menu[category] = [] |
131 | 584 self.menu[category].append((item, callback)) |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
585 if shortcut: |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
586 assert(shortcut not in self.shortcuts.keys()) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
587 self.shortcuts[shortcut] = (category, item, callback) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
588 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
589 def rows(self,size,focus=False): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
590 return self.display_widget(size, focus).rows(size, focus) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
591 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
592 def render(self, size, focus=False): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
593 return self.display_widget(size, focus).render(size, focus) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
594 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
595 def display_widget(self, size, focus): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
596 render_txt = [] |
131 | 597 idx = 0 |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
598 for menu in self.menu_keys: |
131 | 599 if focus and idx == self.focus_menu: |
600 render_txt.append(('selected_menu', '[ %s ]' % menu)) | |
601 render_txt.append(' ') | |
602 else: | |
603 render_txt.append('[ %s ] ' % menu) | |
604 idx += 1 | |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
605 return urwid.AttrMap(urwid.Text(render_txt), 'menubar') |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
606 |
131 | 607 def onClick(self, widget): |
608 category = self.menu_keys[self.focus_menu] | |
609 item = widget.getValue() | |
610 for menu_item in self.menu[category]: | |
611 if item == menu_item[0]: | |
612 callback = menu_item[1] | |
613 break | |
614 if callback: | |
615 self.keypress(None,'up') | |
616 callback((category, item)) | |
617 | |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
618 class MenuRoller(urwid.WidgetWrap): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
619 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
620 def __init__(self,menus_list): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
621 """Create a MenuRoller |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
622 @param menus_list: list of tuple with (name, Menu_instance), name can be None |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
623 """ |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
624 assert (menus_list) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
625 self.selected = 0 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
626 self.name_list = [] |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
627 self.menus = {} |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
628 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
629 self.columns = urwid.Columns([urwid.Text(''),urwid.Text('')]) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
630 urwid.WidgetWrap.__init__(self, self.columns) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
631 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
632 for menu_tuple in menus_list: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
633 name,menu = menu_tuple |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
634 self.addMenu(name, menu) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
635 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
636 def __showSelected(self): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
637 """show menu selected""" |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
638 name_txt = u'\u21c9 '+self.name_list[self.selected]+u' \u21c7 ' |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
639 current_name = ClickableText(name_txt) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
640 name_len = len(name_txt) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
641 current_menu = self.menus[self.name_list[self.selected]] |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
642 current_menu.setOrigX(name_len) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
643 self.columns.widget_list[0] = current_name |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
644 self.columns.column_types[0]=('fixed', name_len) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
645 self.columns.widget_list[1] = current_menu |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
646 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
647 def keypress(self, size, key): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
648 if key=='up': |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
649 if self.columns.get_focus_column()==0 and self.selected > 0: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
650 self.selected -= 1 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
651 self.__showSelected() |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
652 elif key=='down': |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
653 if self.columns.get_focus_column()==0 and self.selected < len(self.name_list)-1: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
654 self.selected += 1 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
655 self.__showSelected() |
139
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
656 elif key=='right': |
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
657 if self.columns.get_focus_column()==0 and \ |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
658 (isinstance(self.columns.widget_list[1], urwid.Text) or \ |
139
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
659 self.menus[self.name_list[self.selected]].getMenuSize()==0): |
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
660 return #if we have no menu or the menu is empty, we don't go the right column |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
661 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
662 return super(MenuRoller, self).keypress(size, key) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
663 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
664 def addMenu(self, name_param, menu): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
665 name = name_param or '' |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
666 if name not in self.name_list: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
667 self.name_list.append(name) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
668 self.menus[name] = menu |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
669 if self.name_list[self.selected] == name: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
670 self.__showSelected() #if we are on the menu, we update it |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
671 |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
672 def removeMenu(self, name): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
673 if name in self.name_list: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
674 self.name_list.remove(name) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
675 if name in self.menus.keys(): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
676 del self.menus[name] |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
677 self.selected = 0 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
678 self.__showSelected() |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
679 |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
680 def checkShortcuts(self, key): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
681 for menu in self.name_list: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
682 key = self.menus[menu].checkShortcuts(key) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
683 return key |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
684 |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
685 |
120 | 686 ## DIALOGS ## |
687 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
688 class GenericDialog(urwid.WidgetWrap): |
113 | 689 |
690 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
691 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
692 | |
693 buttons = None | |
694 | |
695 if "OK/CANCEL" in style: | |
696 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
697 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
698 elif "YES/NO" in style: | |
699 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
700 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
114 | 701 if "OK" in style: |
702 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
113 | 703 if buttons: |
704 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
705 body_content = urwid.SimpleListWalker(widgets_lst) | |
706 frame_body = urwid.ListBox(body_content) | |
172
726786e914c3
Primitivus: buttons are now in the bottom of Dialogs
Goffi <goffi@goffi.org>
parents:
167
diff
changeset
|
707 frame = urwid.Frame(frame_body, frame_header, buttons_flow if buttons else None) |
113 | 708 decorated_frame = urwid.LineBox(frame) |
709 urwid.WidgetWrap.__init__(self, decorated_frame) | |
710 | |
711 | |
712 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
713 class InputDialog(GenericDialog): |
121 | 714 """Dialog with an edit box""" |
113 | 715 |
120 | 716 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs): |
113 | 717 instr_wid = urwid.Text(instrucions+':') |
120 | 718 edit_box = urwid.Edit(edit_text=default_txt) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
719 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) |
113 | 720 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
721 class ConfirmDialog(GenericDialog): |
121 | 722 """Dialog with buttons for confirm or cancel an action""" |
113 | 723 |
724 def __init__(self, title, style=['YES/NO'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
725 GenericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |
114 | 726 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
727 class Alert(GenericDialog): |
121 | 728 """Dialog with just a message and a OK button""" |
114 | 729 |
730 def __init__(self, title, message, style=['OK'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
731 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
|
732 |
120 | 733 ## CONTAINERS ## |
734 | |
163
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
735 class ColumnsRoller(urwid.FlowWidget): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
736 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
737 def __init__(self, widget_list = None, focus_column=0): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
738 self.widget_list = widget_list or [] |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
739 self.focus_column = focus_column |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
740 self.__start = 0 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
741 self.__next = False |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
742 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
743 def addWidget(self, widget, width): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
744 self.widget_list.append((width,widget)) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
745 if len(self.widget_list) == 1: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
746 self.set_focus(0) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
747 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
748 def selectable(self): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
749 try: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
750 return self.widget_list[self.focus_column][1].selectable() |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
751 except IndexError: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
752 return False |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
753 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
754 def keypress(self, size, key): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
755 if key=='left': |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
756 if self.focus_column>0: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
757 self.focus_column-=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
758 self._invalidate() |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
759 return |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
760 if key=='right': |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
761 if self.focus_column<len(self.widget_list)-1: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
762 self.focus_column+=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
763 self._invalidate() |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
764 return |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
765 if self.focus_column<len(self.widget_list): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
766 return self.widget_list[self.focus_column][1].keypress(size,key) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
767 return key |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
768 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
769 def set_focus(self, idx): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
770 if idx>len(self.widget_list)-1: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
771 idx = len(self.widget_list)-1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
772 self.focus_column = idx |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
773 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
774 def rows(self,size,focus=False): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
775 return 1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
776 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
777 def __calculate_limits(self, size): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
778 (maxcol,) = size |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
779 _prev = _next = False |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
780 start_wid = 0 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
781 end_wid = len(self.widget_list)-1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
782 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
783 total_wid = sum([w[0] for w in self.widget_list]) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
784 while total_wid > maxcol: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
785 if self.focus_column == end_wid: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
786 if not _prev: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
787 total_wid+=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
788 _prev = True |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
789 total_wid-=self.widget_list[start_wid][0] |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
790 start_wid+=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
791 else: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
792 if not _next: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
793 total_wid+=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
794 _next = True |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
795 total_wid-=self.widget_list[end_wid][0] |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
796 end_wid-=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
797 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
798 cols_left = maxcol - total_wid |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
799 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
800 return _prev,_next,start_wid,end_wid,cols_left |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
801 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
802 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
803 def mouse_event(self, size, event, button, x, y, focus): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
804 (maxcol,)=size |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
805 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
806 if urwid.is_mouse_press(event) and button == 1: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
807 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
808 if x==0 and _prev: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
809 self.keypress(size,'left') |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
810 return True |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
811 if x==maxcol-1 and _next: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
812 self.keypress(size,'right') |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
813 return True |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
814 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
815 current_pos = 1 if _prev else 0 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
816 idx = 0 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
817 while current_pos<x and idx<len(self.widget_list): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
818 width,widget = self.widget_list[idx] |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
819 if x<=current_pos+width: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
820 self.focus_column = idx |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
821 self._invalidate() |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
822 if not hasattr(widget,'mouse_event'): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
823 return False |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
824 return widget.mouse_event((width,0), event, button, |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
825 x-current_pos, 0, focus) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
826 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
827 current_pos+=self.widget_list[idx][0] |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
828 idx+=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
829 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
830 return False |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
831 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
832 def render(self, size, focus=False): |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
833 if not self.widget_list: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
834 return SolidCanvas(" ", size[0], 1) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
835 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
836 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
837 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
838 idx=start_wid |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
839 render = [] |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
840 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
841 for width,widget in self.widget_list[start_wid:end_wid+1]: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
842 _focus = idx == self.focus_column and focus |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
843 render.append((widget.render((width,),_focus),False,_focus,width)) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
844 idx+=1 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
845 if _prev: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
846 render.insert(0,(urwid.Text([u"◀"]).render((1,),False),False,False,1)) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
847 if _next: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
848 render.append((urwid.Text([u"▶"],align='right').render((1+cols_left,),False),False,False,1+cols_left)) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
849 else: |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
850 render.append((urwid.SolidCanvas(" "*cols_left, size[0], 1),False,False,cols_left)) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
851 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
852 return urwid.CanvasJoin(render) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
853 |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
854 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
855 class FocusFrame(urwid.Frame): |
121 | 856 """Frame which manage 'tab' key""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
857 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
858 def keypress(self, size, key): |
167
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
859 ret = urwid.Frame.keypress(self, size, key) |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
860 if not ret: |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
861 return |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
862 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
863 if key == 'tab': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
864 focus_list = ('header','body','footer') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
865 focus_idx = focus_list.index(self.focus_part) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
866 for i in range(2): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
867 focus_idx = (focus_idx + 1) % len(focus_list) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
868 focus_name = focus_list[focus_idx] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
869 widget = getattr(self,'_'+focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
870 if widget!=None and widget.selectable(): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
871 self.set_focus(focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
872 |
167
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
873 return ret |
120 | 874 |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
875 class TabsContainer(urwid.WidgetWrap): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
876 signals = ['click'] |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
877 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
878 def __init__(self): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
879 #self._current_tab = 0 |
163
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
880 self._buttons_cont = ColumnsRoller() |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
881 self.tabs = [] |
167
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
882 self.__frame = FocusFrame(urwid.Filler(urwid.Text('')),urwid.Pile([self._buttons_cont,urwid.Divider(u"─")])) |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
883 urwid.WidgetWrap.__init__(self, self.__frame) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
884 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
885 def keypress(self, size, key): |
167
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
886 if key=='tab': |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
887 self._w.keypress(size,key) |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
888 return |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
889 return self._w.keypress(size,key) |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
890 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
891 def __buttonClicked(self, button, invisible=False): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
892 """Called when a button on the tab is changed, |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
893 change the page |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
894 @param button: button clicked |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
895 @param invisible: emit signal only if False""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
896 tab_name = button.get_label() |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
897 for tab in self.tabs: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
898 if tab[0] == tab_name: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
899 break |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
900 if tab[0] != tab_name: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
901 error(_("INTERNAL ERROR: Tab not found")) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
902 assert(False) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
903 self.__frame.body = tab[1] |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
904 if not invisible: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
905 self._emit('click') |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
906 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
907 def __appendButton(self, name): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
908 """Append a button to the frame header, |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
909 and link it to the page change method""" |
163
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
910 button = CustomButton(name, self.__buttonClicked, left_border = '', right_border=' | ') |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
911 self._buttons_cont.addWidget(button, button.getSize()) |
f582fddd0cc0
Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
912 if len(self._buttons_cont.widget_list) == 1: |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
913 #first button: we set the focus and the body |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
914 self._buttons_cont.set_focus(0) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
915 self.__buttonClicked(button,True) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
916 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
917 def addTab(self,name,content=[]): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
918 """Add a page to the container |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
919 @param name: name of the page (what appear on the tab) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
920 @param content: content of the page |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
921 @return: ListBox (content of the page)""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
922 listbox = urwid.ListBox(urwid.SimpleListWalker(content)) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
923 self.tabs.append([name,listbox]) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
924 self.__appendButton(name) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
925 return listbox |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
926 |
167
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
927 def addFooter(self, widget): |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
928 """Add a widget on the bottom of the tab (will be displayed on all pages) |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
929 @param widget: FlowWidget""" |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
930 self._w.footer = widget |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
931 |
6fd053c99421
Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents:
163
diff
changeset
|
932 |
120 | 933 ## DECORATORS ## |
934 class LabelLine(urwid.LineBox): | |
121 | 935 """Like LineBox, but with a Label centered in the top line""" |
120 | 936 |
937 def __init__(self, original_widget, label_widget): | |
938 urwid.LineBox.__init__(self, original_widget) | |
939 top_columns = self._w.widget_list[0] | |
940 top_columns.widget_list[1] = label_widget | |
124 | 941 |
942 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap): | |
176 | 943 def __init__(self, original_widget, left_char = u"│", right_char = ''): |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
944 """Draw a separator on left and/or right of original_widget.""" |
120 | 945 |
124 | 946 widgets = [original_widget] |
947 if left_char: | |
948 widgets.insert(0, ('fixed', 1, urwid.SolidFill(left_char))) | |
949 if right_char: | |
950 widgets.append(('fixed', 1, urwid.SolidFill(right_char))) | |
951 columns = urwid.Columns(widgets, box_columns = [0,2], focus_column = 1) | |
952 urwid.WidgetDecoration.__init__(self, original_widget) | |
953 urwid.WidgetWrap.__init__(self, columns) | |
954 | |
955 |