File: drag_polygon.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 (138 lines) | stat: -rw-r--r-- 4,004 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
""" A drag drawn polygon. """

from enthought.enable.primitives.api import Polygon
from enthought.enable.api import Pointer
from enthought.pyface.action.api import MenuManager
from enthought.traits.api import Delegate, Instance

from drawing_tool import DrawingTool

class DragPolygon(DrawingTool):
    """ A drag drawn polygon. """

    poly = Instance(Polygon, args=())

    draw_mode = "overlay"

    #### Visible style. ####

    # Override the vertex color so as to not draw it.
    vertex_color = Delegate('poly', modify=True)

    # Override the vertex size so as to not draw it.
    vertex_size = Delegate('poly', modify=True)

    background_color = Delegate('poly', modify=True)

    #### Pointers. ####

    # Pointer for the complete state.
    complete_pointer = Pointer('cross')

    # Pointer for the drawing state.
    drawing_pointer = Pointer('cross')

    # Pointer for the normal state.
    normal_pointer = Pointer('cross')


    #### Miscellaneous. ####

    # The context menu for the polygon.
    menu = Instance(MenuManager)
    

    def reset(self):
        self.vertex_color = (0,0,0,0)
        self.vertex_size = 0
        self.poly.model.points = []
        self.event_state = "normal"
        return

    ###########################################################################
    # 'Component' interface.
    ###########################################################################

    #### 'complete' state #####################################################

    def complete_draw ( self, gc ):
        """ Draw the completed polygon. """
        gc.save_state()
        self.poly.border_dash = None
        self.poly._draw_closed( gc )
        gc.restore_state()
        return

    def complete_left_down ( self, event ):
        """ Draw a new polygon. """
        self.reset()
        self.normal_left_down( event )
        return

    def complete_right_down ( self, event ):
        """ Do the context menu if available. """
        if self.menu is not None:
            if self._is_in((event.x + self.x, event.y - self.y)):
                menu = self.menu.create_menu(event.window.control)
                ### FIXME : The call to _flip_y is necessary but inappropriate.
                menu.show(event.x, event.window._flip_y(event.y))
        return
        
    #### 'drawing' state ######################################################

    def drawing_draw ( self, gc ):
        """ Draw the polygon while in 'drawing' state. """
        
        gc.save_state()
        self.poly.border_dash = (4.0, 2.0)
        self.poly._draw_open( gc )
        gc.restore_state()
        
        return

    def drawing_left_up ( self, event ):
        """ Handle the left mouse button coming up in 'drawing' state. """

        self.event_state = 'complete'
        self.pointer = self.complete_pointer
        
        self.request_redraw()

        self.complete = True
        
        return

    def drawing_mouse_move ( self, event ):
        """ Handle the mouse moving in 'drawing' state. """

        last_point = self.poly.model.points[-1]

        # If we have moved, we need to add a point.
        if last_point != (event.x + self.x, event.y - self.y):
            self.poly.model.points.append((event.x + self.x, event.y - self.y))
            self.request_redraw()
            
        return

    #### 'normal' state #######################################################

    def normal_left_down ( self, event ):
        """ Handle the left button down in the 'normal' state. """

        self.poly.model.points.append((event.x + self.x, event.y - self.y))

        self.event_state = 'drawing'
        self.pointer = self.drawing_pointer

        self.request_redraw()

        return
    
    def normal_mouse_move ( self, event ):
        """ Handle the mouse moving in the 'normal' state. """

        self.pointer = self.normal_pointer

        return

#### EOF ######################################################################