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