Subversion Repositories basico

Rev

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

Rev Author Line No. Line
374 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
"""
4
# File: setup.py
5
# Author: Tomás Vírseda
6
# License: GPL v3
377 t00mlabs 7
# Description: splash screen for Basico
374 t00mlabs 8
#
9
# !!!
377 t00mlabs 10
# Code borrowed and modified from: https://github.com/SolydXK/solydxk-system/blob/origin/master/usr/lib/solydxk/system/splash.py
374 t00mlabs 11
# !!!
12
#
13
"""
14
 
15
# Make sure the right Gtk version is loaded
16
import gi
17
gi.require_version('Gtk', '3.0')
18
from gi.repository import Gtk, Gdk
19
from os.path import exists
20
from basico.core.mod_log import get_logger
21
 
22
 
376 t00mlabs 23
class Splash():
377 t00mlabs 24
    def __init__(self, title, width=400, height=250, font=36, font_weight='normal', font_color='000000', background_color='ffffff', background_image=None, app=None):
374 t00mlabs 25
        self.log = get_logger(__class__.__name__)
26
        self.title = title
27
        self.width = width
28
        self.height = height
29
        self.font = font
30
        self.font_weight = font_weight
31
        self.font_color = self.prep_hex_color(font_color)
32
        self.background_image = '' if background_image is None else background_image
377 t00mlabs 33
        self.app = app
374 t00mlabs 34
 
35
        # Window settings
36
        self.window = Gtk.Window(Gtk.WindowType.POPUP)
37
        self.window.set_type_hint(Gdk.WindowTypeHint.SPLASHSCREEN)
38
        self.window.set_position(Gtk.WindowPosition.CENTER)
39
        self.window.set_title(self.title)
40
 
41
        # Create overlay with a background image
42
        overlay = Gtk.Overlay()
43
        self.window.add(overlay)
44
        if exists(self.background_image):
45
            # Window will adjust to image size automatically
46
            bg = Gtk.Image.new_from_file(self.background_image)
47
            overlay.add(bg)
48
        else:
49
            # Set window dimensions
50
            self.window.set_default_size(self.width, self.height)
51
            # Set background color
52
            self.window.override_background_color(Gtk.StateType.NORMAL,
53
                                                  self.hex_to_rgba(background_color, True))
54
 
55
        # Add box with label
56
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
57
        box.set_margin_top(self.height / 3)
58
        box.set_margin_left(20)
59
        box.set_margin_right(20)
377 t00mlabs 60
 
374 t00mlabs 61
        # Add the box to a new overlay in the existing overlay
62
        overlay.add_overlay(box)
63
        lbl_title = Gtk.Label()
64
        lbl_title.set_line_wrap(True)
65
        lbl_title.set_justify(Gtk.Justification.CENTER)
66
        # Markup format: https://developer.gnome.org/pango/stable/PangoMarkupFormat.html
67
        lbl_title.set_markup('<span font="{}" color="#{}" weight="{}">{}</span>'.format(self.font,
68
                                                                                        self.font_color,
69
                                                                                        self.font_weight,
70
                                                                                        self.title))
71
        box.pack_start(lbl_title, False, True, 0)
377 t00mlabs 72
 
376 t00mlabs 73
        self.run()
374 t00mlabs 74
 
377 t00mlabs 75
 
374 t00mlabs 76
    def run(self):
77
        # Show the splash screen
78
        self.window.show_all()
79
        # Without this ugly one-liner, the window won't show
377 t00mlabs 80
        while Gtk.events_pending():
81
            Gtk.main_iteration()
387 t00mlabs 82
        # ~ self.log.debug("Show the splash screen")
374 t00mlabs 83
 
84
 
85
    def prep_hex_color(self, hex_color):
86
        hex_color = hex_color.strip('#')
87
        # Fill up with last character until length is 6 characters
88
        if len(hex_color) < 6:
89
            hex_color = hex_color.ljust(6, hex_color[-1])
90
        # Add alpha channel if it's not there
91
        hex_color = hex_color.ljust(8, 'f')
92
        return hex_color
93
 
377 t00mlabs 94
 
374 t00mlabs 95
    def hex_to_rgba(self, hex_color, as_gdk_rgba=False):
96
        hex_color = self.prep_hex_color(hex_color)
97
        # Create a list with rgba values from hex_color
98
        rgba = list(int(hex_color[i : i + 2], 16) for i in (0, 2 ,4, 6))
99
        if as_gdk_rgba:
100
            # Change values to float between 0 and 1 for Gdk.RGBA
101
            for i, val in enumerate(rgba):
102
                if val > 0:
103
                    rgba[i] = 1 / (255 / val)
104
            # Return the Gdk.RGBA object
105
            return Gdk.RGBA(rgba[0], rgba[1], rgba[2], rgba[3])
106
        return rgba
107
 
377 t00mlabs 108
 
374 t00mlabs 109
    def show(self):
110
        self.window.show()
377 t00mlabs 111
        self.log.debug("Show the splash screen")
374 t00mlabs 112
 
377 t00mlabs 113
 
374 t00mlabs 114
    def hide(self):
115
        self.window.hide()
387 t00mlabs 116
        # ~ self.log.debug("Hide the splash screen")
374 t00mlabs 117
 
377 t00mlabs 118
 
374 t00mlabs 119
    def destroy(self):
120
        while Gtk.events_pending(): Gtk.main_iteration()
121
        self.window.destroy()
387 t00mlabs 122
        # ~ self.log.debug("Hide the splash screen")