Subversion Repositories basico

Rev

Rev 407 | 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
 
65
 
340 t00mlabs 66
    def get_services(self):
67
        """
68
        Missing method docstring (missing-docstring)
69
        """
70
        self.srvgui = self.app.get_service('GUI')
71
        self.srvclb = self.app.get_service('Callbacks')
72
        self.srvuif = self.app.get_service('UIF')
73
 
74
 
75
    def get_window(self):
76
        """
77
        Missing method docstring (missing-docstring)
78
        """
79
        return self.window
80
 
81
 
82
    def get_controller(self):
83
        """
84
        Missing method docstring (missing-docstring)
85
        """
86
        return self.app
87
 
88
 
89
class GUI(Service):
90
    """
91
    Missing class docstring (missing-docstring)
92
    """
93
    uiapp = None
94
    srvsap = None
95
    widgets = {} # Widget name : Object
96
    keys = {} # Key : Value; keys: doctype, property, values
369 t00mlabs 97
    signals = {} # Signals dictionary for all widgets (widget::signal)
340 t00mlabs 98
 
99
    def initialize(self):
100
        """
101
        Setup GUI Service
102
        """
374 t00mlabs 103
        pass
104
        # ~ self.srvsap = self.get_service('SAP')
340 t00mlabs 105
 
106
 
107
    def run(self):
108
        """
109
        Let GUI service start
110
        """
376 t00mlabs 111
        GObject.threads_init()
340 t00mlabs 112
        self.uiapp = UIApp(self.app)
113
        self.log.debug("Setting up GUI")
387 t00mlabs 114
        # ~ splash = self.app.get_splash()
115
        # ~ splash.show()
340 t00mlabs 116
        self.uiapp.run()
117
 
118
 
119
    def quit(self, window, event):
120
        """
121
        GUI destroyed
122
        """
123
        self.app.stop()
124
 
125
 
126
    def end(self):
127
        """
128
        End GUI Service
129
        """
130
        self.uiapp.quit()
131
        self.log.debug("GUI terminated")
132
 
133
 
134
    def swap_widget(self, parent, widget):
135
        """
136
        Swap widget inside a container
137
        """
138
        try:
139
            child = parent.get_children()[0]
140
            parent.remove(child)
141
            parent.add(widget)
142
        except IndexError:
143
            parent.add(widget)
144
        except Exception:
145
            self.print_traceback()
146
            raise
147
 
148
        widget.show_all()
149
 
150
 
151
    def get_keys(self):
152
        return self.keys
153
 
154
 
369 t00mlabs 155
    def get_keys_starting_with(self, name):
156
        matches = []
157
        for key in self.keys:
158
            if key.startswith(name):
159
                matches.append(key)
160
 
161
        return matches
162
 
163
 
340 t00mlabs 164
    def get_key_value(self, key):
165
        """
166
        Return current value from var cache
167
        """
168
        return self.keys[key]
169
 
170
 
171
    def set_key_value(self, key, value):
172
        """
173
        Set current value for var cache
174
        """
175
        self.keys[key] = value
176
 
177
 
369 t00mlabs 178
    def add_signal(self, widget, signal, value):
179
        """
180
        Add signal to signals cache
181
        """
182
        signal_name = "%s::%s" % (widget, signal)
183
        self.signals[signal_name] = value
184
 
185
 
186
    def get_signal(self, widget, signal):
187
        """
188
        Return signal from cache
189
        """
190
        signal_name = "%s::%s" % (widget, signal)
191
        try:
192
            return self.signals[signal_name]
193
        except KeyError:
194
            return None
195
 
374 t00mlabs 196
 
340 t00mlabs 197
    def add_widget(self, name, obj=None):
198
        """
199
        Add widget to cache
200
        """
201
        if obj is not None:
202
            self.widgets[name] = obj
203
 
204
        return obj
205
 
206
 
207
    def get_widget(self, name):
208
        """
209
        Return widget from cache
210
        """
211
        try:
212
            return self.widgets[name]
405 t00mlabs 213
        except KeyError as warning:
214
            self.log.warning(warning)
215
            # ~ self.log.error(self.get_traceback())
216
            raise
340 t00mlabs 217
            return None
218
 
219
 
220
    def get_widgets(self):
221
        """
222
        Missing method docstring (missing-docstring)
223
        """
224
        return self.widgets
225
 
226
 
227
    def get_app(self):
228
        """
229
        Missing method docstring (missing-docstring)
230
        """
231
        return self.uiapp
232
 
233
 
234
    def get_window(self):
235
        """
236
        Missing method docstring (missing-docstring)
237
        """
238
        return self.uiapp.get_window()