~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to enthought/chaco/color_bar.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 19:03:54 UTC
  • mfrom: (7.2.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110406190354-rwd55l2ezjecfo41
Tags: 3.4.0-2
d/rules: fix pyshared directory path (Closes: #621116)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
""" Defines the ColorBar class.
2
2
"""
 
3
 
 
4
from __future__ import with_statement
 
5
 
3
6
# Major library imports
4
7
from numpy import array, arange, ones, transpose, uint8
5
8
 
6
9
# Enthought library imports
7
 
from enthought.traits.api import Any, Bool, Enum, Instance, Property
8
 
from enthought.kiva.backend_image import GraphicsContext
 
10
from enthought.traits.api import Any, Bool, Enum, Instance, Property, \
 
11
                                 cached_property, on_trait_change
 
12
from enthought.kiva.image import GraphicsContext
9
13
 
10
14
# Local imports
11
15
from base_xy_plot import BaseXYPlot
24
28
 
25
29
    # Screen mapper for color data
26
30
    color_mapper = Property #Instance(ColorMapper)
 
31
    
 
32
    # Screen mapper for value data (synonym for color_mapper)
 
33
    value_mapper = Property(depends_on='color_mapper')
27
34
 
28
35
    # Optional index data source for generic tools to attach metadata to.
29
36
    index = Property
123
130
        """ Draws the 'plot' layer.
124
131
        """
125
132
        self._update_mappers()
126
 
        gc.save_state()
127
 
        if self.orientation == 'h':
128
 
            perpendicular_dim = 1
129
 
            axis_dim = 0
130
 
        else:
131
 
            perpendicular_dim = 0
132
 
            axis_dim = 1
133
 
        
134
 
        mapper = self.index_mapper
135
 
 
136
 
        scrn_points = arange(mapper.low_pos, mapper.high_pos+1)
137
 
        
138
 
        # Get the data values associated with the list of screen points.
139
 
        if mapper.range.low == mapper.range.high:
140
 
            # LogMapper.map_data() returns something unexpected if low==high,
141
 
            # so we'll handle that case here.
142
 
            data_points = array([mapper.range.high])
143
 
        else:
144
 
            data_points = mapper.map_data(scrn_points)
145
 
 
146
 
        if self.direction == 'flipped':
147
 
            data_points = data_points[::-1]
148
 
            
149
 
        # Get the colors associated with the data points.
150
 
        colors = self.color_mapper.map_screen(data_points)
151
 
        
152
 
        img = self._make_color_image(colors, self.bounds[perpendicular_dim],
153
 
                                                self.orientation, self.direction)
154
 
        try:
 
133
        with gc:
 
134
            if self.orientation == 'h':
 
135
                perpendicular_dim = 1
 
136
                axis_dim = 0
 
137
            else:
 
138
                perpendicular_dim = 0
 
139
                axis_dim = 1
 
140
            
 
141
            mapper = self.index_mapper
 
142
 
 
143
            scrn_points = arange(mapper.low_pos, mapper.high_pos+1)
 
144
            
 
145
            # Get the data values associated with the list of screen points.
 
146
            if mapper.range.low == mapper.range.high:
 
147
                # LogMapper.map_data() returns something unexpected if low==high,
 
148
                # so we'll handle that case here.
 
149
                data_points = array([mapper.range.high])
 
150
            else:
 
151
                data_points = mapper.map_data(scrn_points)
 
152
 
 
153
            if self.direction == 'flipped':
 
154
                data_points = data_points[::-1]
 
155
                
 
156
            # Get the colors associated with the data points.
 
157
            colors = self.color_mapper.map_screen(data_points)
 
158
            
 
159
            img = self._make_color_image(colors, self.bounds[perpendicular_dim],
 
160
                                                    self.orientation, self.direction)
155
161
            gc.draw_image(img, (self.x, self.y, self.width, self.height))
156
 
        finally:
157
 
            gc.restore_state()
158
162
    
159
163
    def _make_color_image(self, color_values, width, orientation, direction):
160
164
        """
215
219
    def _updated_changed_for_color_mapper(self):
216
220
        self._update_mappers()
217
221
 
 
222
    @on_trait_change('[index_mapper,color_mapper].+')
218
223
    def _either_mapper_changed(self):
219
224
        self.invalidate_draw()
220
225
        self.request_redraw()
230
235
    def _color_mapper_changed(self):
231
236
        self._either_mapper_changed()
232
237
 
 
238
    def _value_mapper_changed(self):
 
239
        self._color_mapper_changed()
 
240
 
233
241
    def _plot_changed(self):
234
242
        self.request_redraw()
235
243
    
267
275
 
268
276
    def _set_color_mapper(self, val):
269
277
        self._color_mapper = val
 
278
    
 
279
    @cached_property
 
280
    def _get_value_mapper(self):
 
281
        return self._get_color_mapper()
 
282
    
 
283
    def _set_value_mapper(self, val):
 
284
        self._set_color_mapper(val)
270
285
 
271
286
    def _get_index(self):
272
287
        if self.plot and hasattr(self.plot, "color_data"):