Subversion Repositories basico

Rev

Rev 369 | Rev 376 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
340 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
"""
4
# File: srv_gui.py
5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: GUI service
8
"""
9
 
10
import gi
11
gi.require_version('Gtk', '3.0')
12
from gi.repository import Gtk
13
from gi.repository import GLib
14
from gi.repository import GObject
15
from gi.repository import Gio
16
from basico.core.mod_srv import Service
17
from basico.core.mod_win import GtkAppWindow
18
from basico.core.mod_log import get_logger
374 t00mlabs 19
# ~ from basico.widgets.wdg_splash import Splash
340 t00mlabs 20
 
21
 
22
class UIApp(Gtk.Application):
23
    """
24
    Missing class docstring (missing-docstring)
25
    """
26
    uiapp = None
27
    window = None
28
 
29
    def __init__(self, *args, **kwargs):
30
        """
31
        Missing method docstring (missing-docstring)
32
        """
33
        super().__init__(application_id="net.t00mlabs.basico", flags=Gio.ApplicationFlags.FLAGS_NONE)
34
        GLib.set_application_name("Basico")
35
        GLib.set_prgname('basico')
36
        self.log = get_logger(__class__.__name__)
37
        self.app = args[0]
38
        self.get_services()
39
 
40
 
41
    def do_activate(self):
42
        # DOC: https://wiki.gnome.org/HowDoI/GtkApplication
43
        # https://stackoverflow.com/questions/41883527/uniqueness-of-gtk-application-without-creating-any-window
44
        if not hasattr(self, "my_app_settings"):
45
            self.hold()
46
            self.my_app_settings = "Primary application instance."
47
            self.window = self.srvgui.add_widget('gtk_app_window_main', GtkAppWindow(self))
48
            self.window.connect("delete-event", self.srvgui.quit)
374 t00mlabs 49
            # ~ db = self.app.get_service('DB')
50
            # ~ gui = self.app.get_service('GUI')
51
            # ~ db.load_notes()
52
            # ~ visor_sapnotes = gui.get_widget('visor_sapnotes')
53
            # ~ visor_sapnotes.populate()
340 t00mlabs 54
            self.log.debug("New Basico instance created")
55
            self.srvuif.statusbar_msg("Welcome to Basico", True)
56
        else:
57
            self.log.debug("Basico is already running!")
374 t00mlabs 58
        splash = self.app.get_splash()
59
        splash.hide()
340 t00mlabs 60
 
61
    def get_services(self):
62
        """
63
        Missing method docstring (missing-docstring)
64
        """
65
        self.srvgui = self.app.get_service('GUI')
66
        self.srvclb = self.app.get_service('Callbacks')
67
        self.srvuif = self.app.get_service('UIF')
68
 
69
 
70
    def get_window(self):
71
        """
72
        Missing method docstring (missing-docstring)
73
        """
74
        return self.window
75
 
76
 
77
    def get_controller(self):
78
        """
79
        Missing method docstring (missing-docstring)
80
        """
81
        return self.app
82
 
83
 
84
class GUI(Service):
85
    """
86
    Missing class docstring (missing-docstring)
87
    """
88
    uiapp = None
89
    srvsap = None
90
    widgets = {} # Widget name : Object
91
    keys = {} # Key : Value; keys: doctype, property, values
369 t00mlabs 92
    signals = {} # Signals dictionary for all widgets (widget::signal)
340 t00mlabs 93
 
94
    def initialize(self):
95
        """
96
        Setup GUI Service
97
        """
374 t00mlabs 98
        pass
99
        # ~ self.srvsap = self.get_service('SAP')
340 t00mlabs 100
 
101
 
102
    def run(self):
103
        """
104
        Let GUI service start
105
        """
374 t00mlabs 106
        # ~ GObject.threads_init()
340 t00mlabs 107
        self.uiapp = UIApp(self.app)
108
        self.log.debug("Setting up GUI")
361 t00mlabs 109
        window = self.get_window()
374 t00mlabs 110
        # ~ splash = self.app.get_splash()
111
        # ~ splash.start()
340 t00mlabs 112
        self.uiapp.run()
113
 
114
 
115
    def quit(self, window, event):
116
        """
117
        GUI destroyed
118
        """
119
        self.app.stop()
120
 
121
 
122
    def end(self):
123
        """
124
        End GUI Service
125
        """
126
        self.uiapp.quit()
127
        self.log.debug("GUI terminated")
128
 
129
 
130
    def swap_widget(self, parent, widget):
131
        """
132
        Swap widget inside a container
133
        """
134
        try:
135
            child = parent.get_children()[0]
136
            parent.remove(child)
137
            parent.add(widget)
138
        except IndexError:
139
            parent.add(widget)
140
        except Exception:
141
            self.print_traceback()
142
            raise
143
 
144
        widget.show_all()
145
 
146
 
147
    def get_keys(self):
148
        return self.keys
149
 
150
 
369 t00mlabs 151
    def get_keys_starting_with(self, name):
152
        matches = []
153
        for key in self.keys:
154
            if key.startswith(name):
155
                matches.append(key)
156
 
157
        return matches
158
 
159
 
340 t00mlabs 160
    def get_key_value(self, key):
161
        """
162
        Return current value from var cache
163
        """
164
        return self.keys[key]
165
 
166
 
167
    def set_key_value(self, key, value):
168
        """
169
        Set current value for var cache
170
        """
171
        self.keys[key] = value
172
 
173
 
369 t00mlabs 174
    def add_signal(self, widget, signal, value):
175
        """
176
        Add signal to signals cache
177
        """
178
        signal_name = "%s::%s" % (widget, signal)
179
        self.signals[signal_name] = value
180
 
181
 
182
    def get_signal(self, widget, signal):
183
        """
184
        Return signal from cache
185
        """
186
        signal_name = "%s::%s" % (widget, signal)
187
        try:
188
            return self.signals[signal_name]
189
        except KeyError:
190
            return None
191
 
374 t00mlabs 192
 
340 t00mlabs 193
    def add_widget(self, name, obj=None):
194
        """
195
        Add widget to cache
196
        """
197
        if obj is not None:
198
            self.widgets[name] = obj
199
 
200
        return obj
201
 
202
 
203
    def get_widget(self, name):
204
        """
205
        Return widget from cache
206
        """
207
        try:
208
            return self.widgets[name]
209
        except KeyError:
210
            return None
211
 
212
 
213
    def get_widgets(self):
214
        """
215
        Missing method docstring (missing-docstring)
216
        """
217
        return self.widgets
218
 
219
 
220
    def get_app(self):
221
        """
222
        Missing method docstring (missing-docstring)
223
        """
224
        return self.uiapp
225
 
226
 
227
    def get_window(self):
228
        """
229
        Missing method docstring (missing-docstring)
230
        """
231
        return self.uiapp.get_window()