Subversion Repositories basico

Rev

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