bzr branch
http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
1
by Scott James Remnant
Commit the first version of bzrk. |
1 |
# -*- coding: UTF-8 -*-
|
2 |
"""Cell renderer for directed graph.
|
|
3 |
||
4 |
This module contains the implementation of a custom GtkCellRenderer that
|
|
5 |
draws part of the directed graph based on the lines suggested by the code
|
|
6 |
in graph.py.
|
|
7 |
||
8 |
Because we're shiny, we use Cairo to do this, and because we're naughty
|
|
9 |
we cheat and draw over the bits of the TreeViewColumn that are supposed to
|
|
10 |
just be for the background.
|
|
11 |
"""
|
|
12 |
||
13 |
__copyright__ = "Copyright © 2005 Canonical Ltd." |
|
14 |
__author__ = "Scott James Remnant <scott@ubuntu.com>" |
|
15 |
||
16 |
||
17 |
import math |
|
18 |
||
19 |
import gtk |
|
20 |
import gobject |
|
21 |
import pango |
|
22 |
import cairo |
|
23 |
||
24 |
||
25 |
class CellRendererGraph(gtk.GenericCellRenderer): |
|
26 |
"""Cell renderer for directed graph. |
|
27 |
||
28 |
Properties:
|
|
29 |
node (column, colour) tuple to draw revision node,
|
|
30 |
in_lines (start, end, colour) tuple list to draw inward lines,
|
|
31 |
out_lines (start, end, colour) tuple list to draw outward lines.
|
|
32 |
"""
|
|
256.4.1
by Gary van der Merwe
* Set a width and use ellips on Revision No column. |
33 |
|
34 |
columns_len = 0 |
|
1
by Scott James Remnant
Commit the first version of bzrk. |
35 |
|
36 |
__gproperties__ = { |
|
37 |
"node": ( gobject.TYPE_PYOBJECT, "node", |
|
38 |
"revision node instruction", |
|
39 |
gobject.PARAM_WRITABLE |
|
40 |
),
|
|
41 |
"in-lines": ( gobject.TYPE_PYOBJECT, "in-lines", |
|
42 |
"instructions to draw lines into the cell", |
|
43 |
gobject.PARAM_WRITABLE |
|
44 |
),
|
|
45 |
"out-lines": ( gobject.TYPE_PYOBJECT, "out-lines", |
|
46 |
"instructions to draw lines out of the cell", |
|
47 |
gobject.PARAM_WRITABLE |
|
48 |
),
|
|
49 |
}
|
|
256.4.1
by Gary van der Merwe
* Set a width and use ellips on Revision No column. |
50 |
|
1
by Scott James Remnant
Commit the first version of bzrk. |
51 |
def do_set_property(self, property, value): |
52 |
"""Set properties from GObject properties.""" |
|
53 |
if property.name == "node": |
|
54 |
self.node = value |
|
55 |
elif property.name == "in-lines": |
|
56 |
self.in_lines = value |
|
57 |
elif property.name == "out-lines": |
|
58 |
self.out_lines = value |
|
59 |
else: |
|
60 |
raise AttributeError, "no such property: '%s'" % property.name |
|
61 |
||
62 |
def box_size(self, widget): |
|
63 |
"""Calculate box size based on widget's font. |
|
64 |
||
65 |
Cache this as it's probably expensive to get. It ensures that we
|
|
66 |
draw the graph at least as large as the text.
|
|
67 |
"""
|
|
68 |
try: |
|
69 |
return self._box_size |
|
70 |
except AttributeError: |
|
71 |
pango_ctx = widget.get_pango_context() |
|
72 |
font_desc = widget.get_style().font_desc |
|
73 |
metrics = pango_ctx.get_metrics(font_desc) |
|
74 |
||
75 |
ascent = pango.PIXELS(metrics.get_ascent()) |
|
9
by Scott James Remnant
Fix the busted font size stuff, and then increase the sizes a bit to |
76 |
descent = pango.PIXELS(metrics.get_descent()) |
1
by Scott James Remnant
Commit the first version of bzrk. |
77 |
|
9
by Scott James Remnant
Fix the busted font size stuff, and then increase the sizes a bit to |
78 |
self._box_size = ascent + descent + 6 |
1
by Scott James Remnant
Commit the first version of bzrk. |
79 |
return self._box_size |
80 |
||
81 |
def set_colour(self, ctx, colour, bg, fg): |
|
82 |
"""Set the context source colour. |
|
83 |
||
84 |
Picks a distinct colour based on an internal wheel; the bg
|
|
85 |
parameter provides the value that should be assigned to the 'zero'
|
|
86 |
colours and the fg parameter provides the multiplier that should be
|
|
87 |
applied to the foreground colours.
|
|
88 |
"""
|
|
256.2.43
by Gary van der Merwe
Make the mainline allways black - which often makes the graph easier to read. |
89 |
mainline_color = ( 0.0, 0.0, 0.0 ) |
1
by Scott James Remnant
Commit the first version of bzrk. |
90 |
colours = [ |
91 |
( 1.0, 0.0, 0.0 ), |
|
92 |
( 1.0, 1.0, 0.0 ), |
|
93 |
( 0.0, 1.0, 0.0 ), |
|
94 |
( 0.0, 1.0, 1.0 ), |
|
95 |
( 0.0, 0.0, 1.0 ), |
|
96 |
( 1.0, 0.0, 1.0 ), |
|
97 |
]
|
|
98 |
||
256.2.43
by Gary van der Merwe
Make the mainline allways black - which often makes the graph easier to read. |
99 |
if colour == 0: |
100 |
colour_rgb = mainline_color |
|
101 |
else: |
|
102 |
colour_rgb = colours[colour % len(colours)] |
|
103 |
||
104 |
red = (colour_rgb[0] * fg) or bg |
|
105 |
green = (colour_rgb[1] * fg) or bg |
|
106 |
blue = (colour_rgb[2] * fg) or bg |
|
1
by Scott James Remnant
Commit the first version of bzrk. |
107 |
|
108 |
ctx.set_source_rgb(red, green, blue) |
|
109 |
||
110 |
def on_get_size(self, widget, cell_area): |
|
111 |
"""Return the size we need for this cell. |
|
112 |
||
113 |
Each cell is drawn individually and is only as wide as it needs
|
|
114 |
to be, we let the TreeViewColumn take care of making them all
|
|
115 |
line up.
|
|
116 |
"""
|
|
66.2.19
by Aaron Bentley
Increase box height by 1 to fix diagonal jagginess |
117 |
box_size = self.box_size(widget) + 1 |
1
by Scott James Remnant
Commit the first version of bzrk. |
118 |
|
256.4.1
by Gary van der Merwe
* Set a width and use ellips on Revision No column. |
119 |
width = box_size * (self.columns_len + 1) |
1
by Scott James Remnant
Commit the first version of bzrk. |
120 |
height = box_size |
121 |
||
122 |
# FIXME I have no idea how to use cell_area properly
|
|
123 |
return (0, 0, width, height) |
|
124 |
||
125 |
def on_render(self, window, widget, bg_area, cell_area, exp_area, flags): |
|
126 |
"""Render an individual cell. |
|
127 |
||
128 |
Draws the cell contents using cairo, taking care to clip what we
|
|
129 |
do to within the background area so we don't draw over other cells.
|
|
130 |
Note that we're a bit naughty there and should really be drawing
|
|
131 |
in the cell_area (or even the exposed area), but we explicitly don't
|
|
132 |
want any gutter.
|
|
133 |
||
134 |
We try and be a little clever, if the line we need to draw is going
|
|
135 |
to cross other columns we actually draw it as in the .---' style
|
|
136 |
instead of a pure diagonal ... this reduces confusion by an
|
|
137 |
incredible amount.
|
|
138 |
"""
|
|
139 |
ctx = window.cairo_create() |
|
140 |
ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height) |
|
141 |
ctx.clip() |
|
142 |
||
6
by Scott James Remnant
Also increase the width of the lines in accordance with the font size. |
143 |
box_size = self.box_size(widget) |
144 |
||
9
by Scott James Remnant
Fix the busted font size stuff, and then increase the sizes a bit to |
145 |
ctx.set_line_width(box_size / 8) |
256.2.50
by Gary van der Merwe
First implementation of broken lines. |
146 |
ctx.set_line_cap(cairo.LINE_CAP_ROUND) |
1
by Scott James Remnant
Commit the first version of bzrk. |
147 |
|
148 |
# Draw lines into the cell
|
|
149 |
for start, end, colour in self.in_lines: |
|
256.2.7
by Gary van der Merwe
Use nice Bézier curves for viz |
150 |
self.render_line (ctx, cell_area, box_size, |
151 |
bg_area.y, bg_area.height, |
|
152 |
start, end, colour) |
|
1
by Scott James Remnant
Commit the first version of bzrk. |
153 |
|
154 |
# Draw lines out of the cell
|
|
155 |
for start, end, colour in self.out_lines: |
|
256.2.7
by Gary van der Merwe
Use nice Bézier curves for viz |
156 |
self.render_line (ctx, cell_area, box_size, |
157 |
bg_area.y + bg_area.height, bg_area.height, |
|
158 |
start, end, colour) |
|
1
by Scott James Remnant
Commit the first version of bzrk. |
159 |
|
160 |
# Draw the revision node in the right column
|
|
161 |
(column, colour) = self.node |
|
162 |
ctx.arc(cell_area.x + box_size * column + box_size / 2, |
|
163 |
cell_area.y + cell_area.height / 2, |
|
9
by Scott James Remnant
Fix the busted font size stuff, and then increase the sizes a bit to |
164 |
box_size / 4, 0, 2 * math.pi) |
1
by Scott James Remnant
Commit the first version of bzrk. |
165 |
|
166 |
self.set_colour(ctx, colour, 0.0, 0.5) |
|
167 |
ctx.stroke_preserve() |
|
168 |
||
169 |
self.set_colour(ctx, colour, 0.5, 1.0) |
|
170 |
ctx.fill() |
|
256.2.7
by Gary van der Merwe
Use nice Bézier curves for viz |
171 |
|
172 |
def render_line (self, ctx, cell_area, box_size, mid, height, start, end, colour): |
|
256.2.50
by Gary van der Merwe
First implementation of broken lines. |
173 |
if start is None: |
174 |
x = cell_area.x + box_size * end + box_size / 2 |
|
175 |
ctx.move_to(x, mid + height / 3) |
|
176 |
ctx.line_to(x, mid + height / 3) |
|
177 |
ctx.move_to(x, mid + height / 6) |
|
178 |
ctx.line_to(x, mid + height / 6) |
|
179 |
||
180 |
elif end is None: |
|
181 |
x = cell_area.x + box_size * start + box_size / 2 |
|
182 |
ctx.move_to(x, mid - height / 3) |
|
183 |
ctx.line_to(x, mid - height / 3) |
|
184 |
ctx.move_to(x, mid - height / 6) |
|
185 |
ctx.line_to(x, mid - height / 6) |
|
256.2.7
by Gary van der Merwe
Use nice Bézier curves for viz |
186 |
else: |
256.2.50
by Gary van der Merwe
First implementation of broken lines. |
187 |
startx = cell_area.x + box_size * start + box_size / 2 |
188 |
endx = cell_area.x + box_size * end + box_size / 2 |
|
189 |
||
190 |
ctx.move_to(startx, mid - height / 2) |
|
191 |
||
192 |
if start - end == 0 : |
|
193 |
ctx.line_to(endx, mid + height / 2) |
|
194 |
else: |
|
195 |
ctx.curve_to(startx, mid - height / 5, |
|
196 |
startx, mid - height / 5, |
|
197 |
startx + (endx - startx) / 2, mid) |
|
198 |
||
199 |
ctx.curve_to(endx, mid + height / 5, |
|
200 |
endx, mid + height / 5 , |
|
201 |
endx, mid + height / 2) |
|
202 |
||
256.2.7
by Gary van der Merwe
Use nice Bézier curves for viz |
203 |
self.set_colour(ctx, colour, 0.0, 0.65) |
204 |
ctx.stroke() |
|
205 |