/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

  • Committer: Curtis Hovey
  • Date: 2011-09-03 01:25:04 UTC
  • mto: This revision was merged to the branch mainline in revision 741.
  • Revision ID: sinzui.is@verizon.net-20110903012504-0jr4diz9033g5df2
Menu fixes.

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 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 cairo
 
23
 
 
24
 
 
25
class CellRendererGraph(Gtk.CellRenderer):
 
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
    columns_len = 0
 
35
 
 
36
    __gproperties__ = {
 
37
        "node":         ( GObject.TYPE_PYOBJECT, "node",
 
38
                          "revision node instruction",
 
39
                          GObject.PARAM_WRITABLE
 
40
                        ),
 
41
        "tags":         ( GObject.TYPE_PYOBJECT, "tags",
 
42
                          "list of tags associated with the node",
 
43
                          GObject.PARAM_WRITABLE
 
44
                        ),
 
45
        "in-lines":     ( GObject.TYPE_PYOBJECT, "in-lines",
 
46
                          "instructions to draw lines into the cell",
 
47
                          GObject.PARAM_WRITABLE
 
48
                        ),
 
49
        "out-lines":    ( GObject.TYPE_PYOBJECT, "out-lines",
 
50
                          "instructions to draw lines out of the cell",
 
51
                          GObject.PARAM_WRITABLE
 
52
                        ),
 
53
        }
 
54
 
 
55
    def do_set_property(self, property, value):
 
56
        """Set properties from GObject properties."""
 
57
        if property.name == "node":
 
58
            self.node = value
 
59
        elif property.name == "tags":
 
60
            self.tags = value
 
61
        elif property.name == "in-lines":
 
62
            self.in_lines = value
 
63
        elif property.name == "out-lines":
 
64
            self.out_lines = value
 
65
        else:
 
66
            raise AttributeError, "no such property: '%s'" % property.name
 
67
 
 
68
    def box_size(self, widget):
 
69
        """Calculate box size based on widget's font.
 
70
 
 
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.
 
73
        """
 
74
        try:
 
75
            return self._box_size
 
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)
 
80
 
 
81
            def PANGO_PIXELS(d):
 
82
                # Macro from  Pango header.
 
83
                return (d + 512) / 1000
 
84
 
 
85
            ascent = PANGO_PIXELS(metrics.get_ascent())
 
86
            descent = PANGO_PIXELS(metrics.get_descent())
 
87
 
 
88
            self._box_size = ascent + descent + 6
 
89
            return self._box_size
 
90
 
 
91
    def set_colour(self, ctx, colour, bg, fg):
 
92
        """Set the context source colour.
 
93
 
 
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.
 
98
        """
 
99
        mainline_color = ( 0.0, 0.0, 0.0 )
 
100
        colours = [
 
101
            ( 1.0, 0.0, 0.0 ),
 
102
            ( 1.0, 1.0, 0.0 ),
 
103
            ( 0.0, 1.0, 0.0 ),
 
104
            ( 0.0, 1.0, 1.0 ),
 
105
            ( 0.0, 0.0, 1.0 ),
 
106
            ( 1.0, 0.0, 1.0 ),
 
107
            ]
 
108
 
 
109
        if colour == 0:
 
110
            colour_rgb = mainline_color
 
111
        else:
 
112
            colour_rgb = colours[colour % len(colours)]
 
113
 
 
114
        red   = (colour_rgb[0] * fg) or bg
 
115
        green = (colour_rgb[1] * fg) or bg
 
116
        blue  = (colour_rgb[2] * fg) or bg
 
117
 
 
118
        ctx.set_source_rgb(red, green, blue)
 
119
 
 
120
    def on_get_size(self, widget, cell_area):
 
121
        """Return the size we need for this cell.
 
122
 
 
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
 
125
        line up.
 
126
        """
 
127
        box_size = self.box_size(widget) + 1
 
128
 
 
129
        width = box_size * (self.columns_len + 1)
 
130
        height = box_size
 
131
 
 
132
        # FIXME I have no idea how to use cell_area properly
 
