Subversion Repositories basico

Rev

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

Rev Author Line No. Line
232 t00m 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: settings.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Settings Widget
7
 
8
from os.path import sep as SEP
9
from cgi import escape
10
import gi
11
gi.require_version('Gtk', '3.0')
12
gi.require_version('Gdk', '3.0')
13
from gi.repository import Gtk
14
from gi.repository import Gdk
15
from gi.repository import Pango
16
 
233 t00m 17
from basico.core.env import ROOT, USER_DIR, APP, LPATH, GPATH, FILE
18
from basico.core.service import Service
232 t00m 19
 
20
class SettingsView(Gtk.ScrolledWindow, Service):
21
    def __init__(self, app):
22
        Gtk.ScrolledWindow.__init__(self)
23
        Service.__init__(self)
24
        self.app = app
25
        self.log = app.log
26
        self.get_services()
27
        self.setup()
28
 
29
    def get_services(self):
30
        """Load services to be used in this class
31
        """
32
        self.gui = self.app.get_service("GUI")
33
 
34
 
35
    def setup(self):
36
        self.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
37
        self.set_shadow_type(Gtk.ShadowType.IN)
38
        self.set_hexpand(True)
39
        self.set_vexpand(True)
40
        viewport = Gtk.Viewport()
41
        self.treeview = Gtk.TreeView()
42
        viewport.add(self.treeview)
43
        self.add(viewport)
44
 
45
        # Setup model
46
        self.model = Gtk.TreeStore(
47
            str,        # Primary key: SECTION-KEY-VALUE
48
            str,        # Key
49
            str,        # Value
50
        )
51
 
52
        # Setup columns
53
        # Primary key: SECTION-KEY-VALUE
54
        self.renderer_pkey = Gtk.CellRendererText()
55
        self.column_pkey = Gtk.TreeViewColumn('Primary Key', self.renderer_pkey, text=0)
56
        self.column_pkey.set_visible(False)
57
        self.column_pkey.set_expand(False)
58
        self.column_pkey.set_clickable(False)
59
        self.column_pkey.set_sort_indicator(False)
60
        self.treeview.append_column(self.column_pkey)
61
 
62
        # Key
63
        self.renderer_key = Gtk.CellRendererText()
64
        self.column_key = Gtk.TreeViewColumn('Key', self.renderer_key, markup=1)
65
        self.column_key.set_visible(True)
66
        self.column_key.set_expand(False)
67
        self.column_key.set_clickable(False)
68
        self.column_key.set_sort_indicator(False)
69
        self.treeview.append_column(self.column_key)
70
 
71
        # Value
72
        self.renderer_value = Gtk.CellRendererText()
73
        self.column_value = Gtk.TreeViewColumn('Value', self.renderer_value, markup=2)
74
        self.column_value.set_visible(True)
75
        self.column_value.set_expand(True)
76
        self.column_value.set_clickable(False)
77
        self.column_value.set_sort_indicator(False)
78
        self.treeview.append_column(self.column_value)
79
 
80
        # TreeView common
81
        self.sorted_model = Gtk.TreeModelSort(model=self.model)
82
        self.sorted_model.set_sort_column_id(1, Gtk.SortType.ASCENDING)
83
        self.treeview.set_model(self.sorted_model)
84
 
85
        self.treeview.set_can_focus(False)
86
        self.treeview.set_headers_visible(True)
87
        self.treeview.set_enable_search(True)
88
        self.treeview.set_hover_selection(False)
89
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
90
        # ~ self.treeview.modify_font(Pango.FontDescription('Monospace 10'))
91
 
92
        # Selection
93
        self.selection = self.treeview.get_selection()
94
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)
95
 
96
        self.show_all()
97
 
98
 
99
    def update(self):
100
        from cgi import escape
101
        self.model.clear()
102
        self.settings = self.app.get_service('Settings')
103
        config = self.settings.load()
104
        sdict = {}
105
 
106
        # ENVIRONMENT NODE
107
        root = self.model.append(None, ['root', '<big><b>Environment</b></big>', ''])
108
 
109
 
110
        self.model.append(root, ['', '<span color="blue">App System Dir</span>', ROOT])
111
 
112
        pid = self.model.append(root, ['', '<b>APP</b>', ''])
113
        for key in APP:
114
            value = APP[key]
115
            if isinstance(value, list):
116
                value = ('\n'.join(value))
117
            value = escape(value)
118
            self.model.append(pid, ['APP-%s' % key, '<span color="blue">%s</span>' % key, value])
119
        pid = self.model.append(root, ['', '<b>LPATH</b>', ''])
120
        for key in LPATH:
121
            self.model.append(pid, ['LPATH-%s' % key, '<span color="blue">%s</span>' % key, LPATH[key]])
122
        pid = self.model.append(root, ['', '<b>GPATH</b>', ''])
123
        for key in GPATH:
124
            self.model.append(pid, ['GPATH-%s' % key, '<span color="blue">%s</span>' % key, GPATH[key]])
125
        pid = self.model.append(root, ['', '<b>FILE</b>', ''])
126
        for key in FILE:
127
            self.model.append(pid, ['FILE-%s' % key, '<span color="blue">%s</span>' % key, FILE[key]])
128
 
129
        # SETTINGS NODE
130
        root = self.model.append(None, ['root', '<big><b>Settings</b></big>', ''])
131
 
132
        def get_pid(key):
133
            try:
134
                pid = sdict[key]
135
            except:
136
                pid = self.model.append(root, [key, '<b>%s</b>' % key, ''])
137
                sdict[key] = pid
138
            return pid
139
 
140
        for skey in config.keys():
141
            pid = get_pid(skey)
142
            for key in config[skey]:
143
                value = str(config[skey][key])
144
                pkey = '%s-%s-%s' % (skey, key, value)
145
                self.model.append(pid, [pkey, '<span color="blue">%s</span>' % key, value])
146
 
147
 
148
        # WIDGETS NODE
149
        root = self.model.append(None, ['root', '<big><b>Runtime Objects</b></big>', ''])
150
 
151
        wdgdict = {}
152
        widgets = self.gui.get_widgets()
153
        for name in widgets:
154
            objname = escape(str(widgets[name]))
155
            pobj = gobject = objname[4:objname.find(' ')]
156
            gobj_left = gobject[:gobject.find('.')]
157
            gobj_right = gobject[gobject.find('.')+1:]
158
            pobj = gobject[:gobject.find('.')]
159
            try:
160
                ppid_left = wdgdict[gobj_left]
161
            except:
162
                ppid_left = self.model.append(root, [gobj_left, '<b>%s</b>' % gobj_left, ''])
163
                wdgdict[gobj_left] = ppid_left
164
 
165
 
166
            try:
167
                ppid_right = wdgdict[gobj_right]
168
            except:
169
                ppid_right = self.model.append(ppid_left, [gobj_right, '<b>%s</b>' % gobj_right, ''])
170
                wdgdict[gobj_right] = ppid_right
171
 
172
            self.model.append(ppid_right, [gobject, name, ''])