Mercurial > libervia-web
comparison src/browser/sat_browser/xmlui.py @ 720:bf0a7da558e8
browser side (xmlui): factory is instantiated on Panel init + main panel is passed as first argument to widgets
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 21 Aug 2015 15:37:21 +0200 |
parents | 9a9e2fcc6347 |
children | 102bab805ec1 |
comparison
equal
deleted
inserted
replaced
719:9a9e2fcc6347 | 720:bf0a7da558e8 |
---|---|
41 import nativedom | 41 import nativedom |
42 | 42 |
43 | 43 |
44 class EmptyWidget(xmlui.EmptyWidget, Label): | 44 class EmptyWidget(xmlui.EmptyWidget, Label): |
45 | 45 |
46 def __init__(self, _xmlui_parent): | 46 def __init__(self, xmlui_main, xmlui_parent): |
47 Label.__init__(self, '') | 47 Label.__init__(self, '') |
48 | 48 |
49 | 49 |
50 class TextWidget(xmlui.TextWidget, Label): | 50 class TextWidget(xmlui.TextWidget, Label): |
51 | 51 |
52 def __init__(self, _xmlui_parent, value): | 52 def __init__(self, xmlui_main, xmlui_parent, value): |
53 Label.__init__(self, value) | 53 Label.__init__(self, value) |
54 | 54 |
55 | 55 |
56 class LabelWidget(xmlui.LabelWidget, TextWidget): | 56 class LabelWidget(xmlui.LabelWidget, TextWidget): |
57 | 57 |
58 def __init__(self, _xmlui_parent, value): | 58 def __init__(self, xmlui_main, xmlui_parent, value): |
59 TextWidget.__init__(self, _xmlui_parent, value + ": ") | 59 TextWidget.__init__(self, xmlui_main, xmlui_parent, value + ": ") |
60 | 60 |
61 | 61 |
62 class JidWidget(xmlui.JidWidget, TextWidget): | 62 class JidWidget(xmlui.JidWidget, TextWidget): |
63 | 63 |
64 def __init__(self, _xmlui_parent, value): | 64 def __init__(self, xmlui_main, xmlui_parent, value): |
65 TextWidget.__init__(self, _xmlui_parent, value) | 65 TextWidget.__init__(self, xmlui_main, xmlui_parent, value) |
66 | 66 |
67 | 67 |
68 class DividerWidget(xmlui.DividerWidget, HTML): | 68 class DividerWidget(xmlui.DividerWidget, HTML): |
69 | 69 |
70 def __init__(self, _xmlui_parent, style='line'): | 70 def __init__(self, xmlui_main, xmlui_parent, style='line'): |
71 """Add a divider | 71 """Add a divider |
72 | 72 |
73 @param _xmlui_parent | 73 @param xmlui_parent |
74 @param style (unicode): one of: | 74 @param style (unicode): one of: |
75 - line: a simple line | 75 - line: a simple line |
76 - dot: a line of dots | 76 - dot: a line of dots |
77 - dash: a line of dashes | 77 - dash: a line of dashes |
78 - plain: a full thick line | 78 - plain: a full thick line |
82 self.addStyleName(style) | 82 self.addStyleName(style) |
83 | 83 |
84 | 84 |
85 class StringWidget(xmlui.StringWidget, TextBox): | 85 class StringWidget(xmlui.StringWidget, TextBox): |
86 | 86 |
87 def __init__(self, _xmlui_parent, value, read_only=False): | 87 def __init__(self, xmlui_main, xmlui_parent, value, read_only=False): |
88 TextBox.__init__(self) | 88 TextBox.__init__(self) |
89 self.setText(value) | 89 self.setText(value) |
90 self.setReadonly(read_only) | 90 self.setReadonly(read_only) |
91 | 91 |
92 def _xmluiSetValue(self, value): | 92 def _xmluiSetValue(self, value): |
99 self.addChangeListener(callback) | 99 self.addChangeListener(callback) |
100 | 100 |
101 | 101 |
102 class JidInputWidget(xmlui.JidInputWidget, StringWidget): | 102 class JidInputWidget(xmlui.JidInputWidget, StringWidget): |
103 | 103 |
104 def __init__(self, _xmlui_parent, value, read_only=False): | 104 def __init__(self, xmlui_main, xmlui_parent, value, read_only=False): |
105 StringWidget.__init__(self, _xmlui_parent, value, read_only) | 105 StringWidget.__init__(self, xmlui_main, xmlui_parent, value, read_only) |
106 | 106 |
107 | 107 |
108 class PasswordWidget(xmlui.PasswordWidget, PasswordTextBox): | 108 class PasswordWidget(xmlui.PasswordWidget, PasswordTextBox): |
109 | 109 |
110 def __init__(self, _xmlui_parent, value, read_only=False): | 110 def __init__(self, xmlui_main, xmlui_parent, value, read_only=False): |
111 PasswordTextBox.__init__(self) | 111 PasswordTextBox.__init__(self) |
112 self.setText(value) | 112 self.setText(value) |
113 self.setReadonly(read_only) | 113 self.setReadonly(read_only) |
114 | 114 |
115 def _xmluiSetValue(self, value): | 115 def _xmluiSetValue(self, value): |
122 self.addChangeListener(callback) | 122 self.addChangeListener(callback) |
123 | 123 |
124 | 124 |
125 class TextBoxWidget(xmlui.TextBoxWidget, TextArea): | 125 class TextBoxWidget(xmlui.TextBoxWidget, TextArea): |
126 | 126 |
127 def __init__(self, _xmlui_parent, value, read_only=False): | 127 def __init__(self, xmlui_main, xmlui_parent, value, read_only=False): |
128 TextArea.__init__(self) | 128 TextArea.__init__(self) |
129 self.setText(value) | 129 self.setText(value) |
130 self.setReadonly(read_only) | 130 self.setReadonly(read_only) |
131 | 131 |
132 def _xmluiSetValue(self, value): | 132 def _xmluiSetValue(self, value): |
139 self.addChangeListener(callback) | 139 self.addChangeListener(callback) |
140 | 140 |
141 | 141 |
142 class BoolWidget(xmlui.BoolWidget, CheckBox): | 142 class BoolWidget(xmlui.BoolWidget, CheckBox): |
143 | 143 |
144 def __init__(self, _xmlui_parent, state, read_only=False): | 144 def __init__(self, xmlui_main, xmlui_parent, state, read_only=False): |
145 CheckBox.__init__(self) | 145 CheckBox.__init__(self) |
146 self.setChecked(state) | 146 self.setChecked(state) |
147 self.setReadonly(read_only) | 147 self.setReadonly(read_only) |
148 | 148 |
149 def _xmluiSetValue(self, value): | 149 def _xmluiSetValue(self, value): |
156 self.addClickListener(callback) | 156 self.addClickListener(callback) |
157 | 157 |
158 | 158 |
159 class IntWidget(xmlui.IntWidget, TextBox): | 159 class IntWidget(xmlui.IntWidget, TextBox): |
160 | 160 |
161 def __init__(self, _xmlui_parent, value, read_only=False): | 161 def __init__(self, xmlui_main, xmlui_parent, value, read_only=False): |
162 TextBox.__init__(self) | 162 TextBox.__init__(self) |
163 self.setText(value) | 163 self.setText(value) |
164 self.setReadonly(read_only) | 164 self.setReadonly(read_only) |
165 | 165 |
166 def _xmluiSetValue(self, value): | 166 def _xmluiSetValue(self, value): |
173 self.addChangeListener(callback) | 173 self.addChangeListener(callback) |
174 | 174 |
175 | 175 |
176 class ButtonWidget(xmlui.ButtonWidget, Button): | 176 class ButtonWidget(xmlui.ButtonWidget, Button): |
177 | 177 |
178 def __init__(self, _xmlui_parent, value, click_callback): | 178 def __init__(self, xmlui_main, xmlui_parent, value, click_callback): |
179 Button.__init__(self, value, click_callback) | 179 Button.__init__(self, value, click_callback) |
180 | 180 |
181 def _xmluiOnClick(self, callback): | 181 def _xmluiOnClick(self, callback): |
182 self.addClickListener(callback) | 182 self.addClickListener(callback) |
183 | 183 |
184 | 184 |
185 class ListWidget(xmlui.ListWidget, ListBox): | 185 class ListWidget(xmlui.ListWidget, ListBox): |
186 | 186 |
187 def __init__(self, _xmlui_parent, options, selected, flags): | 187 def __init__(self, xmlui_main, xmlui_parent, options, selected, flags): |
188 ListBox.__init__(self) | 188 ListBox.__init__(self) |
189 multi_selection = 'single' not in flags | 189 multi_selection = 'single' not in flags |
190 self.setMultipleSelect(multi_selection) | 190 self.setMultipleSelect(multi_selection) |
191 if multi_selection: | 191 if multi_selection: |
192 self.setVisibleItemCount(5) | 192 self.setVisibleItemCount(5) |
234 self.append(widget) | 234 self.append(widget) |
235 | 235 |
236 | 236 |
237 class AdvancedListContainer(xmlui.AdvancedListContainer, Grid): | 237 class AdvancedListContainer(xmlui.AdvancedListContainer, Grid): |
238 | 238 |
239 def __init__(self, _xmlui_parent, columns, selectable='no'): | 239 def __init__(self, xmlui_main, xmlui_parent, columns, selectable='no'): |
240 Grid.__init__(self, 0, columns) | 240 Grid.__init__(self, 0, columns) |
241 self.columns = columns | 241 self.columns = columns |
242 self.row = -1 | 242 self.row = -1 |
243 self.col = 0 | 243 self.col = 0 |
244 self._xmlui_rows_idx = [] | 244 self._xmlui_rows_idx = [] |
280 self._xmlui_select_cb = callback | 280 self._xmlui_select_cb = callback |
281 | 281 |
282 | 282 |
283 class PairsContainer(xmlui.PairsContainer, Grid): | 283 class PairsContainer(xmlui.PairsContainer, Grid): |
284 | 284 |
285 def __init__(self, _xmlui_parent): | 285 def __init__(self, xmlui_main, xmlui_parent): |
286 Grid.__init__(self, 0, 0) | 286 Grid.__init__(self, 0, 0) |
287 self.row = 0 | 287 self.row = 0 |
288 self.col = 0 | 288 self.col = 0 |
289 | 289 |
290 def _xmluiAppend(self, widget): | 290 def _xmluiAppend(self, widget): |
297 self.col = 0 | 297 self.col = 0 |
298 | 298 |
299 | 299 |
300 class TabsContainer(LiberviaContainer, xmlui.TabsContainer, TabPanel): | 300 class TabsContainer(LiberviaContainer, xmlui.TabsContainer, TabPanel): |
301 | 301 |
302 def __init__(self, _xmlui_parent): | 302 def __init__(self, xmlui_main, xmlui_parent): |
303 TabPanel.__init__(self) | 303 TabPanel.__init__(self) |
304 self.setStyleName('liberviaTabPanel') | 304 self.setStyleName('liberviaTabPanel') |
305 | 305 |
306 def _xmluiAddTab(self, label): | 306 def _xmluiAddTab(self, label): |
307 tab_panel = VerticalContainer(self) | 307 tab_panel = VerticalContainer(self) |
312 | 312 |
313 | 313 |
314 class VerticalContainer(LiberviaContainer, xmlui.VerticalContainer, VerticalPanel): | 314 class VerticalContainer(LiberviaContainer, xmlui.VerticalContainer, VerticalPanel): |
315 __bases__ = (LiberviaContainer, xmlui.VerticalContainer, VerticalPanel) | 315 __bases__ = (LiberviaContainer, xmlui.VerticalContainer, VerticalPanel) |
316 | 316 |
317 def __init__(self, _xmlui_parent): | 317 def __init__(self, xmlui_main, xmlui_parent): |
318 VerticalPanel.__init__(self) | 318 VerticalPanel.__init__(self) |
319 | 319 |
320 | 320 |
321 ## Dialogs ## | 321 ## Dialogs ## |
322 | 322 |
330 pass | 330 pass |
331 | 331 |
332 | 332 |
333 class MessageDialog(Dlg, xmlui.MessageDialog, dialog.InfoDialog): | 333 class MessageDialog(Dlg, xmlui.MessageDialog, dialog.InfoDialog): |
334 | 334 |
335 def __init__(self, _xmlui_parent, title, message, level): | 335 def __init__(self, xmlui_main, xmlui_parent, title, message, level): |
336 #TODO: level is not managed | 336 #TODO: level is not managed |
337 title = html_tools.html_sanitize(title) | 337 title = html_tools.html_sanitize(title) |
338 message = strings.addURLToText(html_tools.XHTML2Text(message)) | 338 message = strings.addURLToText(html_tools.XHTML2Text(message)) |
339 Dlg.__init__(self) | 339 Dlg.__init__(self) |
340 xmlui.MessageDialog.__init__(self, _xmlui_parent) | 340 xmlui.MessageDialog.__init__(self, xmlui_main, xmlui_parent) |
341 dialog.InfoDialog.__init__(self, title, message, self._xmluiValidated()) | 341 dialog.InfoDialog.__init__(self, title, message, self._xmluiValidated()) |
342 | 342 |
343 | 343 |
344 class NoteDialog(xmlui.NoteDialog, MessageDialog): | 344 class NoteDialog(xmlui.NoteDialog, MessageDialog): |
345 # TODO: separate NoteDialog | 345 # TODO: separate NoteDialog |
346 | 346 |
347 def __init__(self, _xmlui_parent, title, message, level): | 347 def __init__(self, xmlui_main, xmlui_parent, title, message, level): |
348 xmlui.NoteDialog.__init__(self, _xmlui_parent) | 348 xmlui.NoteDialog.__init__(self, xmlui_main, xmlui_parent) |
349 MessageDialog.__init__(self, _xmlui_parent, title, message, level) | 349 MessageDialog.__init__(self, xmlui_main, xmlui_parent, title, message, level) |
350 | 350 |
351 | 351 |
352 class ConfirmDialog(xmlui.ConfirmDialog, Dlg, dialog.ConfirmDialog): | 352 class ConfirmDialog(xmlui.ConfirmDialog, Dlg, dialog.ConfirmDialog): |
353 | 353 |
354 def __init__(self, _xmlui_parent, title, message, level): | 354 def __init__(self, xmlui_main, xmlui_parent, title, message, level): |
355 #TODO: level is not managed | 355 #TODO: level is not managed |
356 title = html_tools.html_sanitize(title) | 356 title = html_tools.html_sanitize(title) |
357 message = strings.addURLToText(html_tools.XHTML2Text(message)) | 357 message = strings.addURLToText(html_tools.XHTML2Text(message)) |
358 xmlui.ConfirmDialog.__init__(self, _xmlui_parent) | 358 xmlui.ConfirmDialog.__init__(self, xmlui_main, xmlui_parent) |
359 Dlg.__init__(self) | 359 Dlg.__init__(self) |
360 dialog.ConfirmDialog.__init__(self, self.answered, message, title) | 360 dialog.ConfirmDialog.__init__(self, self.answered, message, title) |
361 | 361 |
362 def answered(self, validated): | 362 def answered(self, validated): |
363 if validated: | 363 if validated: |
367 | 367 |
368 | 368 |
369 class FileDialog(xmlui.FileDialog, Dlg): | 369 class FileDialog(xmlui.FileDialog, Dlg): |
370 #TODO: | 370 #TODO: |
371 | 371 |
372 def __init__(self, _xmlui_parent, title, message, level, filetype): | 372 def __init__(self, xmlui_main, xmlui_parent, title, message, level, filetype): |
373 raise NotImplementedError("FileDialog is not implemented in Libervia yet") | 373 raise NotImplementedError("FileDialog is not implemented in Libervia yet") |
374 | 374 |
375 | 375 |
376 class GenericFactory(object): | 376 class GenericFactory(object): |
377 # XXX: __getattr__ doens't work here with pyjamas for an unknown reason | 377 # XXX: __getattr__ doens't work here with pyjamas for an unknown reason |
378 # so an introspection workaround is used | 378 # so an introspection workaround is used |
379 | 379 |
380 def __init__(self): | 380 def __init__(self, xmlui_main): |
381 | 381 self.xmlui_main = xmlui_main |
382 for name, cls in globals().items(): | 382 for name, cls in globals().items(): |
383 if name.endswith("Widget") or name.endswith("Container") or name.endswith("Dialog"): | 383 if name.endswith("Widget") or name.endswith("Container") or name.endswith("Dialog"): |
384 log.info("registering: %s" % name) | 384 log.info("registering: %s" % name) |
385 def createCreater(cls): | 385 def createCreater(cls): |
386 return lambda *args, **kwargs: self._genericCreate(cls, *args, **kwargs) | 386 return lambda *args, **kwargs: self._genericCreate(cls, *args, **kwargs) |
387 setattr(self, "create%s" % name, createCreater(cls)) | 387 setattr(self, "create%s" % name, createCreater(cls)) |
388 | 388 |
389 def _genericCreate(self, cls, *args, **kwargs): | 389 def _genericCreate(self, cls, *args, **kwargs): |
390 instance = cls(*args, **kwargs) | 390 instance = cls(self.xmlui_main, *args, **kwargs) |
391 return instance | 391 return instance |
392 | 392 |
393 # def __getattr__(self, attr): | 393 # def __getattr__(self, attr): |
394 # if attr.startswith("create"): | 394 # if attr.startswith("create"): |
395 # cls = globals()[attr[6:]] | 395 # cls = globals()[attr[6:]] |
399 | 399 |
400 class WidgetFactory(GenericFactory): | 400 class WidgetFactory(GenericFactory): |
401 | 401 |
402 def _genericCreate(self, cls, *args, **kwargs): | 402 def _genericCreate(self, cls, *args, **kwargs): |
403 instance = GenericFactory._genericCreate(self, cls, *args, **kwargs) | 403 instance = GenericFactory._genericCreate(self, cls, *args, **kwargs) |
404 instance._xmlui_main = self._xmlui_main | |
405 return instance | 404 return instance |
406 | 405 |
407 class LiberviaXMLUIBase(object): | 406 class LiberviaXMLUIBase(object): |
408 | 407 |
409 def _xmluiLaunchAction(self, action_id, data): | 408 def _xmluiLaunchAction(self, action_id, data): |
410 self.host.launchAction(action_id, data) | 409 self.host.launchAction(action_id, data) |
411 | 410 |
412 | 411 |
413 class XMLUIPanel(LiberviaXMLUIBase, xmlui.XMLUIPanel, VerticalPanel): | 412 class XMLUIPanel(LiberviaXMLUIBase, xmlui.XMLUIPanel, VerticalPanel): |
414 widget_factory = WidgetFactory() | |
415 | 413 |
416 def __init__(self, host, parsed_xml, title=None, flags=None, callback=None, profile=C.PROF_KEY_NONE): | 414 def __init__(self, host, parsed_xml, title=None, flags=None, callback=None, profile=C.PROF_KEY_NONE): |
417 self.widget_factory._xmlui_main = self | 415 self.widget_factory = WidgetFactory(self) |
416 self.host = host | |
418 VerticalPanel.__init__(self) | 417 VerticalPanel.__init__(self) |
419 self.setSize('100%', '100%') | 418 self.setSize('100%', '100%') |
420 xmlui.XMLUIPanel.__init__(self, host, parsed_xml, title, flags, callback, profile) | 419 xmlui.XMLUIPanel.__init__(self, host, parsed_xml, title, flags, callback, profile) |
421 | 420 |
422 def setCloseCb(self, close_cb): | 421 def setCloseCb(self, close_cb): |
458 | 457 |
459 class XMLUIDialog(LiberviaXMLUIBase, xmlui.XMLUIDialog): | 458 class XMLUIDialog(LiberviaXMLUIBase, xmlui.XMLUIDialog): |
460 dialog_factory = GenericFactory() | 459 dialog_factory = GenericFactory() |
461 | 460 |
462 def __init__(self, host, parsed_dom, title = None, flags = None, callback=None, profile=C.PROF_KEY_NONE): | 461 def __init__(self, host, parsed_dom, title = None, flags = None, callback=None, profile=C.PROF_KEY_NONE): |
462 self.dialog_factory = GenericFactory(self) | |
463 xmlui.XMLUIDialog.__init__(self, host, parsed_dom, title, flags, callback, profile) | 463 xmlui.XMLUIDialog.__init__(self, host, parsed_dom, title, flags, callback, profile) |
464 self.host = host | |
464 | 465 |
465 xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel) | 466 xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel) |
466 xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog) | 467 xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog) |
467 | 468 |
468 def create(*args, **kwargs): | 469 def create(*args, **kwargs): |