/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 branchview/graphcell.py

Commit messages never contain config options

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2011 Canonical Ltd."
 
14
__author__    = "Scott James Remnant <scott@ubuntu.com>"
 
15
 
 
16
 
 
17
import math
 
18
 
 
19
from gi.repository import Gtk
 
20
from gi.repository import GObject
 
21
from gi.repository import Pango
 
22
from gi.repository import PangoCairo
 
23
 
 
24
 
 
25
# Cairo constants are not exported yet. These are taken from documentation.
 
26
CAIRO_LINE_CAP_BUTT = 0
 
27
CAIRO_LINE_CAP_ROUND = 1
 
28
CAIRO_LINE_CAP_SQUARE = 2
 
29
 
 
30
 
 
31
CAIRO_FILL_RULE_WINDING = 0
 
32
CAIRO_FILL_RULE_EVEN_ODD = 1
 
33
 
 
34
 
 
35
# Macro from  Pango header.
 
36
def PANGO_PIXELS(d):
 
37
    return (d + 512) / 1000
 
38
 
 
39
 
 
40
class CellRendererGraph(Gtk.CellRendererPixbuf):
 
41
    """Cell renderer for directed graph.
 
42
 
 
43
    Properties:
 
44
      node              (column, colour) tuple to draw revision node,
 
45
      in_lines          (start, end, colour) tuple list to draw inward lines,
 
46
      out_lines         (start, end, colour) tuple list to draw outward lines.
 
47
    """
 
48
 
 
49
    columns_len = 0
 
50
 
 
51
    __gproperties__ = {
 
52
        "node":         ( GObject.TYPE_PYOBJECT, "node",
 
53
                          "revision node instruction",
 
54
                          GObject.PARAM_WRITABLE
 
55
                        ),
 
56
        "tags":         ( GObject.TYPE_PYOBJECT, "tags",
 
57
                          "list of tags associated with the node",
 
58
                          GObject.PARAM_WRITABLE
 
59
                        ),
 
60
        "in-lines":     ( GObject.TYPE_PYOBJECT, "in-lines",
 
61
                          "instructions to draw lines into the cell",
 
62
                          GObject.PARAM_WRITABLE
 
63
                        ),
 
64
        "out-lines":    ( GObject.TYPE_PYOBJECT, "out-lines",
 
65
                          "instructions to draw lines out of the cell",
 
66
                          GObject.PARAM_WRITABLE
 
67
                        ),
 
68
        }
 
69
 
 
70
    def do_set_property(self, property, value):
 
71
        """Set properties from GObject properties."""
 
72
        if property.name == "node":
 
73
            self.node = value
 
74
        elif property.name == "tags":
 
75
            self.tags = value
 
76
        elif property.name == "in-lines":
 
77
            self.in_lines = value
 
78
        elif property.name == "out-lines":
 
79
            self.out_lines = value
 
80
        else:
 
81
            raise AttributeError, "no such property: '%s'" % property.name
 
82
 
 
83
    def box_size(self, widget):
 
84
        """Calculate box size based on widget's font.
 
85
 
 
86
        Cache this as it's probably expensive to get.  It ensures that we
 
87
        draw the graph at least as large as the text.
 
88
        """
 
89
        try:
 
90
            return self._box_size
 
91
        except AttributeError:
 
92
            pango_ctx = widget.get_pango_context()
 
93
            font_desc = widget.get_style_context().get_font(
 
94
                Gtk.StateType.NORMAL)
 
95
            metrics = pango_ctx.get_metrics(font_desc, None)
 
96
            ascent = PANGO_PIXELS(metrics.get_ascent())
 
97
            descent = PANGO_PIXELS(metrics.get_descent())
 
98
            self._box_size = ascent + descent + 6
 
99
            return self._box_size
 
100
 
 
101
    def set_colour(self, ctx, colour, bg, fg):
 
102
        """Set the context source colour.
 
103
 
 
104
        Picks a distinct colour based on an internal wheel; the bg
 
105
        parameter provides the value that should be assigned to the 'zero'
 
106
        colours and the fg parameter provides the multiplier that should be
 
107
        applied to the foreground colours.
 
108
        """
 
109
        mainline_color = ( 0.0, 0.0, 0.0 )
 
110
        colours = [
 
111
            ( 1.0, 0.0, 0.0 ),
 
112
            ( 1.0, 1.0, 0.0 ),
 
113
            ( 0.0, 1.0, 0.0 ),
 
114
            ( 0.0, 1.0, 1.0 ),
 
115
            ( 0.0, 0.0, 1.0 ),
 
116
            ( 1.0, 0.0, 1.0 ),
 
117
            ]
 
