Subversion Repositories basico

Rev

Rev 374 | Rev 377 | Go to most recent revision | 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
7
# Description: setup.py tells you that the module/package you are about
8
# to install has been packaged and distributed with Distutils, which is
9
# the standard for distributing Python Modules.
10
#
11
# !!!
12
# Code borrowed from: https://github.com/SolydXK/solydxk-system/blob/origin/master/usr/lib/solydxk/system/splash.py
13
# !!!
14
#
15
"""
16
 
17
# ====================================================================
18
# Class to show a splash screen while application is loading
19
# ====================================================================
20
# Initiate the splash screen:
21
# from splash import Splash
22
# splash = Splash(title='Splash Screen')
23
#     Other arguments: width, height, font, font_weight, font_color, background_color, background_image.
24
#     width and height are ignored when background_image is used.
25
#     background_color is ignored when background_image is provided.
26
#     font can be the font name with size: font='Roboto Slab 18', or only the size in which it will use the system default font.
27
#     font_weight can be ultralight, light, normal, bold, ultrabold, heavy, or a numeric weight.
28
#
29
# Start the splash screen:
30
# splash.start()
31
#
32
# When done:
33
# splash.destroy()
34
#
35
# Note: you can hide and show the splash screen when needed:
36
# splash.hide()
37
# splash.show()
38
# ====================================================================
39
 
40
# Make sure the right Gtk version is loaded
41
import gi
42
gi.require_version('Gtk', '3.0')
43
 
44
# from gi.repository import Gtk, GdkPixbuf, GObject, Pango, Gdk
376 t00mlabs 45
# ~ import time
374 t00mlabs 46
from gi.repository import Gtk, Gdk
376 t00mlabs 47
# ~ from threading import Thread
374 t00mlabs 48
from os.path import exists
49
from basico.core.mod_log import get_logger
50
 
51
 
376 t00mlabs 52
class Splash():
374 t00mlabs 53
    def __init__(self, title, width=400, height=250, font=16, font_weight='normal', font_color='000000', background_color='ffffff', background_image=None):
376 t00mlabs 54
        # ~ Thread.__init__(self)
374 t00mlabs 55
        self.log = get_logger(__class__.__name__)
56
        self.title = title
57
        self.width = width
58
        self.height = height
59
        self.font = font
60
        self.font_weight = font_weight
61
        self.font_color = self.prep_hex_color(font_color)
62
        self.background_image = '' if background_image is None else background_image
63
 
64
        # Window settings
65
        self.window = Gtk.Window(Gtk.WindowType.POPUP)
66
        self.window.set_type_hint(Gdk.WindowTypeHint.SPLASHSCREEN)
67
        self.window.set_position(Gtk.WindowPosition.CENTER)
68
        self.window.set_title(self.title)
69
 
70
        # Create overlay with a background image
71
        overlay = Gtk.Overlay()
72
        self.window.add(overlay)
73
        if exists(self.background_image):
74
            # Window will adjust to image size automatically
75
            bg = Gtk.Image.new_from_file(self.background_image)
76
            overlay.add(bg)
77
        else:
78
            # Set window dimensions
79
            self.window.set_default_size(self.width, self.height)
80
            # Set background color
81
            self.window.override_background_color(Gtk.StateType.NORMAL,
82
                                                  self.hex_to_rgba(background_color, True))
83
 
84
        # Add box with label
85
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
86
        box.set_margin_top(self.height / 3)
87
        box.set_margin_left(20)
88
        box.set_margin_right(20)
89
        # Add the box to a new overlay in the existing overlay
90
        overlay.add_overlay(box)
91
        lbl_title = Gtk.Label()
92
        lbl_title.set_line_wrap(True)
93
        lbl_title.set_justify(Gtk.Justification.CENTER)
94
        # Markup format: https://developer.gnome.org/pango/stable/PangoMarkupFormat.html
95
        lbl_title.set_markup('<span font="{}" color="#{}" weight="{}">{}</span>'.format(self.font,
96
                                                                                        self.font_color,
97
                                                                                        self.font_weight,
98
                                                                                        self.title))
99
        box.pack_start(lbl_title, False, True, 0)
376 t00mlabs 100
        self.run()
374 t00mlabs 101
 
102
    def run(self):
103
        # Show the splash screen
104
        self.window.show_all()
105
        # Without this ugly one-liner, the window won't show
106
        while Gtk.events_pending(): Gtk.main_iteration()
107
        self.log.debug("Show the splash screen")
108
 
109
 
110
    def prep_hex_color(self, hex_color):
111
        hex_color = hex_color.strip('#')
112
        # Fill up with last character until length is 6 characters
113
        if len(hex_color) < 6:
114
            hex_color = hex_color.ljust(6, hex_color[-1])
115
        # Add alpha channel if it's not there
116
        hex_color = hex_color.ljust(8, 'f')
117
        return hex_color
118
 
119
    def hex_to_rgba(self, hex_color, as_gdk_rgba=False):
120
        hex_color = self.prep_hex_color(hex_color)
121
        # Create a list with rgba values from hex_color
122
        rgba = list(int(hex_color[i : i + 2], 16) for i in (0, 2 ,4, 6))
123
        if as_gdk_rgba:
124
            # Change values to float between 0 and 1 for Gdk.RGBA
125
            for i, val in enumerate(rgba):
126
                if val > 0:
127
                    rgba[i] = 1 / (255 / val)
128
            # Return the Gdk.RGBA object
129
            return Gdk.RGBA(rgba[0], rgba[1], rgba[2], rgba[3])
130
        return rgba
131
 
132
    def show(self):
133
        self.window.show()
134
        # ~ self.log.debug("Show the splash screen")
135
        # ~ time.sleep(3)
136
 
137
    def hide(self):
138
        self.window.hide()
139
        # ~ self.log.debug("Hide the splash screen")
140
 
141
    def destroy(self):
142
        while Gtk.events_pending(): Gtk.main_iteration()
143
        self.window.destroy()
144
        self.log.debug("Hide the splash screen")