1
# -*- coding: UTF-8 -*-
2
"""Cell renderer for directed graph.
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
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.
13
__copyright__ = "Copyright © 2005 Canonical Ltd."
14
__author__ = "Scott James Remnant <scott@ubuntu.com>"
25
class CellRendererGraph(gtk.GenericCellRenderer):
26
"""Cell renderer for directed graph.
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.
35
"node": ( gobject.TYPE_PYOBJECT, "node",
36
"revision node instruction",
37
gobject.PARAM_WRITABLE
39
"in-lines": ( gobject.TYPE_PYOBJECT, "in-lines",
40
"instructions to draw lines into the cell",
41
gobject.PARAM_WRITABLE
43
"out-lines": ( gobject.TYPE_PYOBJECT, "out-lines",
44
"instructions to draw lines out of the cell",
45
gobject.PARAM_WRITABLE
49
def do_set_property(self, property, value):
50
"""Set properties from GObject properties."""
51
if property.name == "node":
53
elif property.name == "in-lines":
55
elif property.name == "out-lines":
56
self.out_lines = value
58
raise AttributeError, "no such property: '%s'" % property.name
60
def box_size(self, widget):
61
"""Calculate box size based on widget's font.
63
Cache this as it's probably expensive to get. It ensures that we
64
draw the graph at least as large as the text.
68
except AttributeError:
69
pango_ctx = widget.get_pango_context()
70
font_desc = widget.get_style().font_desc
71
metrics = pango_ctx.get_metrics(font_desc)
73
ascent = pango.PIXELS(metrics.get_ascent())
74
descent = pango.PIXELS(metrics.get_descent())
76
self._box_size = ascent + descent + 6
79
def set_colour(self, ctx, colour, bg, fg):
80
"""Set the context source colour.
82
Picks a distinct colour based on an internal wheel; the bg
83
parameter provides the value that should be assigned to the 'zero'
84
colours and the fg parameter provides the multiplier that should be
85
applied to the foreground colours.
96
colour %= len(colours)
97
red = (colours[colour][0] * fg) or bg
98
green = (colours[colour][1] * fg) or bg
99
blue = (colours[colour][2] * fg) or bg
101
ctx.set_source_rgb(red, green, blue)
103
def on_get_size(self, widget, cell_area):
104
"""Return the size we need for this cell.
106
Each cell is drawn individually and is only as wide as it needs
107
to be, we let the TreeViewColumn take care of making them all
110
box_size = self.box_size(widget) + 1
113
for start, end, colour in self.in_lines + self.out_lines:
114
cols = max(cols, start, end)
116
width = box_size * (cols + 1)
119
# FIXME I have no idea how to use cell_area properly
120
return (0, 0, width, height)
122
def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
123
"""Render an individual cell.
125
Draws the cell contents using cairo, taking care to clip what we
126
do to within the background area so we don't draw over other cells.
127
Note that we're a bit naughty there and should really be drawing
128
in the cell_area (or even the exposed area), but we explicitly don't
131
We try and be a little clever, if the line we need to draw is going
132
to cross other columns we actually draw it as in the .---' style
133
instead of a pure diagonal ... this reduces confusion by an
136
ctx = window.cairo_create()
137
ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
140
box_size = self.box_size(widget)
142
ctx.set_line_width(box_size / 8)
143
ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
145
# Draw lines into the cell
146
for start, end, colour in self.in_lines:
147
ctx.move_to(cell_area.x + box_size * start + box_size / 2,
148
bg_area.y - bg_area.height / 2)
151
ctx.line_to(cell_area.x + box_size * start, bg_area.y)
152
ctx.line_to(cell_area.x + box_size * end + box_size, bg_area.y)
153
elif start - end < -1:
154
ctx.line_to(cell_area.x + box_size * start + box_size,
156
ctx.line_to(cell_area.x + box_size * end, bg_area.y)
158
ctx.line_to(cell_area.x + box_size * end + box_size / 2,
159
bg_area.y + bg_area.height / 2)
161
self.set_colour(ctx, colour, 0.0, 0.65)
164
# Draw lines out of the cell
165
for start, end, colour in self.out_lines:
166
ctx.move_to(cell_area.x + box_size * start + box_size / 2,
167
bg_area.y + bg_area.height / 2)
170
ctx.line_to(cell_area.x + box_size * start,
171
bg_area.y + bg_area.height)
172
ctx.line_to(cell_area.x + box_size * end + box_size,
173
bg_area.y + bg_area.height)
174
elif start - end < -1:
175
ctx.line_to(cell_area.x + box_size * start + box_size,
176
bg_area.y + bg_area.height)
177
ctx.line_to(cell_area.x + box_size * end,
178
bg_area.y + bg_area.height)
180
ctx.line_to(cell_area.x + box_size * end + box_size / 2,
181
bg_area.y + bg_area.height / 2 + bg_area.height)
183
self.set_colour(ctx, colour, 0.0, 0.65)
186
# Draw the revision node in the right column
187
(column, colour) = self.node
188
ctx.arc(cell_area.x + box_size * column + box_size / 2,
189
cell_area.y + cell_area.height / 2,
190
box_size / 4, 0, 2 * math.pi)
192
self.set_colour(ctx, colour, 0.0, 0.5)
193
ctx.stroke_preserve()
195
self.set_colour(ctx, colour, 0.5, 1.0)