Mercurial > urwid-satext
annotate frontends/primitivus/custom_widgets.py @ 4:c94cdbfdf3e8
primitivus: added edition zone at the bottom
- primitivus: new AdvancedEdit widget, which is like Edit but manage some new keys (C-a, C-e, C-k, C-w)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 03 Jul 2010 13:56:44 +0800 |
parents | 6290022ae74d |
children | 592cd64933dd |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import urwid | |
23 | |
24 class Password(urwid.Edit): | |
25 | |
26 def __init__(self, *args, **kwargs): | |
27 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*' | |
28 self.__real_text='' | |
29 super(Password, self).__init__(*args, **kwargs) | |
30 | |
31 def set_edit_text(self, text): | |
32 self.__real_text = text | |
33 hidden_txt = len(text)*'*' | |
34 super(Password, self).set_edit_text(hidden_txt) | |
35 | |
36 def get_edit_text(self): | |
37 return self.__real_text | |
38 | |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
39 class AdvancedEdit(urwid.Edit): |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
40 """Edit box with some custom improvments""" |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
41 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
42 def keypress(self, size, key): |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
43 #TODO: insert mode is not managed yet |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
44 if key == 'ctrl a': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
45 key = 'home' |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
46 elif key == 'ctrl e': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
47 key = 'end' |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
48 elif key == 'ctrl k': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
49 self._delete_highlighted() |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
50 self.set_edit_text(self.edit_text[:self.edit_pos]) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
51 elif key == 'ctrl w': |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
52 before = self.edit_text[:self.edit_pos] |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
53 pos = before.rstrip().rfind(" ")+1 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
54 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:]) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
55 self.set_edit_pos(pos) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
56 return super(AdvancedEdit, self).keypress(size, key) |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
57 |
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
58 |
0 | 59 class SelectableText(urwid.FlowWidget): |
60 signals = ['change'] | |
61 | |
62 def __init__(self, text, align='left'): | |
63 self.text=unicode(text) | |
64 self.align = align | |
65 self.__selected=False | |
66 | |
67 def getValue(self): | |
68 return self.text | |
69 | |
70 def setState(self, selected, invisible=False): | |
71 """Change state | |
72 @param selected: boolean state value | |
73 @param invisible: don't emit change signal if True""" | |
74 assert(type(selected)==bool) | |
75 self.__selected=selected | |
76 if not invisible: | |
77 self._emit("change", self.__selected) | |
78 self._invalidate() | |
79 | |
80 def getState(self): | |
81 return self.__selected | |
82 | |
83 def selectable(self): | |
84 return True | |
85 | |
86 def keypress(self, size, key): | |
87 if key==' ' or key=='enter': | |
88 self.setState(not self.__selected) | |
89 else: | |
90 return key | |
91 | |
92 def rows(self,size,focus=False): | |
93 return self.display_widget(size, focus).rows(size, focus) | |
94 | |
95 def render(self, size, focus=False): | |
96 return self.display_widget(size, focus).render(size, focus) | |
97 | |
98 def display_widget(self, size, focus): | |
99 attr = 'selected' if self.__selected else 'default' | |
100 if focus: | |
101 attr+="_focus" | |
102 return urwid.Text((attr,self.text), align=self.align) | |
103 | |
104 class List(urwid.WidgetWrap): | |
105 signals = ['change'] | |
106 | |
107 def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None): | |
108 self.single = 'single' in style | |
109 self.align = align | |
110 | |
111 if on_state_change: | |
112 urwid.connect_signal(self, 'change', on_state_change, user_data) | |
113 | |
1 | 114 self.content = urwid.SimpleListWalker([]) |
0 | 115 self.list_box = urwid.ListBox(self.content) |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
116 urwid.WidgetWrap.__init__(self, self.list_box) |
1 | 117 self.changeValues(options) |
0 | 118 |
119 def __onStateChange(self, widget, selected): | |
120 if self.single: | |
121 if not selected: | |
122 #if in single mode, it's forbidden to unselect a value | |
123 widget.setState(True, invisible=True) | |
124 return | |
125 else: | |
126 self.unselectAll(invisible=True) | |
127 widget.setState(True, invisible=True) | |
128 self._emit("change") | |
129 | |
130 | |
131 def unselectAll(self, invisible=False): | |
132 for widget in self.content: | |
133 if widget.getState(): | |
134 widget.setState(False, invisible) | |
135 widget._invalidate() | |
136 | |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
137 def deleteValue(self, value): |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
138 """Delete the first value equal to the param given""" |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
139 try: |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
140 self.content.remove(value) |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
141 except ValueError: |
3 | 142 raise ValuError("%s ==> %s" % (str(value),str(self.content))) |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
143 |
1 | 144 def getValue(self): |
145 """Convenience method to get the value selected as a string in single mode, or None""" | |
146 values = self.getValues() | |
147 return values[0] if values else None | |
148 | |
0 | 149 def getValues(self): |
150 result = [] | |
151 for widget in self.content: | |
152 if widget.getState(): | |
153 result.append(widget.getValue()) | |
154 return result | |
155 | |
156 def changeValues(self, new_values): | |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
157 """Change all value in one shot""" |
1 | 158 widgets = [SelectableText(option, self.align) for option in new_values] |
159 for widget in widgets: | |
160 urwid.connect_signal(widget, 'change', self.__onStateChange) | |
0 | 161 self.content[:] = widgets |
1 | 162 if self.single and new_values: |
163 self.content[0].setState(True) | |
3 | 164 display_widget = urwid.BoxAdapter(self.list_box, min(len(new_values),5) or 1) |
4
c94cdbfdf3e8
primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
165 self._set_w(display_widget) |
0 | 166 |
167 def selectValue(self, value): | |
168 self.unselectAll() | |
169 idx = 0 | |
170 for widget in self.content: | |
171 if widget.getValue() == value: | |
172 widget.setState(True) | |
173 self.list_box.set_focus(idx) | |
174 return | |
175 idx+=1 | |
176 | |
177 class genericDialog(urwid.WidgetWrap): | |
178 | |
179 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
180 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
181 | |
182 buttons = None | |
183 | |
184 if "OK/CANCEL" in style: | |
185 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
186 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
187 elif "YES/NO" in style: | |
188 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
189 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
1 | 190 if "OK" in style: |
191 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
0 | 192 if buttons: |
193 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
194 widgets_lst.append(buttons_flow) | |
195 body_content = urwid.SimpleListWalker(widgets_lst) | |
196 frame_body = urwid.ListBox(body_content) | |
197 frame = urwid.Frame(frame_body, frame_header) | |
198 decorated_frame = urwid.LineBox(frame) | |
199 urwid.WidgetWrap.__init__(self, decorated_frame) | |
200 | |
201 | |
202 | |
203 class InputDialog(genericDialog): | |
204 | |
205 def __init__(self, title, instrucions, style=['OK/CANCEL'], **kwargs): | |
206 instr_wid = urwid.Text(instrucions+':') | |
207 edit_box = urwid.Edit() | |
208 genericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) | |
209 | |
210 class ConfirmDialog(genericDialog): | |
211 | |
212 def __init__(self, title, style=['YES/NO'], **kwargs): | |
213 genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) | |
1 | 214 |
215 class Alert(genericDialog): | |
216 | |
217 def __init__(self, title, message, style=['OK'], **kwargs): | |
218 genericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) |