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

« back to all changes in this revision

Viewing changes to examples/enable/shapes/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 centre of the shape.
24
 
    centre = 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
 
            self.container.bring_to_top(self)
72
 
 
73
 
        return
74
 
 
75
 
    def moving_mouse_move(self, event):
76
 
        """ Event handler. """
77
 
 
78
 
        self.position = [event.x - self._offset_x, event.y - self._offset_y]
79
 
        self.request_redraw()
80
 
 
81
 
        return
82
 
 
83
 
    def moving_left_up(self, event):
84
 
        """ Event handler. """
85
 
 
86
 
        self.event_state = 'normal'
87
 
 
88
 
        event.window.set_pointer(self.normal_pointer)
89
 
        event.window.mouse_owner = None
90
 
 
91
 
        self.request_redraw()
92
 
 
93
 
        return
94
 
    
95
 
    def moving_mouse_leave(self, event):
96
 
        """ Event handler. """
97
 
 
98
 
        self.moving_left_up(event)
99
 
 
100
 
        return
101
 
 
102
 
    ###########################################################################
103
 
    # 'Shape' interface
104
 
    ###########################################################################
105
 
 
106
 
    def _get_centre(self):
107
 
        """ Property getter. """
108
 
 
109
 
        dx, dy = self.bounds
110
 
        ox, oy = self.position
111
 
 
112
 
        cx = ox + dx/2
113
 
        cy = oy + dy/2
114
 
 
115
 
        return cx, cy
116
 
    
117
 
    def _text_default(self):
118
 
        """ Trait initializer. """
119
 
 
120
 
        return type(self).__name__
121
 
    
122
 
    ###########################################################################
123
 
    # Protected 'Shape' interface
124
 
    ###########################################################################
125
 
 
126
 
    def _distance_between(self, (x1, y1), (x2, y2)):
127
 
        """ Return the distance between two points. """
128
 
 
129
 
        return math.sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
130
 
        
131
 
    def _draw_text(self, gc):
132
 
        """ Draw the shape's text. """
133
 
 
134
 
        if len(self.text) > 0:
135
 
            gc.set_fill_color(self._get_text_color(self.event_state))
136
 
 
137
 
            gc.set_font(Font(family=MODERN, size=16))
138
 
            tx, ty, tw, th = gc.get_text_extent(self.text)
139
 
            
140
 
            dx, dy = self.bounds
141
 
            x, y = self.position
142
 
            gc.set_text_position(x+(dx-tw)/2, y+(dy-th)/2)
143
 
            
144
 
            gc.show_text(self.text)
145
 
 
146
 
        return
147
 
    
148
 
    def _get_fill_color(self, event_state):
149
 
        """ Return the fill color based on the event state. """
150
 
 
151
 
        if event_state == 'normal':
152
 
            fill_color = self.fill_color_
153
 
 
154
 
        else:
155
 
            r, g, b, a = self.fill_color_
156
 
            fill_color = (r, g, b, 0.5)
157
 
 
158
 
        return fill_color
159
 
 
160
 
    def _get_text_color(self, event_state):
161
 
        """ Return the text color based on the event state. """
162
 
 
163
 
        if event_state == 'normal':
164
 
            text_color = self.text_color_
165
 
 
166
 
        else:
167
 
            r, g, b, a = self.text_color_
168
 
            text_color = (r, g, b, 0.5)
169
 
 
170
 
        return text_color
171
 
 
172
 
#### EOF ######################################################################