Back to index

system-config-printer  1.3.9+20120706
gtkinklevel.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Copyright (C) 2009, 2010 Red Hat, Inc.
00004 ## Authors:
00005 ##  Tim Waugh <twaugh@redhat.com>
00006 
00007 ## This program is free software; you can redistribute it and/or modify
00008 ## it under the terms of the GNU General Public License as published by
00009 ## the Free Software Foundation; either version 2 of the License, or
00010 ## (at your option) any later version.
00011 
00012 ## This program is distributed in the hope that it will be useful,
00013 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 ## GNU General Public License for more details.
00016 
00017 ## You should have received a copy of the GNU General Public License
00018 ## along with this program; if not, write to the Free Software
00019 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 
00021 import gtk
00022 import cairo
00023 
00024 class GtkInkLevel (gtk.DrawingArea):
00025     def __init__ (self, color, level=0):
00026         gtk.DrawingArea.__init__ (self)
00027         self.connect ('expose-event', self.expose_event)
00028         self._level = level
00029         try:
00030             self._color = gtk.gdk.color_parse (color)
00031         except (ValueError, TypeError):
00032             self._color = gtk.gdk.color_parse ('#cccccc')
00033 
00034         self.set_size_request (30, 45)
00035 
00036     def set_level (self, level):
00037         self._level = level
00038         self.queue_resize ()
00039 
00040     def get_level (self):
00041         return self._level
00042 
00043     def expose_event (self, widget, event):
00044         ctx = self.window.cairo_create ()
00045         ctx.rectangle (event.area.x, event.area.y,
00046                        event.area.width,
00047                        event.area.height)
00048         ctx.clip ()
00049 
00050         (w, h) = self.window.get_size ()
00051         ratio = 1.0 * h / w
00052         if ratio < 1.5:
00053             w = h * 2.0 / 3.0
00054         else:
00055             h = w * 3.0 / 2.0
00056         thickness = 1
00057         ctx.translate (thickness, thickness)
00058         ctx.scale (w - 2 * thickness, h - 2 * thickness)
00059         thickness = max (ctx.device_to_user_distance (thickness, thickness))
00060         self.draw (ctx, thickness)
00061 
00062     def draw (self, ctx, thickness):
00063         r = self._color.red / 65535.0
00064         g = self._color.green / 65535.0
00065         b = self._color.blue / 65535.0
00066         fill_point = self._level / 100.0
00067 
00068         ctx.move_to (0.5, 0.0)
00069         ctx.curve_to (0.5, 0.33, 1.0, 0.5, 1.0, 0.67)
00070         ctx.curve_to (1.0, 0.85, 0.85, 1.0, 0.5, 1.0)
00071         ctx.curve_to (0.15, 1.0, 0.0, 0.85, 0.0, 0.67)
00072         ctx.curve_to (0.0, 0.5, 0.1, 0.2, 0.5, 0.0)
00073         ctx.close_path ()
00074         ctx.set_source_rgb (r, g, b)
00075         ctx.set_line_width (thickness)
00076         ctx.stroke_preserve ()
00077         if fill_point > 0.0:
00078             grad_width = 0.10
00079             grad_start = fill_point - (grad_width / 2)
00080             if grad_start < 0:
00081                 grad_start = 0
00082 
00083             pat = cairo.LinearGradient (0, 1, 0, 0)
00084             pat.add_color_stop_rgba (0, r, g, b, 1)
00085             pat.add_color_stop_rgba ((self._level - 5) / 100.0, r, g, b, 1)
00086             pat.add_color_stop_rgba ((self._level + 5)/ 100.0, 1, 1, 1, 1)
00087             pat.add_color_stop_rgba (1.0, 1, 1, 1, 1)
00088             ctx.set_source (pat)
00089             ctx.fill ()
00090         else:
00091             ctx.set_source_rgb (1, 1, 1)
00092             ctx.fill ()
00093 
00094         ctx.set_line_width (thickness / 2)
00095 
00096         ctx.move_to (0.5, 0.0)
00097         ctx.line_to (0.5, 1.0)
00098         ctx.set_source_rgb (r, g, b)
00099         ctx.stroke ()
00100 
00101         # 50% marker
00102         ctx.move_to (0.4, 0.5)
00103         ctx.line_to (0.6, 0.5)
00104         ctx.set_source_rgb (r, g, b)
00105         ctx.stroke ()
00106 
00107         # 25% marker
00108         ctx.move_to (0.45, 0.75)
00109         ctx.line_to (0.55, 0.75)
00110         ctx.set_source_rgb (r, g, b)
00111         ctx.stroke ()
00112 
00113         # 75% marker
00114         ctx.move_to (0.45, 0.25)
00115         ctx.line_to (0.55, 0.25)
00116         ctx.set_source_rgb (r, g, b)
00117         ctx.stroke ()
00118 
00119 if __name__ == '__main__':
00120     # Try it out.
00121     import gobject
00122     import time
00123     def adjust_level (level):
00124         gtk.gdk.threads_enter ()
00125         l = level.get_level ()
00126         l += 1
00127         if l > 100:
00128             l = 0
00129         level.set_level (l)
00130         gtk.gdk.threads_leave ()
00131         return True
00132 
00133     w = gtk.Window ()
00134     w.set_border_width (12)
00135     vbox = gtk.VBox (spacing=6)
00136     w.add (vbox)
00137     hbox = gtk.HBox (spacing=6)
00138     vbox.pack_start (hbox, False, False, 0)
00139     klevel = GtkInkLevel ("black", level=100)
00140     clevel = GtkInkLevel ("cyan", level=60)
00141     mlevel = GtkInkLevel ("magenta", level=30)
00142     ylevel = GtkInkLevel ("yellow", level=100)
00143     hbox.pack_start (klevel)
00144     hbox.pack_start (clevel)
00145     hbox.pack_start (mlevel)
00146     hbox.pack_start (ylevel)
00147     gobject.timeout_add (10, adjust_level, klevel)
00148     gobject.timeout_add (10, adjust_level, clevel)
00149     gobject.timeout_add (10, adjust_level, mlevel)
00150     gobject.timeout_add (10, adjust_level, ylevel)
00151     w.show_all ()
00152     w.connect ('delete_event', gtk.main_quit)
00153     gtk.gdk.threads_init ()
00154     gtk.main ()