~pyroom-dev/pyroom/0.3

« back to all changes in this revision

Viewing changes to status_label.py

  • Committer: Florian Heinle
  • Date: 2008-07-12 14:49:16 UTC
  • Revision ID: florian-launchpad@planet-tiax.de-20080712144916-bz6i08bmux3zrfj0
status_label is part of the gui, it belongs to gui.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# -----------------------------------------------------------------------------
3
 
# PyRoom - A clone of WriteRoom
4
 
# Copyright (c) 2007 Nicolas P. Rougier & NoWhereMan
5
 
# Copyright (c) 2008 The Pyroom Team - See AUTHORS file for more information
6
 
#
7
 
# This program is free software: you can redistribute it and/or modify it under
8
 
# the terms of the GNU General Public License as published by the Free Software
9
 
# Foundation, either version 3 of the License, or (at your option) any later
10
 
# version.
11
 
#
12
 
# This program is distributed in the hope that it will be useful, but WITHOUT
13
 
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
 
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15
 
# details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License along with
18
 
# this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
# -----------------------------------------------------------------------------
20
 
 
21
 
"""
22
 
status bar
23
 
 
24
 
provides a fading status bar that displays information to the user
25
 
"""
26
 
 
27
 
 
28
 
import gobject
29
 
import gtk
30
 
 
31
 
 
32
 
class FadeLabel(gtk.Label):
33
 
    """ GTK Label with timed fade out effect """
34
 
 
35
 
    active_duration = 3000  # Fade start after this time
36
 
    fade_duration = 1500.0  # Fade duration
37
 
 
38
 
    def __init__(self, message='', active_color=None, inactive_color=None):
39
 
        gtk.Label.__init__(self, message)
40
 
        if not active_color:
41
 
            active_color = '#ffffff'
42
 
        self.active_color = active_color
43
 
        if not inactive_color:
44
 
            inactive_color = '#000000'
45
 
        self.fade_level = 0
46
 
        self.inactive_color = inactive_color
47
 
        self.idle = 0
48
 
 
49
 
    def set_text(self, message, duration=None):
50
 
        """change text that is displayed
51
 
        @param message: message to display
52
 
        @param duration: duration in miliseconds"""
53
 
        if not duration:
54
 
            duration = self.active_duration
55
 
        self.modify_fg(gtk.STATE_NORMAL,
56
 
                       gtk.gdk.color_parse(self.active_color))
57
 
        gtk.Label.set_text(self, message)
58
 
        if self.idle:
59
 
            gobject.source_remove(self.idle)
60
 
        self.idle = gobject.timeout_add(duration, self.fade_start)
61
 
 
62
 
    def fade_start(self):
63
 
        """start fading timer"""
64
 
        self.fade_level = 1.0
65
 
        if self.idle:
66
 
            gobject.source_remove(self.idle)
67
 
        self.idle = gobject.timeout_add(25, self.fade_out)
68
 
 
69
 
    def fade_out(self):
70
 
        """now fade out"""
71
 
        color = gtk.gdk.color_parse(self.inactive_color)
72
 
        (red1, green1, blue1) = (color.red, color.green, color.blue)
73
 
        color = gtk.gdk.color_parse(self.active_color)
74
 
        (red2, green2, blue2) = (color.red, color.green, color.blue)
75
 
        red = red1 + int(self.fade_level * abs(red1 - red2))
76
 
        green = green1 + int(self.fade_level * abs(green1 - green2))
77
 
        blue = blue1 + int(self.fade_level * abs(blue1 - blue2))
78
 
        self.modify_fg(gtk.STATE_NORMAL, gtk.gdk.Color(red, green, blue))
79
 
        self.fade_level -= 1.0 / (self.fade_duration / 25)
80
 
        if self.fade_level > 0:
81
 
            return True
82
 
        self.idle = 0
83
 
        return False