Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/addons/changecolor.py
blob: 27ae284cbd049f36317aa6c065209d5045fad782 (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
# Copyright (C) 2009, Tutorius.org
#
# 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 time

import gobject

import gtk, gtk.gdk

from sugar.tutorius.actions import Action
from sugar.tutorius.properties import TUAMProperty
from sugar.tutorius.gtkutils import find_widget

from sugar import profile

# for easy profile access
xo_line_color = profile.get_color().get_stroke_color()
xo_fill_color = profile.get_color().get_fill_color()

class ChangeColor(Action):
    """
    ChangeColorEvent
    """
    widaddr = TUAMProperty("0")

    def __init__(self, widaddr=None):
        """Constructor - Change a widget color
        @param widaddr: the widget for which you want to change the color (UAM)
        """
        Action.__init__(self)

        if widaddr: self.widaddr = widaddr

        self._old_color = None
        self._new_color = None
        self.tmp_color = None

        self.wid = None        

        self._handler_id = None
    
    def do(self, **kwargs):
        """
        do.
        Change the color of the widaddr widget with the chosen color
        """
        
        if not "activity" in kwargs:
            raise TypeError("activity argument is Mandatory")
        
        # get widget instance
        self.wid = find_widget(kwargs["activity"], self.widaddr)
    
        if not self.wid:
            # TODO: error
            raise TypeError("widget not found")
        
        # we have to get the initial color in the sugar rc theme
        current_style = self.wid.rc_get_style()
        self._old_color = current_style.bg[gtk.STATE_NORMAL]

        # define new color
        self._new_color = gtk.gdk.color_parse(xo_fill_color) 

        # set change color timeout (flash)
        self.timeout = 500
        self.tmp_color = self._old_color
        self._handler_id = gobject.timeout_add(self.timeout, self._timeout_cb)

    def undo(self):
        """
        Go back to the original color
        """

        if self._handler_id:
            try:
                #remove the timer
                gobject.source_remove(self._handler_id)
            except:
                pass

        # modify bg color (go back to original color)
        self.wid.modify_bg(gtk.STATE_NORMAL, self._old_color)

    def _timeout_cb(self):
        """
        _timeout_cb triggers the eventfilter callback.

        It is necessary because gobject timers only stop if the callback they
        trigger returns False
        """

        if self.tmp_color == self._old_color:
            self.tmp_color = self._new_color
        else:
            self.tmp_color = self._old_color

        # modify bg color
        self.wid.modify_bg(gtk.STATE_NORMAL, self.tmp_color)

        if self._handler_id:
            try:
                #remove the timer
                gobject.source_remove(self._handler_id)
            except:
                pass

        # TODO: reset        
        self._handler_id = gobject.timeout_add(self.timeout, self._timeout_cb)
        
__action__ = {
    "name" : "ChangeColor",
    "display_name" : "Change widget color",
    "icon" : "message-bubble",
    "class" : ChangeColor,
    "mandatory_props" : ["widaddr"]
}