Mercurial > libervia-backend
annotate sat_frontends/jp/xmlui_manager.py @ 2629:346887f256f0
doc (README): removed version from README to avoid having to update it each time + removed mentions to future frontends
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Jul 2018 08:25:04 +0200 |
parents | 56f94936df1e |
children | bdb8276fd2da |
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__) |
23 from sat_frontends.tools import xmlui as xmlui_manager | |
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 | |
53 while not isinstance(root, xmlui_manager.XMLUIBase): | |
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 | |
176 class EmptyWidget(xmlui_manager.EmptyWidget, Widget): | |
177 def __init__(self, _xmlui_parent): | |
178 Widget.__init__(self) | |
179 | |
180 | |
181 class TextWidget(xmlui_manager.TextWidget, ValueWidget): | |
182 type = u"text" | |
183 | |
184 def show(self): | |
185 self.host.disp(self.value) | |
186 | |
187 | |
188 class LabelWidget(xmlui_manager.LabelWidget, ValueWidget): | |
189 type = u"label" | |
190 | |
191 @property | |
192 def for_name(self): | |
193 try: | |
194 return self._xmlui_for_name | |
195 except AttributeError: | |
196 return None | |
197 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
198 def show(self, no_lf=False, ansi=u""): |
2408 | 199 """show label |
200 | |
201 @param no_lf(bool): same as for [JP.disp] | |
202 @param ansi(unicode): ansi escape code to print before label | |
203 """ | |
204 self.disp(A.color(ansi, self.value), no_lf=no_lf) | |
205 | |
206 | |
207 class StringWidget(xmlui_manager.StringWidget, InputWidget): | |
208 type = u"string" | |
209 | |
210 def show(self): | |
211 if self.read_only: | |
212 self.disp(self.value) | |
213 else: | |
214 elems = [] | |
215 self.verboseName(elems) | |
216 if self.value: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
217 elems.append(_(u"(enter: {default})").format(default=self.value)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
218 elems.extend([C.A_HEADER, u"> "]) |
2408 | 219 value = raw_input(A.color(*elems)) |
220 if value: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
221 # TODO: empty value should be possible |
2408 | 222 # an escape key should be used for default instead of enter with empty value |
223 self.value = value | |
224 | |
225 | |
226 class JidInputWidget(xmlui_manager.JidInputWidget, StringWidget): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
227 type = u"jid_input" |
2408 | 228 |
229 | |
230 class TextBoxWidget(xmlui_manager.TextWidget, StringWidget): | |
231 type = u"textbox" | |
232 | |
233 | |
234 class ListWidget(xmlui_manager.ListWidget, OptionsWidget): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
235 type = u"list" |
2408 | 236 # TODO: handle flags, notably multi |
237 | |
238 def show(self): | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
239 if self.root.values_only: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
240 for value in self.values: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
241 self.disp(self.value) |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
242 return |
2408 | 243 if not self.options: |
244 return | |
245 | |
246 # list display | |
247 self.verboseName() | |
248 | |
249 for idx, (value, label) in enumerate(self.options): | |
250 elems = [] | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
251 if not self.root.read_only: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
252 elems.extend([C.A_SUBHEADER, unicode(idx), A.RESET, u": "]) |
2408 | 253 elems.append(label) |
254 self.verboseName(elems, value) | |
255 self.disp(A.color(*elems)) | |
256 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
257 if self.root.read_only: |
2408 | 258 return |
259 | |
260 if len(self.options) == 1: | |
261 # we have only one option, no need to ask | |
262 self.value = self.options[0][0] | |
263 return | |
264 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 # we ask use to choose an option |
2408 | 266 choice = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
267 limit_max = len(self.options) - 1 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
268 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
|
269 choice = raw_input( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
270 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
|
271 ) |
2408 | 272 try: |
273 choice = int(choice) | |
274 except ValueError: | |
275 choice = None | |
276 self.value = self.options[choice][0] | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
277 self.disp("") |
2408 | 278 |
279 | |
280 class BoolWidget(xmlui_manager.BoolWidget, InputWidget): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
281 type = u"bool" |
2408 | 282 |
283 def show(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 disp_true = A.color(A.FG_GREEN, u"TRUE") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
285 disp_false = A.color(A.FG_RED, u"FALSE") |
2408 | 286 if self.read_only: |
287 self.disp(disp_true if self.value else disp_false) | |
288 else: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
289 self.disp(A.color(C.A_HEADER, u"0: ", disp_false)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
290 self.disp(A.color(C.A_HEADER, u"1: ", disp_true)) |
2408 | 291 choice = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 while choice not in ("0", "1"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
293 elems = [C.A_HEADER, _(u"your choice (0,1): ")] |
2408 | 294 self.verboseName(elems) |
295 choice = raw_input(A.color(*elems)) | |
296 self.value = bool(int(choice)) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
297 self.disp("") |
2408 | 298 |
299 def _xmluiGetValue(self): | |
300 return C.boolConst(self.value) | |
301 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
302 |
2408 | 303 ## Containers ## |
304 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
305 |
2408 | 306 class Container(Base): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
307 category = u"container" |
2408 | 308 |
309 def __init__(self, xmlui_parent): | |
310 super(Container, self).__init__(xmlui_parent) | |
311 self.children = [] | |
312 | |
313 def __iter__(self): | |
314 return iter(self.children) | |
315 | |
316 def _xmluiAppend(self, widget): | |
317 self.children.append(widget) | |
318 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
319 def _xmluiRemove(self, widget): |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
320 self.children.remove(widget) |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
321 |
2408 | 322 def show(self): |
323 for child in self.children: | |
324 child.show() | |
325 | |
326 | |
327 class VerticalContainer(xmlui_manager.VerticalContainer, Container): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
328 type = u"vertical" |
2408 | 329 |
330 | |
331 class PairsContainer(xmlui_manager.PairsContainer, Container): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
332 type = u"pairs" |
2408 | 333 |
334 | |
335 class LabelContainer(xmlui_manager.PairsContainer, Container): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
336 type = u"label" |
2408 | 337 |
338 def show(self): | |
339 for child in self.children: | |
340 no_lf = False | |
341 # we check linked widget type | |
342 # 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
|
343 if child.type == u"label": |
2408 | 344 for_name = child.for_name |
345 if for_name is not None: | |
346 for_widget = self.root.widgets[for_name] | |
347 wid_type = for_widget.type | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
348 if self.root.values_only or wid_type in ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
349 "text", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
350 "string", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
351 "jid_input", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
352 ): |
2408 | 353 no_lf = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
354 elif wid_type == "bool" and for_widget.read_only: |
2408 | 355 no_lf = True |
356 child.show(no_lf=no_lf, ansi=A.FG_CYAN) | |
357 else: | |
358 child.show() | |
359 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
360 |
2408 | 361 ## Dialogs ## |
362 | |
363 | |
364 class Dialog(object): | |
365 def __init__(self, xmlui_parent): | |
366 self.xmlui_parent = xmlui_parent | |
367 self.host = self.xmlui_parent.host | |
368 | |
369 def disp(self, *args, **kwargs): | |
370 self.host.disp(*args, **kwargs) | |
371 | |
372 def show(self): | |
373 """display current dialog | |
374 | |
375 must be overriden by subclasses | |
376 """ | |
377 raise NotImplementedError(self.__class__) | |
378 | |
379 | |
380 class NoteDialog(xmlui_manager.NoteDialog, Dialog): | |
381 def show(self): | |
382 # TODO: handle title and level | |
383 self.disp(self.message) | |
384 | |
385 def __init__(self, _xmlui_parent, title, message, level): | |
386 Dialog.__init__(self, _xmlui_parent) | |
387 xmlui_manager.NoteDialog.__init__(self, _xmlui_parent) | |
388 self.title, self.message, self.level = title, message, level | |
389 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
390 |
2408 | 391 ## Factory ## |
392 | |
393 | |
394 class WidgetFactory(object): | |
395 def __getattr__(self, attr): | |
396 if attr.startswith("create"): | |
397 cls = globals()[attr[6:]] | |
398 return cls | |
399 | |
400 | |
401 class XMLUIPanel(xmlui_manager.XMLUIPanel): | |
402 widget_factory = WidgetFactory() | |
403 _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
|
404 read_only = False |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
405 values_only = False |
2408 | 406 workflow = None |
407 _submit_cb = None | |
408 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
409 def __init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
410 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
411 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
412 parsed_dom, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
413 title=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
414 flags=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
415 callback=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
416 ignore=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
417 whitelist=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
418 profile=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
419 ): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
420 xmlui_manager.XMLUIPanel.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
421 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
422 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
423 parsed_dom, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
424 title=title, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
425 flags=flags, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
426 ignore=ignore, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
427 whitelist=whitelist, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
428 profile=host.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
429 ) |
2408 | 430 self.submitted = False |
431 | |
432 @property | |
433 def command(self): | |
434 return self.host.command | |
435 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
436 def show(self, workflow=None, read_only=False, values_only=False): |
2408 | 437 """display the panel |
438 | |
439 @param workflow(list, None): command to execute if not None | |
440 put here for convenience, the main workflow is the class attribute | |
441 (because workflow can continue in subclasses) | |
442 command are a list of consts or lists: | |
443 - SUBMIT is the only constant so far, it submits the XMLUI | |
444 - 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
|
445 @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
|
446 @param values_only(bool): if True, only show select values (imply read_only) |
2408 | 447 """ |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
448 self.read_only = read_only |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
449 self.values_only = values_only |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
450 if self.values_only: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
451 self.read_only = True |
2408 | 452 if workflow: |
453 XMLUIPanel.workflow = workflow | |
454 if XMLUIPanel.workflow: | |
455 self.runWorkflow() | |
456 else: | |
457 self.main_cont.show() | |
458 | |
459 def runWorkflow(self): | |
460 """loop into workflow commands and execute commands | |
461 | |
462 SUBMIT will interrupt workflow (which will be continue on callback) | |
463 @param workflow(list): same as [show] | |
464 """ | |
465 workflow = XMLUIPanel.workflow | |
466 while True: | |
467 try: | |
468 cmd = workflow.pop(0) | |
469 except IndexError: | |
470 break | |
471 if cmd == SUBMIT: | |
472 self.onFormSubmitted() | |
473 self.submit_id = None # avoid double submit | |
474 return | |
475 elif isinstance(cmd, list): | |
476 name, value = cmd | |
2412
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
477 widget = self.widgets[name] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
478 if widget.type == "bool": |
2412
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
479 value = C.bool(value) |
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
480 widget.value = value |
2408 | 481 self.show() |
482 | |
483 def submitForm(self, callback=None): | |
484 XMLUIPanel._submit_cb = callback | |
485 self.onFormSubmitted() | |
486 | |
487 def onFormSubmitted(self, ignore=None): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
488 # self.submitted is a Q&D workaround to avoid |
2408 | 489 # double submit when a workflow is set |
490 if self.submitted: | |
491 return | |
492 self.submitted = True | |
493 super(XMLUIPanel, self).onFormSubmitted(ignore) | |
494 | |
495 def _xmluiClose(self): | |
496 pass | |
497 | |
498 def _launchActionCb(self, data): | |
499 XMLUIPanel._actions -= 1 | |
500 assert XMLUIPanel._actions >= 0 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
501 if u"xmlui" in data: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
502 xmlui_raw = data["xmlui"] |
2408 | 503 xmlui = xmlui_manager.create(self.host, xmlui_raw) |
504 xmlui.show() | |
505 if xmlui.submit_id: | |
506 xmlui.onFormSubmitted() | |
507 # TODO: handle data other than XMLUI | |
508 if not XMLUIPanel._actions: | |
509 if self._submit_cb is None: | |
510 self.host.quit() | |
511 else: | |
512 self._submit_cb() | |
513 | |
514 def _xmluiLaunchAction(self, action_id, data): | |
515 XMLUIPanel._actions += 1 | |
516 self.host.bridge.launchAction( | |
517 action_id, | |
518 data, | |
519 self.profile, | |
520 callback=self._launchActionCb, | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
521 errback=partial( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
522 self.command.errback, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
523 msg=_(u"can't launch XMLUI action: {}"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
524 exit_code=C.EXIT_BRIDGE_ERRBACK, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
525 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
526 ) |
2408 | 527 |
528 | |
529 class XMLUIDialog(xmlui_manager.XMLUIDialog): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
530 type = "dialog" |
2408 | 531 dialog_factory = WidgetFactory() |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
532 read_only = False |
2408 | 533 |
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
|
534 def show(self, dummy=None): |
2408 | 535 self.dlg.show() |
536 | |
537 def _xmluiClose(self): | |
538 pass | |
539 | |
540 | |
541 xmlui_manager.registerClass(xmlui_manager.CLASS_PANEL, XMLUIPanel) | |
542 xmlui_manager.registerClass(xmlui_manager.CLASS_DIALOG, XMLUIDialog) | |
543 create = xmlui_manager.create |