Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/view/pulsingicon.py
blob: 9e7b3d90d9105736d45bb77c76c0eb9068856a0e (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
# Copyright (C) 2006-2007 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gobject

from sugar.graphics.icon import CanvasIcon

class PulsingIcon(CanvasIcon):
    __gproperties__ = {
        'paused'     : (bool, None, None, False,
                        gobject.PARAM_READWRITE),
        'colors'     : (object, None, None,
                        gobject.PARAM_READWRITE),
        'pulse-time' : (float, None, None,
                        0.0, 500.0, 0.0,
                        gobject.PARAM_READWRITE),
    }

    def __init__(self, **kwargs):
        self._paused = False
        self._pulse_time = 0.0
        self._colors = None
        self._pulse_sid = 0
        self._pos = 0

        CanvasIcon.__init__(self, **kwargs)

    def do_set_property(self, pspec, value):
        CanvasIcon.do_set_property(self, pspec, value)

        if pspec.name == 'pulse-time':
            self._pulse_time = value
            self._stop()
            if not self._paused and self._pulse_time > 0.0:
                self._start()
        elif pspec.name == 'colors':
            self._colors = value
            self._pos = 0
            self._update_colors()
        elif pspec.name == 'paused':
            self._paused = value
            if not self._paused and self._pulse_time > 0.0:
                self._start()
            else:
                self._stop()

    def do_get_property(self, pspec):
        CanvasIcon.do_get_property(self, pspec)

        if pspec.name == 'pulse-time':
            return self._pulse_time
        elif pspec.name == 'colors':
            return self._colors

    def _update_colors(self):
        self.props.stroke_color = self._colors[self._pos][0]
        self.props.fill_color = self._colors[self._pos][1]

    def _pulse_timeout(self):
        if self._colors:
            self._update_colors()

        self._pos += 1
        if self._pos == len(self._colors):
            self._pos = 0

        return True

    def _start(self):
        if self._pulse_sid == 0:
            self._pulse_sid = gobject.timeout_add(
                    int(self._pulse_time * 1000), self._pulse_timeout)

    def _stop(self):
        if self._pulse_sid:
            gobject.source_remove(self._pulse_sid)
            self._pulse_sid = 0