/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 olive/frontend/gtk/viz/graphcell.py

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-09-27 19:11:59 UTC
  • mfrom: (0.8.90 merge)
  • mto: (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 103.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060927191159-cc4e54f613575779
Merge all changes. Release 0.11.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Modified for use with Olive:
2
 
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
3
 
# Original copyright holder:
4
 
# Copyright (C) 2005 by Canonical Ltd. (Scott James Remnant <scott@ubuntu.com>)
5
 
#
6
 
# This program is free software; you can redistribute it and/or modify
7
 
# it under the terms of the GNU General Public License as published by
8
 
# the Free Software Foundation; either version 2 of the License, or
9
 
# (at your option) any later version.
10
 
#
11
 
# This program is distributed in the hope that it will be useful,
12
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
# GNU General Public License for more details.
15
 
#
16
 
# You should have received a copy of the GNU General Public License
17
 
# along with this program; if not, write to the Free Software
18
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
 
20
 
"""Cell renderer for directed graph.
21
 
 
22
 
This module contains the implementation of a custom GtkCellRenderer that
23
 
draws part of the directed graph based on the lines suggested by the code
24
 
in graph.py.
25
 
 
26
 
Because we're shiny, we use Cairo to do this, and because we're naughty
27
 
we cheat and draw over the bits of the TreeViewColumn that are supposed to
28
 
just be for the background.
29
 
"""
30
 
 
31
 
__copyright__ = "Copyright © 2005 Canonical Ltd."
32
 
__author__    = "Scott James Remnant <scott@ubuntu.com>"
33
 
 
34
 
 
35
 
import math
36
 
 
37
 
import gtk
38
 
import gobject
39
 
import pango
40
 
import cairo
41
 
 
42
 
 
43
 
class CellRendererGraph(gtk.GenericCellRenderer):
44
 
    """Cell renderer for directed graph.
45
 
 
46
 
    Properties:
47
 
      node              (column, colour) tuple to draw revision node,
48
 
      in_lines          (start, end, colour) tuple list to draw inward lines,
49
 
      out_lines         (start, end, colour) tuple list to draw outward lines.
50
 
    """
51
 
 
52
 
    __gproperties__ = {
53
 
        "node":         ( gobject.TYPE_PYOBJECT, "node",
54
 
                          "revision node instruction",
55
 
                          gobject.PARAM_WRITABLE
56
 
                        ),
57
 
        "in-lines":     ( gobject.TYPE_PYOBJECT, "in-lines",
58
 
                          "instructions to draw lines into the cell",
59
 
                          gobject.PARAM_WRITABLE
60
 
                        ),
61
 
        "out-lines":    ( gobject.TYPE_PYOBJECT, "out-lines",
62
 
                          "instructions to draw lines out of the cell",
63
 
                          gobject.PARAM_WRITABLE
64
 
                        ),
65
 
        }
66
 
 
67
 
    def do_set_property(self, property, value):
68
 
        """Set properties from GObject properties."""
69
 
        if property.name == "node":
70
 
            self.node = value
71
 
        elif property.name == "in-lines":
72
 
            self.in_lines = value
73
 
        elif property.name == "out-lines":
74
 
            self.out_lines = value
75
 
        else:
76
 
            raise AttributeError, "no such property: '%s'" % property.name
77
 
 
78
 
    def box_size(self, widget):
79
 
        """Calculate box size based on widget's font.
80
 
 
81
 
        Cache this as it's probably expensive to get.  It ensures that we
82
 
        draw the graph at least as large as the text.
83
 
        """
84
 
        try:
85
 
            return self._box_size
86
 
        except AttributeError:
87
 
            pango_ctx = widget.get_pango_context()
88
 
            font_desc = widget.get_style().font_desc
89
 
            metrics = pango_ctx.get_metrics(font_desc)
90
 
 
91
 
            ascent = pango.PIXELS(metrics.get_ascent())
92
 
            descent = pango.PIXELS(metrics.get_descent())
93
 
 
94
 
            self._box_size = ascent + descent + 6
95
 
            return self._box_size
96
 
 
97
 
    def set_colour(self, ctx, colour, bg, fg):
98
 
        """Set the context source colour.
99
 
 
100
 
        Picks a distinct colour based on an internal wheel; the bg
101
 
        parameter provides the value that should be assigned to the 'zero'
102
 
        colours and the fg parameter provides the multiplier that should be
103
 
        applied to the foreground colours.
104
 
        """
105
 
        colours = [
106
 
            ( 1.0, 0.0, 0.0 ),
107
 
            ( 1.0, 1.0, 0.0 ),
108
 
            ( 0.0, 1.0, 0.0 ),
109
 
            ( 0.0, 1.0, 1.0 ),
110
 
            ( 0.0, 0.0, 1.0 ),
111
 
            ( 1.0, 0.0, 1.0 ),
112
 
            ]
113
 
 
114
 
        colour %= len(colours)
115
 
        red   = (colours[colour][0] * fg) or bg
116
 
        green = (colours[colour][1] * fg) or bg
117
 
        blue  = (colours[colour][2] * fg) or bg
118
 
 
119
 
        ctx.set_source_rgb(red, green, blue)
120
 
 
121
 
    def on_get_size(self, widget, cell_area):
122
 
        """Return the size we need for this cell.
123
 
 
124
 
        Each cell is drawn individually and is only as wide as it needs
125
 
        to be, we let the TreeViewColumn take care of making them all
126
 
        line up.
127
 
        """
128
 
        box_size = self.box_size(widget)
129
 
 
130
 
        cols = self.node[0]
131
 
        for start, end, colour in self.in_lines + self.out_lines:
132
 
            cols = max(cols, start, end)
133
 
 
134
 
        width = box_size * (cols + 1)
135
 
        height = box_size
136
 
 
137
 
        # FIXME I have no idea how to use cell_area properly
138
 
        return (0, 0, width, height)
139
 
 
140
 
    def on_render(self, window, widget, bg_area, cell_area, exp_area, flags):
141
 
        """Render an individual cell.
142
 
 
143
 
        Draws the cell contents using cairo, taking care to clip what we
144
 
        do to within the background area so we don't draw over other cells.
145
 
        Note that we're a bit naughty there and should really be drawing
146
 
        in the cell_area (or even the exposed area), but we explicitly don't
147
 
        want any gutter.
148
 
 
149
 
        We try and be a little clever, if the line we need to draw is going
150
 
        to cross other columns we actually draw it as in the .---' style
151
 
        instead of a pure diagonal ... this reduces confusion by an
152
 
        incredible amount.
153
 
        """
154
 
        ctx = window.cairo_create()
155
 
        ctx.rectangle(bg_area.x, bg_area.y, bg_area.width, bg_area.height)
156
 
        ctx.clip()
157
 
 
158
 
        box_size = self.box_size(widget)
159
 
 
160
 
        ctx.set_line_width(box_size / 8)
161
 
        ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
162
 
 
163
 
        # Draw lines into the cell
164
 
        for start, end, colour in self.in_lines:
165
 
            ctx.move_to(cell_area.x + box_size * start + box_size / 2,
166
 
                        bg_area.y - bg_area.height / 2)
167
 
 
168
 
            if start - end > 1:
169
 
                ctx.line_to(cell_area.x + box_size * start, bg_area.y)
170
 
                ctx.line_to(cell_area.x + box_size * end + box_size, bg_area.y)
171
 
            elif start - end < -1:
172
 
                ctx.line_to(cell_area.x + box_size * start + box_size,
173
 
                            bg_area.y)
174
 
                ctx.line_to(cell_area.x + box_size * end, bg_area.y)
175
 
 
176
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
177
 
                        bg_area.y + bg_area.height / 2)
