File: line.py

package info (click to toggle)
python-enable 3.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,392 kB
  • ctags: 17,135
  • sloc: cpp: 79,151; python: 29,601; makefile: 2,926; sh: 43
file content (117 lines) | stat: -rwxr-xr-x 3,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
""" A line segment component. """

from numpy import array, resize

# Enthought library imports.
from enthought.kiva import FILL_STROKE, STROKE
from enthought.traits.api import Any, Event, Float, List, Trait, Bool

# Local imports.
from enthought.enable.api import border_size_trait, Component
from enthought.enable.colors import ColorTrait


class Line(Component):
    """A line segment component"""

    # Event fired when the points are no longer updating.
    # PZW: there seems to be a missing defn here; investigate.

    # An event to indicate that the point list has changed
    updated = Event

    # The color of the line.
    line_color = ColorTrait("black")

    # The dash pattern for the line.
    line_dash = Any

    # The width of the line.
    line_width = Trait(1, border_size_trait)

    # The points that make up this polygon.
    points = List  # List of Tuples

    # The color of each vertex.
    vertex_color = ColorTrait("black")

    # The size of each vertex.
    vertex_size = Float(3.0)

    # Whether to draw the path closed, with a line back to the first point
    close_path = Bool(True)
    
    #--------------------------------------------------------------------------
    # 'Line' interface
    #--------------------------------------------------------------------------

    def reset(self):
        "Reset the polygon to the initial state"

        self.points = []
        self.event_state = 'normal'
        self.updated = self
        return

    #--------------------------------------------------------------------------
    # 'Component' interface
    #--------------------------------------------------------------------------

    def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
        "Draw this line in the specified graphics context"
        gc.save_state()

        try:
            # Set the drawing parameters.
            gc.set_stroke_color(self.line_color_)
            gc.set_line_dash(self.line_dash)
            gc.set_line_width(self.line_width)

            # Draw the path as lines.
            gc.begin_path()
            offset_points = [(x, y) for x, y in self.points ]
            offset_points = resize(array(offset_points), (len(self.points),2))
            gc.lines(offset_points)
            if self.close_path:
                gc.close_path()
            gc.draw_path(STROKE)

            # Draw the vertices.
            self._draw_points(gc)
        finally:
            gc.restore_state()
        return

    #--------------------------------------------------------------------------
    # Private interface
    #--------------------------------------------------------------------------

    def _draw_points(self, gc):
        "Draw the points of the line"

        # Shortcut out if we would draw transparently.
        if self.vertex_color_[3] != 0:
            gc.save_state()
            try:
                gc.set_fill_color(self.vertex_color_)
                gc.set_line_dash(None)

                offset_points = [(x, y) for x, y in self.points ]
                offset_points = resize(array(offset_points), (len(self.points),2))
                offset = self.vertex_size / 2.0
                if hasattr(gc, 'draw_path_at_points'):
                    path = gc.get_empty_path()
                    path.rect( -offset, -offset,
                               self.vertex_size, self.vertex_size)
                    gc.draw_path_at_points(offset_points, path, FILL_STROKE)
                else:
                    for x, y in offset_points:
                        gc.begin_path()
                        gc.rect(x-offset, y-offset,
                                self.vertex_size, self.vertex_size)
                        gc.fill_path()
            finally:
                gc.restore_state()
        return

# EOF