view src/cagou/core/xmlui.py @ 43:12fdc7d1feb1

core: newWidget implementation, it only display a not when a MUC room has been joined
author Goffi <goffi@goffi.org>
date Sun, 28 Aug 2016 16:50:56 +0200
parents c21d1be2e54c
children 65775152aac1
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Cagou: a SàT frontend
# Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sat.core.i18n import _
from sat_frontends.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat_frontends.tools import xmlui
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from cagou import G


## Widgets ##


class TextWidget(xmlui.TextWidget, Label):

    def __init__(self, xmlui_parent, value):
        Label.__init__(self, text=value)


class PasswordWidget(xmlui.PasswordWidget, TextInput):

    def __init__(self, _xmlui_parent, value, read_only=False):
        TextInput.__init__(self, password=True, multiline=False,
            text=value, readonly=read_only, size=(100,25), size_hint=(1,None))

    def _xmluiSetValue(self, value):
        self.text = value

    def _xmluiGetValue(self):
        return self.text


## Containers ##


class VerticalContainer(xmlui.VerticalContainer, BoxLayout):

    def __init__(self, xmlui_parent):
        BoxLayout.__init__(self, orientation='vertical')

    def _xmluiAppend(self, widget):
        self.add_widget(widget)


## Dialogs ##


class NoteDialog(xmlui.NoteDialog):

    def __init__(self, _xmlui_parent, title, message, level):
        xmlui.NoteDialog.__init__(self, _xmlui_parent)
        self.title, self.message, self.level = title, message, level

    def _xmluiShow(self):
        G.host.addNote(self.title, self.message, self.level)


## Factory ##


class WidgetFactory(object):

    def __getattr__(self, attr):
        if attr.startswith("create"):
            cls = globals()[attr[6:]]
            return cls


## Core ##


class Title(Label):

    def __init__(self, *args, **kwargs):
        kwargs['size'] = (100, 25)
        kwargs['size_hint'] = (1,None)
        super(Title, self).__init__(*args, **kwargs)


class XMLUIPanel(xmlui.XMLUIPanel, BoxLayout):
    widget_factory = WidgetFactory()

    def __init__(self, host, parsed_xml, title=None, flags=None, callback=None, profile=C.PROF_KEY_NONE):
        self.close_cb = None
        BoxLayout.__init__(self, orientation='vertical')
        xmlui.XMLUIPanel.__init__(self, host, parsed_xml, title, flags, callback, profile)

    def setCloseCb(self, close_cb):
        self.close_cb = close_cb

    def _xmluiClose(self):
        if self.close_cb is not None:
            self.close_cb(self)
        else:
            G.host.closeUI()

    def constructUI(self, parsed_dom):
        xmlui.XMLUIPanel.constructUI(self, parsed_dom)
        if self.xmlui_title:
            self.add_widget(Title(text=self.xmlui_title))
        self.add_widget(self.main_cont)
        if self.type == 'form':
            submit_btn = Button(text=_(u"Submit"), size_hint=(1,0.2))
            submit_btn.bind(on_press=self.onFormSubmitted)
            self.add_widget(submit_btn)
            if not 'NO_CANCEL' in self.flags:
                cancel_btn = Button(text=_(u"Cancel"), size_hint=(1,0.2))
                cancel_btn.bind(on_press=self.onFormCancelled)
                self.add_widget(cancel_btn)
        self.add_widget(Widget()) # to have elements on the top

    def show(self, *args, **kwargs):
        if not self.user_action and not kwargs.get("force", False):
            G.host.addNotifUI(self)
        else:
            G.host.showUI(self)


class XMLUIDialog(xmlui.XMLUIDialog):
    dialog_factory = WidgetFactory()


xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel)
xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog)
create = xmlui.create