Progress Cell Renderer

About

Here is a Progress Cell Renderer for pygtk based applications. It allows one to embed a progress bar inside a TreeView. It's being used in Smart to present progress for downloads of packages and other items.

Code

   1 class ProgressCellRenderer(gtk.GenericCellRenderer):
   2 
   3     __gproperties__ = {
   4         "percent": (gobject.TYPE_INT, "Percent",
   5                     "Progress percentage", 0, 100, 0,
   6                     gobject.PARAM_READWRITE),
   7     }
   8 
   9     def __init__(self):
  10         self.__gobject_init__()
  11         self.percent = 0
  12 
  13     def do_set_property(self, pspec, value):
  14         setattr(self, pspec.name, value)
  15 
  16     def do_get_property(self, pspec):
  17         return getattr(self, pspec.name)
  18 
  19     def on_render(self, window, widget, background_area,
  20                   cell_area, expose_area, flags):
  21         (x_offset, y_offset,
  22          width, height) = self.on_get_size(widget, cell_area)
  23         widget.style.paint_box(window,
  24                                gtk.STATE_NORMAL,
  25                                gtk.SHADOW_IN,
  26                                None, widget, "trough",
  27                                cell_area.x+x_offset,
  28                                cell_area.y+y_offset,
  29                                width, height)
  30         xt = widget.style.xthickness
  31         xpad = self.get_property("xpad")
  32         space = (width-2*xt-2*xpad)*(self.percent/100.)
  33         widget.style.paint_box(window,
  34                                gtk.STATE_PRELIGHT,
  35                                gtk.SHADOW_OUT,
  36                                None, widget, "bar",
  37                                cell_area.x+x_offset+xt,
  38                                cell_area.y+y_offset+xt,
  39                                int(space), height-2*xt)
  40 
  41     def on_get_size(self, widget, cell_area):
  42         xpad = self.get_property("xpad")
  43         ypad = self.get_property("ypad")
  44         if cell_area:
  45             width = cell_area.width
  46             height = cell_area.height
  47             x_offset = xpad
  48             y_offset = ypad
  49         else:
  50             width = self.get_property("width")
  51             height = self.get_property("height")
  52             if width == -1: width = 100
  53             if height == -1: height = 30
  54             width += xpad*2
  55             height += ypad*2
  56             x_offset = 0
  57             y_offset = 0
  58         return x_offset, y_offset, width, height


CategorySnippet

snippets/progress-cell (last edited 2008-03-03 03:40:22 by GustavoNiemeyer)