Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/addons/changecolor.py
blob: eac891a2a0af4eeb12d9cf90e8b745d44884af7a (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
# 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
    """
    # widget address property
    widaddr = TUAMProperty("0")

    # set timeout
    timeout = 500
    
    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.init_style = None
        self._new_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")
        activity = kwargs["activity"]

        if not "probe" in kwargs:
            raise TypeError("probe argument is Mandatory")
        probe = kwargs["probe"]
        
        # get widget instance
        self.wid = probe.find_widget(activity, self.widaddr, ignore_errors=False)
    
        if not self.wid:
            raise NameError("widget not found")
        
        # we have to get the initial color in the sugar rc theme
        self.init_style = self.wid.rc_get_style()

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

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

    def undo(self):
        """
        Remove timer and 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.init_style.bg[gtk.STATE_NORMAL])
        self.wid.modify_bg(gtk.STATE_PRELIGHT, self.init_style.bg[gtk.STATE_PRELIGHT])
        self.wid.modify_bg(gtk.STATE_ACTIVE, self.init_style.bg[gtk.STATE_ACTIVE])
        self.wid.modify_bg(gtk.STATE_INSENSITIVE, self.init_style.bg[gtk.STATE_INSENSITIVE])

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

        if self.wid.rc_get_style().bg[gtk.STATE_NORMAL] == self._new_color:
            # modify bg color (go back to original color)
            self.wid.modify_bg(gtk.STATE_NORMAL, self.init_style.bg[gtk.STATE_NORMAL])
            self.wid.modify_bg(gtk.STATE_PRELIGHT, self.init_style.bg[gtk.STATE_PRELIGHT])
            self.wid.modify_bg(gtk.STATE_ACTIVE, self.init_style.bg[gtk.STATE_ACTIVE])
            self.wid.modify_bg(gtk.STATE_INSENSITIVE, self.init_style.bg[gtk.STATE_INSENSITIVE])
        else:
            # modify bg color (to new color)
            self.wid.modify_bg(gtk.STATE_NORMAL, self._new_color)
            self.wid.modify_bg(gtk.STATE_PRELIGHT, self._new_color)
            self.wid.modify_bg(gtk.STATE_ACTIVE, self._new_color)
            self.wid.modify_bg(gtk.STATE_INSENSITIVE, self._new_color)
        
        return True

__action__ = {
    "name" : "ChangeColor",
    "display_name" : "Change widget color",
    "icon" : "message-bubble",
    "class" : ChangeColor,
    "mandatory_props" : ["widaddr"]
}