diff options
Diffstat (limited to 'addons/changecolor.py')
-rw-r--r-- | addons/changecolor.py | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/addons/changecolor.py b/addons/changecolor.py new file mode 100644 index 0000000..cf3431c --- /dev/null +++ b/addons/changecolor.py @@ -0,0 +1,126 @@ +# 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, DragWrapper +from sugar.tutorius.properties import TStringProperty, TUAMProperty, TColorProperty, TIntProperty +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 widaddred widget with the chosen color + """ + + if not "activity" in kwargs: + logging.debug("ACTIVITY NOT IN KWARGS **********") + + # get widget instance + self.wid = find_widget(kwargs["activity"], self.widaddr) + + # 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: + #XXX What happens if this was already triggered? + #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) + self.wid.modify_bg(gtk.STATE_PRELIGHT, self._old_color) + self.wid.modify_bg(gtk.STATE_ACTIVE, self._old_color) + self.wid.modify_bg(gtk.STATE_INSENSITIVE, 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) + self.wid.modify_bg(gtk.STATE_PRELIGHT, self.tmp_color) + self.wid.modify_bg(gtk.STATE_ACTIVE, self.tmp_color) + self.wid.modify_bg(gtk.STATE_INSENSITIVE, self.tmp_color) + + 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"] +} + |