/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to viz/graphcell.py

  • Committer: Aaron Bentley
  • Date: 2006-03-20 14:07:43 UTC
  • mfrom: (0.4.1 gannotate)
  • mto: This revision was merged to the branch mainline in revision 49.
  • Revision ID: abentley@panoramicfeedback.com-20060320140743-c0e744f040f51301
Merge other changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- coding: UTF-8 -*-
3
 
"""Cell renderer for directed graph.
4
 
 
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
7
 
in graph.py.
8
 
 
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.
12
 
"""
13
 
 
14
 
__copyright__ = "Copyright © 2005 Canonical Ltd."
15
 
__author__    = "Scott James Remnant <scott@ubuntu.com>"
16
 
 
17
 
 
18
 
import math
19
 
 
20
 
import gtk
21
 
import gobject
22
 
import pango
23
 
import cairo
24
 
 
25
 
 
26
 
class CellRendererGraph(gtk.GenericCellRenderer):
27
 
    """Cell renderer for directed graph.
28
 
 
29
 
    Properties:
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.
33
 
    """
34
 
 
35
 
    __gproperties__ = {
36
 
        "node":         ( gobject.TYPE_PYOBJECT, "node",
37
 
                          "revision node instruction",
38
 
                          gobject.PARAM_WRITABLE
39
 
                        ),
40
 
        "in-lines":     ( gobject.TYPE_PYOBJECT, "in-lines",
41
 
                          "instructions to draw lines into the cell",
42
 
                          gobject.PARAM_WRITABLE
43
 
                        ),
44
 
        "out-lines":    ( gobject.TYPE_PYOBJECT, "out-lines",
45
 
                          "instructions to draw lines out of the cell",
46
 
                          gobject.PARAM_WRITABLE
47
 
                        ),
48
 
        }
49
 
 
50
 
    def do_set_property(self, property, value):
51
 
        """Set properties from GObject properties."""
52
 
        if property.name == "node":
53
 
            self.node = value
54
 
        elif property.name == "in-lines":
55
 
            self.in_lines = value
56
 
        elif property.name == "out-lines":
57
 
            self.out_lines = value
58
 
        else:
59
 
            raise AttributeError, "no such property: '%s'" % property.name
60
 
 
61
 
    def box_size(self, widget):
62
 
        """Calculate box size based on widget's font.
63
 
 
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.
66
 
        """
67
 
        try:
68
 
            return self._box_size
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)
73
 
 
74
 
            ascent = pango.PIXELS(metrics.get_ascent())
75
 
            descent = pango.PIXELS(metrics.get_descent())
76
 
 
77
 
            self._box_size = ascent + descent + 6
78
 
            return self._box_size
79
 
 
80
 
    def set_colour(self, ctx, colour, bg, fg):
81
 
        """Set the context source colour.
82
 
 
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.
87
 
        """
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
 
 
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
101
 
 
102
 
        ctx.set_source_rgb(red, green, blue)
103
 
 
104
 
    def on_get_size(self, widget, cell_area):
105
 
        """Return the size we need for this cell.
106
 
 
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
109
 
        line up.
110
 
        """
111
 
        box_size = self.box_size(widget)
112
 
 
113
 
        cols = self.node[0]
114
 
        for start, end, colour in self.in_lines + self.out_lines:
115
 
            cols = max(cols, start, end)
116
 
 
117
 
        width = box_size * (cols + 1)
118
 
        height = box_size
119
 
 
120
 
        # FIXME I have no idea how to use cell_area properly
121
 
        return (0, 0, width, height)
122
 
 
123
 
    def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
124
 
        """Render an individual cell.
125
 
 
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
130
 
        want any gutter.
131
 
 
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
135
 
        incredible amount.
136
 
        """
137
 
        ctx = window.cairo_create()
138
 
        ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
139
 
        ctx.clip()
140
 
 
141
 
        box_size = self.box_size(widget)
142
 
 
143
 
        ctx.set_line_width(box_size / 8)
144
 
        ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
145
 
 
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)
150
 
 
151
 
            if start - end > 1:
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,
156
 
                            bg_area.y)
157
 
                ctx.line_to(cell_area.x + box_size * end, bg_area.y)
158
 
 
159
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
160
 
                        bg_area.y + bg_area.height / 2)
161
 
 
162
 
            self.set_colour(ctx, colour, 0.0, 0.65)
163
 
            ctx.stroke()
164
 
 
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)
169
 
 
170
 
            if start - end > 1:
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)
180
 
 
181
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
182
 
                        bg_area.y + bg_area.height / 2 + bg_area.height)
183
 
 
184
 
            self.set_colour(ctx, colour, 0.0, 0.65)
185
 
            ctx.stroke()
186
 
 
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 / 4, 0, 2 * math.pi)
192
 
 
193
 
        self.set_colour(ctx, colour, 0.0, 0.5)
194
 
        ctx.stroke_preserve()
195
 
 
196
 
        self.set_colour(ctx, colour, 0.5, 1.0)
197
 
        ctx.fill()