~ubuntu-branches/ubuntu/trusty/python-enable/trusty-proposed

« back to all changes in this revision

Viewing changes to enthought/enable/primitives/shape.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2010-02-28 14:56:36 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100228145636-9ghfhe3uy37tt3q6
Tags: 3.3.0-1
* New upstream release
* Bump Standards-Version to 3.8.4
* Switch to source format 3.0
* Update patches/freetype2.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" The base class for moveable shapes. """
 
2
 
 
3
 
 
4
# Standard library imports.
 
5
import math
 
6
 
 
7
# Enthought library imports.
 
8
from enthought.enable.api import ColorTrait, Component, Pointer
 
9
from enthought.kiva import Font, MODERN
 
10
from enthought.traits.api import Float, Property, Str, Tuple
 
11
 
 
12
 
 
13
class Shape(Component):
 
14
    """ The base class for moveable shapes. """
 
15
 
 
16
    #### 'Component' interface ################################################
 
17
 
 
18
    # The background color of this component.
 
19
    bgcolor = 'transparent'
 
20
    
 
21
    #### 'Shape' interface ####################################################
 
22
 
 
23
    # The coordinates of the center of the shape.
 
24
    center = Property(Tuple)
 
25
    
 
26
    # The fill color.
 
27
    fill_color = ColorTrait
 
28
 
 
29
    # The pointer for the 'normal' event state.
 
30
    normal_pointer = Pointer('arrow')
 
31
 
 
32
    # The pointer for the 'moving' event state.
 
33
    moving_pointer = Pointer('hand')
 
34
 
 
35
    # The text color.
 
36
    text_color = ColorTrait
 
37
    
 
38
    # The text displayed in the shape.
 
39
    text = Str
 
40
    
 
41
    #### 'Private' interface ##################################################
 
42
 
 
43
    # The difference between the location of a mouse-click and the component's
 
44
    # origin.
 
45
    _offset_x = Float
 
46
    _offset_y = Float
 
47
        
 
48
    ###########################################################################
 
49
    # 'Interactor' interface
 
50
    ###########################################################################
 
51
 
 
52
    def normal_key_pressed(self, event):
 
53
        """ Event handler. """
 
54
        
 
55
        print 'normal_key_pressed', event.character
 
56
 
 
57
        return
 
58
    
 
59
    def normal_left_down(self, event):
 
60
        """ Event handler. """
 
61
 
 
62
        if self.is_in(event.x, event.y):
 
63
            self.event_state = 'moving'
 
64
            
 
65
            event.window.mouse_owner = self
 
66
            event.window.set_pointer(self.moving_pointer)
 
67
            
 
68
            self._offset_x = event.x - self.x
 
69
            self._offset_y = event.y - self.y
 
70
 
 
71
            # move this shape to the top of the z order. The components are
 
72
            # drawn in order, so the last one will be drawn on top
 
73
            siblings = self.container.components
 
74
            if len(siblings) > 1:
 
75
                siblings.remove(self)
 
76
                siblings.append(self)
 
77
 
 
78
        return
 
79
 
 
80
    def moving_mouse_move(self, event):
 
81
        """ Event handler. """
 
82
 
 
83
        top = event.y + self._offset_y
 
84
        bottom = event.y - self._offset_y
 
85
        left = event.x - self._offset_x
 
86
        right = event.x + self._offset_x
 
87
        
 
88
        # Keep the shape fully in the container 
 
89
        
 
90
        if bottom < 0:
 
91
            bottom = 0
 
92
        elif top > self.container.height:
 
93
            bottom = self.container.height - self.height
 
94
                    
 
95
        if left < 0:
 
96
            left = 0
 
97
        elif right > self.container.width:
 
98
            left = self.container.width - self.width
 
99
        
 
100
        self.position = [left, bottom]
 
101
        self.request_redraw()
 
102
 
 
103
        return
 
104
 
 
105
    def moving_left_up(self, event):
 
106
        """ Event handler. """
 
107
 
 
108
        self.event_state = 'normal'
 
109
 
 
110
        event.window.set_pointer(self.normal_pointer)
 
111
        event.window.mouse_owner = None
 
112
 
 
113
        self.request_redraw()
 
114
 
 
115
        return
 
116
    
 
117
    def moving_mouse_leave(self, event):
 
118
        """ Event handler. """
 
119
 
 
120
        self.moving_left_up(event)
 
121
 
 
122
        return
 
123
 
 
124
    ###########################################################################
 
125
    # 'Shape' interface
 
126
    ###########################################################################
 
127
 
 
128
    def _get_center(self):
 
129
        """ Property getter. """
 
130
 
 
131
        dx, dy = self.bounds
 
132
        ox, oy = self.position
 
133
 
 
134
        cx = ox + dx/2
 
135
        cy = oy + dy/2
 
136
 
 
137
        return cx, cy
 
138
    
 
139
    def _text_default(self):
 
140
        """ Trait initializer. """
 
141
 
 
142
        return type(self).__name__
 
143
    
 
144
    ###########################################################################
 
145
    # Protected 'Shape' interface
 
146
    ###########################################################################
 
147
 
 
148
    def _distance_between(self, (x1, y1), (x2, y2)):
 
149
        """ Return the distance between two points. """
 
150
 
 
151
        return math.sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
 
152
        
 
153
    def _draw_text(self, gc):
 
154
        """ Draw the shape's text. """
 
155
 
 
156
        if len(self.text) > 0:
 
157
            gc.set_fill_color(self._get_text_color(self.event_state))
 
158
 
 
159
            gc.set_font(Font(family=MODERN, size=16))
 
160
            tx, ty, tw, th = gc.get_text_extent(self.text)
 
161
            
 
162
            dx, dy = self.bounds
 
163
            x, y = self.position
 
164
            gc.set_text_position(x+(dx-tw)/2, y+(dy-th)/2)
 
165
            
 
166
            gc.show_text(self.text)
 
167
 
 
168
        return
 
169
    
 
170
    def _get_fill_color(self, event_state):
 
171
        """ Return the fill color based on the event state. """
 
172
 
 
173
        if event_state == 'normal':
 
174
            fill_color = self.fill_color_
 
175
 
 
176
        else:
 
177
            r, g, b, a = self.fill_color_
 
178
            fill_color = (r, g, b, 0.5)
 
179
 
 
180
        return fill_color
 
181
 
 
182
    def _get_text_color(self, event_state):
 
183
        """ Return the text color based on the event state. """
 
184
 
 
185
        if event_state == 'normal':
 
186
            text_color = self.text_color_
 
187
 
 
188
        else:
 
189
            r, g, b, a = self.text_color_
 
190
            text_color = (r, g, b, 0.5)
 
191
 
 
192
        return text_color
 
193
 
 
194
#### EOF ######################################################################