118
 
 
119
        if colour == 0:
 
120
            colour_rgb = mainline_color
 
121
        else:
 
122
            colour_rgb = colours[colour % len(colours)]
 
123
 
 
124
        red   = (colour_rgb[0] * fg) or bg
 
125
        green = (colour_rgb[1] * fg) or bg
 
126
        blue  = (colour_rgb[2] * fg) or bg
 
127
 
 
128
        ctx.set_source_rgb(red, green, blue)
 
129
 
 
130
    def do_activate(event, widget, path, bg_area, cell_area, flags):
 
131
        """Renderers cannot be activated; always return True."""
 
132
        return True
 
133
 
 
134
    def do_editing_started(event, widget, path, fb_area, cell_area, flags):
 
135
        """Renderers cannot be edited; always return None."""
 
136
        return None
 
137
 
 
138
    def do_get_size(self, widget, cell_area):
 
139
        """Return the size we need for this cell.
 
140
 
 
141
        Each cell is drawn individually and is only as wide as it needs
 
142
        to be, we let the TreeViewColumn take care of making them all
 
143
        line up.
 
144
        """
 
145
        box_size = self.box_size(widget) + 1
 
146
 
 
147
        width = box_size * (self.columns_len + 1)
 
148
        height = box_size
 
149
 
 
150
        # FIXME I have no idea how to use cell_area properly
 
151
        return (0, 0, width, height)
 
152
 
 
153
    def do_render(self, ctx, widget, bg_area, cell_area, flags):
 
154
        """Render an individual cell.
 
155
 
 
156
        Draws the cell contents using cairo, taking care to clip what we
 
157
        do to within the background area so we don't draw over other cells.
 
158
        Note that we're a bit naughty there and should really be drawing
 
159
        in the cell_area (or even the exposed area), but we explicitly don't
 
160
        want any gutter.
 
161
 
 
162
        We try and be a little clever, if the line we need to draw is going
 
163
        to cross other columns we actually draw it as in the .---' style
 
164
        instead of a pure diagonal ... this reduces confusion by an
 
165
        incredible amount.
 
166
        """
 
167
        ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
 
168
        ctx.clip()
 
169
 
 
170
        box_size = self.box_size(widget)
 
171
 
 
172
        ctx.set_line_width(box_size / 8)
 
173
 
 
174
        # Draw lines into the cell
 
175
        for start, end, colour in self.in_lines:
 
176
            self.render_line (ctx, cell_area, box_size,
 
177
                         bg_area.y, bg_area.height,
 
178
                         start, end, colour, flags)
 
179
 
 
180
        # Draw lines out of the cell
 
181
        for start, end, colour in self.out_lines:
 
182
            self.render_line (ctx, cell_area, box_size,
 
183
                         bg_area.y + bg_area.height, bg_area.height,
 
184
                         start, end, colour, flags)
 
185
 
 
186
        # Draw the revision node in the right column
 
187
        (column, colour) = self.node
 
188
        ctx.arc(cell_area.x + box_size * column + box_size / 2,
 
189
                cell_area.y + cell_area.height / 2,
 
190
                box_size / 4, 0, 2 * math.pi)
 
191
 
 
192
        if flags & Gtk.CellRendererState.SELECTED:
 
193
            ctx.set_source_rgb(1.0, 1.0, 1.0)
 
194
            ctx.set_line_width(box_size / 4)
 
195
            ctx.stroke_preserve()
 
196
            ctx.set_line_width(box_size / 8)
 
197
 
 
198
        self.set_colour(ctx, colour, 0.0, 0.5)
 
199
        ctx.stroke_preserve()
 
200
 
 
201
        self.set_colour(ctx, colour, 0.5, 1.0)
 
202
        ctx.fill()
 
203
 
 
204
        self.render_tags(ctx, widget.create_pango_context(), cell_area, box_size)
 
205
 
 
206
    def render_line(self, ctx, cell_area, box_size,
 
207
                    mid, height, start, end, colour, flags):
 
208
        if start is None:
 
209
            ctx.set_line_cap(CAIRO_LINE_CAP_ROUND)
 
210
            x = cell_area.x + box_size * end + box_size / 2
 
211
            ctx.move_to(x, mid + height / 3)
 
