Mercurial > urwid-satext
comparison frontends/primitivus/custom_widgets.py @ 0:cb72fb2bfa0d
Primitivus: profile manager
- new custom widgets: Password, List, GenericDialog, InputDialog, ConfirmationDialog
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 01 Jul 2010 15:35:56 +0800 |
parents | |
children | a5c9603dac37 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:cb72fb2bfa0d |
---|---|
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 assert(options) | |
89 self.single = 'single' in style | |
90 self.align = align | |
91 | |
92 if on_state_change: | |
93 urwid.connect_signal(self, 'change', on_state_change, user_data) | |
94 | |
95 widgets = [SelectableText(option,align) for option in options] | |
96 for widget in widgets: | |
97 urwid.connect_signal(widget, 'change', self.__onStateChange) | |
98 self.content = urwid.SimpleListWalker(widgets) | |
99 self.list_box = urwid.ListBox(self.content) | |
100 if self.single: | |
101 self.content[0].setState(True) | |
102 display_widget = urwid.BoxAdapter(self.list_box, min(len(options),5)) | |
103 | |
104 urwid.WidgetWrap.__init__(self, display_widget) | |
105 | |
106 def __onStateChange(self, widget, selected): | |
107 if self.single: | |
108 if not selected: | |
109 #if in single mode, it's forbidden to unselect a value | |
110 widget.setState(True, invisible=True) | |
111 return | |
112 else: | |
113 self.unselectAll(invisible=True) | |
114 widget.setState(True, invisible=True) | |
115 self._emit("change") | |
116 | |
117 | |
118 def unselectAll(self, invisible=False): | |
119 for widget in self.content: | |
120 if widget.getState(): | |
121 widget.setState(False, invisible) | |
122 widget._invalidate() | |
123 | |
124 def getValues(self): | |
125 result = [] | |
126 for widget in self.content: | |
127 if widget.getState(): | |
128 result.append(widget.getValue()) | |
129 return result | |
130 | |
131 def changeValues(self, new_values): | |
132 widgets = [SelectableText(self, option) for option in new_values] | |
133 self.content[:] = widgets | |
134 | |
135 def selectValue(self, value): | |
136 self.unselectAll() | |
137 idx = 0 | |
138 for widget in self.content: | |
139 if widget.getValue() == value: | |
140 widget.setState(True) | |
141 self.list_box.set_focus(idx) | |
142 return | |
143 idx+=1 | |
144 | |
145 class genericDialog(urwid.WidgetWrap): | |
146 | |
147 def __init__(self, widgets_lst, title, style=[], **kwargs): | |
148 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') | |
149 | |
150 buttons = None | |
151 | |
152 if "OK/CANCEL" in style: | |
153 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), | |
154 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] | |
155 elif "YES/NO" in style: | |
156 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), | |
157 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] | |
158 if buttons: | |
159 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
160 widgets_lst.append(buttons_flow) | |
161 body_content = urwid.SimpleListWalker(widgets_lst) | |
162 frame_body = urwid.ListBox(body_content) | |
163 frame = urwid.Frame(frame_body, frame_header) | |
164 decorated_frame = urwid.LineBox(frame) | |
165 urwid.WidgetWrap.__init__(self, decorated_frame) | |
166 | |
167 | |
168 | |
169 class InputDialog(genericDialog): | |
170 | |
171 def __init__(self, title, instrucions, style=['OK/CANCEL'], **kwargs): | |
172 instr_wid = urwid.Text(instrucions+':') | |
173 edit_box = urwid.Edit() | |
174 genericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) | |
175 | |
176 class ConfirmDialog(genericDialog): | |
177 | |
178 def __init__(self, title, style=['YES/NO'], **kwargs): | |
179 genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |