/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:
16
16
 
17
17
import math
18
18
 
19
 
import gtk
20
 
import gobject
21
 
import pango
22
 
import cairo
23
 
 
24
 
 
25
 
class CellRendererGraph(gtk.GenericCellRenderer):
 
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
26
    """Cell renderer for directed graph.
27
27
 
28
28
    Properties:
31
31
      out_lines         (start, end, colour) tuple list to draw outward lines.
32
32
    """
33
33
 
 
34
    columns_len = 0
 
35
 
34
36
    __gproperties__ = {
35
 
        "node":         ( gobject.TYPE_PYOBJECT, "node",
 
37
        "node":         ( GObject.TYPE_PYOBJECT, "node",
36
38
                          "revision node instruction",
37
 
                          gobject.PARAM_WRITABLE
38
 
                        ),
39
 
        "in-lines":     ( gobject.TYPE_PYOBJECT, "in-lines",
 
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",
40
46
                          "instructions to draw lines into the cell",
41
 
                          gobject.PARAM_WRITABLE
 
47
                          GObject.PARAM_WRITABLE
42
48
                        ),
43
 
        "out-lines":    ( gobject.TYPE_PYOBJECT, "out-lines",
 
49
        "out-lines":    ( GObject.TYPE_PYOBJECT, "out-lines",
44
50
                          "instructions to draw lines out of the cell",
45
 
                          gobject.PARAM_WRITABLE
 
51
                          GObject.PARAM_WRITABLE
46
52
                        ),
47
53
        }
48
54
 
50
56
        """Set properties from GObject properties."""
51
57
        if property.name == "node":
52
58
            self.node = value
 
59
        elif property.name == "tags":
 
60
            self.tags = value
53
61
        elif property.name == "in-lines":
54
62
            self.in_lines = value
55
63
        elif property.name == "out-lines":
68
76
        except AttributeError:
69
77
            pango_ctx = widget.get_pango_context()
70
78
            font_desc = widget.get_style().font_desc
71
 
            metrics = pango_ctx.get_metrics(font_desc)
72
 
 
73
 
            ascent = pango.PIXELS(metrics.get_ascent())
74
 
            descent = pango.PIXELS(metrics.get_descent())
 
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())
75
87
 
76
88
            self._box_size = ascent + descent + 6
77
89
            return self._box_size
84
96
        colours and the fg parameter provides the multiplier that should be
85
97
        applied to the foreground colours.
86
98
        """
 
99
        mainline_color = ( 0.0, 0.0, 0.0 )
87
100
        colours = [
88
101
            ( 1.0, 0.0, 0.0 ),
89
102
            ( 1.0, 1.0, 0.0 ),
93
106
            ( 1.0, 0.0, 1.0 ),
94
107
            ]
95
108
 
96
 
        colour %= len(colours)
97
 
        red   = (colours[colour][0] * fg) or bg
98
 
        green = (colours[colour][1] * fg) or bg
99
 
        blue  = (colours[colour][2] * fg) or bg
 
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
100
117
 
101
118
        ctx.set_source_rgb(red, green, blue)
102
119
 
107
124
        to be, we let the TreeViewColumn take care of making them all
108
125
        line up.
109
126
        """
110
 
        box_size = self.box_size(widget)
111
 
 
112
 
        cols = self.node[0]
113
 
        for start, end, colour in self.in_lines + self.out_lines:
114
 
            cols = max(cols, start, end)
115
 
 
116
 
        width = box_size * (cols + 1)
 
127
        box_size = self.box_size(widget) + 1
 
128
 
 
129
        width = box_size * (self.columns_len + 1)
117
130
        height = box_size
118
131
 
119
132
        # FIXME I have no idea how to use cell_area properly
140
153
        box_size = self.box_size(widget)
141
154
 
142
155
        ctx.set_line_width(box_size / 8)
143
 
        ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
144
156
 
145
157
        # Draw lines into the cell
146
158
        for start, end, colour in self.in_lines:
147
 
            ctx.move_to(cell_area.x + box_size * start + box_size / 2,
148
 
                        bg_area.y - bg_area.height / 2)
149
 
 
150
 
            if start - end > 1:
151
 
                ctx.line_to(cell_area.x + box_size * start, bg_area.y)
152
 
                ctx.line_to(cell_area.x + box_size * end + box_size, bg_area.y)
153
 
            elif start - end < -1:
154
 
                ctx.line_to(cell_area.x + box_size * start + box_size,
155
 
                            bg_area.y)
156
 
                ctx.line_to(cell_area.x + box_size * end, bg_area.y)
157
 
 
158
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
159
 
                        bg_area.y + bg_area.height / 2)
160
 
 
161
 
            self.set_colour(ctx, colour, 0.0, 0.65)
162
 
            ctx.stroke()
 
159
            self.render_line (ctx, cell_area, box_size,
 
160
                         bg_area.y, bg_area.height,
 
161
                         start, end, colour, flags)
163
162
 
164
163
        # Draw lines out of the cell
165
164
        for start, end, colour in self.out_lines:
166
 
            ctx.move_to(cell_area.x + box_size * start + box_size / 2,
167
 
                        bg_area.y + bg_area.height / 2)
168
 
 
169
 
            if start - end > 1:
170
 
                ctx.line_to(cell_area.x + box_size * start,
171
 
                            bg_area.y + bg_area.height)
172
 
                ctx.line_to(cell_area.x + box_size * end + box_size,
173
 
                            bg_area.y + bg_area.height)
174
 
            elif start - end < -1:
175
 
                ctx.line_to(cell_area.x + box_size * start + box_size,
176
 
                            bg_area.y + bg_area.height)
177
 
                ctx.line_to(cell_area.x + box_size * end,
178
 
                            bg_area.y + bg_area.height)
179
 
 
180
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
181
 
                        bg_area.y + bg_area.height / 2 + bg_area.height)
182
 
 
183
 
            self.set_colour(ctx, colour, 0.0, 0.65)
184
 
            ctx.stroke()
 
165
            self.render_line (ctx, cell_area, box_size,
 
166
                         bg_area.y + bg_area.height, bg_area.height,
 
167
                         start, end, colour, flags)
185
168
 
186
169
        # Draw the revision node in the right column
187
170
        (column, colour) = self.node
189
172
                cell_area.y + cell_area.height / 2,
190
173
                box_size / 4, 0, 2 * math.pi)
191
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
 
192
181
        self.set_colour(ctx, colour, 0.0, 0.5)
193
182
        ctx.stroke_preserve()
194
183
 
195
184
        self.set_colour(ctx, colour, 0.5, 1.0)
196
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