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 |
|
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 |
114
|
94 self.content = urwid.SimpleListWalker([]) |
113
|
95 self.list_box = urwid.ListBox(self.content) |
114
|
96 self.changeValues(options) |
113
|
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 |
114
|
116 def getValue(self): |
|
117 """Convenience method to get the value selected as a string in single mode, or None""" |
|
118 values = self.getValues() |
|
119 return values[0] if values else None |
|
120 |
113
|
121 def getValues(self): |
|
122 result = [] |
|
123 for widget in self.content: |
|
124 if widget.getState(): |
|
125 result.append(widget.getValue()) |
|
126 return result |
|
127 |
|
128 def changeValues(self, new_values): |
114
|
129 widgets = [SelectableText(option, self.align) for option in new_values] |
|
130 for widget in widgets: |
|
131 urwid.connect_signal(widget, 'change', self.__onStateChange) |
113
|
132 self.content[:] = widgets |
114
|
133 if self.single and new_values: |
|
134 self.content[0].setState(True) |
|
135 display_widget = urwid.BoxAdapter(self.list_box, min(len(new_values),5)) |
|
136 urwid.WidgetWrap.__init__(self, display_widget) |
113
|
137 |
|
138 def selectValue(self, value): |
|
139 self.unselectAll() |
|
140 idx = 0 |
|
141 for widget in self.content: |
|
142 if widget.getValue() == value: |
|
143 widget.setState(True) |
|
144 self.list_box.set_focus(idx) |
|
145 return |
|
146 idx+=1 |
|
147 |
|
148 class genericDialog(urwid.WidgetWrap): |
|
149 |
|
150 def __init__(self, widgets_lst, title, style=[], **kwargs): |
|
151 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') |
|
152 |
|
153 buttons = None |
|
154 |
|
155 if "OK/CANCEL" in style: |
|
156 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), |
|
157 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] |
|
158 elif "YES/NO" in style: |
|
159 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), |
|
160 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] |
114
|
161 if "OK" in style: |
|
162 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] |
113
|
163 if buttons: |
|
164 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') |
|
165 widgets_lst.append(buttons_flow) |
|
166 body_content = urwid.SimpleListWalker(widgets_lst) |
|
167 frame_body = urwid.ListBox(body_content) |
|
168 frame = urwid.Frame(frame_body, frame_header) |
|
169 decorated_frame = urwid.LineBox(frame) |
|
170 urwid.WidgetWrap.__init__(self, decorated_frame) |
|
171 |
|
172 |
|
173 |
|
174 class InputDialog(genericDialog): |
|
175 |
|
176 def __init__(self, title, instrucions, style=['OK/CANCEL'], **kwargs): |
|
177 instr_wid = urwid.Text(instrucions+':') |
|
178 edit_box = urwid.Edit() |
|
179 genericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) |
|
180 |
|
181 class ConfirmDialog(genericDialog): |
|
182 |
|
183 def __init__(self, title, style=['YES/NO'], **kwargs): |
|
184 genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) |
114
|
185 |
|
186 class Alert(genericDialog): |
|
187 |
|
188 def __init__(self, title, message, style=['OK'], **kwargs): |
|
189 genericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) |