Mercurial > libervia-backend
annotate src/tools/common/template_xmlui.py @ 2402:f905dfe69fcc
template: new item_value filter
this filter is used with XMLUI to specify filters before using a generate method.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 27 Oct 2017 18:17:35 +0200 |
parents | 8253ea069781 |
children | 8b37a62336c3 |
rev | line source |
---|---|
2362 | 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 | |
2367
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
52 @property |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
53 def values(self): |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
54 return [self.value] |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
55 |
2362 | 56 |
57 class InputWidget(ValueWidget): | |
58 | |
59 def __init__(self, xmlui_parent, value, read_only=False): | |
60 super(InputWidget, self).__init__(xmlui_parent, value) | |
61 self.read_only = read_only | |
62 | |
63 | |
64 class OptionsWidget(Widget): | |
65 | |
66 def __init__(self, xmlui_parent, options, selected, style): | |
67 super(OptionsWidget, self).__init__(xmlui_parent) | |
68 self.options = options | |
69 self.selected = selected | |
70 self.style = style | |
71 | |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
72 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
73 def values(self): |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
74 return self.selected |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
75 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
76 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
77 def labels(self): |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
78 """return only labels from self.items""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
79 for value, label in self.items: |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
80 yield label |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
81 |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
82 @property |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
83 def items(self): |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
84 """return suitable items, according to style""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
85 no_select = self.no_select |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
86 for value,label in self.options: |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
87 if no_select or value in self.selected: |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
88 yield value,label |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
89 |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
90 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
91 def inline(self): |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
92 return u'inline' in self.style |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
93 |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
94 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
95 def no_select(self): |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
96 return u'noselect' in self.style |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
97 |
2362 | 98 |
99 class EmptyWidget(xmlui.EmptyWidget, Widget): | |
100 | |
101 def __init__(self, _xmlui_parent): | |
102 Widget.__init__(self) | |
103 | |
104 | |
105 class TextWidget(xmlui.TextWidget, ValueWidget): | |
106 type = u"text" | |
107 | |
108 | |
109 class LabelWidget(xmlui.LabelWidget, ValueWidget): | |
110 type = u"label" | |
111 | |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
112 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
113 def for_name(self): |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
114 try: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
115 return self._xmlui_for_name |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
116 except AttributeError: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
117 return None |
2362 | 118 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
119 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
120 class StringWidget(xmlui.StringWidget, InputWidget): |
2362 | 121 type = u"string" |
122 | |
123 | |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
124 class TextBoxWidget(xmlui.TextWidget, InputWidget): |
2362 | 125 type = u"textbox" |
126 | |
127 | |
128 class ListWidget(xmlui.ListWidget, OptionsWidget): | |
129 type = u"list" | |
130 | |
131 | |
132 ## Containers ## | |
133 | |
134 class Container(object): | |
135 category = u'container' | |
136 type = None | |
137 | |
138 def __init__(self, xmlui_parent): | |
139 self.xmlui_parent = xmlui_parent | |
140 self.children = [] | |
141 | |
142 def __iter__(self): | |
143 return iter(self.children) | |
144 | |
145 def _xmluiAppend(self, widget): | |
146 self.children.append(widget) | |
147 | |
148 | |
149 class VerticalContainer(xmlui.VerticalContainer, Container): | |
150 type = u'vertical' | |
151 | |
152 | |
153 class PairsContainer(xmlui.PairsContainer, Container): | |
154 type = u'pairs' | |
155 | |
156 | |
157 class LabelContainer(xmlui.PairsContainer, Container): | |
158 type = u'label' | |
159 | |
160 | |
161 ## Factory ## | |
162 | |
163 class WidgetFactory(object): | |
164 | |
165 def __getattr__(self, attr): | |
166 if attr.startswith("create"): | |
167 cls = globals()[attr[6:]] | |
168 return cls | |
169 | |
170 ## Core ## | |
171 | |
172 | |
173 class XMLUIPanel(xmlui.XMLUIPanel): | |
174 widget_factory = WidgetFactory() | |
175 | |
176 def show(self, *args, **kwargs): | |
177 raise NotImplementedError | |
178 | |
179 | |
180 class XMLUIDialog(xmlui.XMLUIDialog): | |
181 dialog_factory = WidgetFactory() | |
182 | |
183 def __init__(*args, **kwargs): | |
184 raise NotImplementedError | |
185 | |
186 | |
187 xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel) | |
188 xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog) | |
189 create = xmlui.create |