133
        return (0, 0, width, height)
 
134
 
 
135
    def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
 
136
        """Render an individual cell.
 
137
 
 
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
 
142
        want any gutter.
 
143
 
 
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
 
147
        incredible amount.
 
148
        """
 
149
        ctx = window.cairo_create()
 
150
        ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
 
151
        ctx.clip()
 
152
 
 
153
        box_size = self.box_size(widget)
 
154
 
 
155
        ctx.set_line_width(box_size / 8)
 
156
 
 
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)
 
162
 
 
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)
 
168
 
 
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)
 
174
 
 
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)
 
180
 
 
181
        self.set_colour(ctx, colour, 0.0, 0.5)
 
182
        ctx.stroke_preserve()
 
183
 
 
184
        self.set_colour(ctx, colour, 0.5, 1.0)
 
185
        ctx.fill()
 
186
 
 
187
        self.render_tags(ctx, widget.create_pango_context(), cell_area, box_size)
 
188
 
 
189
    def render_line(self, ctx, cell_area, box_size, mid, height, start, end, colour, flags):
 
190
        if start is None:
 
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)
 
197
 
 
198
        elif end is None:
 
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)
 
205
 
 
206
        else:
 
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
 
210
 
 
211
            ctx.move_to(startx, mid - height / 2)
 
212
 
 
213
            if start - end == 0 :
 
214
                ctx.line_to(endx, mid + height / 2 + 1)
 
215
            else:
 
216
                ctx.curve_to(startx, mid - height / 5,
 
217
                             startx, mid - height / 5,
 
218
                             startx + (endx - startx) / 2, mid)
 
219
 
 
220
                ctx.curve_to(endx, mid + height / 5,
 
221
                             endx, mid + height / 5 ,
 
222
                             endx, mid + height / 2 + 1)
 
223
 
 
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)
 
229
 
 
230
        self.set_colour(ctx, colour, 0.0, 0.65)
 
231
 
 
232
        ctx.stroke()
 
233
 
 
234
    def render_tags(self, ctx, pango_ctx, cell_area, box_size):
 
235
        # colour ID used in self.set_colour on the tags
 
236
        TAG_COLOUR_ID = 1
 
237
 
 
238
        (column, colour) = self.node
 
239
 
 
240
        font_desc = Pango.FontDescription()
 
241
        font_desc.set_size(Pango.SCALE * 7)
 
242
 
 
243
        tag_layout = Pango.Layout(pango_ctx)
 
244
        tag_layout.set_font_description(font_desc)
 
245
 
 
246
        # The width of the tag label stack
 
247
        width = 0
 
248
 
 
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()
 
252
 
 
253
            x0 = cell_area.x + \
 
254
                 box_size * (column + 1.3) + width
 
255
 
 
256
            y0 = cell_area.y + \
 
257
                 cell_area.height / 2 - \
 
258
                 text_height / 2
 
259
 
 
260
            width += text_width + 5
 
261
 
 
262
            # Draw the tag border
 
263
            ctx.move_to(x0 - box_size / 3, y0 + text_height / 2)
 
264
            ctx.line_to(x0, y0)
 
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)
 
269
 
 
270
            ctx.new_sub_path()
 
271
            ctx.arc(x0 - box_size / 12,
 
272
                        y0 + text_height / 2,
 
273
                        box_size / 7,
 
274
                        0, 2 * math.pi);
 
275
 
 
276
            self.set_colour(ctx, TAG_COLOUR_ID, 0.0, 0.5)
 
277
            ctx.stroke_preserve()
 
278
 
 
279
            ctx.set_fill_rule (cairo.FILL_RULE_EVEN_ODD)
 
280
            self.set_colour(ctx, TAG_COLOUR_ID, 0.5, 1.0)
 
281
            ctx.fill()
 
282
 
 
283
            # Draw the tag text
 
284
            self.set_colour(ctx, 0, 0.0, 0.0)
 
285
            ctx.move_to(x0, y0)
 
286
            ctx.show_layout(tag_layout)
 
287