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
This commit is contained in:
Goffi 2014-08-13 14:48:49 +02:00
commit b6563c596b
4 changed files with 516 additions and 228 deletions

View file

@ -25,7 +25,7 @@ from sat.core.log import getLogger
log = getLogger(__name__)
from sat_frontends.tools.games import TarotCard
from sat_frontends.quick_frontend.quick_card_game import QuickCardGame
from sat_frontends.wix.xmlui import XMLUI
from sat_frontends.wix import xmlui
CARD_WIDTH = 74
CARD_HEIGHT = 136
@ -103,7 +103,7 @@ class CardPanel(QuickCardGame, wx.Panel):
def chooseContrat(self, xml_data):
"""Called when the player has to select his contrat
@param xml_data: SàT xml representation of the form"""
XMLUI(self.parent.host, xml_data, title=_('Please choose your contrat'), flags=['NO_CANCEL'])
xmlui.create(self.parent.host, xml_data, title=_('Please choose your contrat'), flags=['NO_CANCEL'])
def showScores(self, xml_data, winners, loosers):
"""Called when the round is over, display the scores
@ -112,7 +112,7 @@ class CardPanel(QuickCardGame, wx.Panel):
title = _("Draw game")
else:
title = _('You win \o/') if self.player_nick in winners else _('You loose :(')
XMLUI(self.parent.host, xml_data, title=title, flags=['NO_CANCEL'])
xmlui.create(self.parent.host, xml_data, title=title, flags=['NO_CANCEL'])
def cardsPlayed(self, player, cards):
"""A card has been played by player"""

View file

@ -25,7 +25,7 @@ from sat_frontends.quick_frontend.quick_app import QuickApp
import wx
from sat_frontends.wix.contact_list import ContactList
from sat_frontends.wix.chat import Chat
from sat_frontends.wix.xmlui import XMLUI
from sat_frontends.wix import xmlui
from sat_frontends.wix.profile import Profile
from sat_frontends.wix.profile_manager import ProfileManager
import os.path
@ -192,16 +192,16 @@ class MainWindow(wx.Frame, QuickApp):
popup.Show()
wx.CallLater(5000,popup.Destroy)
def showDialog(self, message, title="", type="info", answer_cb = None, answer_data = None):
if type == 'info':
def showDialog(self, message, title="", type_="info", answer_cb = None, answer_data = None):
if type_ == 'info':
flags = wx.OK | wx.ICON_INFORMATION
elif type == 'error':
elif type_ == 'error':
flags = wx.OK | wx.ICON_ERROR
elif type == 'yes/no':
elif type_ == 'yes/no':
flags = wx.YES_NO | wx.ICON_QUESTION
else:
flags = wx.OK | wx.ICON_INFORMATION
log.error(_('unmanaged dialog type: %s'), type)
log.error(_('unmanaged dialog type: %s'), type_)
dlg = wx.MessageDialog(self, message, title, flags)
answer = dlg.ShowModal()
dlg.Destroy()
@ -243,7 +243,8 @@ class MainWindow(wx.Frame, QuickApp):
pass
elif "xmlui" in data:
log.debug (_("XML user interface received"))
XMLUI(self, xml_data = data['xmlui'])
ui = xmlui.create(self, xml_data = data['xmlui'])
ui.show()
elif "authenticated_profile" in data:
assert("caller" in data)
if data["caller"] == "profile_manager":
@ -345,7 +346,7 @@ class MainWindow(wx.Frame, QuickApp):
title = _('Registration')
misc['target'] = data['target']
misc['action_back'] = self.bridge.gatewayRegister
XMLUI(self, title=title, xml_data = data['xml'], misc = misc)
xmlui.create(self, title=title, xml_data = data['xml'], misc = misc)
elif type_ == "RESULT":
self.current_action_ids.remove(id_)
if self.current_action_ids_cb.has_key(id_):
@ -413,7 +414,7 @@ class MainWindow(wx.Frame, QuickApp):
def onParam(self, e):
log.debug(_("Param request"))
def success(params):
XMLUI(self, xml_data=params, title=_("Configuration"))
xmlui.create(self, xml_data=params, title=_("Configuration"))
def failure(error):
dlg = wx.MessageDialog(self, error.message,

View file

@ -24,7 +24,6 @@ from sat_frontends.primitivus.constants import Const as C
import wx
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.tools.jid import JID
NO_SELECTION_ENTRY = ' '
@ -147,7 +146,7 @@ class ProfileManager(wx.Panel):
return
if self.selected_profile:
self.profile_name.SetValue(self.selected_profile)
self.host.profile = profile_name # FIXME: EXTREMELY DIRTY, needed for sat_frontends.tools.xmlui.XMLUI._xmluiLaunchAction
self.host.profile = profile_name # FIXME: EXTREMELY DIRTY, needed for sat_frontends.tools.xmlui.XMLUI.submit
self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, {'caller': 'profile_manager'}, profile_key=profile_name)
def onConnectButton(self, event):

View file

@ -23,9 +23,8 @@ from sat.core.i18n import _
import wx
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.tools.jid import JID
from sat_frontends.tools import xmlui
from sat_frontends.constants import Const
from sat_frontends.constants import Const as C
class EventWidget(object):
@ -53,20 +52,20 @@ class ValueWidget(WixWidget):
class EmptyWidget(WixWidget, xmlui.EmptyWidget, wx.Window):
def __init__(self, parent):
wx.Window.__init__(self, parent, -1)
def __init__(self, _xmlui_parent):
wx.Window.__init__(self, _xmlui_parent, -1)
class TextWidget(WixWidget, xmlui.TextWidget, wx.StaticText):
def __init__(self, parent, value):
wx.StaticText.__init__(self, parent, -1, value)
def __init__(self, _xmlui_parent, value):
wx.StaticText.__init__(self, _xmlui_parent, -1, value)
class LabelWidget(xmlui.LabelWidget, TextWidget):
def __init__(self, parent, value):
super(LabelWidget, self).__init__(parent, value+": ")
def __init__(self, _xmlui_parent, value):
super(LabelWidget, self).__init__(_xmlui_parent, value+": ")
class JidWidget(xmlui.JidWidget, TextWidget):
@ -75,49 +74,49 @@ class JidWidget(xmlui.JidWidget, TextWidget):
class DividerWidget(WixWidget, xmlui.DividerWidget, wx.StaticLine):
def __init__(self, parent, style='line'):
wx.StaticLine.__init__(self, parent, -1)
def __init__(self, _xmlui_parent, style='line'):
wx.StaticLine.__init__(self, _xmlui_parent, -1)
class StringWidget(EventWidget, ValueWidget, xmlui.StringWidget, wx.TextCtrl):
_xmlui_change_event = wx.EVT_TEXT
def __init__(self, parent, value, read_only=False):
def __init__(self, _xmlui_parent, value, read_only=False):
style = wx.TE_READONLY if read_only else 0
wx.TextCtrl.__init__(self, parent, -1, value, style=style)
wx.TextCtrl.__init__(self, _xmlui_parent, -1, value, style=style)
self._xmlui_proportion = 1
class PasswordWidget(EventWidget, ValueWidget, xmlui.PasswordWidget, wx.TextCtrl):
_xmlui_change_event = wx.EVT_TEXT
def __init__(self, parent, value, read_only=False):
def __init__(self, _xmlui_parent, value, read_only=False):
style = wx.TE_PASSWORD
if read_only:
style |= wx.TE_READONLY
wx.TextCtrl.__init__(self, parent, -1, value, style=style)
wx.TextCtrl.__init__(self, _xmlui_parent, -1, value, style=style)
self._xmlui_proportion = 1
class TextBoxWidget(EventWidget, ValueWidget, xmlui.TextBoxWidget, wx.TextCtrl):
_xmlui_change_event = wx.EVT_TEXT
def __init__(self, parent, value, read_only=False):
def __init__(self, _xmlui_parent, value, read_only=False):
style = wx.TE_MULTILINE
if read_only:
style |= wx.TE_READONLY
wx.TextCtrl.__init__(self, parent, -1, value, style=style)
wx.TextCtrl.__init__(self, _xmlui_parent, -1, value, style=style)
self._xmlui_proportion = 1
class BoolWidget(EventWidget, ValueWidget, xmlui.BoolWidget, wx.CheckBox):
_xmlui_change_event = wx.EVT_CHECKBOX
def __init__(self, parent, state, read_only=False):
def __init__(self, _xmlui_parent, state, read_only=False):
style = wx.CHK_2STATE
if read_only:
style |= wx.TE_READONLY
wx.CheckBox.__init__(self, parent, -1, "", style=wx.CHK_2STATE)
wx.CheckBox.__init__(self, _xmlui_parent, -1, "", style=wx.CHK_2STATE)
self.SetValue(state)
self._xmlui_proportion = 1
@ -131,22 +130,22 @@ class BoolWidget(EventWidget, ValueWidget, xmlui.BoolWidget, wx.CheckBox):
class ButtonWidget(EventWidget, WixWidget, xmlui.ButtonWidget, wx.Button):
_xmlui_change_event = wx.EVT_BUTTON
def __init__(self, parent, value, click_callback):
wx.Button.__init__(self, parent, -1, value)
def __init__(self, _xmlui_parent, value, click_callback):
wx.Button.__init__(self, _xmlui_parent, -1, value)
self._xmlui_click_callback = click_callback
parent.Bind(wx.EVT_BUTTON, lambda evt: click_callback(evt.GetEventObject()), self)
self.parent = parent
_xmlui_parent.Bind(wx.EVT_BUTTON, lambda evt: click_callback(evt.GetEventObject()), self)
self._xmlui_parent = _xmlui_parent
def _xmluiOnClick(self, callback):
self.parent.Bind(wx.EVT_BUTTON, lambda evt: callback(evt.GetEventObject()), self)
self._xmlui_parent.Bind(wx.EVT_BUTTON, lambda evt: callback(evt.GetEventObject()), self)
class ListWidget(EventWidget, WixWidget, xmlui.ListWidget, wx.ListBox):
_xmlui_change_event = wx.EVT_LISTBOX
def __init__(self, parent, options, selected, flags):
def __init__(self, _xmlui_parent, options, selected, flags):
styles = wx.LB_MULTIPLE if not 'single' in flags else wx.LB_SINGLE
wx.ListBox.__init__(self, parent, -1, choices=[option[1] for option in options], style=styles)
wx.ListBox.__init__(self, _xmlui_parent, -1, choices=[option[1] for option in options], style=styles)
self._xmlui_attr_map = {label: value for value, label in options}
self._xmlui_proportion = 1
self._xmluiSelectValues(selected)
@ -192,8 +191,8 @@ class WixContainer(object):
class AdvancedListContainer(WixContainer, xmlui.AdvancedListContainer, wx.ScrolledWindow):
def __init__(self, parent, columns, selectable='no'):
wx.ScrolledWindow.__init__(self, parent)
def __init__(self, _xmlui_parent, columns, selectable='no'):
wx.ScrolledWindow.__init__(self, _xmlui_parent)
self._xmlui_selectable = selectable != 'no'
if selectable:
columns += 1
@ -230,8 +229,8 @@ class AdvancedListContainer(WixContainer, xmlui.AdvancedListContainer, wx.Scroll
class PairsContainer(WixContainer, xmlui.PairsContainer, wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
def __init__(self, _xmlui_parent):
wx.Panel.__init__(self, _xmlui_parent)
self.sizer = wx.FlexGridSizer(cols=2)
self.sizer.AddGrowableCol(1) #The growable column need most of time to be the right one in pairs
self.SetSizer(self.sizer)
@ -239,8 +238,8 @@ class PairsContainer(WixContainer, xmlui.PairsContainer, wx.Panel):
class TabsContainer(WixContainer, xmlui.TabsContainer, wx.Notebook):
def __init__(self, parent):
wx.Notebook.__init__(self, parent, -1, style=wx.NB_LEFT if self._xmlui_main.type=='param' else 0)
def __init__(self, _xmlui_parent):
wx.Notebook.__init__(self, _xmlui_parent, -1, style=wx.NB_LEFT if self._xmlui_main.type=='param' else 0)
def _xmluiAddTab(self, label):
tab_panel = wx.Panel(self, -1)
@ -253,30 +252,109 @@ class TabsContainer(WixContainer, xmlui.TabsContainer, wx.Notebook):
class VerticalContainer(WixContainer, xmlui.VerticalContainer, wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
def __init__(self, _xmlui_parent):
wx.Panel.__init__(self, _xmlui_parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.sizer)
class WidgetFactory(object):
## Dialogs ##
class WixDialog(object):
def __init__(self, _xmlui_parent, level):
self.host = _xmlui_parent.host
self.ok_cb = None
self.cancel_cb = None
if level == C.XMLUI_DATA_LVL_INFO:
self.flags = wx.ICON_INFORMATION
elif level == C.XMLUI_DATA_LVL_ERROR:
self.flags = wx.ICON_ERROR
else:
self.flags = wx.ICON_INFORMATION
log.warning(_("Unmanaged dialog level: %s") % level)
def _xmluiShow(self):
answer = self.ShowModal()
if answer == wx.ID_YES or answer == wx.ID_OK:
self._xmluiValidated()
else:
self._xmluiCancelled()
def _xmluiClose(self):
self.Destroy()
class MessageDialog(WixDialog, xmlui.MessageDialog, wx.MessageDialog):
def __init__(self, _xmlui_parent, title, message, level):
WixDialog.__init__(self, _xmlui_parent, level)
xmlui.MessageDialog.__init__(self, _xmlui_parent)
self.flags |= wx.OK
wx.MessageDialog.__init__(self, _xmlui_parent.host, message, title, style = self.flags)
class NoteDialog(xmlui.NoteDialog, MessageDialog):
# TODO: separate NoteDialog
pass
class ConfirmDialog(WixDialog, xmlui.ConfirmDialog, wx.MessageDialog):
def __init__(self, _xmlui_parent, title, message, level, buttons_set):
WixDialog.__init__(self, _xmlui_parent, level)
xmlui.ConfirmDialog.__init__(self, _xmlui_parent)
if buttons_set == C.XMLUI_DATA_BTNS_SET_YESNO:
self.flags |= wx.YES_NO
else:
self.flags |= wx.OK | wx.CANCEL
wx.MessageDialog.__init__(self, _xmlui_parent.host, message, title, style = self.flags)
class FileDialog(WixDialog, xmlui.FileDialog, wx.FileDialog):
def __init__(self, _xmlui_parent, title, message, level, filetype):
# TODO: message and filetype are not managed yet
WixDialog.__init__(self, _xmlui_parent, level)
self.flags = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT # FIXME: use the legacy flags, but must manage cases like dir or open
xmlui.FileDialog.__init__(self, _xmlui_parent)
wx.FileDialog.__init__(self, _xmlui_parent.host, title, style = self.flags)
def _xmluiShow(self):
answer = self.ShowModal()
if answer == wx.ID_OK:
self._xmluiValidated({'path': self.GetPath()})
else:
self._xmluiCancelled()
class GenericFactory(object):
def __getattr__(self, attr):
if attr.startswith("create"):
cls = globals()[attr[6:]]
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, wx.Frame):
class XMLUIPanel(xmlui.XMLUIPanel, wx.Frame):
"""Create an user interface from a SàT XML"""
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
xmlui.XMLUI.__init__(self, host, xml_data, title, flags)
xmlui.XMLUIPanel.__init__(self, host, parsed_xml, title, flags)
def constructUI(self, xml_data):
def constructUI(self, parsed_dom):
style = wx.DEFAULT_FRAME_STYLE & ~wx.CLOSE_BOX if 'NO_CANCEL' in self.flags else wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, None, style=style)
self.sizer = wx.BoxSizer(wx.VERTICAL)
@ -302,7 +380,7 @@ class XMLUI(xmlui.XMLUI, wx.Frame):
self.sizer.Fit(self)
self.Show()
super(XMLUI, self).constructUI(xml_data, postTreat)
super(XMLUIPanel, self).constructUI(parsed_dom, postTreat)
if not 'NO_CANCEL' in self.flags:
self.Bind(wx.EVT_CLOSE, self.onClose, self)
self.MakeModal()
@ -314,12 +392,12 @@ class XMLUI(xmlui.XMLUI, wx.Frame):
###events
def onParamChange(self, ctrl):
super(XMLUI, self).onParamChange(ctrl)
super(XMLUIPanel, self).onParamChange(ctrl)
def onFormSubmitted(self, event):
"""Called when submit button is clicked"""
button = event.GetEventObject()
super(XMLUI, self).onFormSubmitted(button)
super(XMLUIPanel, self).onFormSubmitted(button)
def onClose(self, event):
"""Close event: we have to send the form."""
@ -330,3 +408,11 @@ class XMLUI(xmlui.XMLUI, wx.Frame):
self._xmluiClose()
event.Skip()
class XMLUIDialog(xmlui.XMLUIDialog):
dialog_factory = WidgetFactory()
xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel)
xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog)
create = xmlui.create