File: image.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 (206 lines) | stat: -rw-r--r-- 8,014 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#-------------------------------------------------------------------------------
#
#  Define standard Enable 'image' based control components.
#
#  Written by: David C. Morrill
#
#  Date: 10/10/2003
#
#  (c) Copyright 2003 by Enthought, Inc.
#
#  Classes defined: Image
#                   Inspector
#
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

from enthought.traits.api import Trait, TraitPrefixList
from enthought.traits.ui.api import Group, View

from enthought.enable.base import IDroppedOnHandler, gc_image_for
from enthought.enable.component import Component
from enthought.enable.enable_traits import string_image_trait, NoStretch
from enthought.enable.colors import ColorTrait

#-------------------------------------------------------------------------------
#  'Image' class:
#-------------------------------------------------------------------------------

class Image ( Component ):

    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

    image          = Trait( '=image', string_image_trait )
    stretch_width  = NoStretch
    stretch_height = NoStretch

    #---------------------------------------------------------------------------
    #  Trait view definitions:
    #---------------------------------------------------------------------------

    traits_view = View( Group( '<component>', 'image', id = 'component' ),
                        Group( '<links>',              id = 'links' ) )

    #---------------------------------------------------------------------------
    #  Initialize the object:
    #---------------------------------------------------------------------------

    def __init__ ( self, **traits ):
        Component.__init__( self, **traits )
        self._image_changed( self.image )

    #---------------------------------------------------------------------------
    #  Return an image specified by name:
    #---------------------------------------------------------------------------

    def image_for ( self, image ):
        path   = ''
        prefix = image[:1]
        if prefix == '=':
            path  = self
            image = image[1:]
        elif prefix == '.':
            path  = None
            image = image[1:]
        return gc_image_for( image, path )

    #---------------------------------------------------------------------------
    #  Draw the component in a specified graphics context:
    #---------------------------------------------------------------------------

    def _draw ( self, gc ):
        gc.draw_image( self._image, self.bounds )

    #---------------------------------------------------------------------------
    #  Handle the image being changed:
    #---------------------------------------------------------------------------

    def _image_changed ( self, image ):
        self._image     = image = self.image_for( image )
        self.width = image.width()
        self.height = image.height()

    #---------------------------------------------------------------------------
    #  Return the components that contain a specified (x,y) point:
    #---------------------------------------------------------------------------

    def _components_at ( self, x, y ):
        bmp = self._image.bmp_array
        if ((bmp.shape[2] < 4) or
            (bmp[ int( self.y + self.height - y ) - 1,
                  int( x - self.x ), 3 ] >= 128)):
            return [ self ]
        return []

#-------------------------------------------------------------------------------
#  'DraggableImage' class:
#-------------------------------------------------------------------------------

class DraggableImage ( Image ):

    #---------------------------------------------------------------------------
    #  Allow a copy of the image to be dragged:
    #---------------------------------------------------------------------------

    def _left_down_changed ( self, event ):
        event.handled = True
        self.window.drag( self, None, event, True, alpha = -1.0 )

#-------------------------------------------------------------------------------
#  'Inspector' class:
#-------------------------------------------------------------------------------

class Inspector ( DraggableImage, IDroppedOnHandler ):

    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

    image = Trait( '=inspector', string_image_trait )

    #---------------------------------------------------------------------------
    #  Handle being dropped on a component:
    #---------------------------------------------------------------------------

    def was_dropped_on ( self, component, event ):
        event.handled = True
        component.edit_traits( kind = 'live' )

#-------------------------------------------------------------------------------
#  'ColorChip' class:
#-------------------------------------------------------------------------------

class ColorChip ( Component ):

    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

    color = ColorTrait("yellow")
    item  = Trait( 'fg_color', TraitPrefixList(
                 [ 'fg_color',     'bg_color',
                   'shadow_color', 'alt_color' ] ) )

    #---------------------------------------------------------------------------
    #  Trait view definitions:
    #---------------------------------------------------------------------------

    traits_view = View( Group( '<component>', id = 'component' ),
                        Group( '<links>',     id = 'links' ),
                        Group( 'color',
                               id     = 'color',
                                style = 'custom' ) )

    #---------------------------------------------------------------------------
    #  Initialize the object:
    #---------------------------------------------------------------------------

    def __init__ ( self, **traits ):
        Component.__init__( self, **traits )
        self._item      = self.item
        self._image     = image = self.image_for( '=colorchip' )
        self.min_width  = image.width()
        self.min_height = image.height()
        self.dimensions( self.min_width, self.min_height )

    #---------------------------------------------------------------------------
    #  Draw the component in a specified graphics context:
    #---------------------------------------------------------------------------

    def _draw ( self, gc ):
        gc.save_state()
        x, y, dx, dy = self.bounds
        gc.set_fill_color( self.color_ )
        gc.begin_path()
        gc.rect( x + 1, y + 1, dx - 2, dy - 2 )
        gc.fill_path()
        gc.draw_image( self._image, self.bounds )
        gc.restore_state()

    #---------------------------------------------------------------------------
    #  Mouse event handlers:
    #---------------------------------------------------------------------------

    def _left_down_changed ( self, event ):
        self.item = self._item
        if event.shift_down:
            if event.control_down:
                self.item = 'shadow_color'
            else:
                self.item = 'fg_color'
        elif event.control_down:
            self.item = 'bg_color'
        elif event.alt_down:
            self.item = 'alt_color'
        self.window.drag( self, None, event, True )
        event.handled = True

    def _right_up_changed ( self, event ):
        event.handled = True
        self.edit_traits( view = View( 'color@' ), kind = 'live' )