comparison cagou/core/xmlui.py @ 491:203755bbe0fe

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:32 +0200
parents 3c9ba4a694ef
children
comparison
equal deleted inserted replaced
490:962d17c4078c 491:203755bbe0fe
45 45
46 def __init__(self): 46 def __init__(self):
47 self._xmlui_onchange_cb = None 47 self._xmlui_onchange_cb = None
48 self._got_focus = False 48 self._got_focus = False
49 49
50 def _xmluiOnChange(self, callback): 50 def _xmlui_on_change(self, callback):
51 self._xmlui_onchange_cb = callback 51 self._xmlui_onchange_cb = callback
52 52
53 def on_focus(self, instance, focus): 53 def on_focus(self, instance, focus):
54 # we need to wait for first focus, else initial value 54 # we need to wait for first focus, else initial value
55 # will trigger a on_text 55 # will trigger a on_text
86 def __init__(self, xmlui_parent, value, read_only=False): 86 def __init__(self, xmlui_parent, value, read_only=False):
87 TextInput.__init__(self, text=value) 87 TextInput.__init__(self, text=value)
88 TextInputOnChange.__init__(self) 88 TextInputOnChange.__init__(self)
89 self.readonly = read_only 89 self.readonly = read_only
90 90
91 def _xmluiSetValue(self, value): 91 def _xmlui_set_value(self, value):
92 self.text = value 92 self.text = value
93 93
94 def _xmluiGetValue(self): 94 def _xmlui_get_value(self):
95 return self.text 95 return self.text
96 96
97 97
98 class TextBoxWidget(xmlui.TextBoxWidget, StringWidget): 98 class TextBoxWidget(xmlui.TextBoxWidget, StringWidget):
99 pass 99 pass
108 def __init__(self, _xmlui_parent, value, click_callback): 108 def __init__(self, _xmlui_parent, value, click_callback):
109 Button.__init__(self) 109 Button.__init__(self)
110 self.text = value 110 self.text = value
111 self.callback = click_callback 111 self.callback = click_callback
112 112
113 def _xmluiOnClick(self, callback): 113 def _xmlui_on_click(self, callback):
114 self.callback = callback 114 self.callback = callback
115 115
116 def on_release(self): 116 def on_release(self):
117 self.callback(self) 117 self.callback(self)
118 118
153 def __init__(self, _xmlui_parent, options, selected, flags): 153 def __init__(self, _xmlui_parent, options, selected, flags):
154 ScrollView.__init__(self) 154 ScrollView.__init__(self)
155 self.multi = 'single' not in flags 155 self.multi = 'single' not in flags
156 self._values = [] 156 self._values = []
157 for option in options: 157 for option in options:
158 self.addValue(option) 158 self.add_value(option)
159 self._xmluiSelectValues(selected) 159 self._xmlui_select_values(selected)
160 self._on_change = None 160 self._on_change = None
161 161
162 @property 162 @property
163 def items(self): 163 def items(self):
164 return self.layout.children 164 return self.layout.children
165 165
166 def select(self, item): 166 def select(self, item):
167 if not self.multi: 167 if not self.multi:
168 self._xmluiSelectValues([item.value]) 168 self._xmlui_select_values([item.value])
169 if self._on_change is not None: 169 if self._on_change is not None:
170 self._on_change(self) 170 self._on_change(self)
171 171
172 def addValue(self, option, selected=False): 172 def add_value(self, option, selected=False):
173 """add a value in the list 173 """add a value in the list
174 174
175 @param option(tuple): value, label in a tuple 175 @param option(tuple): value, label in a tuple
176 """ 176 """
177 self._values.append(option) 177 self._values.append(option)
178 item = ListWidgetItem() 178 item = ListWidgetItem()
179 item.value, item.text = option 179 item.value, item.text = option
180 item.selected = selected 180 item.selected = selected
181 self.layout.add_widget(item) 181 self.layout.add_widget(item)
182 182
183 def _xmluiSelectValue(self, value): 183 def _xmlui_select_value(self, value):
184 self._xmluiSelectValues([value]) 184 self._xmlui_select_values([value])
185 185
186 def _xmluiSelectValues(self, values): 186 def _xmlui_select_values(self, values):
187 for item in self.items: 187 for item in self.items:
188 item.selected = item.value in values 188 item.selected = item.value in values
189 if item.selected and not self.multi: 189 if item.selected and not self.multi:
190 self.text = item.text 190 self.text = item.text
191 191
192 def _xmluiGetSelectedValues(self): 192 def _xmlui_get_selected_values(self):
193 return [item.value for item in self.items if item.selected] 193 return [item.value for item in self.items if item.selected]
194 194
195 def _xmluiAddValues(self, values, select=True): 195 def _xmlui_add_values(self, values, select=True):
196 values = set(values).difference([c.value for c in self.items]) 196 values = set(values).difference([c.value for c in self.items])
197 for v in values: 197 for v in values:
198 self.addValue(v, select) 198 self.add_value(v, select)
199 199
200 def _xmluiOnChange(self, callback): 200 def _xmlui_on_change(self, callback):
201 self._on_change = callback 201 self._on_change = callback
202 202
203 203
204 class JidsListWidget(ListWidget): 204 class JidsListWidget(ListWidget):
205 # TODO: real list dedicated to jids 205 # TODO: real list dedicated to jids
213 def __init__(self, _xmlui_parent, value, read_only=False): 213 def __init__(self, _xmlui_parent, value, read_only=False):
214 TextInput.__init__(self, password=True, multiline=False, 214 TextInput.__init__(self, password=True, multiline=False,
215 text=value, readonly=read_only, size=(100,25), size_hint=(1,None)) 215 text=value, readonly=read_only, size=(100,25), size_hint=(1,None))
216 TextInputOnChange.__init__(self) 216 TextInputOnChange.__init__(self)
217 217
218 def _xmluiSetValue(self, value): 218 def _xmlui_set_value(self, value):
219 self.text = value 219 self.text = value
220 220
221 def _xmluiGetValue(self): 221 def _xmlui_get_value(self):
222 return self.text 222 return self.text
223 223
224 224
225 class BoolWidget(xmlui.BoolWidget, Switch): 225 class BoolWidget(xmlui.BoolWidget, Switch):
226 226
227 def __init__(self, _xmlui_parent, state, read_only=False): 227 def __init__(self, _xmlui_parent, state, read_only=False):
228 Switch.__init__(self, active=state) 228 Switch.__init__(self, active=state)
229 if read_only: 229 if read_only:
230 self.disabled = True 230 self.disabled = True
231 231
232 def _xmluiSetValue(self, value): 232 def _xmlui_set_value(self, value):
233 self.active = value 233 self.active = value
234 234
235 def _xmluiGetValue(self): 235 def _xmlui_get_value(self):
236 return C.BOOL_TRUE if self.active else C.BOOL_FALSE 236 return C.BOOL_TRUE if self.active else C.BOOL_FALSE
237 237
238 def _xmluiOnChange(self, callback): 238 def _xmlui_on_change(self, callback):
239 self.bind(active=lambda instance, value: callback(instance)) 239 self.bind(active=lambda instance, value: callback(instance))
240 240
241 241
242 class IntWidget(xmlui.IntWidget, TextInput, TextInputOnChange): 242 class IntWidget(xmlui.IntWidget, TextInput, TextInputOnChange):
243 243
245 TextInput.__init__(self, text=value, input_filter='int', multiline=False) 245 TextInput.__init__(self, text=value, input_filter='int', multiline=False)
246 TextInputOnChange.__init__(self) 246 TextInputOnChange.__init__(self)
247 if read_only: 247 if read_only:
248 self.disabled = True 248 self.disabled = True
249 249
250 def _xmluiSetValue(self, value): 250 def _xmlui_set_value(self, value):
251 self.text = value 251 self.text = value
252 252
253 def _xmluiGetValue(self): 253 def _xmlui_get_value(self):
254 return self.text 254 return self.text
255 255
256 256
257 ## Containers ## 257 ## Containers ##
258 258
261 261
262 def __init__(self, xmlui_parent): 262 def __init__(self, xmlui_parent):
263 self.xmlui_parent = xmlui_parent 263 self.xmlui_parent = xmlui_parent
264 BoxLayout.__init__(self) 264 BoxLayout.__init__(self)
265 265
266 def _xmluiAppend(self, widget): 266 def _xmlui_append(self, widget):
267 self.add_widget(widget) 267 self.add_widget(widget)
268 268
269 269
270 class PairsContainer(xmlui.PairsContainer, GridLayout): 270 class PairsContainer(xmlui.PairsContainer, GridLayout):
271 271
272 def __init__(self, xmlui_parent): 272 def __init__(self, xmlui_parent):
273 self.xmlui_parent = xmlui_parent 273 self.xmlui_parent = xmlui_parent
274 GridLayout.__init__(self) 274 GridLayout.__init__(self)
275 275
276 def _xmluiAppend(self, widget): 276 def _xmlui_append(self, widget):
277 self.add_widget(widget) 277 self.add_widget(widget)
278 278
279 279
280 class LabelContainer(PairsContainer, xmlui.LabelContainer): 280 class LabelContainer(PairsContainer, xmlui.LabelContainer):
281 pass 281 pass
282 282
283 283
284 class TabsPanelContainer(TabbedPanelItem): 284 class TabsPanelContainer(TabbedPanelItem):
285 layout = properties.ObjectProperty(None) 285 layout = properties.ObjectProperty(None)
286 286
287 def _xmluiAppend(self, widget): 287 def _xmlui_append(self, widget):
288 self.layout.add_widget(widget) 288 self.layout.add_widget(widget)
289 289
290 290
291 class TabsContainer(xmlui.TabsContainer, TabbedPanel): 291 class TabsContainer(xmlui.TabsContainer, TabbedPanel):
292 292
293 def __init__(self, xmlui_parent): 293 def __init__(self, xmlui_parent):
294 self.xmlui_parent = xmlui_parent 294 self.xmlui_parent = xmlui_parent
295 TabbedPanel.__init__(self, do_default_tab=False) 295 TabbedPanel.__init__(self, do_default_tab=False)
296 296
297 def _xmluiAddTab(self, label, selected): 297 def _xmlui_add_tab(self, label, selected):
298 tab = TabsPanelContainer(text=label) 298 tab = TabsPanelContainer(text=label)
299 self.add_widget(tab) 299 self.add_widget(tab)
300 return tab 300 return tab
301 301
302 302
317 parent = parent.parent 317 parent = parent.parent
318 if parent is None: 318 if parent is None:
319 log.error("Can't find parent AdvancedListContainer") 319 log.error("Can't find parent AdvancedListContainer")
320 else: 320 else:
321 if parent.selectable: 321 if parent.selectable:
322 self.selected = parent._xmluiToggleSelected(self) 322 self.selected = parent._xmlui_toggle_selected(self)
323 323
324 return super(AdvancedListRow, self).on_touch_down(touch) 324 return super(AdvancedListRow, self).on_touch_down(touch)
325 325
326 326
327 class AdvancedListContainer(xmlui.AdvancedListContainer, BoxLayout): 327 class AdvancedListContainer(xmlui.AdvancedListContainer, BoxLayout):
333 self.selectable = selectable != 'no' 333 self.selectable = selectable != 'no'
334 self._current_row = None 334 self._current_row = None
335 self._selected = [] 335 self._selected = []
336 self._xmlui_select_cb = None 336 self._xmlui_select_cb = None
337 337
338 def _xmluiToggleSelected(self, row): 338 def _xmlui_toggle_selected(self, row):
339 """inverse selection status of an AdvancedListRow 339 """inverse selection status of an AdvancedListRow
340 340
341 @param row(AdvancedListRow): row to (un)select 341 @param row(AdvancedListRow): row to (un)select
342 @return (bool): True if row is selected 342 @return (bool): True if row is selected
343 """ 343 """
349 self._xmlui_select_cb(self) 349 self._xmlui_select_cb(self)
350 return True 350 return True
351 else: 351 else:
352 return False 352 return False
353 353
354 def _xmluiAppend(self, widget): 354 def _xmlui_append(self, widget):
355 if self._current_row is None: 355 if self._current_row is None:
356 log.error("No row set, ignoring append") 356 log.error("No row set, ignoring append")
357 return 357 return
358 self._current_row.add_widget(widget) 358 self._current_row.add_widget(widget)
359 359
360 def _xmluiAddRow(self, idx): 360 def _xmlui_add_row(self, idx):
361 self._current_row = AdvancedListRow() 361 self._current_row = AdvancedListRow()
362 self._current_row.cols = self._columns 362 self._current_row.cols = self._columns
363 self._current_row.index = idx 363 self._current_row.index = idx
364 self.add_widget(self._current_row) 364 self.add_widget(self._current_row)
365 365
366 def _xmluiGetSelectedWidgets(self): 366 def _xmlui_get_selected_widgets(self):
367 return self._selected 367 return self._selected
368 368
369 def _xmluiGetSelectedIndex(self): 369 def _xmlui_get_selected_index(self):
370 if not self._selected: 370 if not self._selected:
371 return None 371 return None
372 return self._selected[0].index 372 return self._selected[0].index
373 373
374 def _xmluiOnSelect(self, callback): 374 def _xmlui_on_select(self, callback):
375 """ Call callback with widget as only argument """ 375 """ Call callback with widget as only argument """
376 self._xmlui_select_cb = callback 376 self._xmlui_select_cb = callback
377 377
378 378
379 ## Dialogs ## 379 ## Dialogs ##
383 383
384 def __init__(self, _xmlui_parent, title, message, level): 384 def __init__(self, _xmlui_parent, title, message, level):
385 xmlui.NoteDialog.__init__(self, _xmlui_parent) 385 xmlui.NoteDialog.__init__(self, _xmlui_parent)
386 self.title, self.message, self.level = title, message, level 386 self.title, self.message, self.level = title, message, level
387 387
388 def _xmluiShow(self): 388 def _xmlui_show(self):
389 G.host.addNote(self.title, self.message, self.level) 389 G.host.add_note(self.title, self.message, self.level)
390 390
391 391
392 class MessageDialog(xmlui.MessageDialog, dialog.MessageDialog): 392 class MessageDialog(xmlui.MessageDialog, dialog.MessageDialog):
393 393
394 def __init__(self, _xmlui_parent, title, message, level): 394 def __init__(self, _xmlui_parent, title, message, level):
398 level=level, 398 level=level,
399 close_cb = self.close_cb) 399 close_cb = self.close_cb)
400 xmlui.MessageDialog.__init__(self, _xmlui_parent) 400 xmlui.MessageDialog.__init__(self, _xmlui_parent)
401 401
402 def close_cb(self): 402 def close_cb(self):
403 self._xmluiClose() 403 self._xmlui_close()
404 404
405 def _xmluiShow(self): 405 def _xmlui_show(self):
406 G.host.addNotifUI(self) 406 G.host.add_notif_ui(self)
407 407
408 def _xmluiClose(self, reason=None): 408 def _xmlui_close(self, reason=None):
409 G.host.closeUI() 409 G.host.close_ui()
410 410
411 def show(self, *args, **kwargs): 411 def show(self, *args, **kwargs):
412 G.host.showUI(self) 412 G.host.show_ui(self)
413 413
414 414
415 class ConfirmDialog(xmlui.ConfirmDialog, dialog.ConfirmDialog): 415 class ConfirmDialog(xmlui.ConfirmDialog, dialog.ConfirmDialog):
416 416
417 def __init__(self, _xmlui_parent, title, message, level, buttons_set): 417 def __init__(self, _xmlui_parent, title, message, level, buttons_set):
421 self.message=message 421 self.message=message
422 self.no_cb = self.no_cb 422 self.no_cb = self.no_cb
423 self.yes_cb = self.yes_cb 423 self.yes_cb = self.yes_cb
424 424
425 def no_cb(self): 425 def no_cb(self):
426 G.host.closeUI() 426 G.host.close_ui()
427 self._xmluiCancelled() 427 self._xmlui_cancelled()
428 428
429 def yes_cb(self): 429 def yes_cb(self):
430 G.host.closeUI() 430 G.host.close_ui()
431 self._xmluiValidated() 431 self._xmlui_validated()
432 432
433 def _xmluiShow(self): 433 def _xmlui_show(self):
434 G.host.addNotifUI(self) 434 G.host.add_notif_ui(self)
435 435
436 def _xmluiClose(self, reason=None): 436 def _xmlui_close(self, reason=None):
437 G.host.closeUI() 437 G.host.close_ui()
438 438
439 def show(self, *args, **kwargs): 439 def show(self, *args, **kwargs):
440 assert kwargs["force"] 440 assert kwargs["force"]
441 G.host.showUI(self) 441 G.host.show_ui(self)
442 442
443 443
444 class FileDialog(xmlui.FileDialog, BoxLayout): 444 class FileDialog(xmlui.FileDialog, BoxLayout):
445 message = properties.ObjectProperty() 445 message = properties.ObjectProperty()
446 446
449 BoxLayout.__init__(self) 449 BoxLayout.__init__(self)
450 self.message.text = message 450 self.message.text = message
451 if filetype == C.XMLUI_DATA_FILETYPE_DIR: 451 if filetype == C.XMLUI_DATA_FILETYPE_DIR:
452 self.file_chooser.dirselect = True 452 self.file_chooser.dirselect = True
453 453
454 def _xmluiShow(self): 454 def _xmlui_show(self):
455 G.host.addNotifUI(self) 455 G.host.add_notif_ui(self)
456 456
457 def _xmluiClose(self, reason=None): 457 def _xmlui_close(self, reason=None):
458 # FIXME: notif UI is not removed if dialog is not shown yet 458 # FIXME: notif UI is not removed if dialog is not shown yet
459 G.host.closeUI() 459 G.host.close_ui()
460 460
461 def onSelect(self, path): 461 def on_select(self, path):
462 try: 462 try:
463 path = path[0] 463 path = path[0]
464 except IndexError: 464 except IndexError:
465 path = None 465 path = None
466 if not path: 466 if not path:
467 self._xmluiCancelled() 467 self._xmlui_cancelled()
468 else: 468 else:
469 self._xmluiValidated({'path': path}) 469 self._xmlui_validated({'path': path})
470 470
471 def show(self, *args, **kwargs): 471 def show(self, *args, **kwargs):
472 assert kwargs["force"] 472 assert kwargs["force"]
473 G.host.showUI(self) 473 G.host.show_ui(self)
474 474
475 475
476 ## Factory ## 476 ## Factory ##
477 477
478 478
529 flags=flags, 529 flags=flags,
530 callback=callback, 530 callback=callback,
531 ignore=ignore, 531 ignore=ignore,
532 whitelist=whitelist, 532 whitelist=whitelist,
533 profile=profile) 533 profile=profile)
534 self.bind(height=self.onHeight) 534 self.bind(height=self.on_height)
535 535
536 def on_touch_down(self, touch, after=False): 536 def on_touch_down(self, touch, after=False):
537 if self._skip_scroll_events: 537 if self._skip_scroll_events:
538 return super(ScrollView, self).on_touch_down(touch) 538 return super(ScrollView, self).on_touch_down(touch)
539 else: 539 else:
549 if self._skip_scroll_events: 549 if self._skip_scroll_events:
550 return super(ScrollView, self).on_touch_move(touch) 550 return super(ScrollView, self).on_touch_move(touch)
551 else: 551 else:
552 return super(XMLUIPanel, self).on_touch_move(touch) 552 return super(XMLUIPanel, self).on_touch_move(touch)
553 553
554 def setCloseCb(self, close_cb): 554 def set_close_cb(self, close_cb):
555 self.close_cb = close_cb 555 self.close_cb = close_cb
556 556
557 def _xmluiClose(self, __=None, reason=None): 557 def _xmlui_close(self, __=None, reason=None):
558 if self.close_cb is not None: 558 if self.close_cb is not None:
559 self.close_cb(self, reason) 559 self.close_cb(self, reason)
560 else: 560 else:
561 G.host.closeUI() 561 G.host.close_ui()
562 562
563 def onParamChange(self, ctrl): 563 def on_param_change(self, ctrl):
564 super(XMLUIPanel, self).onParamChange(ctrl) 564 super(XMLUIPanel, self).on_param_change(ctrl)
565 self.save_btn.disabled = False 565 self.save_btn.disabled = False
566 566
567 def addPostTreat(self, callback): 567 def add_post_treat(self, callback):
568 self._post_treats.append(callback) 568 self._post_treats.append(callback)
569 569
570 def _postTreatCb(self): 570 def _post_treat_cb(self):
571 for cb in self._post_treats: 571 for cb in self._post_treats:
572 cb() 572 cb()
573 del self._post_treats 573 del self._post_treats
574 574
575 def _saveButtonCb(self, button): 575 def _save_button_cb(self, button):
576 button.disabled = True 576 button.disabled = True
577 self.onSaveParams(button) 577 self.on_save_params(button)
578 578
579 def constructUI(self, parsed_dom): 579 def construct_ui(self, parsed_dom):
580 xmlui.XMLUIPanel.constructUI(self, parsed_dom, self._postTreatCb) 580 xmlui.XMLUIPanel.construct_ui(self, parsed_dom, self._post_treat_cb)
581 if self.xmlui_title: 581 if self.xmlui_title:
582 self.layout.add_widget(Title(text=self.xmlui_title)) 582 self.layout.add_widget(Title(text=self.xmlui_title))
583 if isinstance(self.main_cont, TabsContainer): 583 if isinstance(self.main_cont, TabsContainer):
584 # cf. comments above 584 # cf. comments above
585 self._skip_scroll_events = True 585 self._skip_scroll_events = True
586 self.layout.add_widget(self.main_cont) 586 self.layout.add_widget(self.main_cont)
587 if self.type == 'form': 587 if self.type == 'form':
588 submit_btn = SubmitButton() 588 submit_btn = SubmitButton()
589 submit_btn.bind(on_press=self.onFormSubmitted) 589 submit_btn.bind(on_press=self.on_form_submitted)
590 self.layout.add_widget(submit_btn) 590 self.layout.add_widget(submit_btn)
591 if not 'NO_CANCEL' in self.flags: 591 if not 'NO_CANCEL' in self.flags:
592 cancel_btn = CancelButton(text=_("Cancel")) 592 cancel_btn = CancelButton(text=_("Cancel"))
593 cancel_btn.bind(on_press=self.onFormCancelled) 593 cancel_btn.bind(on_press=self.on_form_cancelled)
594 self.layout.add_widget(cancel_btn) 594 self.layout.add_widget(cancel_btn)
595 elif self.type == 'param': 595 elif self.type == 'param':
596 self.save_btn = SaveButton(text=_("Save"), disabled=True) 596 self.save_btn = SaveButton(text=_("Save"), disabled=True)
597 self.save_btn.bind(on_press=self._saveButtonCb) 597 self.save_btn.bind(on_press=self._save_button_cb)
598 self.layout.add_widget(self.save_btn) 598 self.layout.add_widget(self.save_btn)
599 elif self.type == 'window': 599 elif self.type == 'window':
600 cancel_btn = CancelButton(text=_("Cancel")) 600 cancel_btn = CancelButton(text=_("Cancel"))
601 cancel_btn.bind( 601 cancel_btn.bind(
602 on_press=partial(self._xmluiClose, reason=C.XMLUI_DATA_CANCELLED)) 602 on_press=partial(self._xmlui_close, reason=C.XMLUI_DATA_CANCELLED))
603 self.layout.add_widget(cancel_btn) 603 self.layout.add_widget(cancel_btn)
604 604
605 def onHeight(self, __, height): 605 def on_height(self, __, height):
606 if isinstance(self.main_cont, TabsContainer): 606 if isinstance(self.main_cont, TabsContainer):
607 other_children_height = sum([c.height for c in self.layout.children 607 other_children_height = sum([c.height for c in self.layout.children
608 if c is not self.main_cont]) 608 if c is not self.main_cont])
609 self.main_cont.height = height - other_children_height 609 self.main_cont.height = height - other_children_height
610 610
611 def show(self, *args, **kwargs): 611 def show(self, *args, **kwargs):
612 if not self.user_action and not kwargs.get("force", False): 612 if not self.user_action and not kwargs.get("force", False):
613 G.host.addNotifUI(self) 613 G.host.add_notif_ui(self)
614 else: 614 else:
615 G.host.showUI(self) 615 G.host.show_ui(self)
616 616
617 617
618 class XMLUIDialog(xmlui.XMLUIDialog): 618 class XMLUIDialog(xmlui.XMLUIDialog):
619 dialog_factory = WidgetFactory() 619 dialog_factory = WidgetFactory()
620 620