Mercurial > libervia-backend
annotate frontends/primitivus/custom_widgets.py @ 151:7fcb4f083686
Primitivus: custom_widgets imrpovments
- new container: TabsContainer
- menu roller can now remove a menu
- new Button: CustomButton (use ClickableText, and we can define the borders)
- SelectableText now manage mouse events
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 30 Jul 2010 18:43:09 +0800 |
parents | 80661755ea8d |
children | 2fa58703f1b7 |
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 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
61 def keypress(self, size, key): |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
62 #TODO: insert mode is not managed yet |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
63 if key == 'ctrl a': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
64 key = 'home' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
65 elif key == 'ctrl e': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
66 key = 'end' |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
67 elif key == 'ctrl k': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
68 self._delete_highlighted() |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
69 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
|
70 elif key == 'ctrl w': |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
71 before = self.edit_text[:self.edit_pos] |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
72 pos = before.rstrip().rfind(" ")+1 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
73 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
|
74 self.set_edit_pos(pos) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
75 elif key == 'enter': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
76 self._emit('click') |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
77 return super(AdvancedEdit, self).keypress(size, key) |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
78 |
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
79 |
120 | 80 class SurroundedText(urwid.FlowWidget): |
121 | 81 """Text centered on a repeated character (like a Divider, but with a text in the center)""" |
120 | 82 |
83 def __init__(self,text,car=utf8decode('─')): | |
84 self.text=text | |
85 self.car=car | |
86 | |
87 def rows(self,size,focus=False): | |
88 return self.display_widget(size, focus).rows(size, focus) | |
89 | |
90 def render(self, size, focus=False): | |
91 return self.display_widget(size, focus).render(size, focus) | |
92 | |
93 def display_widget(self, size, focus): | |
94 (maxcol,) = size | |
95 middle = (maxcol-len(self.text))/2 | |
96 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car | |
97 return urwid.Text(render_text) | |
98 | |
113 | 99 class SelectableText(urwid.FlowWidget): |
120 | 100 """Text which can be selected with space""" |
113 | 101 signals = ['change'] |
102 | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
103 def __init__(self, text, align='left', header='', select_attr=None, default_attr=None, selected = False, data=None): |
113 | 104 self.text=unicode(text) |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
105 self.header=header |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
106 if data: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
107 self.data=data |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
108 if select_attr: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
109 self.selected = select_attr |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
110 if default_attr: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
111 self.default = default_attr |
113 | 112 self.align = align |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
113 self.__selected=selected |
113 | 114 |
115 def getValue(self): | |
116 return self.text | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
117 |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
118 def setAttribute(self, name, value): |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
119 """Change attribut used for rendering widget |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
120 @param name: one of |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
121 -default: when not selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
122 -selected: when selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
123 @param value: name of the attribute |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
124 /!\ 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
|
125 assert name in ['default', 'selected'] |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
126 self.__setattr__(name,value) |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
127 self._invalidate() |
113 | 128 |
129 def setState(self, selected, invisible=False): | |
130 """Change state | |
131 @param selected: boolean state value | |
132 @param invisible: don't emit change signal if True""" | |
133 assert(type(selected)==bool) | |
134 self.__selected=selected | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
135 self._invalidate() |
113 | 136 if not invisible: |
137 self._emit("change", self.__selected) | |
138 | |
139 def getState(self): | |
140 return self.__selected | |
141 | |
142 def selectable(self): | |
143 return True | |
144 | |
145 def keypress(self, size, key): | |
146 if key==' ' or key=='enter': | |
147 self.setState(not self.__selected) | |
148 else: | |
149 return key | |
150 | |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
151 def mouse_event(self, size, event, button, x, y, focus): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
152 if urwid.is_mouse_press(event) and button == 1: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
153 self.setState(not self.__selected) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
154 return True |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
155 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
156 return False |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
157 |
113 | 158 def rows(self,size,focus=False): |
159 return self.display_widget(size, focus).rows(size, focus) | |
160 | |
161 def render(self, size, focus=False): | |
162 return self.display_widget(size, focus).render(size, focus) | |
163 | |
164 def display_widget(self, size, focus): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
165 try: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
166 select_attr = self.selected |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
167 except AttributeError: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
168 select_attr = 'selected' |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
169 try: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
170 default_attr = self.default |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
171 except AttributeError: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
172 default_attr = 'default' |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
173 attr = select_attr if self.__selected else default_attr |
113 | 174 if focus: |
175 attr+="_focus" | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
176 return urwid.Text((attr,self.header+self.text), align=self.align) |
113 | 177 |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
178 class ClickableText(SelectableText): |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
179 signals = SelectableText.signals + ['click'] |
124 | 180 |
181 def setState(self, selected, invisible=False): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
182 self._emit('click') |
124 | 183 |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
184 class CustomButton(ClickableText): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
185 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
186 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
|
187 self.label = label |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
188 render_txt = "%s%s%s" % (left_border, label, right_border) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
189 self.size = len(render_txt) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
190 super(CustomButton, self).__init__(render_txt) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
191 if on_press: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
192 urwid.connect_signal(self, 'click', on_press, user_data) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
193 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
194 def getSize(self): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
195 """Return representation size of the button""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
196 return self.size |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
197 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
198 def get_label(self): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
199 return self.label |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
200 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
201 class GenericList(urwid.WidgetWrap): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
202 signals = ['click','change'] |
113 | 203 |
124 | 204 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
|
205 """ |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
206 Widget managing list of string and their selection |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
207 @param options: list of strings used for options |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
208 @param style: list of string: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
209 - 'single' if only one must be selected |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
210 - 'no_first_select' nothing selected when list is first displayed |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
211 - 'can_select_none' if we can select nothing |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
212 @param align: alignement of text inside the list |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
213 @param on_click: method called when click signal is emited |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
214 @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
|
215 """ |
113 | 216 self.single = 'single' in style |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
217 self.no_first_select = 'no_first_select' in style |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
218 self.can_select_none = 'can_select_none' in style |
113 | 219 self.align = align |
124 | 220 self.option_type = option_type |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
221 self.first_display = True |
113 | 222 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
223 if on_click: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
224 urwid.connect_signal(self, 'click', on_click, user_data) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
225 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
226 if on_change: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
227 urwid.connect_signal(self, 'change', on_change, user_data) |
113 | 228 |
114 | 229 self.content = urwid.SimpleListWalker([]) |
113 | 230 self.list_box = urwid.ListBox(self.content) |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
231 urwid.WidgetWrap.__init__(self, self.list_box) |
114 | 232 self.changeValues(options) |
113 | 233 |
234 def __onStateChange(self, widget, selected): | |
235 if self.single: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
236 if not selected and not self.can_select_none: |
113 | 237 #if in single mode, it's forbidden to unselect a value |
238 widget.setState(True, invisible=True) | |
239 return | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
240 if selected: |
113 | 241 self.unselectAll(invisible=True) |
242 widget.setState(True, invisible=True) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
243 self._emit("click") |
113 | 244 |
245 | |
246 def unselectAll(self, invisible=False): | |
247 for widget in self.content: | |
248 if widget.getState(): | |
249 widget.setState(False, invisible) | |
250 widget._invalidate() | |
251 | |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
252 def deleteValue(self, value): |
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
253 """Delete the first value equal to the param given""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
254 for widget in self.content: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
255 if widget.getValue() == value: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
256 self.content.remove(widget) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
257 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
258 return |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
259 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
|
260 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
261 def getSelectedValue(self): |
114 | 262 """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
|
263 values = self.getSelectedValues() |
114 | 264 return values[0] if values else None |
265 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
266 def getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
267 """Return values of all items""" |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
268 return [widget.getValue() for widget in self.content] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
269 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
270 def getSelectedValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
271 """Return values of selected items""" |
113 | 272 result = [] |
273 for widget in self.content: | |
274 if widget.getState(): | |
275 result.append(widget.getValue()) | |
276 return result | |
277 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
278 def getDisplayWidget(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
279 return self.list_box |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
280 |
113 | 281 def changeValues(self, new_values): |
116
7c482ecac0ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
114
diff
changeset
|
282 """Change all value in one shot""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
283 if not self.first_display: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
284 old_selected = self.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
285 widgets = [] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
286 for option in new_values: |
124 | 287 widget = self.option_type(option, self.align) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
288 if not self.first_display and option in old_selected: |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
289 widget.setState(True) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
290 widgets.append(widget) |
124 | 291 try: |
292 urwid.connect_signal(widget, 'change', self.__onStateChange) | |
293 except NameError: | |
294 pass #the widget given doesn't support 'change' signal | |
113 | 295 self.content[:] = widgets |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
296 if self.first_display and self.single and new_values and not self.no_first_select: |
114 | 297 self.content[0].setState(True) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
298 display_widget = self.getDisplayWidget() |
118
76055a209ed9
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
299 self._set_w(display_widget) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
300 self._emit('change') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
301 self.first_display = False |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
302 |
113 | 303 def selectValue(self, value): |
304 self.unselectAll() | |
305 idx = 0 | |
306 for widget in self.content: | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
307 if widget.getSelectedValue() == value: |
113 | 308 widget.setState(True) |
309 self.list_box.set_focus(idx) | |
310 return | |
311 idx+=1 | |
312 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
313 class List(urwid.FlowWidget): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
314 """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
|
315 signals = ['click','change'] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
316 |
124 | 317 def __init__(self, options, style=[], max_height=5, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): |
318 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
|
319 self.max_height = max_height |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
320 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
321 def selectable(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
322 return True |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
323 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
324 def keypress(self, size, key): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
325 return self.displayWidget(size,True).keypress(size, key) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
326 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
327 def unselectAll(self, invisible=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
328 return self.genericList.unselectAll(invisible) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
329 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
330 def deleteValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
331 return self.genericList.deleteValue(value) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
332 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
333 def getSelectedValue(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
334 return self.genericList.getSelectedValue() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
335 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
336 def getAllValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
337 return self.genericList.getAllValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
338 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
339 def getSelectedValues(self): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
340 return self.genericList.getSelectedValues() |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
341 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
342 def changeValues(self, new_values): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
343 return self.genericList.changeValues(new_values) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
344 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
345 def selectValue(self, value): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
346 return self.genericList.selectValue(value) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
347 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
348 def render(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
349 return self.displayWidget(size, focus).render(size, focus) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
350 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
351 def rows(self, size, focus=False): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
352 return self.displayWidget(size, focus).rows(size, focus) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
353 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
354 def displayWidget(self, size, focus): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
355 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) |
120 | 356 height = min(list_size,self.max_height) or 1 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
357 return urwid.BoxAdapter(self.genericList, height) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
358 |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
359 ## MISC ## |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
360 |
131 | 361 class MenuBox(urwid.WidgetWrap): |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
362 """Show menu items of a category in a box""" |
131 | 363 signals = ['click'] |
364 | |
365 def __init__(self,parent,items): | |
366 self.parent = parent | |
367 self.selected = None | |
368 content = urwid.SimpleListWalker([ClickableText(text,default_attr='menuitem') for text in items]) | |
369 for wid in content: | |
370 urwid.connect_signal(wid, 'click', self.onClick) | |
371 | |
372 self.listBox = urwid.ListBox(content) | |
373 menubox = urwid.LineBox(urwid.BoxAdapter(self.listBox,len(items))) | |
374 urwid.WidgetWrap.__init__(self,menubox) | |
375 | |
376 def getValue(self): | |
377 return self.selected | |
378 | |
379 def keypress(self, size, key): | |
380 if key=='up': | |
381 if self.listBox.get_focus()[1] == 0: | |
382 self.parent.keypress(size, key) | |
383 elif key=='left' or key=='right': | |
384 self.parent.keypress(size,'up') | |
385 self.parent.keypress(size,key) | |
386 return super(MenuBox,self).keypress(size,key) | |
387 | |
388 def onClick(self, wid): | |
389 self.selected = wid.getValue() | |
390 self._emit('click') | |
391 | |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
392 class Menu(urwid.FlowWidget): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
393 |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
394 def __init__(self,loop, x_orig=0): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
395 """Menu widget |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
396 @param loop: main loop of urwid |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
397 @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
|
398 """ |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
399 super(Menu, self).__init__() |
131 | 400 self.loop = loop |
401 self.menu_keys = [] | |
402 self.menu = {} | |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
403 self.x_orig = x_orig |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
404 self.shortcuts = {} #keyboard shortcuts |
131 | 405 self.focus_menu = 0 |
406 self.save_bottom = None | |
407 | |
408 def selectable(self): | |
409 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
|
410 |
e7b8871e9f52
Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents:
137
diff
changeset
|
411 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
|
412 """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
|
413 return len(self.menu_keys) |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
414 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
415 def setOrigX(self, orig_x): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
416 self.x_orig = orig_x |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
417 |
131 | 418 def __buildOverlay(self,menu_key,columns): |
419 """Build the overlay menu which show menuitems | |
420 @param menu_key: name of the category | |
421 @colums: column number where the menubox must be displayed""" | |
422 max_len = 0 | |
423 for item in self.menu[menu_key]: | |
424 if len(item[0]) > max_len: | |
425 max_len = len(item[0]) | |
426 | |
427 self.save_bottom = self.loop.widget | |
428 menu_box = MenuBox(self,[item[0] for item in self.menu[menu_key]]) | |
429 urwid.connect_signal(menu_box, 'click', self.onClick) | |
430 | |
431 self.loop.widget = urwid.Overlay(urwid.AttrMap(menu_box,'menubar'),self.save_bottom,('fixed left', columns),max_len+2,('fixed top',1),None) | |
432 | |
433 def keypress(self, size, key): | |
434 if key == 'right' and self.focus_menu < len(self.menu)-1: | |
435 self.focus_menu += 1 | |
436 self._invalidate() | |
437 elif key == 'left' and self.focus_menu > 0: | |
438 self.focus_menu -= 1 | |
439 self._invalidate() | |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
440 return |
131 | 441 elif key == 'down': |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
442 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
|
443 column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+self.x_orig) |
131 | 444 self.__buildOverlay(self.menu_keys[self.focus_menu],column) |
445 elif key == 'up': | |
446 if self.save_bottom: | |
447 self.loop.widget = self.save_bottom | |
448 self.save_bottom = None | |
449 | |
450 return key | |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
451 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
452 def checkShortcuts(self, key): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
453 for shortcut in self.shortcuts.keys(): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
454 if key == shortcut: |
131 | 455 category, item, callback = self.shortcuts[shortcut] |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
456 callback((category, item)) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
457 return key |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
458 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
459 def addMenu(self, category, item, callback, shortcut=None): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
460 """Add a menu item, create the category if new |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
461 @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
|
462 @param item: menu item (e.g. new/close/about) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
463 @callback: method to call when item is selected""" |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
464 if not category in self.menu.keys(): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
465 self.menu_keys.append(category) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
466 self.menu[category] = [] |
131 | 467 self.menu[category].append((item, callback)) |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
468 if shortcut: |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
469 assert(shortcut not in self.shortcuts.keys()) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
470 self.shortcuts[shortcut] = (category, item, callback) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
471 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
472 def rows(self,size,focus=False): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
473 return self.display_widget(size, focus).rows(size, focus) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
474 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
475 def render(self, size, focus=False): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
476 return self.display_widget(size, focus).render(size, focus) |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
477 |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
478 def display_widget(self, size, focus): |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
479 render_txt = [] |
131 | 480 idx = 0 |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
481 for menu in self.menu_keys: |
131 | 482 if focus and idx == self.focus_menu: |
483 render_txt.append(('selected_menu', '[ %s ]' % menu)) | |
484 render_txt.append(' ') | |
485 else: | |
486 render_txt.append('[ %s ] ' % menu) | |
487 idx += 1 | |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
488 return urwid.AttrMap(urwid.Text(render_txt), 'menubar') |
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
489 |
131 | 490 def onClick(self, widget): |
491 category = self.menu_keys[self.focus_menu] | |
492 item = widget.getValue() | |
493 for menu_item in self.menu[category]: | |
494 if item == menu_item[0]: | |
495 callback = menu_item[1] | |
496 break | |
497 if callback: | |
498 self.keypress(None,'up') | |
499 callback((category, item)) | |
500 | |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
501 class MenuRoller(urwid.WidgetWrap): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
502 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
503 def __init__(self,menus_list): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
504 """Create a MenuRoller |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
505 @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
|
506 """ |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
507 assert (menus_list) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
508 self.selected = 0 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
509 self.name_list = [] |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
510 self.menus = {} |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
511 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
512 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
|
513 urwid.WidgetWrap.__init__(self, self.columns) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
514 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
515 for menu_tuple in menus_list: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
516 name,menu = menu_tuple |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
517 self.addMenu(name, menu) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
518 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
519 def __showSelected(self): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
520 """show menu selected""" |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
521 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
|
522 current_name = ClickableText(name_txt) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
523 name_len = len(name_txt) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
524 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
|
525 current_menu.setOrigX(name_len) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
526 self.columns.widget_list[0] = current_name |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
527 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
|
528 self.columns.widget_list[1] = current_menu |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
529 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
530 def keypress(self, size, key): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
531 if key=='up': |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
532 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
|
533 self.selected -= 1 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
534 self.__showSelected() |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
535 elif key=='down': |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
536 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
|
537 self.selected += 1 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
538 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
|
539 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
|
540 if self.columns.get_focus_column()==0 and \ |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
541 (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
|
542 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
|
543 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
|
544 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
545 return super(MenuRoller, self).keypress(size, key) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
546 |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
547 def addMenu(self, name_param, menu): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
548 name = name_param or '' |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
549 if name not in self.name_list: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
550 self.name_list.append(name) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
551 self.menus[name] = menu |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
552 if self.name_list[self.selected] == name: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
553 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
|
554 |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
555 def removeMenu(self, name): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
556 if name in self.name_list: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
557 self.name_list.remove(name) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
558 if name in self.menus.keys(): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
559 del self.menus[name] |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
560 self.selected = 0 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
561 self.__showSelected() |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
562 |
137
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
563 def checkShortcuts(self, key): |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
564 for menu in self.name_list: |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
565 key = self.menus[menu].checkShortcuts(key) |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
566 return key |
227394eb080c
Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
567 |
128
2240f34f6452
Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
568 |
120 | 569 ## DIALOGS ## |
570 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
571 class GenericDialog(urwid.WidgetWrap): |
113 | 572 |
573 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
574 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
575 | |
576 buttons = None | |
577 | |
578 if "OK/CANCEL" in style: | |
579 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
580 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
581 elif "YES/NO" in style: | |
582 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
583 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
114 | 584 if "OK" in style: |
585 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
113 | 586 if buttons: |
587 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
588 widgets_lst.append(buttons_flow) | |
589 body_content = urwid.SimpleListWalker(widgets_lst) | |
590 frame_body = urwid.ListBox(body_content) | |
591 frame = urwid.Frame(frame_body, frame_header) | |
592 decorated_frame = urwid.LineBox(frame) | |
593 urwid.WidgetWrap.__init__(self, decorated_frame) | |
594 | |
595 | |
596 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
597 class InputDialog(GenericDialog): |
121 | 598 """Dialog with an edit box""" |
113 | 599 |
120 | 600 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs): |
113 | 601 instr_wid = urwid.Text(instrucions+':') |
120 | 602 edit_box = urwid.Edit(edit_text=default_txt) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
603 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) |
113 | 604 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
605 class ConfirmDialog(GenericDialog): |
121 | 606 """Dialog with buttons for confirm or cancel an action""" |
113 | 607 |
608 def __init__(self, title, style=['YES/NO'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
609 GenericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |
114 | 610 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
611 class Alert(GenericDialog): |
121 | 612 """Dialog with just a message and a OK button""" |
114 | 613 |
614 def __init__(self, title, message, style=['OK'], **kwargs): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
615 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
|
616 |
120 | 617 ## CONTAINERS ## |
618 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
619 class FocusFrame(urwid.Frame): |
121 | 620 """Frame which manage 'tab' key""" |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
621 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
622 def keypress(self, size, key): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
623 if key == 'tab': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
624 focus_list = ('header','body','footer') |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
625 focus_idx = focus_list.index(self.focus_part) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
626 for i in range(2): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
627 focus_idx = (focus_idx + 1) % len(focus_list) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
628 focus_name = focus_list[focus_idx] |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
629 widget = getattr(self,'_'+focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
630 if widget!=None and widget.selectable(): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
631 self.set_focus(focus_name) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
632 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
118
diff
changeset
|
633 return urwid.Frame.keypress(self, size, key) |
120 | 634 |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
635 class TabsContainer(urwid.WidgetWrap): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
636 signals = ['click'] |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
637 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
638 def __init__(self): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
639 #self._current_tab = 0 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
640 self._buttons_cont = urwid.GridFlow([],19,1,0,'left') |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
641 self.tabs = [] |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
642 self.__frame = urwid.Frame(urwid.Text(''),urwid.Pile([self._buttons_cont,urwid.Divider(u"─")])) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
643 urwid.WidgetWrap.__init__(self, self.__frame) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
644 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
645 """def selectable(self): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
646 return True |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
647 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
648 def keypress(self, size, key): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
649 return key""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
650 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
651 def __buttonClicked(self, button, invisible=False): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
652 """Called when a button on the tab is changed, |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
653 change the page |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
654 @param button: button clicked |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
655 @param invisible: emit signal only if False""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
656 tab_name = button.get_label() |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
657 for tab in self.tabs: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
658 if tab[0] == tab_name: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
659 break |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
660 if tab[0] != tab_name: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
661 error(_("INTERNAL ERROR: Tab not found")) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
662 assert(False) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
663 self.__frame.body = tab[1] |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
664 if not invisible: |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
665 self._emit('click') |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
666 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
667 def __appendButton(self, name): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
668 """Append a button to the frame header, |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
669 and link it to the page change method""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
670 button = CustomButton(name, self.__buttonClicked, left_border = '', right_border=' |') |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
671 self._buttons_cont.cells.append(button) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
672 if len(self._buttons_cont.cells): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
673 #first button: we set the focus and the body |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
674 self._buttons_cont.set_focus(0) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
675 self.__buttonClicked(button,True) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
676 |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
677 def addTab(self,name,content=[]): |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
678 """Add a page to the container |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
679 @param name: name of the page (what appear on the tab) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
680 @param content: content of the page |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
681 @return: ListBox (content of the page)""" |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
682 listbox = urwid.ListBox(urwid.SimpleListWalker(content)) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
683 self.tabs.append([name,listbox]) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
684 self.__appendButton(name) |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
685 return listbox |
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
686 |
120 | 687 ## DECORATORS ## |
688 class LabelLine(urwid.LineBox): | |
121 | 689 """Like LineBox, but with a Label centered in the top line""" |
120 | 690 |
691 def __init__(self, original_widget, label_widget): | |
692 urwid.LineBox.__init__(self, original_widget) | |
693 top_columns = self._w.widget_list[0] | |
694 top_columns.widget_list[1] = label_widget | |
124 | 695 |
696 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap): | |
697 def __init__(self, original_widget, left_char = utf8decode("│"), right_char = ''): | |
151
7fcb4f083686
Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents:
144
diff
changeset
|
698 """Draw a separator on left and/or right of original_widget.""" |
120 | 699 |
124 | 700 widgets = [original_widget] |
701 if left_char: | |
702 widgets.insert(0, ('fixed', 1, urwid.SolidFill(left_char))) | |
703 if right_char: | |
704 widgets.append(('fixed', 1, urwid.SolidFill(right_char))) | |
705 columns = urwid.Columns(widgets, box_columns = [0,2], focus_column = 1) | |
706 urwid.WidgetDecoration.__init__(self, original_widget) | |
707 urwid.WidgetWrap.__init__(self, columns) | |
708 | |
709 |