Mercurial > urwid-satext
annotate frontends/primitivus/custom_widgets.py @ 2:07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 01 Jul 2010 18:52:51 +0800 |
parents | a5c9603dac37 |
children | 6290022ae74d |
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 | |
39 class SelectableText(urwid.FlowWidget): | |
40 signals = ['change'] | |
41 | |
42 def __init__(self, text, align='left'): | |
43 self.text=unicode(text) | |
44 self.align = align | |
45 self.__selected=False | |
46 | |
47 def getValue(self): | |
48 return self.text | |
49 | |
50 def setState(self, selected, invisible=False): | |
51 """Change state | |
52 @param selected: boolean state value | |
53 @param invisible: don't emit change signal if True""" | |
54 assert(type(selected)==bool) | |
55 self.__selected=selected | |
56 if not invisible: | |
57 self._emit("change", self.__selected) | |
58 self._invalidate() | |
59 | |
60 def getState(self): | |
61 return self.__selected | |
62 | |
63 def selectable(self): | |
64 return True | |
65 | |
66 def keypress(self, size, key): | |
67 if key==' ' or key=='enter': | |
68 self.setState(not self.__selected) | |
69 else: | |
70 return key | |
71 | |
72 def rows(self,size,focus=False): | |
73 return self.display_widget(size, focus).rows(size, focus) | |
74 | |
75 def render(self, size, focus=False): | |
76 return self.display_widget(size, focus).render(size, focus) | |
77 | |
78 def display_widget(self, size, focus): | |
79 attr = 'selected' if self.__selected else 'default' | |
80 if focus: | |
81 attr+="_focus" | |
82 return urwid.Text((attr,self.text), align=self.align) | |
83 | |
84 class List(urwid.WidgetWrap): | |
85 signals = ['change'] | |
86 | |
87 def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None): | |
88 self.single = 'single' in style | |
89 self.align = align | |
90 | |
91 if on_state_change: | |
92 urwid.connect_signal(self, 'change', on_state_change, user_data) | |
93 | |
1 | 94 self.content = urwid.SimpleListWalker([]) |
0 | 95 self.list_box = urwid.ListBox(self.content) |
1 | 96 self.changeValues(options) |
0 | 97 |
98 def __onStateChange(self, widget, selected): | |
99 if self.single: | |
100 if not selected: | |
101 #if in single mode, it's forbidden to unselect a value | |
102 widget.setState(True, invisible=True) | |
103 return | |
104 else: | |
105 self.unselectAll(invisible=True) | |
106 widget.setState(True, invisible=True) | |
107 self._emit("change") | |
108 | |
109 | |
110 def unselectAll(self, invisible=False): | |
111 for widget in self.content: | |
112 if widget.getState(): | |
113 widget.setState(False, invisible) | |
114 widget._invalidate() | |
115 | |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
116 def deleteValue(self, value): |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
117 """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
|
118 try: |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
119 self.content.remove(value) |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
120 except ValueError: |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
121 pass |
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
122 |
1 | 123 def getValue(self): |
124 """Convenience method to get the value selected as a string in single mode, or None""" | |
125 values = self.getValues() | |
126 return values[0] if values else None | |
127 | |
0 | 128 def getValues(self): |
129 result = [] | |
130 for widget in self.content: | |
131 if widget.getState(): | |
132 result.append(widget.getValue()) | |
133 return result | |
134 | |
135 def changeValues(self, new_values): | |
2
07b7dcd314ff
primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
136 """Change all value in one shot""" |
1 | 137 widgets = [SelectableText(option, self.align) for option in new_values] |
138 for widget in widgets: | |
139 urwid.connect_signal(widget, 'change', self.__onStateChange) | |
0 | 140 self.content[:] = widgets |
1 | 141 if self.single and new_values: |
142 self.content[0].setState(True) | |
143 display_widget = urwid.BoxAdapter(self.list_box, min(len(new_values),5)) | |
144 urwid.WidgetWrap.__init__(self, display_widget) | |
0 | 145 |
146 def selectValue(self, value): | |
147 self.unselectAll() | |
148 idx = 0 | |
149 for widget in self.content: | |
150 if widget.getValue() == value: | |
151 widget.setState(True) | |
152 self.list_box.set_focus(idx) | |
153 return | |
154 idx+=1 | |
155 | |
156 class genericDialog(urwid.WidgetWrap): | |
157 | |
158 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
159 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
160 | |
161 buttons = None | |
162 | |
163 if "OK/CANCEL" in style: | |
164 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
165 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
166 elif "YES/NO" in style: | |
167 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
168 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
1 | 169 if "OK" in style: |
170 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
0 | 171 if buttons: |
172 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
173 widgets_lst.append(buttons_flow) | |
174 body_content = urwid.SimpleListWalker(widgets_lst) | |
175 frame_body = urwid.ListBox(body_content) | |
176 frame = urwid.Frame(frame_body, frame_header) | |
177 decorated_frame = urwid.LineBox(frame) | |
178 urwid.WidgetWrap.__init__(self, decorated_frame) | |
179 | |
180 | |
181 | |
182 class InputDialog(genericDialog): | |
183 | |
184 def __init__(self, title, instrucions, style=['OK/CANCEL'], **kwargs): | |
185 instr_wid = urwid.Text(instrucions+':') | |
186 edit_box = urwid.Edit() | |
187 genericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) | |
188 | |
189 class ConfirmDialog(genericDialog): | |
190 | |
191 def __init__(self, title, style=['YES/NO'], **kwargs): | |
192 genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) | |
1 | 193 |
194 class Alert(genericDialog): | |
195 | |
196 def __init__(self, title, message, style=['OK'], **kwargs): | |
197 genericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) |