Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
2361:5defafc8ede6 | 2362:3acbaf5c29f5 |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """ template XMLUI parsing | |
21 | |
22 XMLUI classes from this modules can then be iterated to create the template | |
23 """ | |
24 | |
25 from sat.core.log import getLogger | |
26 log = getLogger(__name__) | |
27 from sat_frontends.tools import xmlui | |
28 | |
29 | |
30 ## Widgets ## | |
31 | |
32 class Widget(object): | |
33 category = u'widget' | |
34 type = None | |
35 enabled = True | |
36 read_only = True | |
37 | |
38 def __init__(self, xmlui_parent): | |
39 self.xmlui_parent = xmlui_parent | |
40 | |
41 @property | |
42 def name(self): | |
43 return self._xmlui_name | |
44 | |
45 | |
46 class ValueWidget(Widget): | |
47 | |
48 def __init__(self, xmlui_parent, value): | |
49 super(ValueWidget, self).__init__(xmlui_parent) | |
50 self.value = value | |
51 | |
52 | |
53 class InputWidget(ValueWidget): | |
54 | |
55 def __init__(self, xmlui_parent, value, read_only=False): | |
56 super(InputWidget, self).__init__(xmlui_parent, value) | |
57 self.read_only = read_only | |
58 | |
59 | |
60 class OptionsWidget(Widget): | |
61 | |
62 def __init__(self, xmlui_parent, options, selected, style): | |
63 super(OptionsWidget, self).__init__(xmlui_parent) | |
64 self.options = options | |
65 self.selected = selected | |
66 self.style = style | |
67 | |
68 | |
69 class EmptyWidget(xmlui.EmptyWidget, Widget): | |
70 | |
71 def __init__(self, _xmlui_parent): | |
72 Widget.__init__(self) | |
73 | |
74 | |
75 class TextWidget(xmlui.TextWidget, ValueWidget): | |
76 type = u"text" | |
77 | |
78 | |
79 class LabelWidget(xmlui.LabelWidget, ValueWidget): | |
80 type = u"label" | |
81 | |
82 | |
83 class StringWidget(xmlui.LabelWidget, InputWidget): | |
84 type = u"string" | |
85 | |
86 | |
87 class TextBoxWidget(xmlui.LabelWidget, InputWidget): | |
88 type = u"textbox" | |
89 | |
90 | |
91 class ListWidget(xmlui.ListWidget, OptionsWidget): | |
92 type = u"list" | |
93 | |
94 | |
95 ## Containers ## | |
96 | |
97 class Container(object): | |
98 category = u'container' | |
99 type = None | |
100 | |
101 def __init__(self, xmlui_parent): | |
102 self.xmlui_parent = xmlui_parent | |
103 self.children = [] | |
104 | |
105 def __iter__(self): | |
106 return iter(self.children) | |
107 | |
108 def _xmluiAppend(self, widget): | |
109 self.children.append(widget) | |
110 | |
111 | |
112 class VerticalContainer(xmlui.VerticalContainer, Container): | |
113 type = u'vertical' | |
114 | |
115 | |
116 class PairsContainer(xmlui.PairsContainer, Container): | |
117 type = u'pairs' | |
118 | |
119 | |
120 class LabelContainer(xmlui.PairsContainer, Container): | |
121 type = u'label' | |
122 | |
123 | |
124 ## Factory ## | |
125 | |
126 class WidgetFactory(object): | |
127 | |
128 def __getattr__(self, attr): | |
129 if attr.startswith("create"): | |
130 cls = globals()[attr[6:]] | |
131 return cls | |
132 | |
133 ## Core ## | |
134 | |
135 | |
136 class XMLUIPanel(xmlui.XMLUIPanel): | |
137 widget_factory = WidgetFactory() | |
138 | |
139 def show(self, *args, **kwargs): | |
140 raise NotImplementedError | |
141 | |
142 | |
143 class XMLUIDialog(xmlui.XMLUIDialog): | |
144 dialog_factory = WidgetFactory() | |
145 | |
146 def __init__(*args, **kwargs): | |
147 raise NotImplementedError | |
148 | |
149 | |
150 xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel) | |
151 xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog) | |
152 create = xmlui.create |