212
            ctx.line_to(x, mid + height / 3)
 
213
            ctx.move_to(x, mid + height / 6)
 
214
            ctx.line_to(x, mid + height / 6)
 
215
 
 
216
        elif end is None:
 
217
            ctx.set_line_cap(CAIRO_LINE_CAP_ROUND)
 
218
            x = cell_area.x + box_size * start + box_size / 2
 
219
            ctx.move_to(x, mid - height / 3)
 
220
            ctx.line_to(x, mid - height / 3)
 
221
            ctx.move_to(x, mid - height / 6)
 
222
            ctx.line_to(x, mid - height / 6)
 
223
 
 
224
        else:
 
225
            ctx.set_line_cap(CAIRO_LINE_CAP_BUTT)
 
226
            startx = cell_area.x + box_size * start + box_size / 2
 
227
            endx = cell_area.x + box_size * end + box_size / 2
 
228
 
 
229
            ctx.move_to(startx, mid - height / 2)
 
230
 
 
231
            if start - end == 0 :
 
232
                ctx.line_to(endx, mid + height / 2 + 1)
 
233
            else:
 
234
                ctx.curve_to(startx, mid - height / 5,
 
235
                             startx, mid - height / 5,
 
236
                             startx + (endx - startx) / 2, mid)
 
237
 
 
238
                ctx.curve_to(endx, mid + height / 5,
 
239
                             endx, mid + height / 5 ,
 
240
                             endx, mid + height / 2 + 1)
 
241
 
 
242
        if flags & Gtk.CellRendererState.SELECTED:
 
243
            ctx.set_source_rgb(1.0, 1.0, 1.0)
 
244
            ctx.set_line_width(box_size / 5)
 
245
            ctx.stroke_preserve()
 
246
            ctx.set_line_width(box_size / 8)
 
247
 
 
248
        self.set_colour(ctx, colour, 0.0, 0.65)
 
249
 
 
250
        ctx.stroke()
 
251
 
 
252
    def render_tags(self, ctx, pango_ctx, cell_area, box_size):
 
253
        # colour ID used in self.set_colour on the tags
 
254
        TAG_COLOUR_ID = 1
 
255
 
 
256
        (column, colour) = self.node
 
257
 
 
258
        font_desc = Pango.FontDescription()
 
259
        font_desc.set_size(Pango.SCALE * 7)
 
260
 
 
261
        tag_layout = Pango.Layout(pango_ctx)
 
262
        tag_layout.set_font_description(font_desc)
 
263
 
 
264
        # The width of the tag label stack
 
265
        width = 0
 
266
 
 
267
        for tag_idx, tag in enumerate(self.tags):
 
268
            tag_layout.set_text(" " + tag + " ", -1)
 
269
            text_width, text_height = tag_layout.get_pixel_size()
 
270
 
 
271
            x0 = cell_area.x + \
 
272
                 box_size * (column + 1.3) + width
 
273
 
 
274
            y0 = cell_area.y + \
 
275
                 cell_area.height / 2 - \
 
276
                 text_height / 2
 
277
 
 
278
            width += text_width + 5
 
279
 
 
280
            # Draw the tag border
 
281
            ctx.move_to(x0 - box_size / 3, y0 + text_height / 2)
 
282
            ctx.line_to(x0, y0)
 
283
            ctx.line_to(x0 + text_width, y0)
 
284
            ctx.line_to(x0 + text_width, y0 + text_height)
 
285
            ctx.line_to(x0, y0 + text_height)
 
286
            ctx.line_to(x0 - box_size / 3, y0 + text_height / 2)
 
287
 
 
288
            ctx.new_sub_path()
 
289
            ctx.arc(x0 - box_size / 12,
 
290
                        y0 + text_height / 2,
 
291
                        box_size / 7,
 
292
                        0, 2 * math.pi);
 
293
 
 
294
            self.set_colour(ctx, TAG_COLOUR_ID, 0.0, 0.5)
 
295
            ctx.stroke_preserve()
 
296
 
 
297
            ctx.set_fill_rule (CAIRO_FILL_RULE_EVEN_ODD)
 
298
            self.set_colour(ctx, TAG_COLOUR_ID, 0.5, 1.0)
 
299
            ctx.fill()
 
300
 
 
301
            # Draw the tag text
 
302
            self.set_colour(ctx, 0, 0.0, 0.0)
 
303
            ctx.move_to(x0, y0)
 
304
            PangoCairo.show_layout(ctx, tag_layout)