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
|
import pygtk
pygtk.require('2.0')
import gtk
import cairo
#------------------------------------------------------------------------------
class Reservoir(gtk.DrawingArea):
"""Dessine un reservoir partiellement rempli en couleur."""
def __init__(self, color, percent):
gtk.DrawingArea.__init__(self)
self.connect('expose-event', self.ev_expose)
self.c = color
self.p = percent
self.txt = str(self.p) + "%"
if sum(map(lambda x: x**2, color)) < 0.1:
self.fg = 0.5
else:
self.fg = 0.0
return
def ev_expose(self, widget=None, event=None):
"""Gestion de l'affichage et du redimensionnement."""
self.context = widget.window.cairo_create()
self.context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
self.context.clip()
self.draw()
return False
def draw(self):
"""Dessine le reservoir."""
self.context.set_line_width(2.0)
self.context.set_line_cap(cairo.LINE_CAP_ROUND)
self.context.set_line_join(cairo.LINE_JOIN_ROUND)
rect = self.get_allocation()
r = rect.height - 4
if r < 0:
r = 0
if self.p != None:
n = 2+(r*(100-self.p))/100
# Fond
self.context.set_source_rgb(1.0, 1.0, 1.0)
self.context.rectangle(rect.width/4, 2, rect.width/2, r)
self.context.fill()
# Niveau
self.context.set_source_rgb(self.c[0], self.c[1], self.c[2])
self.context.rectangle(rect.width/4, n, rect.width/2, r-n+2)
self.context.fill()
# Texte
xb, yb, w, h =self.context.text_extents(self.txt)[:4]
self.context.set_source_rgb(self.fg, self.fg, self.fg)
self.context.move_to((rect.width-w)/2-xb, (rect.height-h)/2-yb)
self.context.show_text(self.txt)
# Contour
self.context.set_source_rgb(0.0, 0.0, 0.0)
self.context.rectangle(rect.width/4, 2, rect.width/2, r)
self.context.stroke()
# Graduations
self.context.set_source_rgba(0.0, 0.0, 0.0, 0.5)
self.context.move_to(rect.width/4, rect.height/2)
self.context.line_to((3*rect.width)/10, rect.height/2)
self.context.move_to(rect.width/4, 1+r/4)
self.context.line_to((3*rect.width)/10, 1+r/4)
self.context.move_to(rect.width/4, 3+3*r/4)
self.context.line_to((3*rect.width)/10, 3+3*r/4)
self.context.stroke()
return
#------------------------------------------------------------------------------
if __name__ == '__main__':
win = gtk.Window()
win.connect('destroy', gtk.main_quit)
win.set_default_size(150, 250)
r = Reservoir([0.0, 1.0, 1.0], 33)
win.add(r)
win.show_all()
gtk.main()
#------------------------------------------------------------------------------ |
Partager