2
# -*- coding: UTF-8 -*-
3
"""Cell renderer for directed graph.
5
This module contains the implementation of a custom GtkCellRenderer that
6
draws part of the directed graph based on the lines suggested by the code
9
Because we're shiny, we use Cairo to do this, and because we're naughty
10
we cheat and draw over the bits of the TreeViewColumn that are supposed to
11
just be for the background.
14
__copyright__ = "Copyright © 2005 Canonical Ltd."
15
__author__ = "Scott James Remnant <scott@ubuntu.com>"
26
class CellRendererGraph(gtk.GenericCellRenderer):
27
"""Cell renderer for directed graph.
30
node (column, colour) tuple to draw revision node,
31
in_lines (start, end, colour) tuple list to draw inward lines,
32
out_lines (start, end, colour) tuple list to draw outward lines.
36
"node": ( gobject.TYPE_PYOBJECT, "node",
37
"revision node instruction",
38
gobject.PARAM_WRITABLE
40
"in-lines": ( gobject.TYPE_PYOBJECT, "in-lines",
41
"instructions to draw lines into the cell",
42
gobject.PARAM_WRITABLE
44
"out-lines": ( gobject.TYPE_PYOBJECT, "out-lines",
45
"instructions to draw lines out of the cell",
46
gobject.PARAM_WRITABLE
50
def do_set_property(self, property, value):
51
"""Set properties from GObject properties."""
52
if property.name == "node":
54
elif property.name == "in-lines":
56
elif property.name == "out-lines":
57
self.out_lines = value
59
raise AttributeError, "no such property: '%s'" % property.name
61
def box_size(self, widget):
62
"""Calculate box size based on widget's font.
64
Cache this as it's probably expensive to get. It ensures that we
65
draw the graph at least as large as the text.
69
except AttributeError:
70
pango_ctx = widget.get_pango_context()
71
font_desc = widget.get_style().font_desc
72
metrics = pango_ctx.get_metrics(font_desc)
74
ascent = pango.PIXELS(metrics.get_ascent())
75
descent = pango.PIXELS(metrics.get_ascent())
77
self._box_size = ascent + descent
80
def set_colour(self, ctx, colour, bg, fg):
81
"""Set the context source colour.
83
Picks a distinct colour based on an internal wheel; the bg
84
parameter provides the value that should be assigned to the 'zero'
85
colours and the fg parameter provides the multiplier that should be
86
applied to the foreground colours.
97
colour %= len(colours)
98
red = (colours[colour][0] * fg) or bg
99
green = (colours[colour][1] * fg) or bg
100
blue = (colours[colour][2] * fg) or bg
102
ctx.set_source_rgb(red, green, blue)
104
def on_get_size(self, widget, cell_area):
105
"""Return the size we need for this cell.
107
Each cell is drawn individually and is only as wide as it needs
108
to be, we let the TreeViewColumn take care of making them all
111
box_size = self.box_size(widget)
114
for start, end, colour in self.in_lines + self.out_lines:
115
cols = max(cols, start, end)
117
width = box_size * (cols + 1)
120
# FIXME I have no idea how to use cell_area properly
121
return (0, 0, width, height)
123
def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
124
"""Render an individual cell.
126
Draws the cell contents using cairo, taking care to clip what we
127
do to within the background area so we don't draw over other cells.
128
Note that we're a bit naughty there and should really be drawing
129
in the cell_area (or even the exposed area), but we explicitly don't
132
We try and be a little clever, if the line we need to draw is going
133
to cross other columns we actually draw it as in the .---' style
134
instead of a pure diagonal ... this reduces confusion by an
137
ctx = window.cairo_create()
138
ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
141
ctx.set_line_width(2)
142
ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
144
box_size = self.box_size(widget)
146
# Draw lines into the cell
147
for start, end, colour in self.in_lines:
148
ctx.move_to(cell_area.x + box_size * start + box_size / 2,
149
bg_area.y - bg_area.height / 2)
152
ctx.line_to(cell_area.x + box_size * start, bg_area.y)
153
ctx.line_to(cell_area.x + box_size * end + box_size, bg_area.y)
154
elif start - end < -1:
155
ctx.line_to(cell_area.x + box_size * start + box_size,
157
ctx.line_to(cell_area.x + box_size * end, bg_area.y)
159
ctx.line_to(cell_area.x + box_size * end + box_size / 2,
160
bg_area.y + bg_area.height / 2)
162
self.set_colour(ctx, colour, 0.0, 0.65)
165
# Draw lines out of the cell
166
for start, end, colour in self.out_lines:
167
ctx.move_to(cell_area.x + box_size * start + box_size / 2,
168
bg_area.y + bg_area.height / 2)
171
ctx.line_to(cell_area.x + box_size * start,
172
bg_area.y + bg_area.height)
173
ctx.line_to(cell_area.x + box_size * end + box_size,
174
bg_area.y + bg_area.height)
175
elif start - end < -1:
176
ctx.line_to(cell_area.x + box_size * start + box_size,
177
bg_area.y + bg_area.height)
178
ctx.line_to(cell_area.x + box_size * end,
179
bg_area.y + bg_area.height)
181
ctx.line_to(cell_area.x + box_size * end + box_size / 2,
182
bg_area.y + bg_area.height / 2 + bg_area.height)
184
self.set_colour(ctx, colour, 0.0, 0.65)
187
# Draw the revision node in the right column
188
(column, colour) = self.node
189
ctx.arc(cell_area.x + box_size * column + box_size / 2,
190
cell_area.y + cell_area.height / 2,
191
box_size / 5, 0, 2 * math.pi)
193
self.set_colour(ctx, colour, 0.0, 0.5)
194
ctx.stroke_preserve()
196
self.set_colour(ctx, colour, 0.5, 1.0)