Subversion Repositories basico

Rev

Rev 141 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: gui.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: GUI module

import os
import sys
import subprocess

from datetime import datetime

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository.GdkPixbuf import Pixbuf
from gi.repository import Pango
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gio

from .service import Service
from .uiapp import UIApp
from .env import STATS


class GUI(Service):
    def initialize(self):
        '''
        Setup GUI Service
        '''

        # Invoke services
        self.sap = self.app.get_service('SAP')

        # Setup caches
        self.widgets = {} # Widget name : Object
        self.keys = {} # Key : Value; keys: doctype, property, values


    def run(self):
        '''
        Let GUI service start
        '''

        self.debug("Setting up GUI")
        GObject.threads_init()
        self.sap.run()
        self.ui = UIApp(self.app)
        DEBUG_END_TIME = datetime.now()
        STATS['BASICO_STARTUP_END'] = datetime.now()
        self.debug("Startup duration: %s" % (STATS['BASICO_STARTUP_END'] - STATS['BASICO_STARTUP_START']))
        self.ui.run()


    def quit(self, *args):
        '''
        GUI destroyed
        '''

        self.app.stop()


    def end(self):
        '''
        End GUI Service
        '''

        self.debug("GUI terminated")
        self.ui.quit()


    def swap_widget(self, parent, widget):
        '''
        Swap widget inside a container
        '''

        try:
            child = parent.get_children()[0]
            parent.remove(child)
            del (child)
            parent.add(widget)
        except IndexError:
            parent.add(widget)
        except Exception as error:
            self.print_traceback()
            raise

        widget.show_all()


    def get_key(self, key):
        '''
        Return current value from var cache
        '''

        return self.keys[key]


    def set_key(self, key, value):
        '''
        Set current value for var cache
        '''

        self.keys[key] = value


    def add_widget(self, name, obj=None):
        '''
        Add widget to cache
        '''

        try:
            if obj is not None:
                self.widgets[name] = obj
                return self.widgets[name]
            else:
                return None
        except:
            return None


    def get_widget(self, name):
        '''
        Return widget from cache
        '''

        try:
            return self.widgets[name]
        except KeyError:
            return self.add_widget(name)


    def get_widgets(self):
        return self.widgets


    def get_app(self):
        return self.ui


    def get_window(self):
        return self.ui.get_window()