Mercurial > libervia-backend
annotate sat_frontends/jp/xmlui_manager.py @ 2739:e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
- JidWidget is now handled basically using TextWidget.
- EmptyWidget display an empty line.
- BoolWidget display current value with an asterisk when write is allowed.
- fixed an bug in LabelContainer when for_name was not specified.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 02 Jan 2019 18:50:47 +0100 |
parents | bdb8276fd2da |
children | 378188abe941 |
rev | line source |
---|---|
2408 | 1 #!/usr/bin/env python2 |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # JP: a SàT frontend | |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
2408 | 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 from sat.core.log import getLogger | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
21 |
2408 | 22 log = getLogger(__name__) |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
23 from sat_frontends.tools import xmlui as xmlui_base |
2408 | 24 from sat_frontends.jp.constants import Const as C |
25 from sat.tools.common.ansi import ANSI as A | |
26 from sat.core.i18n import _ | |
27 from functools import partial | |
28 | |
29 # workflow constants | |
30 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
31 SUBMIT = "SUBMIT" # submit form |
2408 | 32 |
33 | |
34 ## Widgets ## | |
35 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
36 |
2408 | 37 class Base(object): |
38 """Base for Widget and Container""" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 |
2408 | 40 type = None |
41 _root = None | |
42 | |
43 def __init__(self, xmlui_parent): | |
44 self.xmlui_parent = xmlui_parent | |
45 self.host = self.xmlui_parent.host | |
46 | |
47 @property | |
48 def root(self): | |
49 """retrieve main XMLUI parent class""" | |
50 if self._root is not None: | |
51 return self._root | |
52 root = self | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
53 while not isinstance(root, xmlui_base.XMLUIBase): |
2408 | 54 root = root.xmlui_parent |
55 self._root = root | |
56 return root | |
57 | |
58 def disp(self, *args, **kwargs): | |
59 self.host.disp(*args, **kwargs) | |
60 | |
61 | |
62 class Widget(Base): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 category = u"widget" |
2408 | 64 enabled = True |
65 | |
66 @property | |
67 def name(self): | |
68 return self._xmlui_name | |
69 | |
70 def show(self): | |
71 """display current widget | |
72 | |
73 must be overriden by subclasses | |
74 """ | |
75 raise NotImplementedError(self.__class__) | |
76 | |
77 def verboseName(self, elems=None, value=None): | |
78 """add name in color to the elements | |
79 | |
80 helper method to display name which can then be used to automate commands | |
81 elems is only modified if verbosity is > 0 | |
82 @param elems(list[unicode], None): elements to display | |
83 None to display name directly | |
84 @param value(unicode, None): value to show | |
85 use self.name if None | |
86 """ | |
87 if value is None: | |
88 value = self.name | |
89 if self.host.verbosity: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 to_disp = [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 A.FG_MAGENTA, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
92 u" " if elems else u"", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
93 u"({})".format(value), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 A.RESET, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 ] |
2408 | 96 if elems is None: |
97 self.host.disp(A.color(*to_disp)) | |
98 else: | |
99 elems.extend(to_disp) | |
100 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 |
2408 | 102 class ValueWidget(Widget): |
103 def __init__(self, xmlui_parent, value): | |
104 super(ValueWidget, self).__init__(xmlui_parent) | |
105 self.value = value | |
106 | |
107 @property | |
108 def values(self): | |
109 return [self.value] | |
110 | |
111 | |
112 class InputWidget(ValueWidget): | |
113 def __init__(self, xmlui_parent, value, read_only=False): | |
114 super(InputWidget, self).__init__(xmlui_parent, value) | |
115 self.read_only = read_only | |
116 | |
117 def _xmluiGetValue(self): | |
118 return self.value | |
119 | |
120 | |
121 class OptionsWidget(Widget): | |
122 def __init__(self, xmlui_parent, options, selected, style): | |
123 super(OptionsWidget, self).__init__(xmlui_parent) | |
124 self.options = options | |
125 self.selected = selected | |
126 self.style = style | |
127 | |
128 @property | |
129 def values(self): | |
130 return self.selected | |
131 | |
132 @values.setter | |
133 def values(self, values): | |
134 self.selected = values | |
135 | |
136 @property | |
137 def value(self): | |
138 return self.selected[0] | |
139 | |
140 @value.setter | |
141 def value(self, value): | |
142 self.selected = [value] | |
143 | |
144 def _xmluiSelectValue(self, value): | |
145 self.value = value | |
146 | |
147 def _xmluiSelectValues(self, values): | |
148 self.values = values | |
149 | |
150 def _xmluiGetSelectedValues(self): | |
151 return self.values | |
152 | |
153 @property | |
154 def labels(self): | |
155 """return only labels from self.items""" | |
156 for value, label in self.items: | |
157 yield label | |
158 | |
159 @property | |
160 def items(self): | |
161 """return suitable items, according to style""" | |
162 no_select = self.no_select | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
163 for value, label in self.options: |
2408 | 164 if no_select or value in self.selected: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 yield value, label |
2408 | 166 |
167 @property | |
168 def inline(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 return u"inline" in self.style |
2408 | 170 |
171 @property | |
172 def no_select(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 return u"noselect" in self.style |
2408 | 174 |
175 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
176 class EmptyWidget(xmlui_base.EmptyWidget, Widget): |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
177 |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
178 def __init__(self, xmlui_parent): |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
179 Widget.__init__(self, xmlui_parent) |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
180 |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
181 def show(self): |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
182 self.host.disp(u'') |
2408 | 183 |
184 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
185 class TextWidget(xmlui_base.TextWidget, ValueWidget): |
2408 | 186 type = u"text" |
187 | |
188 def show(self): | |
189 self.host.disp(self.value) | |
190 | |
191 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
192 class LabelWidget(xmlui_base.LabelWidget, ValueWidget): |
2408 | 193 type = u"label" |
194 | |
195 @property | |
196 def for_name(self): | |
197 try: | |
198 return self._xmlui_for_name | |
199 except AttributeError: | |
200 return None | |
201 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
202 def show(self, no_lf=False, ansi=u""): |
2408 | 203 """show label |
204 | |
205 @param no_lf(bool): same as for [JP.disp] | |
206 @param ansi(unicode): ansi escape code to print before label | |
207 """ | |
208 self.disp(A.color(ansi, self.value), no_lf=no_lf) | |
209 | |
210 | |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
211 class JidWidget(xmlui_base.JidWidget, TextWidget): |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
212 type = u"jid" |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
213 |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
214 class StringWidget(xmlui_base.StringWidget, InputWidget): |
2408 | 215 type = u"string" |
216 | |
217 def show(self): | |
218 if self.read_only: | |
219 self.disp(self.value) | |
220 else: | |
221 elems = [] | |
222 self.verboseName(elems) | |
223 if self.value: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
224 elems.append(_(u"(enter: {default})").format(default=self.value)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
225 elems.extend([C.A_HEADER, u"> "]) |
2408 | 226 value = raw_input(A.color(*elems)) |
227 if value: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
228 # TODO: empty value should be possible |
2408 | 229 # an escape key should be used for default instead of enter with empty value |
230 self.value = value | |
231 | |
232 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
233 class JidInputWidget(xmlui_base.JidInputWidget, StringWidget): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
234 type = u"jid_input" |
2408 | 235 |
236 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
237 class TextBoxWidget(xmlui_base.TextWidget, StringWidget): |
2408 | 238 type = u"textbox" |
239 | |
240 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
241 class ListWidget(xmlui_base.ListWidget, OptionsWidget): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
242 type = u"list" |
2408 | 243 # TODO: handle flags, notably multi |
244 | |
245 def show(self): | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
246 if self.root.values_only: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
247 for value in self.values: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
248 self.disp(self.value) |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
249 return |
2408 | 250 if not self.options: |
251 return | |
252 | |
253 # list display | |
254 self.verboseName() | |
255 | |
256 for idx, (value, label) in enumerate(self.options): | |
257 elems = [] | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
258 if not self.root.read_only: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
259 elems.extend([C.A_SUBHEADER, unicode(idx), A.RESET, u": "]) |
2408 | 260 elems.append(label) |
261 self.verboseName(elems, value) | |
262 self.disp(A.color(*elems)) | |
263 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
264 if self.root.read_only: |
2408 | 265 return |
266 | |
267 if len(self.options) == 1: | |
268 # we have only one option, no need to ask | |
269 self.value = self.options[0][0] | |
270 return | |
271 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
272 # we ask use to choose an option |
2408 | 273 choice = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
274 limit_max = len(self.options) - 1 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
275 while choice is None or choice < 0 or choice > limit_max: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
276 choice = raw_input( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
277 A.color(C.A_HEADER, _(u"your choice (0-{max}): ").format(max=limit_max)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
278 ) |
2408 | 279 try: |
280 choice = int(choice) | |
281 except ValueError: | |
282 choice = None | |
283 self.value = self.options[choice][0] | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 self.disp("") |
2408 | 285 |
286 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
287 class BoolWidget(xmlui_base.BoolWidget, InputWidget): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
288 type = u"bool" |
2408 | 289 |
290 def show(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
291 disp_true = A.color(A.FG_GREEN, u"TRUE") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 disp_false = A.color(A.FG_RED, u"FALSE") |
2408 | 293 if self.read_only: |
294 self.disp(disp_true if self.value else disp_false) | |
295 else: | |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
296 self.disp(A.color(C.A_HEADER, u"0: ", |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
297 disp_false, A.RESET, |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
298 u" *" if not self.value else u"")) |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
299 self.disp(A.color(C.A_HEADER, u"1: ", |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
300 disp_true, A.RESET, |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
301 u" *" if self.value else u"")) |
2408 | 302 choice = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
303 while choice not in ("0", "1"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
304 elems = [C.A_HEADER, _(u"your choice (0,1): ")] |
2408 | 305 self.verboseName(elems) |
306 choice = raw_input(A.color(*elems)) | |
307 self.value = bool(int(choice)) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
308 self.disp("") |
2408 | 309 |
310 def _xmluiGetValue(self): | |
311 return C.boolConst(self.value) | |
312 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 |
2408 | 314 ## Containers ## |
315 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 |
2408 | 317 class Container(Base): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
318 category = u"container" |
2408 | 319 |
320 def __init__(self, xmlui_parent): | |
321 super(Container, self).__init__(xmlui_parent) | |
322 self.children = [] | |
323 | |
324 def __iter__(self): | |
325 return iter(self.children) | |
326 | |
327 def _xmluiAppend(self, widget): | |
328 self.children.append(widget) | |
329 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
330 def _xmluiRemove(self, widget): |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
331 self.children.remove(widget) |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
332 |
2408 | 333 def show(self): |
334 for child in self.children: | |
335 child.show() | |
336 | |
337 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
338 class VerticalContainer(xmlui_base.VerticalContainer, Container): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
339 type = u"vertical" |
2408 | 340 |
341 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
342 class PairsContainer(xmlui_base.PairsContainer, Container): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
343 type = u"pairs" |
2408 | 344 |
345 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
346 class LabelContainer(xmlui_base.PairsContainer, Container): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
347 type = u"label" |
2408 | 348 |
349 def show(self): | |
350 for child in self.children: | |
351 no_lf = False | |
352 # we check linked widget type | |
353 # to see if we want the label on the same line or not | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
354 if child.type == u"label": |
2408 | 355 for_name = child.for_name |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
356 if for_name: |
2408 | 357 for_widget = self.root.widgets[for_name] |
358 wid_type = for_widget.type | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
359 if self.root.values_only or wid_type in ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
360 "text", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
361 "string", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
362 "jid_input", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
363 ): |
2408 | 364 no_lf = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
365 elif wid_type == "bool" and for_widget.read_only: |
2408 | 366 no_lf = True |
367 child.show(no_lf=no_lf, ansi=A.FG_CYAN) | |
368 else: | |
369 child.show() | |
370 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
371 |
2408 | 372 ## Dialogs ## |
373 | |
374 | |
375 class Dialog(object): | |
376 def __init__(self, xmlui_parent): | |
377 self.xmlui_parent = xmlui_parent | |
378 self.host = self.xmlui_parent.host | |
379 | |
380 def disp(self, *args, **kwargs): | |
381 self.host.disp(*args, **kwargs) | |
382 | |
383 def show(self): | |
384 """display current dialog | |
385 | |
386 must be overriden by subclasses | |
387 """ | |
388 raise NotImplementedError(self.__class__) | |
389 | |
390 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
391 class NoteDialog(xmlui_base.NoteDialog, Dialog): |
2408 | 392 def show(self): |
393 # TODO: handle title and level | |
394 self.disp(self.message) | |
395 | |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
396 def __init__(self, xmlui_parent, title, message, level): |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
397 Dialog.__init__(self, xmlui_parent) |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
398 xmlui_base.NoteDialog.__init__(self, xmlui_parent) |
2408 | 399 self.title, self.message, self.level = title, message, level |
400 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
401 |
2408 | 402 ## Factory ## |
403 | |
404 | |
405 class WidgetFactory(object): | |
406 def __getattr__(self, attr): | |
407 if attr.startswith("create"): | |
408 cls = globals()[attr[6:]] | |
409 return cls | |
410 | |
411 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
412 class XMLUIPanel(xmlui_base.XMLUIPanel): |
2408 | 413 widget_factory = WidgetFactory() |
414 _actions = 0 # use to keep track of bridge's launchAction calls | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
415 read_only = False |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
416 values_only = False |
2408 | 417 workflow = None |
418 _submit_cb = None | |
419 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
420 def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
421 ignore=None, whitelist=None, profile=None): |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
422 xmlui_base.XMLUIPanel.__init__( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
423 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
424 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
425 parsed_dom, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
426 title=title, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
427 flags=flags, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
428 ignore=ignore, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
429 whitelist=whitelist, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
430 profile=host.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
431 ) |
2408 | 432 self.submitted = False |
433 | |
434 @property | |
435 def command(self): | |
436 return self.host.command | |
437 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
438 def show(self, workflow=None, read_only=False, values_only=False): |
2408 | 439 """display the panel |
440 | |
441 @param workflow(list, None): command to execute if not None | |
442 put here for convenience, the main workflow is the class attribute | |
443 (because workflow can continue in subclasses) | |
444 command are a list of consts or lists: | |
445 - SUBMIT is the only constant so far, it submits the XMLUI | |
446 - list must contain widget name/widget value to fill | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
447 @param read_only(bool): if True, don't request values |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
448 @param values_only(bool): if True, only show select values (imply read_only) |
2408 | 449 """ |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
450 self.read_only = read_only |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
451 self.values_only = values_only |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
452 if self.values_only: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
453 self.read_only = True |
2408 | 454 if workflow: |
455 XMLUIPanel.workflow = workflow | |
456 if XMLUIPanel.workflow: | |
457 self.runWorkflow() | |
458 else: | |
459 self.main_cont.show() | |
460 | |
461 def runWorkflow(self): | |
462 """loop into workflow commands and execute commands | |
463 | |
464 SUBMIT will interrupt workflow (which will be continue on callback) | |
465 @param workflow(list): same as [show] | |
466 """ | |
467 workflow = XMLUIPanel.workflow | |
468 while True: | |
469 try: | |
470 cmd = workflow.pop(0) | |
471 except IndexError: | |
472 break | |
473 if cmd == SUBMIT: | |
474 self.onFormSubmitted() | |
475 self.submit_id = None # avoid double submit | |
476 return | |
477 elif isinstance(cmd, list): | |
478 name, value = cmd | |
2412
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
479 widget = self.widgets[name] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
480 if widget.type == "bool": |
2412
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
481 value = C.bool(value) |
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
482 widget.value = value |
2408 | 483 self.show() |
484 | |
485 def submitForm(self, callback=None): | |
486 XMLUIPanel._submit_cb = callback | |
487 self.onFormSubmitted() | |
488 | |
489 def onFormSubmitted(self, ignore=None): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
490 # self.submitted is a Q&D workaround to avoid |
2408 | 491 # double submit when a workflow is set |
492 if self.submitted: | |
493 return | |
494 self.submitted = True | |
495 super(XMLUIPanel, self).onFormSubmitted(ignore) | |
496 | |
497 def _xmluiClose(self): | |
498 pass | |
499 | |
500 def _launchActionCb(self, data): | |
501 XMLUIPanel._actions -= 1 | |
502 assert XMLUIPanel._actions >= 0 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
503 if u"xmlui" in data: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
504 xmlui_raw = data["xmlui"] |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
505 xmlui = create(self.host, xmlui_raw) |
2408 | 506 xmlui.show() |
507 if xmlui.submit_id: | |
508 xmlui.onFormSubmitted() | |
509 # TODO: handle data other than XMLUI | |
510 if not XMLUIPanel._actions: | |
511 if self._submit_cb is None: | |
512 self.host.quit() | |
513 else: | |
514 self._submit_cb() | |
515 | |
516 def _xmluiLaunchAction(self, action_id, data): | |
517 XMLUIPanel._actions += 1 | |
518 self.host.bridge.launchAction( | |
519 action_id, | |
520 data, | |
521 self.profile, | |
522 callback=self._launchActionCb, | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
523 errback=partial( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
524 self.command.errback, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
525 msg=_(u"can't launch XMLUI action: {}"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
526 exit_code=C.EXIT_BRIDGE_ERRBACK, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
527 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
528 ) |
2408 | 529 |
530 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
531 class XMLUIDialog(xmlui_base.XMLUIDialog): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
532 type = "dialog" |
2408 | 533 dialog_factory = WidgetFactory() |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
534 read_only = False |
2408 | 535 |
2410
40e6e779a253
jp (xmlui): XMLUIDialog.show has now a dummy argument, so workflow arguments can be put there
Goffi <goffi@goffi.org>
parents:
2408
diff
changeset
|
536 def show(self, dummy=None): |
2408 | 537 self.dlg.show() |
538 | |
539 def _xmluiClose(self): | |
540 pass | |
541 | |
542 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
543 create = partial(xmlui_base.create, class_map={ |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
544 xmlui_base.CLASS_PANEL: XMLUIPanel, |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
545 xmlui_base.CLASS_DIALOG: XMLUIDialog}) |