1
 
# Modified for use with Olive:
 
2
 
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
3
 
# Original copyright holder:
 
4
 
# Copyright (C) 2005 by Canonical Ltd. (Scott James Remnant <scott@ubuntu.com>)
 
6
 
# This program is free software; you can redistribute it and/or modify
 
7
 
# it under the terms of the GNU General Public License as published by
 
8
 
# the Free Software Foundation; either version 2 of the License, or
 
9
 
# (at your option) any later version.
 
11
 
# This program is distributed in the hope that it will be useful,
 
12
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 
# GNU General Public License for more details.
 
16
 
# You should have received a copy of the GNU General Public License
 
17
 
# along with this program; if not, write to the Free Software
 
18
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 
"""Cell renderer for directed graph.
 
22
 
This module contains the implementation of a custom GtkCellRenderer that
 
23
 
draws part of the directed graph based on the lines suggested by the code
 
26
 
Because we're shiny, we use Cairo to do this, and because we're naughty
 
27
 
we cheat and draw over the bits of the TreeViewColumn that are supposed to
 
28
 
just be for the background.
 
31
 
__copyright__ = "Copyright © 2005 Canonical Ltd."
 
32
 
__author__    = "Scott James Remnant <scott@ubuntu.com>"
 
43
 
class CellRendererGraph(gtk.GenericCellRenderer):
 
44
 
    """Cell renderer for directed graph.
 
47
 
      node              (column, colour) tuple to draw revision node,
 
48
 
      in_lines          (start, end, colour) tuple list to draw inward lines,
 
49
 
      out_lines         (start, end, colour) tuple list to draw outward lines.
 
