changeset 114:77f48939ad6e

primitivus: added Alert widget
author Goffi <goffi@goffi.org>
date Thu, 01 Jul 2010 18:18:34 +0800
parents e5ca22113280
children eed4f77c942e
files frontends/primitivus/custom_widgets.py
diffstat 1 files changed, 21 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/primitivus/custom_widgets.py	Thu Jul 01 15:35:56 2010 +0800
+++ b/frontends/primitivus/custom_widgets.py	Thu Jul 01 18:18:34 2010 +0800
@@ -85,23 +85,15 @@
     signals = ['change']
 
     def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None):
-        assert(options)
         self.single = 'single' in style
         self.align = align
         
         if on_state_change:
             urwid.connect_signal(self, 'change', on_state_change, user_data)
         
-        widgets = [SelectableText(option,align) for option in options]
-        for widget in widgets:
-            urwid.connect_signal(widget, 'change', self.__onStateChange)
-        self.content = urwid.SimpleListWalker(widgets)
+        self.content = urwid.SimpleListWalker([])
         self.list_box = urwid.ListBox(self.content)
-        if self.single:
-            self.content[0].setState(True)
-        display_widget = urwid.BoxAdapter(self.list_box, min(len(options),5))
-        
-        urwid.WidgetWrap.__init__(self, display_widget)
+        self.changeValues(options)
 
     def __onStateChange(self, widget, selected):
         if self.single:
@@ -121,6 +113,11 @@
                 widget.setState(False, invisible)
                 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):
         result = []
         for widget in self.content:
@@ -129,8 +126,14 @@
         return result
 
     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
+        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):
         self.unselectAll()
@@ -155,6 +158,8 @@
         elif "YES/NO" in style:
             buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']),
                       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:
             buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
             widgets_lst.append(buttons_flow)
@@ -177,3 +182,8 @@
 
     def __init__(self, title, style=['YES/NO'], **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)