| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Copyright (C) 2009, 2010, 2012 Red Hat, Inc. |
| 4 |
## Authors: |
| 5 |
## Tim Waugh <[email protected]> |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
|
| 21 |
from gi.repository import Gdk |
| 22 |
from gi.repository import Gtk |
| 23 |
import cairo |
| 24 |
|
| 25 |
class GtkInkLevel (Gtk.DrawingArea): |
| 26 |
def __init__ (self, color, level=0): |
| 27 |
Gtk.DrawingArea.__init__ (self) |
| 28 |
self.connect ('draw', self.draw) |
| 29 |
self._level = level |
| 30 |
self._color = None |
| 31 |
if color: |
| 32 |
self._color = Gdk.color_parse (color) |
| 33 |
if not self._color: |
| 34 |
self._color = Gdk.color_parse ('#cccccc') |
| 35 |
|
| 36 |
self.set_size_request (30, 45) |
| 37 |
|
| 38 |
def set_level (self, level): |
| 39 |
self._level = level |
| 40 |
self.queue_resize () |
| 41 |
|
| 42 |
def get_level (self): |
| 43 |
return self._level |
| 44 |
|
| 45 |
def draw (self, widget, ctx): |
| 46 |
w = widget.get_allocated_width () |
| 47 |
h = widget.get_allocated_height () |
| 48 |
ratio = 1.0 * h / w |
| 49 |
if ratio < 1.5: |
| 50 |
w = h * 2.0 / 3.0 |
| 51 |
else: |
| 52 |
h = w * 3.0 / 2.0 |
| 53 |
thickness = 1 |
| 54 |
ctx.translate (thickness, thickness) |
| 55 |
ctx.scale (w - 2 * thickness, h - 2 * thickness) |
| 56 |
thickness = max (ctx.device_to_user_distance (thickness, thickness)) |
| 57 |
|
| 58 |
r = self._color.red / 65535.0 |
| 59 |
g = self._color.green / 65535.0 |
| 60 |
b = self._color.blue / 65535.0 |
| 61 |
fill_point = self._level / 100.0 |
| 62 |
|
| 63 |
ctx.move_to (0.5, 0.0) |
| 64 |
ctx.curve_to (0.5, 0.33, 1.0, 0.5, 1.0, 0.67) |
| 65 |
ctx.curve_to (1.0, 0.85, 0.85, 1.0, 0.5, 1.0) |
| 66 |
ctx.curve_to (0.15, 1.0, 0.0, 0.85, 0.0, 0.67) |
| 67 |
ctx.curve_to (0.0, 0.5, 0.1, 0.2, 0.5, 0.0) |
| 68 |
ctx.close_path () |
| 69 |
ctx.set_source_rgb (r, g, b) |
| 70 |
ctx.set_line_width (thickness) |
| 71 |
ctx.stroke_preserve () |
| 72 |
if fill_point > 0.0: |
| 73 |
grad_width = 0.10 |
| 74 |
grad_start = fill_point - (grad_width / 2) |
| 75 |
if grad_start < 0: |
| 76 |
grad_start = 0 |
| 77 |
|
| 78 |
pat = cairo.LinearGradient (0, 1, 0, 0) |
| 79 |
pat.add_color_stop_rgba (0, r, g, b, 1) |
| 80 |
pat.add_color_stop_rgba ((self._level - 5) / 100.0, r, g, b, 1) |
| 81 |
pat.add_color_stop_rgba ((self._level + 5)/ 100.0, 1, 1, 1, 1) |
| 82 |
pat.add_color_stop_rgba (1.0, 1, 1, 1, 1) |
| 83 |
ctx.set_source (pat) |
| 84 |
ctx.fill () |
| 85 |
else: |
| 86 |
ctx.set_source_rgb (1, 1, 1) |
| 87 |
ctx.fill () |
| 88 |
|
| 89 |
ctx.set_line_width (thickness / 2) |
| 90 |
|
| 91 |
ctx.move_to (0.5, 0.0) |
| 92 |
ctx.line_to (0.5, 1.0) |
| 93 |
ctx.set_source_rgb (r, g, b) |
| 94 |
ctx.stroke () |
| 95 |
|
| 96 |
# 50% marker |
| 97 |
ctx.move_to (0.4, 0.5) |
| 98 |
ctx.line_to (0.6, 0.5) |
| 99 |
ctx.set_source_rgb (r, g, b) |
| 100 |
ctx.stroke () |
| 101 |
|
| 102 |
# 25% marker |
| 103 |
ctx.move_to (0.45, 0.75) |
| 104 |
ctx.line_to (0.55, 0.75) |
| 105 |
ctx.set_source_rgb (r, g, b) |
| 106 |
ctx.stroke () |
| 107 |
|
| 108 |
# 75% marker |
| 109 |
ctx.move_to (0.45, 0.25) |
| 110 |
ctx.line_to (0.55, 0.25) |
| 111 |
ctx.set_source_rgb (r, g, b) |
| 112 |
ctx.stroke () |
| 113 |
|
| 114 |
if __name__ == '__main__': |
| 115 |
# Try it out. |
| 116 |
from gi.repository import GLib |
| 117 |
import time |
| 118 |
def adjust_level (level): |
| 119 |
Gdk.threads_enter () |
| 120 |
l = level.get_level () |
| 121 |
l += 1 |
| 122 |
if l > 100: |
| 123 |
l = 0 |
| 124 |
level.set_level (l) |
| 125 |
Gdk.threads_leave () |
| 126 |
return True |
| 127 |
|
| 128 |
w = Gtk.Window () |
| 129 |
w.set_border_width (12) |
| 130 |
vbox = Gtk.VBox (spacing=6) |
| 131 |
w.add (vbox) |
| 132 |
hbox = Gtk.HBox (spacing=6) |
| 133 |
vbox.pack_start (hbox, False, False, 0) |
| 134 |
klevel = GtkInkLevel ("black", level=100) |
| 135 |
clevel = GtkInkLevel ("cyan", level=60) |
| 136 |
mlevel = GtkInkLevel ("magenta", level=30) |
| 137 |
ylevel = GtkInkLevel ("yellow", level=100) |
| 138 |
hbox.pack_start (klevel, False, False, 0) |
| 139 |
hbox.pack_start (clevel, False, False, 0) |
| 140 |
hbox.pack_start (mlevel, False, False, 0) |
| 141 |
hbox.pack_start (ylevel, False, False, 0) |
| 142 |
GLib.timeout_add (10, adjust_level, klevel) |
| 143 |
GLib.timeout_add (10, adjust_level, clevel) |
| 144 |
GLib.timeout_add (10, adjust_level, mlevel) |
| 145 |
GLib.timeout_add (10, adjust_level, ylevel) |
| 146 |
w.show_all () |
| 147 |
w.connect ('delete_event', Gtk.main_quit) |
| 148 |
Gdk.threads_init () |
| 149 |
Gtk.main () |