53
 
        "node":         ( gobject.TYPE_PYOBJECT, "node",
 
54
 
                          "revision node instruction",
 
55
 
                          gobject.PARAM_WRITABLE
 
57
 
        "in-lines":     ( gobject.TYPE_PYOBJECT, "in-lines",
 
58
 
                          "instructions to draw lines into the cell",
 
59
 
                          gobject.PARAM_WRITABLE
 
61
 
        "out-lines":    ( gobject.TYPE_PYOBJECT, "out-lines",
 
62
 
                          "instructions to draw lines out of the cell",
 
63
 
                          gobject.PARAM_WRITABLE
 
67
 
    def do_set_property(self, property, value):
 
68
 
        """Set properties from GObject properties."""
 
69
 
        if property.name == "node":
 
71
 
        elif property.name == "in-lines":
 
73
 
        elif property.name == "out-lines":
 
74
 
            self.out_lines = value
 
76
 
            raise AttributeError, "no such property: '%s'" % property.name
 
78
 
    def box_size(self, widget):
 
79
 
        """Calculate box size based on widget's font.
 
81
 
        Cache this as it's probably expensive to get.  It ensures that we
 
82
 
        draw the graph at least as large as the text.
 
86
 
        except AttributeError:
 
87
 
            pango_ctx = widget.get_pango_context()
 
88
 
            font_desc = widget.get_style().font_desc
 
89
 
            metrics = pango_ctx.get_metrics(font_desc)
 
91
 
            ascent = pango.PIXELS(metrics.get_ascent())
 
92
 
            descent = pango.PIXELS(metrics.get_descent())
 
94
 
            self._box_size = ascent + descent + 6
 
97
 
    def set_colour(self, ctx, colour, bg, fg):
 
98
 
        """Set the context source colour.
 
100
 
        Picks a distinct colour based on an internal wheel; the bg
 
101
 
        parameter provides the value that should be assigned to the 'zero'
 
102
 
        colours and the fg parameter provides the multiplier that should be
 
103
 
        applied to the foreground colours.
 
114
 
        colour %= len(colours)
 
115
 
        red   = (colours[colour][0] * fg) or bg
 
116
 
        green = (colours[colour][1] * fg) or bg
 
117
 
        blue  = (colours[colour][2] * fg) or bg
 
119
 
        ctx.set_source_rgb(red, green, blue)
 
121
 
    def on_get_size(self, widget, cell_area):
 
122
 
        """Return the size we need for this cell.
 
124
 
        Each cell is drawn individually and is only as wide as it needs
 
125
 
        to be, we let the TreeViewColumn take care of making them all
 
128
 
        box_size = self.box_size(widget)
 
131
 
        for start, end, colour in self.in_lines + self.out_lines:
 
132
 
            cols = max(cols, start, end)
 
134
 
        width = box_size * (cols + 1)
 
137
 
        # FIXME I have no idea how to use cell_area properly
 
138
 
        return (0, 0, width, height)
 
140
 
    def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
 
141
 
        """Render an individual cell.
 
143
 
        Draws the cell contents using cairo, taking care to clip what we
 
144
 
        do to within the background area so we don't draw over other cells.
 
145
 
        Note that we're a bit naughty there and should really be drawing
 
146
 
        in the cell_area (or even the exposed area), but we explicitly don't
 
149
 
        We try and be a little clever, if the line we need to draw is going
 
150
 
        to cross other columns we actually draw it as in the .---' style
 
151
 
        instead of a pure diagonal ... this reduces confusion by an
 
154
 
        ctx = window.cairo_create()
 
155
 
        ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
 
158
 
        box_size = self.box_size(widget)
 
160
 
        ctx.set_line_width(box_size / 8)
 
161
 
        ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
 
163
 
        # Draw lines into the cell
 
164
 
        for start, end, colour in self.in_lines:
 
165
 
            ctx.move_to(cell_area.x + box_size * start + box_size / 2,
 
166
 
                        bg_area.y - bg_area.height / 2)
 
169
 
                ctx.line_to(cell_area.x + box_size * start, bg_area.y)
 
170
 
                ctx.line_to(cell_area.x + box_size * end + box_size, bg_area.y)
 
171
 
            elif start - end < -1:
 
172
 
                ctx.line_to(cell_area.x + box_size * start + box_size,
 
174
 
                ctx.line_to(cell_area.x + box_size * end, bg_area.y)
 
176
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
 
177
 
                        bg_area.y + bg_area.height / 2)
 
179
 
            self.set_colour(ctx, colour, 0.0, 0.65)
 
182
 
        # Draw lines out of the cell
 
183
 
        for start, end, colour in self.out_lines:
 
184
 
            ctx.move_to(cell_area.x + box_size * start + box_size / 2,
 
185
 
                        bg_area.y + bg_area.height / 2)
 
188
 
                ctx.line_to(cell_area.x + box_size * start,
 
189
 
                            bg_area.y + bg_area.height)
 
190
 
                ctx.line_to(cell_area.x + box_size * end + box_size,
 
191
 
                            bg_area.y + bg_area.height)
 
192
 
            elif start - end < -1:
 
193
 
                ctx.line_to(cell_area.x + box_size * start + box_size,
 
194
 
                            bg_area.y + bg_area.height)
 
195
 
                ctx.line_to(cell_area.x + box_size * end,
 
196
 
                            bg_area.y + bg_area.height)
 
198
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
 
199
 
                        bg_area.y + bg_area.height / 2 + bg_area.height)
 
201
 
            self.set_colour(ctx, colour, 0.0, 0.65)
 
204
 
        # Draw the revision node in the right column
 
205
 
        (column, colour) = self.node
 
206
 
        ctx.arc(cell_area.x + box_size * column + box_size / 2,
 
207
 
                cell_area.y + cell_area.height / 2,
 
208
 
                box_size / 4, 0, 2 * math.pi)
 
210
 
        self.set_colour(ctx, colour, 0.0, 0.5)
 
211
 
        ctx.stroke_preserve()
 
213
 
        self.set_colour(ctx, colour, 0.5, 1.0)