comparison frontends/primitivus/custom_widgets.py @ 114:77f48939ad6e

primitivus: added Alert widget
author Goffi <goffi@goffi.org>
date Thu, 01 Jul 2010 18:18:34 +0800
parents e5ca22113280
children 7c482ecac0ff
comparison
equal deleted inserted replaced
113:e5ca22113280 114:77f48939ad6e
83 83
84 class List(urwid.WidgetWrap): 84 class List(urwid.WidgetWrap):
85 signals = ['change'] 85 signals = ['change']
86 86
87 def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None): 87 def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None):
88 assert(options)
89 self.single = 'single' in style 88 self.single = 'single' in style
90 self.align = align 89 self.align = align
91 90
92 if on_state_change: 91 if on_state_change:
93 urwid.connect_signal(self, 'change', on_state_change, user_data) 92 urwid.connect_signal(self, 'change', on_state_change, user_data)
94 93
95 widgets = [SelectableText(option,align) for option in options] 94 self.content = urwid.SimpleListWalker([])
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) 95 self.list_box = urwid.ListBox(self.content)
100 if self.single: 96 self.changeValues(options)
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 97
106 def __onStateChange(self, widget, selected): 98 def __onStateChange(self, widget, selected):
107 if self.single: 99 if self.single:
108 if not selected: 100 if not selected:
109 #if in single mode, it's forbidden to unselect a value 101 #if in single mode, it's forbidden to unselect a value
119 for widget in self.content: 111 for widget in self.content:
120 if widget.getState(): 112 if widget.getState():
121 widget.setState(False, invisible) 113 widget.setState(False, invisible)
122 widget._invalidate() 114 widget._invalidate()
123 115
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
124 def getValues(self): 121 def getValues(self):
125 result = [] 122 result = []
126 for widget in self.content: 123 for widget in self.content:
127 if widget.getState(): 124 if widget.getState():
128 result.append(widget.getValue()) 125 result.append(widget.getValue())
129 return result 126 return result
130 127
131 def changeValues(self, new_values): 128 def changeValues(self, new_values):
132 widgets = [SelectableText(self, option) for option in new_values] 129 widgets = [SelectableText(option, self.align) for option in new_values]
130 for widget in widgets:
131 urwid.connect_signal(widget, 'change', self.__onStateChange)
133 self.content[:] = widgets 132 self.content[:] = widgets
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)
134 137
135 def selectValue(self, value): 138 def selectValue(self, value):
136 self.unselectAll() 139 self.unselectAll()
137 idx = 0 140 idx = 0
138 for widget in self.content: 141 for widget in self.content:
153 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']), 156 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']),
154 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])] 157 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])]
155 elif "YES/NO" in style: 158 elif "YES/NO" in style:
156 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']), 159 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']),
157 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])] 160 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])]
161 if "OK" in style:
162 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])]
158 if buttons: 163 if buttons:
159 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') 164 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
160 widgets_lst.append(buttons_flow) 165 widgets_lst.append(buttons_flow)
161 body_content = urwid.SimpleListWalker(widgets_lst) 166 body_content = urwid.SimpleListWalker(widgets_lst)
162 frame_body = urwid.ListBox(body_content) 167 frame_body = urwid.ListBox(body_content)
175 180
176 class ConfirmDialog(genericDialog): 181 class ConfirmDialog(genericDialog):
177 182
178 def __init__(self, title, style=['YES/NO'], **kwargs): 183 def __init__(self, title, style=['YES/NO'], **kwargs):
179 genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs) 184 genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs)
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)