home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / gtkinklevel.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  4.8 KB  |  155 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Copyright (C) 2009, 2010 Red Hat, Inc.
  4. ## Authors:
  5. ##  Tim Waugh <twaugh@redhat.com>
  6.  
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11.  
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16.  
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import gtk
  22. import cairo
  23.  
  24. class GtkInkLevel (gtk.DrawingArea):
  25.     def __init__ (self, color, level=0):
  26.         gtk.DrawingArea.__init__ (self)
  27.         self.connect ('expose-event', self.expose_event)
  28.         self._level = level
  29.         try:
  30.             self._color = gtk.gdk.color_parse (color)
  31.         except (ValueError, TypeError):
  32.             self._color = gtk.gdk.color_parse ('#cccccc')
  33.  
  34.         self.set_size_request (30, 45)
  35.  
  36.     def set_level (self, level):
  37.         self._level = level
  38.         self.queue_resize ()
  39.  
  40.     def get_level (self):
  41.         return self._level
  42.  
  43.     def expose_event (self, widget, event):
  44.         ctx = self.window.cairo_create ()
  45.         ctx.rectangle (event.area.x, event.area.y,
  46.                        event.area.width,
  47.                        event.area.height)
  48.         ctx.clip ()
  49.  
  50.         (w, h) = self.window.get_size ()
  51.         ratio = 1.0 * h / w
  52.         if ratio < 1.5:
  53.             w = h * 2.0 / 3.0
  54.         else:
  55.             h = w * 3.0 / 2.0
  56.         thickness = 1
  57.         ctx.translate (thickness, thickness)
  58.         ctx.scale (w - 2 * thickness, h - 2 * thickness)
  59.         thickness = max (ctx.device_to_user_distance (thickness, thickness))
  60.         self.draw (ctx, thickness)
  61.  
  62.     def draw (self, ctx, thickness):
  63.         r = self._color.red / 65535.0
  64.         g = self._color.green / 65535.0
  65.         b = self._color.blue / 65535.0
  66.         fill_point = self._level / 100.0
  67.  
  68.         ctx.move_to (0.5, 0.0)
  69.         ctx.curve_to (0.5, 0.33, 1.0, 0.5, 1.0, 0.67)
  70.         ctx.curve_to (1.0, 0.85, 0.85, 1.0, 0.5, 1.0)
  71.         ctx.curve_to (0.15, 1.0, 0.0, 0.85, 0.0, 0.67)
  72.         ctx.curve_to (0.0, 0.5, 0.1, 0.2, 0.5, 0.0)
  73.         ctx.close_path ()
  74.         ctx.set_source_rgb (r, g, b)
  75.         ctx.set_line_width (thickness)
  76.         ctx.stroke_preserve ()
  77.         if fill_point > 0.0:
  78.             grad_width = 0.10
  79.             grad_start = fill_point - (grad_width / 2)
  80.             if grad_start < 0:
  81.                 grad_start = 0
  82.  
  83.             pat = cairo.LinearGradient (0, 1, 0, 0)
  84.             pat.add_color_stop_rgba (0, r, g, b, 1)
  85.             pat.add_color_stop_rgba ((self._level - 5) / 100.0, r, g, b, 1)
  86.             pat.add_color_stop_rgba ((self._level + 5)/ 100.0, 1, 1, 1, 1)
  87.             pat.add_color_stop_rgba (1.0, 1, 1, 1, 1)
  88.             ctx.set_source (pat)
  89.             ctx.fill ()
  90.         else:
  91.             ctx.set_source_rgb (1, 1, 1)
  92.             ctx.fill ()
  93.  
  94.         ctx.set_line_width (thickness / 2)
  95.  
  96.         ctx.move_to (0.5, 0.0)
  97.         ctx.line_to (0.5, 1.0)
  98.         ctx.set_source_rgb (r, g, b)
  99.         ctx.stroke ()
  100.  
  101.         # 50% marker
  102.         ctx.move_to (0.4, 0.5)
  103.         ctx.line_to (0.6, 0.5)
  104.         ctx.set_source_rgb (r, g, b)
  105.         ctx.stroke ()
  106.  
  107.         # 25% marker
  108.         ctx.move_to (0.45, 0.75)
  109.         ctx.line_to (0.55, 0.75)
  110.         ctx.set_source_rgb (r, g, b)
  111.         ctx.stroke ()
  112.  
  113.         # 75% marker
  114.         ctx.move_to (0.45, 0.25)
  115.         ctx.line_to (0.55, 0.25)
  116.         ctx.set_source_rgb (r, g, b)
  117.         ctx.stroke ()
  118.  
  119. if __name__ == '__main__':
  120.     # Try it out.
  121.     import gobject
  122.     import time
  123.     def adjust_level (level):
  124.         gtk.gdk.threads_enter ()
  125.         l = level.get_level ()
  126.         l += 1
  127.         if l > 100:
  128.             l = 0
  129.         level.set_level (l)
  130.         gtk.gdk.threads_leave ()
  131.         return True
  132.  
  133.     w = gtk.Window ()
  134.     w.set_border_width (12)
  135.     vbox = gtk.VBox (spacing=6)
  136.     w.add (vbox)
  137.     hbox = gtk.HBox (spacing=6)
  138.     vbox.pack_start (hbox, False, False, 0)
  139.     klevel = GtkInkLevel ("black", level=100)
  140.     clevel = GtkInkLevel ("cyan", level=60)
  141.     mlevel = GtkInkLevel ("magenta", level=30)
  142.     ylevel = GtkInkLevel ("yellow", level=100)
  143.     hbox.pack_start (klevel)
  144.     hbox.pack_start (clevel)
  145.     hbox.pack_start (mlevel)
  146.     hbox.pack_start (ylevel)
  147.     gobject.timeout_add (10, adjust_level, klevel)
  148.     gobject.timeout_add (10, adjust_level, clevel)
  149.     gobject.timeout_add (10, adjust_level, mlevel)
  150.     gobject.timeout_add (10, adjust_level, ylevel)
  151.     w.show_all ()
  152.     w.connect ('delete_event', gtk.main_quit)
  153.     gtk.gdk.threads_init ()
  154.     gtk.main ()
  155.