Subversion Repositories basico

Rev

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

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: gui.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: GUI module
7
 
8
import os
9
import sys
10
import subprocess
11
 
12
from datetime import datetime
13
 
14
import gi
15
gi.require_version('Gtk', '3.0')
16
from gi.repository import Gtk
17
from gi.repository import Gdk
18
from gi.repository.GdkPixbuf import Pixbuf
19
from gi.repository import Pango
20
from gi.repository import GLib
21
from gi.repository import GObject
22
from gi.repository import Gio
23
 
24
from .service import Service
25
from .uiapp import UIApp
26
 
27
 
28
class GUI(Service):
29
    def initialize(self):
30
        '''
31
        Setup GUI Service
32
        '''
33
        # Invoke services
34
        self.sap = self.app.get_service('SAP')
35
 
36
        # Setup caches
37
        self.widgets = {} # Widget name : Object
38
        self.keys = {} # Key : Value; keys: doctype, property, values
39
 
40
        # Setup Glade builder
41
        self.setup_builder()
42
 
43
 
44
    def run(self):
45
        '''
46
        Let GUI service start
47
        '''
48
        self.log.debug("Setting up GUI")
49
        #~ Gdk.threads_init()
50
 
51
        GObject.threads_init()
52
        self.sap.run()
53
        self.ui = UIApp(self.app)
54
        self.ui.run()
55
 
56
 
57
    def quit(self, *args):
58
        '''
59
        GUI destroyed
60
        '''
61
        self.app.stop()
62
 
63
 
64
    def end(self):
65
        '''
66
        End GUI Service
67
        '''
68
        self.log.debug("GUI terminated")
69
        self.ui.quit()
70
 
71
 
72
    def setup_builder(self):
73
        '''
74
        Setup Gtk.Builder object
75
        '''
76
        DIR_UI = self.get_var('UI')
77
        self.builder = Gtk.Builder()
78
        self.builder.add_from_file(DIR_UI + 'basico.ui')
79
 
80
 
81
    def get_builder(self):
82
        '''
83
        Return Gtk.Builder object
84
        '''
85
        return self.builder()
86
 
87
 
88
    def swap_widget(self, parent, widget):
89
        '''
90
        Swap widget inside a container
91
        '''
92
        try:
93
            child = parent.get_children()[0]
94
            parent.remove(child)
95
            parent.add(widget)
96
            del (child)
97
        except:
98
            parent.add(widget)
99
        widget.show_all()
100
 
101
 
102
    def get_key(self, key):
103
        '''
104
        Return current value from var cache
105
        '''
106
        return self.keys[key]
107
 
108
 
109
    def set_key(self, key, value):
110
        '''
111
        Set current value for var cache
112
        '''
113
        self.keys[key] = value
114
 
115
 
116
    def add_widget(self, name, obj=None):
117
        '''
118
        Add widget to cache
119
        '''
120
        try:
121
            if obj is not None:
122
                self.widgets[name] = obj
123
            else:
124
                self.widgets[name] = self.builder.get_object(name)
125
            return self.widgets[name]
126
        except Exception as error:
127
            self.log.error (self.get_traceback())
128
            return None
129
 
130
 
131
    def get_widget(self, name):
132
        '''
133
        Return widget from cache
134
        '''
135
        try:
136
            return self.widgets[name]
137
        except KeyError:
138
            return self.add_widget(name)
139
 
140
 
141
    def get_widgets(self):
142
        return self.widgets
143
 
144
 
145
    def get_app(self):
146
        return self.ui
147
 
148
 
149
    def get_window(self):
150
        return self.ui.get_window()