/b-gtk/fix-viz

To get this branch, use:
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
    """
33
34
    __gproperties__ = {
35
        "node":         ( gobject.TYPE_PYOBJECT, "node",
36
                          "revision node instruction",
37
                          gobject.PARAM_WRITABLE
38
                        ),
39
        "in-lines":     ( gobject.TYPE_PYOBJECT, "in-lines",
40
                          "instructions to draw lines into the cell",
41
                          gobject.PARAM_WRITABLE
42
                        ),
43
        "out-lines":    ( gobject.TYPE_PYOBJECT, "out-lines",
44
                          "instructions to draw lines out of the cell",
45
                          gobject.PARAM_WRITABLE
46
                        ),
47
        }
48
49
    def do_set_property(self, property, value):
50
        """Set properties from GObject properties."""
51
        if property.name == "node":
52
            self.node = value
53
        elif property.name == "in-lines":
54
            self.in_lines = value
55
        elif property.name == "out-lines":
56
            self.out_lines = value
57
        else:
58
            raise AttributeError, "no such property: '%s'" % property.name
59
60
    def box_size(self, widget):
61
        """Calculate box size based on widget's font.
62
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.
65
        """
66
        try:
67
            return self._box_size
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)
72
73
            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
74
            descent = pango.PIXELS(metrics.get_descent())
1 by Scott James Remnant
Commit the first version of bzrk.
75
9 by Scott James Remnant
Fix the busted font size stuff, and then increase the sizes a bit to
76
            self._box_size = ascent + descent + 6
1 by Scott James Remnant
Commit the first version of bzrk.
77
            return self._box_size
78
79
    def set_colour(self, ctx, colour, bg, fg):
80
        """Set the context source colour.
81
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.
86
        """
256.2.43 by Gary van der Merwe
Make the mainline allways black - which often makes the graph easier to read.
87
        mainline_color = ( 0.0, 0.0, 0.0 )
1 by Scott James Remnant
Commit the first version of bzrk.
88
        colours = [
89
            ( 1.0, 0.0, 0.0 ),
90
            ( 1.0, 1.0, 0.0 ),
91
            ( 0.0, 1.0, 0.0 ),
92
            ( 0.0, 1.0, 1.0 ),
93
            ( 0.0, 0.0, 1.0 ),
94
            ( 1.0, 0.0, 1.0 ),
95
            ]
96
256.2.43 by Gary van der Merwe
Make the mainline allways black - which often makes the graph easier to read.
97
        if colour == 0:
98
            colour_rgb = mainline_color
99
        else:
100
            colour_rgb = colours[colour % len(colours)]
101
102
        red   = (colour_rgb[0] * fg) or bg
103
        green = (colour_rgb[1] * fg) or bg
104
        blue  = (colour_rgb[2] * fg) or bg
1 by Scott James Remnant
Commit the first version of bzrk.
105
106
        ctx.set_source_rgb(red, green, blue)
107
108
    def on_get_size(self, widget, cell_area):
109
        """Return the size we need for this cell.
110
111
        Each cell is drawn individually and is only as wide as it needs
112
        to be, we let the TreeViewColumn take care of making them all
113
        line up.
114
        """
66.2.19 by Aaron Bentley
Increase box height by 1 to fix diagonal jagginess
115
        box_size = self.box_size(widget) + 1
1 by Scott James Remnant
Commit the first version of bzrk.
116
117
        cols = self.node[0]
118
        for start, end, colour in self.in_lines + self.out_lines:
119
            cols = max(cols, start, end)
120
121
        width = box_size * (cols + 1)
122
        height = box_size
123
124
        # FIXME I have no idea how to use cell_area properly
125
        return (0, 0, width, height)
126
127
    def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
128
        """Render an individual cell.
129
130
        Draws the cell contents using cairo, taking care to clip what we
131
        do to within the background area so we don't draw over other cells.
132
        Note that we're a bit naughty there and should really be drawing
133
        in the cell_area (or even the exposed area), but we explicitly don't
134
        want any gutter.
135
136
        We try and be a little clever, if the line we need to draw is going
137
        to cross other columns we actually draw it as in the .---' style
138
        instead of a pure diagonal ... this reduces confusion by an
139
        incredible amount.
140
        """
141
        ctx = window.cairo_create()
142
        ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
143
        ctx.clip()
144
6 by Scott James Remnant
Also increase the width of the lines in accordance with the font size.
145
        box_size = self.box_size(widget)
146
9 by Scott James Remnant
Fix the busted font size stuff, and then increase the sizes a bit to
147
        ctx.set_line_width(box_size / 8)
1 by Scott James Remnant
Commit the first version of bzrk.
148
        ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
149
150
        # Draw lines into the cell
151
        for start, end, colour in self.in_lines:
256.2.7 by Gary van der Merwe
Use nice Bézier curves for viz
152
            self.render_line (ctx, cell_area, box_size,
153
                         bg_area.y, bg_area.height,
154
                         start, end, colour)
1 by Scott James Remnant
Commit the first version of bzrk.
155
156
        # Draw lines out of the cell
157
        for start, end, colour in self.out_lines:
256.2.7 by Gary van der Merwe
Use nice Bézier curves for viz
158
            self.render_line (ctx, cell_area, box_size,
159
                         bg_area.y + bg_area.height, bg_area.height,
160
                         start, end, colour)
1 by Scott James Remnant
Commit the first version of bzrk.
161
162
        # Draw the revision node in the right column
163
        (column, colour) = self.node
164
        ctx.arc(cell_area.x + box_size * column + box_size / 2,
165
                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
166
                box_size / 4, 0, 2 * math.pi)
1 by Scott James Remnant
Commit the first version of bzrk.
167
168
        self.set_colour(ctx, colour, 0.0, 0.5)
169
        ctx.stroke_preserve()
170
171
        self.set_colour(ctx, colour, 0.5, 1.0)
172
        ctx.fill()
256.2.7 by Gary van der Merwe
Use nice Bézier curves for viz
173
    
174
    def render_line (self, ctx, cell_area, box_size, mid, height, start, end, colour):
175
        startx = cell_area.x + box_size * start + box_size / 2
176
        endx = cell_area.x + box_size * end + box_size / 2
177
        
178
        ctx.move_to(startx, mid - height / 2)
179
        
180
        if start - end == 0 :
181
            ctx.line_to(endx, mid + height / 2)
182
        else:
183
            ctx.curve_to(startx, mid - height / 5,
184
                         startx, mid - height / 5,
185
                         startx + (endx - startx) / 2, mid)
186
            
187
            ctx.curve_to(endx, mid + height / 5,
188
                         endx, mid + height / 5 ,
189
                         endx, mid + height / 2)
190
            
191
        self.set_colour(ctx, colour, 0.0, 0.65)
192
        ctx.stroke()
193