Well, i made a nice ProgressBar.py, and when it was done i did a cvs update 
and saw dishi already made one recently. I feel stupid. Anyway, dishi's one 
is a bit better and more flexible, but you never now if someone could somehow 
make use of my version, if not it can still serve as an example why running 
cvs update very day is a very good thing to do.

Mvg
den_RDC

PS. I got a ProgressBox too, but that one isn't useless yet as it includes a 
label, and ddishi's one doesn't. Maybe i'll update dishi's code that's 
already in cvs.
#!/usr/bin/env python
#-----------------------------------------------------------------------
# ProgressBar - A progressbar widget for freevo by den_RDC
#-----------------------------------------------------------------------
# $Id: ProgressBar.py,v 1.1 2003/09/08 02:00:00 den_RDC Exp $
# ProgressBar with alpha channel'd progress area.
#       
#-----------------------------------------------------------------------
# $Log: PopupBox.py,v $
# Revision 1.1 2003/09/08 02:00:00 den_RDC
# Initial release
# 
#
#-----------------------------------------------------------------------
#
# Freevo - A Home Theater PC framework
#
# Copyright (C) 2002 Krister Lagerstrom, et al.
#
# 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 MER-
# CHANTABILITY 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.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------


import pygame

from GUIObject import Align, GUIObject
from Color     import Color
from types     import StringType

DEBUG = 0

FALSE = 0
TRUE = 1

class ProgressBar(GUIObject):
    """
    Draws a Progressbar at the given coordinates...
    Use get_progress and set_progress to set an amount of progress
    progress is an integer between 0 and 100 (boundaries included)
    """
    
    __author__   = "den_RDC <[EMAIL PROTECTED]>"
    __revision__ = "$Revision: 1.1 $"

    def __init__(self, parent=None, h_align=None, v_align=None, 
                 width=480, height=60, bar_color=None):
        """
        Draw a progressbar - adds itself to a parent, bar_color is 
        the progressbar color, and alpha aware.. a border is drawed
        if the parent has one
        (parent, h_align, v_align, width, height, bar_color) 
        """
        
        #initialize gui object
        GUIObject.__init__(self, width=width, height=height)
        
        #set alignment
        if h_align:
            self.h_align  = h_align
        else:
            self.h_align  = Align.CENTER

        if v_align:
            self.v_align  = v_align
        else:
            self.v_align  = Align.BOTTOM
        
        #add miself to parent
        if parent:
            parent.add_child(self)
            #self.parent = parent
        
        #i am cold without a border, but only draw a border if my parent has one :)
        if parent.border:
            self.border = parent.bd_width
            self.bd_color = parent.bd_color
        else:
            self.bd_color = Color(self.osd.default_fg_color)
            self.border = 2
        
        #an invisible border is kinda dumb
        if self.bd_color.get_alpha() == 0: self.border = 0
        
        #make a bar_color
        if not bar_color:
            self.bar_color = Color()
            self.bar_color.set_color(0x000000)
            self.bar_color.set_alpha(100)
        else: self.bar_color = bar_color
        
        #init progress var's and rect's
        self.progress = 0
        self.barpos = 0
        self.step = (self.width - 2 * self.border) / 100
        
        #create a rectangle wich covers the entire progressbar (will become the border)
        self.progressbar = pygame.Rect(0, 0, self.width, self.height)
        
        #create a rect wich will become the background, but leave some space for the border
        self.progressbar_bar = pygame.Rect(0 + self.border, 0 + self.border
                                , self.width - (2 * self.border),
                                self.height - (2 * self.border))
                                
        self.surface_changed = TRUE
        



    def _draw(self, surface=None):
        """private draw function"""
        
        if not self.width or not self.height:
            raise TypeError, 'Not all needed variables set.'

        c = self.bg_color.get_color_sdl()

        a = self.bg_color.get_alpha()
        
        bdc = self.bd_color.get_color_sdl()
        
        border = self.border
        
        #initialize a alpha enabled surface
        if surface:
            self.surface = surface
        elif self.surface: pass
        else: self.surface = pygame.Surface(self.get_size(), pygame.SRCALPHA , 32)
        
        #create the progressbar
        self.progressbar_rect = pygame.Rect(0 + border, 0 + border
                                , self.barpos,
                                self.height - (2 * border))

        self.surface.set_alpha(255)
        
        # draw the progressbar atomic elements to the surface
        self.surface.fill(bdc, self.progressbar)

        self.surface.fill(c, self.progressbar_bar)

        self.surface.fill(self.bar_color.get_color_sdl(), self.progressbar_rect)

        self.blit_parent()
        
        self.surface_changed = FALSE
        
        if DEBUG:
            print "ProgressBar::progress :" + str(self.progress) + "  " + str(self.barpos)
            print "ProgressBar::rect :" + str(self.progressbar_rect)
        
        
    def set_progress(self, progress):
        """Update the progress. Progress values < 0 are ignored,
        values above 100 are rounded to 100
        """
        
        if progress <= 0: return
        if progress >= 100:
            self.barpos = self.width - 2 * self.border
            self.progress = 100
        else:
            self.barpos = (self.step * progress)
            self.progress = progress
            
        self.surface_changed = TRUE
            
    def get_progress(self):
        """Get progress position"""
        return self.progress

Reply via email to