~ubuntu-branches/ubuntu/trusty/python-chaco/trusty

« back to all changes in this revision

Viewing changes to enthought/chaco/tools/simple_inspector.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 20:38:02 UTC
  • mfrom: (7.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110708203802-5t32e0ldv441yh90
Tags: 4.0.0-1
* New upstream release
* debian/control:
  - Depend on python-traitsui (Closes: #633604)
  - Bump Standards-Version to 3.9.2
* Update debian/watch file
* Remove debian/patches/* -- no longer needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Simple Inspector tool for plots
2
 
    
3
 
This module provides a simple tool that reports the data-space coordinates of
4
 
the current mouse cursor position in a plot.  It is intended for use with
5
 
SimpleInspectorOverlay, but other objects can potentially hook into its API.
6
 
"""
7
 
 
8
 
from enthought.chaco.image_plot import ImagePlot
9
 
from enthought.enable.api import BaseTool, KeySpec
10
 
from enthought.traits.api import Bool, Event, Tuple, Enum, Callable
11
 
 
12
 
class SimpleInspectorTool(BaseTool):
13
 
    """ Simple inspector tool for plots
14
 
    
15
 
    This is a simple tool that reports the data-space coordinates of the
16
 
    current mouse cursor position in a plot.
17
 
    
18
 
    Interested overlays and other objects can listen for new_value events,
19
 
    which is a dictionary of data about the current location in data space,
20
 
    and can look at the last_mouse_position trait which holds the mouse
21
 
    position in screen space.
22
 
    
23
 
    The tool also provides a visible trait which listeners can use to hide
24
 
    themselves.  By default the 'p' key toggles this.
25
 
    
26
 
    Instances can provide a value_generator function that performs computations
27
 
    to generate additional values in the dictionary that is passed to the
28
 
    new_value event.  Subclasses can override gather_values() to similar
29
 
    effect.
30
 
    """
31
 
 
32
 
    # This event fires whenever the mouse moves over a new image point.
33
 
    # Its value is a dict with default keys "x", "y", "index" and "value".
34
 
    new_value = Event
35
 
 
36
 
    # Indicates whether overlays listening to this tool should be visible.
37
 
    visible = Bool(True)
38
 
 
39
 
    # Stores the last mouse position.  This can be used by overlays to
40
 
    # position themselves around the mouse.
41
 
    last_mouse_position = Tuple
42
 
 
43
 
    # This key will show and hide any overlays listening to this tool.
44
 
    inspector_key = KeySpec('p')
45
 
    
46
 
    # A callable that computes other values for the new_value event
47
 
    # this takes a dictionary as an argument, and returns a dictionary
48
 
    value_generator = Callable
49
 
 
50
 
    # Private Trails ########################################################
51
 
   
52
 
    # Stores the value of self.visible when the mouse leaves the tool,
53
 
    # so that it can be restored when the mouse enters again.
54
 
    _old_visible = Enum(None, True, False) #Trait(None, Bool(True))
55
 
 
56
 
    #########################################################################
57
 
    # SimpleInspectorTool API
58
 
    #########################################################################
59
 
 
60
 
    def gather_values(self, event):
61
 
        """ Generate the values for the new_value dictionary.
62
 
        
63
 
        By default this returns a dictionary with keys "x", "y", "index" and
64
 
        "value".  If there is a value_generator callable, this will be called
65
 
        to modify the dictionary.
66
 
        
67
 
        Parameters
68
 
        ----------
69
 
        
70
 
        event
71
 
            The mouse_move event.
72
 
        
73
 
        Returns
74
 
        -------
75
 
        
76
 
        A dictionary.
77
 
        """
78
 
        x, y, index, value = self.map_to_data(event.x, event.y)
79
 
        d = {'index': index, 'value': value, 'x': x, 'y': y}
80
 
        
81
 
        if isinstance(self.component, ImagePlot):
82
 
            x_ndx, y_ndx = self.component.map_index((event.x, event.y),
83
 
                                                    outside_returns_none=False)
84
 
            
85
 
            # FIXME: off-by-one error. The size of the index is +1 to the size of
86
 
            # the image array
87
 
            if y_ndx == self.component.value.data.shape[0]:
88
 
                y_ndx -= 1
89
 
            if x_ndx == self.component.value.data.shape[1]:
90
 
                x_ndx += 1
91
 
            
92
 
            z =  self.component.value.data[y_ndx, x_ndx]
93
 
            d['z'] = z
94
 
            d['color'] = z
95
 
        
96
 
        if self.value_generator is not None:
97
 
            d = self.value_generator(d)
98
 
        return d
99
 
 
100
 
    def map_to_data(self, x, y):
101
 
        """ Returns the data space coordinates of the given x and y.  
102
 
        
103
 
        Takes into account orientation of the plot and the axis setting.
104
 
        """
105
 
        
106
 
        plot = self.component
107
 
        if plot.orientation == "h":
108
 
            index = x = plot.x_mapper.map_data(x)
109
 
            value = y = plot.y_mapper.map_data(y)
110
 
        else:
111
 
            index = y = plot.y_mapper.map_data(y)
112
 
            value = x = plot.x_mapper.map_data(x)
113
 
        return x, y, index, value
114
 
 
115
 
    #########################################################################
116
 
    # Component API
117
 
    #########################################################################
118
 
 
119
 
    def normal_key_pressed(self, event):
120
 
        if self.inspector_key.match(event):
121
 
            self.visible = not self.visible
122
 
 
123
 
    def normal_mouse_leave(self, event):
124
 
        if self._old_visible is None:
125
 
            self._old_visible = self.visible
126
 
            self.visible = False
127
 
 
128
 
    def normal_mouse_enter(self, event):
129
 
        if self._old_visible is not None:
130
 
            self.visible = self._old_visible
131
 
            self._old_visible = None
132
 
    
133
 
    def normal_mouse_move(self, event):
134
 
        plot = self.component
135
 
        if plot is not None:
136
 
            self.new_value = self.gather_values(event)
137
 
            self.last_mouse_position = (event.x, event.y)