changeset 1085:7a39ae3950f7

frontends (XMLUI): implementation of read_only attributes for widgets String, TextBox, Password and Bool
author Goffi <goffi@goffi.org>
date Wed, 25 Jun 2014 14:01:56 +0200
parents 03dcb6ca7e49
children 2cb30f46e560
files frontends/src/primitivus/xmlui.py frontends/src/tools/xmlui.py frontends/src/wix/xmlui.py
diffstat 3 files changed, 60 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/xmlui.py	Wed Jun 25 14:01:55 2014 +0200
+++ b/frontends/src/primitivus/xmlui.py	Wed Jun 25 14:01:56 2014 +0200
@@ -46,7 +46,7 @@
 
 class PrimitivusTextWidget(xmlui.TextWidget, urwid.Text):
 
-    def __init__(self, parent, value):
+    def __init__(self, parent, value, read_only=False):
         urwid.Text.__init__(self, value)
 
 
@@ -82,8 +82,14 @@
 
 class PrimitivusStringWidget(xmlui.StringWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
 
-    def __init__(self, parent, value):
+    def __init__(self, parent, value, read_only=False):
         sat_widgets.AdvancedEdit.__init__(self, edit_text=value)
+        self.read_only = read_only
+
+    def selectable(self):
+        if self.read_only:
+            return False
+        return super(PrimitivusStringWidget, self).selectable()
 
     def _xmluiSetValue(self, value):
         self.set_edit_text(value)
@@ -94,8 +100,14 @@
 
 class PrimitivusPasswordWidget(xmlui.PasswordWidget, sat_widgets.Password, PrimitivusEvents):
 
-    def __init__(self, parent, value):
+    def __init__(self, parent, value, read_only=False):
         sat_widgets.Password.__init__(self, edit_text=value)
+        self.read_only = read_only
+
+    def selectable(self):
+        if self.read_only:
+            return False
+        return super(PrimitivusPasswordWidget, self).selectable()
 
     def _xmluiSetValue(self, value):
         self.set_edit_text(value)
@@ -106,8 +118,14 @@
 
 class PrimitivusTextBoxWidget(xmlui.TextBoxWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
 
-    def __init__(self, parent, value):
+    def __init__(self, parent, value, read_only=False):
         sat_widgets.AdvancedEdit.__init__(self, edit_text=value, multiline=True)
+        self.read_only = read_only
+
+    def selectable(self):
+        if self.read_only:
+            return False
+        return super(PrimitivusTextBoxWidget, self).selectable()
 
     def _xmluiSetValue(self, value):
         self.set_edit_text(value)
@@ -118,8 +136,14 @@
 
 class PrimitivusBoolWidget(xmlui.BoolWidget, urwid.CheckBox, PrimitivusEvents):
 
-    def __init__(self, parent, state):
+    def __init__(self, parent, state, read_only=False):
         urwid.CheckBox.__init__(self, '', state=state)
+        self.read_only = read_only
+
+    def selectable(self):
+        if self.read_only:
+            return False
+        return super(PrimitivusBoolWidget, self).selectable()
 
     def _xmluiSetValue(self, value):
         self.set_state(value == "true")
--- a/frontends/src/tools/xmlui.py	Wed Jun 25 14:01:55 2014 +0200
+++ b/frontends/src/tools/xmlui.py	Wed Jun 25 14:01:56 2014 +0200
@@ -191,6 +191,16 @@
             raise ValueError(_("XMLUI can have only one main container"))
         self._main_cont = value
 
+    def _isAttrSet(self, name, node):
+        """Returnw widget boolean attribute status
+
+        @param name: name of the attribute (e.g. "read_only")
+        @param node: Node instance
+        @return (bool): True if widget's attribute is set ("true")
+        """
+        read_only = node.getAttribute(name) or "false"
+        return read_only.lower().strip() == "true"
+
     def _parseChilds(self, parent, current_node, wanted = ('container',), data = None):
         """ Recursively parse childNodes of an elemen
         @param parent: widget container with '_xmluiAppend' method
@@ -289,16 +299,16 @@
                     style = node.getAttribute("style") or 'line'
                     ctrl = self.widget_factory.createDividerWidget(parent, style)
                 elif type_=="string":
-                    ctrl = self.widget_factory.createStringWidget(parent, value)
+                    ctrl = self.widget_factory.createStringWidget(parent, value, self._isAttrSet("read_only", node))
                     self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
                 elif type_=="password":
-                    ctrl = self.widget_factory.createPasswordWidget(parent, value)
+                    ctrl = self.widget_factory.createPasswordWidget(parent, value, self._isAttrSet("read_only", node))
                     self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
                 elif type_=="textbox":
-                    ctrl = self.widget_factory.createTextBoxWidget(parent, value)
+                    ctrl = self.widget_factory.createTextBoxWidget(parent, value, self._isAttrSet("read_only", node))
                     self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
                 elif type_=="bool":
-                    ctrl = self.widget_factory.createBoolWidget(parent, value=='true')
+                    ctrl = self.widget_factory.createBoolWidget(parent, value=='true', self._isAttrSet("read_only", node))
                     self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
                 elif type_ == "list":
                     style = [] if node.getAttribute("multi") == 'yes' else ['single']
--- a/frontends/src/wix/xmlui.py	Wed Jun 25 14:01:55 2014 +0200
+++ b/frontends/src/wix/xmlui.py	Wed Jun 25 14:01:56 2014 +0200
@@ -82,31 +82,41 @@
 class StringWidget(EventWidget, ValueWidget, xmlui.StringWidget, wx.TextCtrl):
     _xmlui_change_event = wx.EVT_TEXT
 
-    def __init__(self, parent, value):
-        wx.TextCtrl.__init__(self, parent, -1, value)
+    def __init__(self, parent, value, read_only=False):
+        style = wx.TE_READONLY if read_only else 0
+        wx.TextCtrl.__init__(self, parent, -1, value, style=style)
         self._xmlui_proportion = 1
 
 
 class PasswordWidget(EventWidget, ValueWidget, xmlui.PasswordWidget, wx.TextCtrl):
     _xmlui_change_event = wx.EVT_TEXT
 
-    def __init__(self, parent, value):
-        wx.TextCtrl.__init__(self, parent, -1, value, style=wx.TE_PASSWORD)
+    def __init__(self, parent, value, read_only=False):
+        style = wx.TE_PASSWORD
+        if read_only:
+            style |= wx.TE_READONLY
+        wx.TextCtrl.__init__(self, parent, -1, value, style=style)
         self._xmlui_proportion = 1
 
 
 class TextBoxWidget(EventWidget, ValueWidget, xmlui.TextBoxWidget, wx.TextCtrl):
     _xmlui_change_event = wx.EVT_TEXT
 
-    def __init__(self, parent, value):
-        wx.TextCtrl.__init__(self, parent, -1, value, style=wx.TE_MULTILINE)
+    def __init__(self, parent, value, read_only=False):
+        style = wx.TE_MULTILINE
+        if read_only:
+            style |= wx.TE_READONLY
+        wx.TextCtrl.__init__(self, parent, -1, value, style=style)
         self._xmlui_proportion = 1
 
 
 class BoolWidget(EventWidget, ValueWidget, xmlui.BoolWidget, wx.CheckBox):
     _xmlui_change_event = wx.EVT_CHECKBOX
 
-    def __init__(self, parent, state):
+    def __init__(self, parent, state, read_only=False):
+        style = wx.CHK_2STATE
+        if read_only:
+            style |= wx.TE_READONLY
         wx.CheckBox.__init__(self, parent, -1, "", style=wx.CHK_2STATE)
         self.SetValue(state)
         self._xmlui_proportion = 1