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>"
19
from gi.repository import Gtk
20
from gi.repository import GObject
21
from gi.repository import Pango
22
from gi.repository import cairo
25
class CellRendererGraph(Gtk.CellRenderer):
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.
37
"node": ( GObject.TYPE_PYOBJECT, "node",
38
"revision node instruction",
39
GObject.PARAM_WRITABLE
41
"tags": ( GObject.TYPE_PYOBJECT, "tags",
42
"list of tags associated with the node",
43
GObject.PARAM_WRITABLE
45
"in-lines": ( GObject.TYPE_PYOBJECT, "in-lines",
46
"instructions to draw lines into the cell",
47
GObject.PARAM_WRITABLE
49
"out-lines": ( GObject.TYPE_PYOBJECT, "out-lines",
50
"instructions to draw lines out of the cell",
51
GObject.PARAM_WRITABLE
55
def do_set_property(self, property, value):
56
"""Set properties from GObject properties."""
57
if property.name == "node":
59
elif property.name == "tags":
61
elif property.name == "in-lines":
63
elif property.name == "out-lines":
64
self.out_lines = value
66
raise AttributeError, "no such property: '%s'" % property.name
68
def box_size(self, widget):
69
"""Calculate box size based on widget's font.
71
Cache this as it's probably expensive to get. It ensures that we
72
draw the graph at least as large as the text.
76
except AttributeError:
77
pango_ctx = widget.get_pango_context()
78
font_desc = widget.get_style().font_desc
79
metrics = pango_ctx.get_metrics(font_desc, None)
82
# Macro from Pango header.
83
return (d + 512) / 1000
85
ascent = PANGO_PIXELS(metrics.get_ascent())
86
descent = PANGO_PIXELS(metrics.get_descent())
88
self._box_size = ascent + descent + 6
91
def set_colour(self, ctx, colour, bg, fg):
92
"""Set the context source colour.
94
Picks a distinct colour based on an internal wheel; the bg
95
parameter provides the value that should be assigned to the 'zero'
96
colours and the fg parameter provides the multiplier that should be
97
applied to the foreground colours.
99
mainline_color = ( 0.0, 0.0, 0.0 )
110
colour_rgb = mainline_color
112
colour_rgb = colours[colour % len(colours)]
114
red = (colour_rgb[0] * fg) or bg
115
green = (colour_rgb[1] * fg) or bg
116
blue = (colour_rgb[2] * fg) or bg
118
ctx.set_source_rgb(red, green, blue)
120
def on_get_size(self, widget, cell_area):
121
"""Return the size we need for this cell.
123
Each cell is drawn individually and is only as wide as it needs
124
to be, we let the TreeViewColumn take care of making them all
127
box_size = self.box_size(widget) + 1
129
width = box_size * (self.columns_len + 1)
132
# FIXME I have no idea how to use cell_area properly
133
return (0, 0, width, height)
135
def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
136
"""Render an individual cell.
138
Draws the cell contents using cairo, taking care to clip what we
139
do to within the background area so we don't draw over other cells.
140
Note that we're a bit naughty there and should really be drawing
141
in the cell_area (or even the exposed area), but we explicitly don't
144
We try and be a little clever, if the line we need to draw is going
145
to cross other columns we actually draw it as in the .---' style
146
instead of a pure diagonal ... this reduces confusion by an
149
ctx = window.cairo_create()
150
ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
153
box_size = self.box_size(widget)
155
ctx.set_line_width(box_size / 8)
157
# Draw lines into the cell
158
for start, end, colour in self.in_lines:
159
self.render_line (ctx, cell_area, box_size,
160
bg_area.y, bg_area.height,
161
start, end, colour, flags)
163
# Draw lines out of the cell
164
for start, end, colour in self.out_lines:
165
self.render_line (ctx, cell_area, box_size,
166
bg_area.y + bg_area.height, bg_area.height,
167
start, end, colour, flags)
169
# Draw the revision node in the right column
170
(column, colour) = self.node
171
ctx.arc(cell_area.x + box_size * column + box_size / 2,
172
cell_area.y + cell_area.height / 2,
173
box_size / 4, 0, 2 * math.pi)
175
if flags & Gtk.CELL_RENDERER_SELECTED:
176
ctx.set_source_rgb(1.0, 1.0, 1.0)
177
ctx.set_line_width(box_size / 4)
178
ctx.stroke_preserve()
179
ctx.set_line_width(box_size / 8)
181
self.set_colour(ctx, colour, 0.0, 0.5)
182
ctx.stroke_preserve()
184
self.set_colour(ctx, colour, 0.5, 1.0)
187
self.render_tags(ctx, widget.create_pango_context(), cell_area, box_size)
189
def render_line(self, ctx, cell_area, box_size, mid, height, start, end, colour, flags):
191
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
192
x = cell_area.x + box_size * end + box_size / 2
193
ctx.move_to(x, mid + height / 3)
194
ctx.line_to(x, mid + height / 3)
195
ctx.move_to(x, mid + height / 6)
196
ctx.line_to(x, mid + height / 6)
199
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
200
x = cell_area.x + box_size * start + box_size / 2
201
ctx.move_to(x, mid - height / 3)
202
ctx.line_to(x, mid - height / 3)
203
ctx.move_to(x, mid - height / 6)
204
ctx.line_to(x, mid - height / 6)
207
ctx.set_line_cap(cairo.LINE_CAP_BUTT)
208
startx = cell_area.x + box_size * start + box_size / 2
209
endx = cell_area.x + box_size * end + box_size / 2
211
ctx.move_to(startx, mid - height / 2)
213
if start - end == 0 :
214
ctx.line_to(endx, mid + height / 2 + 1)
216
ctx.curve_to(startx, mid - height / 5,
217
startx, mid - height / 5,
218
startx + (endx - startx) / 2, mid)
220
ctx.curve_to(endx, mid + height / 5,
221
endx, mid + height / 5 ,
222
endx, mid + height / 2 + 1)
224
if flags & Gtk.CELL_RENDERER_SELECTED:
225
ctx.set_source_rgb(1.0, 1.0, 1.0)
226
ctx.set_line_width(box_size / 5)
227
ctx.stroke_preserve()
228
ctx.set_line_width(box_size / 8)
230
self.set_colour(ctx, colour, 0.0, 0.65)
234
def render_tags(self, ctx, pango_ctx, cell_area, box_size):
235
# colour ID used in self.set_colour on the tags
238
(column, colour) = self.node
240
font_desc = Pango.FontDescription()
241
font_desc.set_size(Pango.SCALE * 7)
243
tag_layout = Pango.Layout(pango_ctx)
244
tag_layout.set_font_description(font_desc)
246
# The width of the tag label stack
249
for tag_idx, tag in enumerate(self.tags):
250
tag_layout.set_text(" " + tag + " ")
251
text_width, text_height = tag_layout.get_pixel_size()
254
box_size * (column + 1.3) + width
257
cell_area.height / 2 - \
260
width += text_width + 5
262
# Draw the tag border
263
ctx.move_to(x0 - box_size / 3, y0 + text_height / 2)
265
ctx.line_to(x0 + text_width, y0)
266
ctx.line_to(x0 + text_width, y0 + text_height)
267
ctx.line_to(x0, y0 + text_height)
268
ctx.line_to(x0 - box_size / 3, y0 + text_height / 2)
271
ctx.arc(x0 - box_size / 12,
272
y0 + text_height / 2,
276
self.set_colour(ctx, TAG_COLOUR_ID, 0.0, 0.5)
277
ctx.stroke_preserve()
279
ctx.set_fill_rule (cairo.FILL_RULE_EVEN_ODD)
280
self.set_colour(ctx, TAG_COLOUR_ID, 0.5, 1.0)
284
self.set_colour(ctx, 0, 0.0, 0.0)
286
ctx.show_layout(tag_layout)