home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / serpentine / cairopiechart.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  5.4 KB  |  150 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import gtk
  5. from math import cos, sin
  6. import math
  7. TWO_PI = math.pi * 2
  8. X = 0
  9. Y = 1
  10.  
  11. _getx = lambda x, radius, angle: x + cos(angle) * radius
  12.  
  13. _gety = lambda y, radius, angle: y + sin(angle) * radius
  14.  
  15. _getxy = lambda center, radius, angle: (_getx(center[X], radius, angle), _gety(center[Y], radius, angle))
  16.  
  17. def sketch_arc(ctx, center, radius, offset, apperture):
  18.     angle1 = offset
  19.     angle2 = offset + apperture
  20.     corner1 = _getxy(center, radius, angle1)
  21.     corner2 = _getxy(center, radius, angle2)
  22.     ctx.move_to(*center)
  23.     ctx.line_to(*corner1)
  24.     ctx.arc(center[X], center[Y], radius, angle1, angle2)
  25.     ctx.line_to(center[X], center[Y])
  26.  
  27.  
  28. def sketch_radius(ctx, center, radius, angle):
  29.     ctx.move_to(*center)
  30.     ctx.line_to(*_getxy(center, radius, angle))
  31.  
  32.  
  33. class CairoPieChart(gtk.DrawingArea):
  34.     
  35.     def __init__(self, values, getter):
  36.         super(CairoPieChart, self).__init__()
  37.         self.connect('expose-event', self._on_exposed)
  38.         self.values = values
  39.         self.selected = []
  40.         self.getter = getter
  41.         self.values = values
  42.  
  43.     
  44.     def _on_exposed(self, me, evt):
  45.         self.draw(evt)
  46.  
  47.     
  48.     def draw_slice(self, center, radius, offset, percentage, color, ctx):
  49.         offset *= math.pi * 2
  50.         apperture = percentage * math.pi * 2
  51.         ctx.set_source_color(color)
  52.         sketch_arc(ctx, center, radius, offset, apperture)
  53.         ctx.close_path()
  54.         ctx.fill()
  55.         ctx.set_source_color(self.get_border_color())
  56.         sketch_radius(ctx, center, radius, offset)
  57.         ctx.stroke()
  58.  
  59.     
  60.     def draw_background(self, center, radius, ctx):
  61.         ctx.set_source_color(self.style.bg[gtk.STATE_NORMAL])
  62.         ctx.arc(center[X], center[Y], radius, 0, TWO_PI)
  63.         ctx.fill()
  64.  
  65.     
  66.     def draw_border(self, center, radius, ctx):
  67.         ctx.set_source_color(self.get_border_color())
  68.         ctx.arc(center[X], center[Y], radius, 0, TWO_PI)
  69.         ctx.stroke()
  70.  
  71.     
  72.     def get_border_color(self):
  73.         return self.style.dark[gtk.STATE_NORMAL]
  74.  
  75.     
  76.     def get_normal_background_color(self):
  77.         return self.style.base[gtk.STATE_NORMAL]
  78.  
  79.     
  80.     def get_selected_background_color(self):
  81.         return self.style.base[gtk.STATE_SELECTED]
  82.  
  83.     
  84.     def get_empty_background_color(self):
  85.         return self.style.mid[gtk.STATE_NORMAL]
  86.  
  87.     
  88.     def get_background_color(self):
  89.         return self.style.bg[gtk.STATE_NORMAL]
  90.  
  91.     
  92.     def draw(self, evt):
  93.         rect = self.allocation
  94.         radius = min(rect.width / 2.0, rect.height / 2.0) - 5
  95.         if radius <= 0:
  96.             return None
  97.         
  98.         x = rect.width / 2.0
  99.         y = rect.height / 2.0
  100.         center = (x, y)
  101.         small_radius = radius * 0.20000000000000001
  102.         self.total = float(self.total)
  103.         ctx = self.window.cairo_create()
  104.         ctx.set_line_width(0.75)
  105.         offset = 0.0
  106.         for index, val in enumerate(self.values):
  107.             apperture = self.getter(val) / self.total
  108.             selected = index in self.selected
  109.             if selected:
  110.                 bg = self.get_selected_background_color()
  111.             else:
  112.                 bg = self.get_normal_background_color()
  113.             self.draw_slice(center, radius, offset, apperture, bg, ctx)
  114.             offset += apperture
  115.         
  116.         if offset < 1:
  117.             bg = self.get_empty_background_color()
  118.             self.draw_slice(center, radius, offset, 1.0 - offset, bg, ctx)
  119.         
  120.         self.draw_background(center, small_radius, ctx)
  121.         self.draw_border(center, small_radius, ctx)
  122.         self.draw_border(center, radius, ctx)
  123.  
  124.  
  125.  
  126. def main():
  127.     window = gtk.Window()
  128.     chart = CairoPieChart([
  129.         10,
  130.         12], (lambda x: x))
  131.     chart.total = 22
  132.     chart.selected = [
  133.         0]
  134.     chart.show()
  135.     chart.set_size_request(64, 64)
  136.     btn = gtk.Button('Hi')
  137.     btn.show()
  138.     vbox = gtk.VBox()
  139.     vbox.show()
  140.     vbox.add(btn)
  141.     vbox.add(chart)
  142.     window.add(vbox)
  143.     window.connect('destroy', gtk.main_quit)
  144.     window.show()
  145.     gtk.main()
  146.  
  147. if __name__ == '__main__':
  148.     main()
  149.  
  150.