diff frontends/src/primitivus/xmlui.py @ 1106:e2e1e27a3680

frontends: XMLUI refactoring + dialogs: - there are now XMLUIPanel and XMLUIDialog both inheriting from XMLUIBase - following dialogs are managed: - MessageDialog - NoteDialog - ConfirmDialog - FileDialog - XMLUI creation is now made using xmlui.create(...) instead of instanciating directly XMLUI - classes must be registed in frontends - "parent" attribute renamed to "_xmlui_parent" to avoid name conflicts with frontends toolkits
author Goffi <goffi@goffi.org>
date Wed, 13 Aug 2014 14:48:49 +0200
parents 7a39ae3950f7
children 6184779544c7
line wrap: on
line diff
--- a/frontends/src/primitivus/xmlui.py	Mon Aug 11 19:10:24 2014 +0200
+++ b/frontends/src/primitivus/xmlui.py	Wed Aug 13 14:48:49 2014 +0200
@@ -21,15 +21,17 @@
 import urwid
 import copy
 from urwid_satext import sat_widgets
+from urwid_satext import files_management
 from sat.core.log import getLogger
 log = getLogger(__name__)
+from sat_frontends.primitivus.constants import Const as C
 from sat_frontends.tools import xmlui
 
 
 class PrimitivusEvents(object):
     """ Used to manage change event of Primitivus widgets """
 
-    def _event_callback(self, ctrl, *args, **ktkwargs):
+    def _event_callback(self, ctrl, *args, **kwargs):
         """" Call xmlui callback and ignore any extra argument """
         args[-1](ctrl)
 
@@ -40,20 +42,20 @@
 
 class PrimitivusEmptyWidget(xmlui.EmptyWidget, urwid.Text):
 
-    def __init__(self, parent):
+    def __init__(self, _xmlui_parent):
         urwid.Text.__init__(self, '')
 
 
 class PrimitivusTextWidget(xmlui.TextWidget, urwid.Text):
 
-    def __init__(self, parent, value, read_only=False):
+    def __init__(self, _xmlui_parent, value, read_only=False):
         urwid.Text.__init__(self, value)
 
 
 class PrimitivusLabelWidget(xmlui.LabelWidget, PrimitivusTextWidget):
 
-    def __init__(self, parent, value):
-        super(PrimitivusLabelWidget, self).__init__(parent, value+": ")
+    def __init__(self, _xmlui_parent, value):
+        super(PrimitivusLabelWidget, self).__init__(_xmlui_parent, value+": ")
 
 
 class PrimitivusJidWidget(xmlui.JidWidget, PrimitivusTextWidget):
@@ -62,7 +64,7 @@
 
 class PrimitivusDividerWidget(xmlui.DividerWidget, urwid.Divider):
 
-    def __init__(self, parent, style='line'):
+    def __init__(self, _xmlui_parent, style='line'):
         if style == 'line':
             div_char = u'─'
         elif style == 'dot':
