[node-patches] Change in ovirt-node[master]: Add ovirt-node.widgets.Options

fabiand at fedoraproject.org fabiand at fedoraproject.org
Tue Dec 11 20:09:33 UTC 2012


Fabian Deutsch has uploaded a new change for review.

Change subject: Add ovirt-node.widgets.Options
......................................................................

Add ovirt-node.widgets.Options

Change-Id: I2da61076ecd2d5aab70c0e2af12968e2d9dfa3cd
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/plugins/__init__.py
M scripts/tui/src/ovirt/node/tui.py
M scripts/tui/src/ovirt/node/valid.py
M scripts/tui/src/ovirt/node/widgets.py
4 files changed, 100 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/75/9875/1

diff --git a/scripts/tui/src/ovirt/node/plugins/__init__.py b/scripts/tui/src/ovirt/node/plugins/__init__.py
index 4094a85..437a5ba 100644
--- a/scripts/tui/src/ovirt/node/plugins/__init__.py
+++ b/scripts/tui/src/ovirt/node/plugins/__init__.py
@@ -306,6 +306,22 @@
     def __init__(self, char=u" "):
         self.char = char
 
+
+class Options(Widget):
+    signals = ["change"]
+    signaling_properties = ["option"]
+
+    def __init__(self, label, options):
+        self.label = label
+        self.options = options
+        super(Options, self).__init__()
+
+    def option(self, option=None):
+        return self._signaling_property("option", \
+                                        lambda: option in self.options,
+                                        option)
+
+
 class InvalidData(Exception):
     """E.g. if a string contains characters which are not allowed
     """
diff --git a/scripts/tui/src/ovirt/node/tui.py b/scripts/tui/src/ovirt/node/tui.py
index 255b901..7161e63 100644
--- a/scripts/tui/src/ovirt/node/tui.py
+++ b/scripts/tui/src/ovirt/node/tui.py
@@ -71,6 +71,7 @@
             ovirt.node.plugins.Button: ovirt.node.widgets.Button,
             ovirt.node.plugins.SaveButton: ovirt.node.widgets.Button,
             ovirt.node.plugins.Divider: ovirt.node.widgets.Divider,
+            ovirt.node.plugins.Options: ovirt.node.widgets.Options,
         }
 
         assert type(item) in item_to_widget_map.keys(), \
@@ -86,6 +87,8 @@
                 value = plugin.model()[path]
 
             widget = widget_class(item.label, value)
+
+            widget.enable(item.enabled)
 
             def on_item_enabled_change_cb(w, v):
                 LOGGER.debug("Model changed, updating widget '%s': %s" % (w,
@@ -137,6 +140,16 @@
 
         elif type(item) in [ovirt.node.plugins.Divider]:
             widget = widget_class(item.char)
+
+        elif type(item) in [ovirt.node.plugins.Options]:
+            widget = widget_class(item.label, item.options,
+                                  plugin.model()[path])
+            def on_widget_change_cb(widget, data):
+                LOGGER.debug(data)
+                item.option(data)
+                plugin._on_ui_change({path: data})
+            urwid.connect_signal(widget, "change", on_widget_change_cb)
+
         return widget
 
     def __build_plugin_widget(self, plugin):
diff --git a/scripts/tui/src/ovirt/node/valid.py b/scripts/tui/src/ovirt/node/valid.py
index 99a8687..3ad4ef9 100644
--- a/scripts/tui/src/ovirt/node/valid.py
+++ b/scripts/tui/src/ovirt/node/valid.py
@@ -222,3 +222,16 @@
         return (FQDN().validate(value) or \
                 IPv4Address().validate(value) or \
                 IPv6Address().validate(value))
+
+
+class Options(Validator):
+    options = None
+    description = "one of: %s"
+
+    def __init__(self, options):
+        assert type(options) is list, "Options must be a list"
+        self.options = options
+
+    def validate(self, value):
+        self.description = self.description % self.options
+        return value in self.options
diff --git a/scripts/tui/src/ovirt/node/widgets.py b/scripts/tui/src/ovirt/node/widgets.py
index 1b492a2..225d2f1 100644
--- a/scripts/tui/src/ovirt/node/widgets.py
+++ b/scripts/tui/src/ovirt/node/widgets.py
@@ -213,9 +213,67 @@
 
         super(Button, self).__init__(self._padding)
 
+
 class Divider(urwid.WidgetWrap):
     def __init__(self, char=u" "):
         self._divider = urwid.Divider(char)
         self._divider_attrmap = urwid.AttrMap(self._divider,
                                               "plugin.widget.divider")
         super(Divider, self).__init__(self._divider_attrmap)
+
+
+class Options(urwid.WidgetWrap):
+    signals = ["change"]
+
+    def __init__(self, label, options, selected_option_key):
+        self._options = options
+        self._button_to_key = {}
+        self._bgroup = []
+        self._buttons = [urwid.Text(label + ":")]
+        for option_key, option_label in self._options:
+            widget = urwid.RadioButton(self._bgroup, option_label,
+                                       on_state_change=self._on_state_change)
+            self._button_to_key[widget] = option_key
+            if option_key == selected_option_key:
+                widget.set_state(True)
+            self._buttons.append(widget)
+        self._columns = urwid.Columns(self._buttons)
+        super(Options, self).__init__(self._columns)
+
+    def _on_state_change(self, widget, new_state):
+        if new_state:
+            data = self._button_to_key[widget]
+            urwid.emit_signal(self, "change", widget, data)
+
+
+#https://github.com/pazz/alot/blob/master/alot/widgets/globals.py
+class ChoiceWidget(urwid.Text):
+    def __init__(self, choices, callback, cancel=None, select=None,
+                 separator=' '):
+        self.choices = choices
+        self.callback = callback
+        self.cancel = cancel
+        self.select = select
+        self.separator = separator
+
+        items = []
+        for k, v in choices.items():
+            if v == select and select is not None:
+                items += ['[', k, ']:', v]
+            else:
+                items += ['(', k, '):', v]
+            items += [self.separator]
+        urwid.Text.__init__(self, items)
+
+    def selectable(self):
+        return True
+
+    def keypress(self, size, key):
+        if key == 'enter' and self.select is not None:
+            self.callback(self.select)
+        elif key == 'esc' and self.cancel is not None:
+            self.callback(self.cancel)
+        elif key in self.choices:
+            self.callback(self.choices[key])
+        else:
+            return key


--
To view, visit http://gerrit.ovirt.org/9875
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2da61076ecd2d5aab70c0e2af12968e2d9dfa3cd
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list