summaryrefslogtreecommitdiff
path: root/plugins/python/welcome/tip.py
blob: 642619e98b6b6afceadb389f79d7a7e19493950d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python
# -*- coding: utf-8 -*-

from welcome.board import SmallBoard
from gi.repository import Gtk
import random

try:
    import cairo
    has_cairo = True
    import os
except:
    has_cairo = False


_tip_messages = [
    "Message 1",
    "Message 2",
    "Message 3",
    "Message 4",
    "Message 5",
    "Message 6"
]

assert(len(_tip_messages) > 0)


class TipOfTheDay(SmallBoard):
    """Présente une série d'astuces du jour."""


    def __init__(self):
        """Construit le panneau avec son contenu."""

        super(TipOfTheDay, self).__init__()

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        self.add(box)

        # Barre de contrôle

        toolbar = Gtk.Box(spacing=10)
        toolbar.props.margin = 20
        box.pack_end(toolbar, False, True, 0)

        btn = Gtk.Button('Previous')
        btn.connect('clicked', self.show_previous_message)
        toolbar.pack_start(btn, False, True, 8)

        btn = Gtk.Button('Next')
        btn.connect('clicked', self.show_next_message)
        toolbar.pack_start(btn, False, True, 8)

        # Contenu de l'astuce courante

        desc = Gtk.Label('', use_markup=True, wrap=True, xalign=0)
        self._message = desc
        desc.set_justify(Gtk.Justification.LEFT)
        box.pack_end(desc, False, True, 0)

        # Titre

        desc = Gtk.Label("<b>Did you know?</b>", use_markup=True, wrap=True, xalign=0)
        desc.set_justify(Gtk.Justification.LEFT)
        box.pack_end(desc, False, True, 0)

        # Image de fond, si possible

        if has_cairo:

            os.chdir(os.path.dirname(__file__))

            self._img = cairo.ImageSurface.create_from_png('tipoftheday.png')

            self.connect_after('draw', self.draw_background)

        # Sélection du message courant

        self._indexes = [i for i in range(len(_tip_messages))]
        random.shuffle(self._indexes)

        self._index = 0

        self.show_message()


    def draw_background(self, widget, cr):
        """Dessine une image de fond pour les conseils."""

        img_width = self._img.get_width()
        img_height = self._img.get_height()

        alloc = self.get_allocation()
        target_height = alloc.height * 0.7

        if img_height > target_height:

            scale = float(target_height) / float(img_height)

            cr.translate(alloc.width - img_width * scale - 10, 10)
            cr.scale(scale, scale)

        else:

            cr.translate(alloc.width - img_width - 10, 10)

        cr.set_source_surface(self._img)
        cr.paint_with_alpha(0.6)


    def show_message(self):
        """Affiche un conseil du jour donné."""

        selected = self._indexes[self._index]

        msg = _tip_messages[selected]

        self._message.set_markup(msg)


    def show_previous_message(self, button):
        """Affiche un conseil du jour précédent."""

        if self._index == 0:
            self._index = len(_tip_messages) - 1
        else:
            self._index = self._index - 1

        self.show_message()


    def show_next_message(self, button):
        """Affiche un conseil du jour suivant."""

        self._index = self._index + 1

        if self._index == len(_tip_messages):
            self._index = 0

        self.show_message()


    def get_location(self):
        """Fournit la localisation souhaitée pour la planche."""

        return [ 2, 1, 2, 2 ]