178
 
 
179
 
            self.set_colour(ctx, colour, 0.0, 0.65)
180
 
            ctx.stroke()
181
 
 
182
 
        # Draw lines out of the cell
183
 
        for start, end, colour in self.out_lines:
184
 
            ctx.move_to(cell_area.x + box_size * start + box_size / 2,
185
 
                        bg_area.y + bg_area.height / 2)
186
 
 
187
 
            if start - end > 1:
188
 
                ctx.line_to(cell_area.x + box_size * start,
189
 
                            bg_area.y + bg_area.height)
190
 
                ctx.line_to(cell_area.x + box_size * end + box_size,
191
 
                            bg_area.y + bg_area.height)
192
 
            elif start - end < -1:
193
 
                ctx.line_to(cell_area.x + box_size * start + box_size,
194
 
                            bg_area.y + bg_area.height)
195
 
                ctx.line_to(cell_area.x + box_size * end,
196
 
                            bg_area.y + bg_area.height)
197
 
 
198
 
            ctx.line_to(cell_area.x + box_size * end + box_size / 2,
199
 
                        bg_area.y + bg_area.height / 2 + bg_area.height)
200
 
 
201
 
            self.set_colour(ctx, colour, 0.0, 0.65)
202
 
            ctx.stroke()
203
 
 
204
 
        # Draw the revision node in the right column
205
 
        (column, colour) = self.node
206
 
        ctx.arc(cell_area.x + box_size * column + box_size / 2,
207
 
                cell_area.y + cell_area.height / 2,
208
 
                box_size / 4, 0, 2 * math.pi)
209
 
 
210
 
        self.set_colour(ctx, colour, 0.0, 0.5)
211
 
        ctx.stroke_preserve()
212
 
 
213
 
        self.set_colour(ctx, colour, 0.5, 1.0)
214
 
        ctx.fill()