primitivus: added Alert widget
This commit is contained in:
parent
ab067174ff
commit
6e4a5c25d2
1 changed files with 21 additions and 11 deletions
|
|
@ -85,23 +85,15 @@ class List(urwid.WidgetWrap):
|
||||||
signals = ['change']
|
signals = ['change']
|
||||||
|
|
||||||
def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None):
|
def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None):
|
||||||
assert(options)
|
|
||||||
self.single = 'single' in style
|
self.single = 'single' in style
|
||||||
self.align = align
|
self.align = align
|
||||||
|
|
||||||
if on_state_change:
|
if on_state_change:
|
||||||
urwid.connect_signal(self, 'change', on_state_change, user_data)
|
urwid.connect_signal(self, 'change', on_state_change, user_data)
|
||||||
|
|
||||||
widgets = [SelectableText(option,align) for option in options]
|
self.content = urwid.SimpleListWalker([])
|
||||||
for widget in widgets:
|
|
||||||
urwid.connect_signal(widget, 'change', self.__onStateChange)
|
|
||||||
self.content = urwid.SimpleListWalker(widgets)
|
|
||||||
self.list_box = urwid.ListBox(self.content)
|
self.list_box = urwid.ListBox(self.content)
|
||||||
if self.single:
|
self.changeValues(options)
|
||||||
self.content[0].setState(True)
|
|
||||||
display_widget = urwid.BoxAdapter(self.list_box, min(len(options),5))
|
|
||||||
|
|
||||||
urwid.WidgetWrap.__init__(self, display_widget)
|
|
||||||
|
|
||||||
def __onStateChange(self, widget, selected):
|
def __onStateChange(self, widget, selected):
|
||||||
if self.single:
|
if self.single:
|
||||||
|
|
@ -121,6 +113,11 @@ class List(urwid.WidgetWrap):
|
||||||
widget.setState(False, invisible)
|
widget.setState(False, invisible)
|
||||||
widget._invalidate()
|
widget._invalidate()
|
||||||
|
|
||||||
|
def getValue(self):
|
||||||
|
"""Convenience method to get the value selected as a string in single mode, or None"""
|
||||||
|
values = self.getValues()
|
||||||
|
return values[0] if values else None
|
||||||
|
|
||||||
def getValues(self):
|
def getValues(self):
|
||||||
result = []
|
result = []
|
||||||
for widget in self.content:
|
for widget in self.content:
|
||||||
|
|
@ -129,8 +126,14 @@ class List(urwid.WidgetWrap):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def changeValues(self, new_values):
|
def changeValues(self, new_values):
|
||||||
widgets = [SelectableText(self, option) for option in new_values]
|
widgets = [SelectableText(option, self.align) for option in new_values]
|
||||||
|
for widget in widgets:
|
||||||
|
urwid.connect_signal(widget, 'change', self.__onStateChange)
|
||||||
self.content[:] = widgets
|
self.content[:] = widgets
|
||||||
|
if self.single and new_values:
|
||||||
|
self.content[0].setState(True)
|
||||||
|
display_widget = urwid.BoxAdapter(self.list_box, min(len(new_values),5))
|
||||||
|
urwid.WidgetWrap.__init__(self, display_widget)
|
||||||
|
|
||||||
def selectValue(self, value):
|
def selectValue(self, value):
|
||||||
self.unselectAll()
|
self.unselectAll()
|
||||||
|
|
@ -155,6 +158,8 @@ class genericDialog(urwid.WidgetWrap):
|
||||||
elif "YES/NO" in style:
|
elif "YES/NO" in style:
|
||||||
buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']),
|
buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']),
|
||||||
urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])]
|
urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])]
|
||||||
|
if "OK" in style:
|
||||||
|
buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])]
|
||||||
if buttons:
|
if buttons:
|
||||||
buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
|
buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
|
||||||
widgets_lst.append(buttons_flow)
|
widgets_lst.append(buttons_flow)
|
||||||
|
|
@ -177,3 +182,8 @@ class ConfirmDialog(genericDialog):
|
||||||
|
|
||||||
def __init__(self, title, style=['YES/NO'], **kwargs):
|
def __init__(self, title, style=['YES/NO'], **kwargs):
|
||||||
genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs)
|
genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs)
|
||||||
|
|
||||||
|
class Alert(genericDialog):
|
||||||
|
|
||||||
|
def __init__(self, title, message, style=['OK'], **kwargs):
|
||||||
|
genericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue