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

« back to all changes in this revision

Viewing changes to enthought/chaco/tooltip.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 ToolTip class.
2
2
"""
3
3
 
 
4
from __future__ import with_statement
 
5
 
4
6
from numpy import array
5
7
 
6
8
# Enthought library imports
7
9
from enthought.enable.api import black_color_trait, white_color_trait
8
 
from enthought.kiva import font_metrics_provider
 
10
from enthought.enable.font_metrics_provider import font_metrics_provider
9
11
from enthought.kiva.traits.kiva_font_trait import KivaFont
10
12
from enthought.traits.api import Any, Bool, List, Int, Float, on_trait_change
11
13
 
39
41
    # are disabled
40
42
    rotate_angle = Float(0.0)
41
43
 
42
 
    # The available space in the four directions, used to determine layout
43
 
    # If -1, assume there is enough space in that direction.
44
 
 
45
 
    # Available space to the left of the tooltip; if -1 there is "enough" space.
46
 
    left_space = Float(-1)
47
 
 
48
 
    # Available space to the right of the tooltip; if -1 there is "enough" space.
49
 
    right_space = Float(-1)
50
 
 
51
 
    # Available space below the tooltip; if -1 there is "enough" space.
52
 
    below_space = Float(-1)
53
 
 
54
 
    # Available space above the tooltip; if -1 there is "enough" space.
55
 
    above_space = Float(-1)
56
 
 
57
 
    # The position of the corner of the tooltip.  Which corner this represents
58
 
    # depends on the space available on each side, set by **left_space**, etc.
59
 
    # If any of those values is -1, there is enough space in that direction.
60
 
    corner_point = List
 
44
    # Should the tooltip automatically reposition itself to remain visible
 
45
    # and unclipped on its overlaid component?
 
46
    auto_adjust = Bool(True)
61
47
 
62
48
    # The tooltip is a fixed size. (Overrides PlotComponent.)
63
49
    resizable = ""
102
88
        
103
89
        Overrides PlotComponent.
104
90
        """
105
 
        gc.save_state()
106
 
        try:
 
91
        with gc:
107
92
            edge_space = self.border_width + self.border_padding
108
93
            gc.translate_ctm(self.x + edge_space, self.y)
109
94
            y = self.height - edge_space
114
99
                label.draw(gc)
115
100
                gc.translate_ctm(0,-y)
116
101
                y -= self.line_spacing
117
 
        finally:
118
 
            gc.restore_state()
119
102
        return
120
103
 
121
104
 
133
116
 
134
117
        self.outer_bounds = outer_bounds
135
118
 
136
 
        if self.corner_point:
137
 
            if self.right_space != -1 and self.right_space<outer_bounds[0]:
138
 
                self.x = self.corner_point[0] - outer_bounds[0]
139
 
            else:
140
 
                self.x = self.corner_point[0]
141
 
            if self.above_space != -1 and self.above_space<outer_bounds[1]:
142
 
                self.y = self.corner_point[1] - outer_bounds[1]
143
 
            else:
144
 
                self.y = self.corner_point[1]
 
119
        if self.auto_adjust and self.component is not None:
 
120
            new_pos = list(self.outer_position)
 
121
            for dimindex in (0,1):
 
122
                pos = self.position[dimindex]
 
123
                extent = outer_bounds[dimindex]
 
124
                c_min = self.component.position[dimindex]
 
125
                c_max = c_min + self.component.bounds[dimindex]
 
126
                # Is the tooltip just too wide/tall?
 
127
                if extent > (c_max - c_min):
 
128
                    new_pos[dimindex] = c_min
 
129
                # Does it extend over the c_max edge?  (right/top)
 
130
                elif pos + extent > c_max:
 
131
                    new_pos[dimindex] = c_max - extent
 
132
 
 
133
                # Does it extend over the c_min edge? This is not an elif so
 
134
                # that we can fix the situation where the c_max edge adjustment
 
135
                # above pushes the position negative.
 
136
                if new_pos[dimindex] < c_min:
 
137
                    new_pos[dimindex] = c_min
 
138
            
 
139
            self.outer_position = new_pos
145
140
 
146
141
        self._layout_needed = False
147
142
 
148
143
    def _recompute_text(self):
149
144
        labels = [Label(text=line, font=self.font, margin=0,
150
145
                        bgcolor='transparent', border_width=0,
151
 
                        color=self.text_color, rotate_angle=self.rotate_angle) 
 
146
                        color=self.text_color, rotate_angle=self.rotate_angle)
152
147
                    for line in self.lines]
153
148
        dummy_gc = self._font_metrics_provider
154
149
        line_sizes = array([label.get_width_height(dummy_gc)
169
164
        self._text_props_valid = False
170
165
        self._layout_needed = True
171
166
 
172
 
    @on_trait_change("border_padding,line_spacing,lines,lines_items,padding," \
173
 
                     "left_space,right_space,below_space,above_space")
 
167
    @on_trait_change("border_padding,line_spacing,lines,lines_items,padding")
174
168
    def _invalidate_layout(self):
175
169
        self._layout_needed = True
176
170
        self.request_redraw()
177
 
 
178