@@ -82,7 +84,7 @@
 
 class PrimitivusStringWidget(xmlui.StringWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
 
-    def __init__(self, parent, value, read_only=False):
+    def __init__(self, _xmlui_parent, value, read_only=False):
         sat_widgets.AdvancedEdit.__init__(self, edit_text=value)
         self.read_only = read_only
 
@@ -100,7 +102,7 @@
 
 class PrimitivusPasswordWidget(xmlui.PasswordWidget, sat_widgets.Password, PrimitivusEvents):
 
-    def __init__(self, parent, value, read_only=False):
+    def __init__(self, _xmlui_parent, value, read_only=False):
         sat_widgets.Password.__init__(self, edit_text=value)
         self.read_only = read_only
 
@@ -118,7 +120,7 @@
 
 class PrimitivusTextBoxWidget(xmlui.TextBoxWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
 
-    def __init__(self, parent, value, read_only=False):
+    def __init__(self, _xmlui_parent, value, read_only=False):
         sat_widgets.AdvancedEdit.__init__(self, edit_text=value, multiline=True)
         self.read_only = read_only
 
@@ -136,7 +138,7 @@
 
 class PrimitivusBoolWidget(xmlui.BoolWidget, urwid.CheckBox, PrimitivusEvents):
 
-    def __init__(self, parent, state, read_only=False):
+    def __init__(self, _xmlui_parent, state, read_only=False):
         urwid.CheckBox.__init__(self, '', state=state)
         self.read_only = read_only
 
@@ -154,7 +156,7 @@
 
 class PrimitivusButtonWidget(xmlui.ButtonWidget, sat_widgets.CustomButton, PrimitivusEvents):
 
-    def __init__(self, parent, value, click_callback):
+    def __init__(self, _xmlui_parent, value, click_callback):
         sat_widgets.CustomButton.__init__(self, value, on_press=click_callback)
 
     def _xmluiOnClick(self, callback):
@@ -163,7 +165,7 @@
 
 class PrimitivusListWidget(xmlui.ListWidget, sat_widgets.List, PrimitivusEvents):
 
-    def __init__(self, parent, options, selected, flags):
+    def __init__(self, _xmlui_parent, options, selected, flags):
         sat_widgets.List.__init__(self, options=options, style=flags)
         self._xmluiSelectValues(selected)
 
@@ -194,7 +196,7 @@
 
 class PrimitivusAdvancedListContainer(xmlui.AdvancedListContainer, sat_widgets.TableContainer, PrimitivusEvents):
 
-    def __init__(self, parent, columns, selectable='no'):
+    def __init__(self, _xmlui_parent, columns, selectable='no'):
         options = {'ADAPT':()}
         if selectable != 'no':
             options['HIGHLIGHT'] = ()
@@ -219,7 +221,7 @@
 
 class PrimitivusPairsContainer(xmlui.PairsContainer, sat_widgets.TableContainer):
 
-    def __init__(self, parent):
+    def __init__(self, _xmlui_parent):
         options = {'ADAPT':(0,), 'HIGHLIGHT':(0,)}
         if self._xmlui_main.type == 'param':
             options['FOCUS_ATTR'] = 'param_selected'
@@ -234,7 +236,7 @@
 
 class PrimitivusTabsContainer(xmlui.TabsContainer, sat_widgets.TabsContainer):
 
-    def __init__(self, parent):
+    def __init__(self, _xmlui_parent):
         sat_widgets.TabsContainer.__init__(self)
 
     def _xmluiAppend(self, widget):
@@ -249,7 +251,7 @@
 class PrimitivusVerticalContainer(xmlui.VerticalContainer, urwid.ListBox):
     BOX_HEIGHT = 5
 
-    def __init__(self, parent):
+    def __init__(self, _xmlui_parent):
         urwid.ListBox.__init__(self, urwid.SimpleListWalker([]))
         self._last_size = None
 
@@ -269,24 +271,81 @@
         return super(PrimitivusVerticalContainer, self).render(size, focus)
 
 
-class WidgetFactory(object):
+### Dialogs ###
+
+
+class PrimitivusDialog(object):
+
+    def __init__(self, _xmlui_parent):
+        self.host = _xmlui_parent.host
+
+    def _xmluiShow(self):
+        self.host.showPopUp(self)
+
+    def _xmluiClose(self):
+        self.host.removePopUp()
+
+
+class PrimitivusMessageDialog(PrimitivusDialog, xmlui.MessageDialog, sat_widgets.Alert):
+
+    def __init__(self, _xmlui_parent, title, message, level):
+        PrimitivusDialog.__init__(self, _xmlui_parent)
+        xmlui.MessageDialog.__init__(self, _xmlui_parent)
+        sat_widgets.Alert.__init__(self, title, message, ok_cb=lambda dummy: self._xmluiValidated())
+
+
+class PrimitivusNoteDialog(xmlui.NoteDialog, PrimitivusMessageDialog):
+    # TODO: separate NoteDialog
+    pass
+
+
+class PrimitivusConfirmDialog(PrimitivusDialog, xmlui.ConfirmDialog, sat_widgets.ConfirmDialog):
+
+    def __init__(self, _xmlui_parent, title, message, level, buttons_set):
+        PrimitivusDialog.__init__(self, _xmlui_parent)
+        xmlui.ConfirmDialog.__init__(self, _xmlui_parent)
+        sat_widgets.ConfirmDialog.__init__(self, title, message, no_cb=lambda dummy: self._xmluiCancelled(), yes_cb=lambda dummy: self._xmluiValidated())
+
+
+class PrimitivusFileDialog(PrimitivusDialog, xmlui.FileDialog, files_management.FileDialog):
+
+    def __init__(self, _xmlui_parent, title, message, level, filetype):
+        # TODO: message is not managed yet
+        PrimitivusDialog.__init__(self, _xmlui_parent)
+        xmlui.FileDialog.__init__(self, _xmlui_parent)
+        style = []
+        if filetype == C.XMLUI_DATA_FILETYPE_DIR:
+            style.append('dir')
+        files_management.FileDialog.__init__(self, ok_cb=lambda path: self._xmluiValidated({'path': path}), cancel_cb=lambda dummy: self._xmluiCancelled(), title=title)
+
+
+class GenericFactory(object):
 
     def __getattr__(self, attr):
         if attr.startswith("create"):
             cls = globals()["Primitivus" + attr[6:]] # XXX: we prefix with "Primitivus" to work around an Urwid bug, WidgetMeta in Urwid don't manage multiple inheritance with same names
+            return cls
+
+
+class WidgetFactory(GenericFactory):
+
+    def __getattr__(self, attr):
+        if attr.startswith("create"):
+            cls = GenericFactory.__getattr__(self, attr)
             cls._xmlui_main = self._xmlui_main
             return cls
 
-class XMLUI(xmlui.XMLUI, urwid.WidgetWrap):
+
+class XMLUIPanel(xmlui.XMLUIPanel, urwid.WidgetWrap):
     widget_factory = WidgetFactory()
 
-    def __init__(self, host, xml_data, title = None, flags = None):
+    def __init__(self, host, parsed_xml, title = None, flags = None):
         self.widget_factory._xmlui_main = self
         self._dest = None
-        xmlui.XMLUI.__init__(self, host, xml_data, title, flags)
+        xmlui.XMLUIPanel.__init__(self, host, parsed_xml, title, flags)
         urwid.WidgetWrap.__init__(self, self.main_cont)
 
-    def constructUI(self, xml_data):
+    def constructUI(self, parsed_dom):
         def postTreat():
             assert self.main_cont.body
 
@@ -311,7 +370,7 @@
                 grid_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
                 tabs_cont.addFooter(grid_wid)
 
-        super(XMLUI, self).constructUI(xml_data, postTreat)
+        xmlui.XMLUIPanel.constructUI(self, parsed_dom, postTreat)
         urwid.WidgetWrap.__init__(self, self.main_cont)
 
     def show(self, show_type=None, valign='middle'):
@@ -321,7 +380,7 @@
             - 'popup'
             - 'window'
         @param valign: vertical alignment when show_type is 'popup'.
-                       Ignored when show_type is 'window'.
+            Ignored when show_type is 'window'.
 
         """
         if show_type is None:
@@ -343,9 +402,17 @@
             assert(False)
         self.host.redraw()
 
-
     def _xmluiClose(self):
         if self._dest == 'window':
             self.host.removeWindow()
         else:
             self.host.removePopUp()
+
+
+class XMLUIDialog(xmlui.XMLUIDialog):
+    dialog_factory = GenericFactory()
+
+
+xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel)
+xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog)
+create = xmlui.create