Subversion Repositories basico

Rev

Rev 232 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: settings.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Settings Widget

from os.path import sep as SEP
from cgi import escape
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango

from basico.core.env import ROOT, USER_DIR, APP, LPATH, GPATH, FILE
from basico.core.service import Service

class SettingsView(Gtk.ScrolledWindow, Service):
    def __init__(self, app):
        Gtk.ScrolledWindow.__init__(self)
        Service.__init__(self)
        self.app = app
        self.log = app.log
        self.get_services()
        self.setup()

    def get_services(self):
        """Load services to be used in this class
        """

        self.gui = self.app.get_service("GUI")


    def setup(self):
        self.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        self.set_shadow_type(Gtk.ShadowType.IN)
        self.set_hexpand(True)
        self.set_vexpand(True)
        viewport = Gtk.Viewport()
        self.treeview = Gtk.TreeView()
        viewport.add(self.treeview)
        self.add(viewport)

        # Setup model
        self.model = Gtk.TreeStore(
            str,        # Primary key: SECTION-KEY-VALUE
            str,        # Key
            str,        # Value
        )

        # Setup columns
        # Primary key: SECTION-KEY-VALUE
        self.renderer_pkey = Gtk.CellRendererText()
        self.column_pkey = Gtk.TreeViewColumn('Primary Key', self.renderer_pkey, text=0)
        self.column_pkey.set_visible(False)
        self.column_pkey.set_expand(False)
        self.column_pkey.set_clickable(False)
        self.column_pkey.set_sort_indicator(False)
        self.treeview.append_column(self.column_pkey)

        # Key
        self.renderer_key = Gtk.CellRendererText()
        self.column_key = Gtk.TreeViewColumn('Key', self.renderer_key, markup=1)
        self.column_key.set_visible(True)
        self.column_key.set_expand(False)
        self.column_key.set_clickable(False)
        self.column_key.set_sort_indicator(False)
        self.treeview.append_column(self.column_key)

        # Value
        self.renderer_value = Gtk.CellRendererText()
        self.column_value = Gtk.TreeViewColumn('Value', self.renderer_value, markup=2)
        self.column_value.set_visible(True)
        self.column_value.set_expand(True)
        self.column_value.set_clickable(False)
        self.column_value.set_sort_indicator(False)
        self.treeview.append_column(self.column_value)

        # TreeView common
        self.sorted_model = Gtk.TreeModelSort(model=self.model)
        self.sorted_model.set_sort_column_id(1, Gtk.SortType.ASCENDING)
        self.treeview.set_model(self.sorted_model)

        self.treeview.set_can_focus(False)
        self.treeview.set_headers_visible(True)
        self.treeview.set_enable_search(True)
        self.treeview.set_hover_selection(False)
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
        # ~ self.treeview.modify_font(Pango.FontDescription('Monospace 10'))

        # Selection
        self.selection = self.treeview.get_selection()
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)

        self.show_all()


    def update(self):
        from cgi import escape
        self.model.clear()
        self.settings = self.app.get_service('Settings')
        config = self.settings.load()
        sdict = {}

        # ENVIRONMENT NODE
        root = self.model.append(None, ['root', '<big><b>Environment</b></big>', ''])


        self.model.append(root, ['', '<span color="blue">App System Dir</span>', ROOT])

        pid = self.model.append(root, ['', '<b>APP</b>', ''])
        for key in APP:
            value = APP[key]
            if isinstance(value, list):
                value = ('\n'.join(value))
            value = escape(value)
            self.model.append(pid, ['APP-%s' % key, '<span color="blue">%s</span>' % key, value])
        pid = self.model.append(root, ['', '<b>LPATH</b>', ''])
        for key in LPATH:
            self.model.append(pid, ['LPATH-%s' % key, '<span color="blue">%s</span>' % key, LPATH[key]])
        pid = self.model.append(root, ['', '<b>GPATH</b>', ''])
        for key in GPATH:
            self.model.append(pid, ['GPATH-%s' % key, '<span color="blue">%s</span>' % key, GPATH[key]])
        pid = self.model.append(root, ['', '<b>FILE</b>', ''])
        for key in FILE:
            self.model.append(pid, ['FILE-%s' % key, '<span color="blue">%s</span>' % key, FILE[key]])

        # SETTINGS NODE
        root = self.model.append(None, ['root', '<big><b>Settings</b></big>', ''])

        def get_pid(key):
            try:
                pid = sdict[key]
            except:
                pid = self.model.append(root, [key, '<b>%s</b>' % key, ''])
                sdict[key] = pid
            return pid

        for skey in config.keys():
            pid = get_pid(skey)
            for key in config[skey]:
                value = str(config[skey][key])
                pkey = '%s-%s-%s' % (skey, key, value)
                self.model.append(pid, [pkey, '<span color="blue">%s</span>' % key, value])


        # WIDGETS NODE
        root = self.model.append(None, ['root', '<big><b>Runtime Objects</b></big>', ''])

        wdgdict = {}
        widgets = self.gui.get_widgets()
        for name in widgets:
            objname = escape(str(widgets[name]))
            pobj = gobject = objname[4:objname.find(' ')]
            gobj_left = gobject[:gobject.find('.')]
            gobj_right = gobject[gobject.find('.')+1:]
            pobj = gobject[:gobject.find('.')]
            try:
                ppid_left = wdgdict[gobj_left]
            except:
                ppid_left = self.model.append(root, [gobj_left, '<b>%s</b>' % gobj_left, ''])
                wdgdict[gobj_left] = ppid_left


            try:
                ppid_right = wdgdict[gobj_right]
            except:
                ppid_right = self.model.append(ppid_left, [gobj_right, '<b>%s</b>' % gobj_right, ''])
                wdgdict[gobj_right] = ppid_right

            self.model.append(ppid_right, [gobject, name, ''])