diff src/tools/common/template_xmlui.py @ 2362:3acbaf5c29f5

template: template XMLUI first draft: this module prepares an object which can be easily used in templates to construct interface from XMLUI.
author Goffi <goffi@goffi.org>
date Sun, 24 Sep 2017 16:39:36 +0200
parents
children 9878635586f3
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tools/common/template_xmlui.py	Sun Sep 24 16:39:36 2017 +0200
@@ -0,0 +1,152 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+
+# SAT: a jabber client
+# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+""" template XMLUI parsing
+
+XMLUI classes from this modules can then be iterated to create the template
+"""
+
+from sat.core.log import getLogger
+log = getLogger(__name__)
+from sat_frontends.tools import xmlui
+
+
+## Widgets ##
+
+class Widget(object):
+    category = u'widget'
+    type = None
+    enabled = True
+    read_only = True
+
+    def __init__(self, xmlui_parent):
+        self.xmlui_parent = xmlui_parent
+
+    @property
+    def name(self):
+        return self._xmlui_name
+
+
+class ValueWidget(Widget):
+
+    def __init__(self, xmlui_parent, value):
+        super(ValueWidget, self).__init__(xmlui_parent)
+        self.value = value
+
+
+class InputWidget(ValueWidget):
+
+    def __init__(self, xmlui_parent, value, read_only=False):
+        super(InputWidget, self).__init__(xmlui_parent, value)
+        self.read_only = read_only
+
+
+class OptionsWidget(Widget):
+
+    def __init__(self, xmlui_parent, options, selected, style):
+        super(OptionsWidget, self).__init__(xmlui_parent)
+        self.options = options
+        self.selected = selected
+        self.style = style
+
+
+class EmptyWidget(xmlui.EmptyWidget, Widget):
+
+    def __init__(self, _xmlui_parent):
+        Widget.__init__(self)
+
+
+class TextWidget(xmlui.TextWidget, ValueWidget):
+    type = u"text"
+
+
+class LabelWidget(xmlui.LabelWidget, ValueWidget):
+    type = u"label"
+
+
+class StringWidget(xmlui.LabelWidget, InputWidget):
+    type = u"string"
+
+
+class TextBoxWidget(xmlui.LabelWidget, InputWidget):
+    type = u"textbox"
+
+
+class ListWidget(xmlui.ListWidget, OptionsWidget):
+    type = u"list"
+
+
+## Containers ##
+
+class Container(object):
+    category = u'container'
+    type = None
+
+    def __init__(self, xmlui_parent):
+        self.xmlui_parent = xmlui_parent
+        self.children = []
+
+    def __iter__(self):
+        return iter(self.children)
+
+    def _xmluiAppend(self, widget):
+        self.children.append(widget)
+
+
+class VerticalContainer(xmlui.VerticalContainer, Container):
+    type = u'vertical'
+
+
+class PairsContainer(xmlui.PairsContainer, Container):
+    type = u'pairs'
+
+
+class LabelContainer(xmlui.PairsContainer, Container):
+    type = u'label'
+
+
+## Factory ##
+
+class WidgetFactory(object):
+
+    def __getattr__(self, attr):
+        if attr.startswith("create"):
+            cls = globals()[attr[6:]]
+            return cls
+
+## Core ##
+
+
+class XMLUIPanel(xmlui.XMLUIPanel):
+    widget_factory = WidgetFactory()
+
+    def show(self, *args, **kwargs):
+        raise NotImplementedError
+
+
+class XMLUIDialog(xmlui.XMLUIDialog):
+    dialog_factory = WidgetFactory()
+
+    def __init__(*args, **kwargs):
+        raise NotImplementedError
+
+
+xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel)
+xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog)
+create = xmlui.create