view src/xmlui.py @ 14:21a432afd06d

plugin system, first draft: - widgets are now handled with plugins, ContactList and WidgetSelector have been changed to be pluggins - everything in PLUGIN_INFO (including PLUGIN_INFO itself) is optional. Best guess is used if a value is missing - WidgetsHandler use default widget from plugins, which is WidgetSelector for now - PLUGIN_INFO is used in the same way as for backed plugins, with (for now) following possible keys: - "name": human readable name - "description": long description of what widget do - "import_name": unique short name used as reference for import - "main": main class name - "factory": callback used to instanciate a widget (see Cagou._defaultFactory) - "kv_file": path to the kv language file to load (same filename with ".kv" will be used if key is not found) other data should be added quickly, as a path to a file used as icon - host.getPluggedWidgets can be used to find loaded widgets. except_cls can be used to excluse a class (self.__class__ usually) - fixed host.switchWidget when old widget is already a CagouWidget - CagouWidget header new display the available widgets
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2016 16:02:44 +0200
parents 160cc95ad7ea
children
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


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


class VerticalContainer(xmlui.VerticalContainer, BoxLayout):

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

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


class WidgetFactory(object):

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


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:
            log.error(u"No close method defined")

    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


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