1
1
""" Defines the ToolTip class.
4
from __future__ import with_statement
4
6
from numpy import array
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
40
42
rotate_angle = Float(0.0)
42
# The available space in the four directions, used to determine layout
43
# If -1, assume there is enough space in that direction.
45
# Available space to the left of the tooltip; if -1 there is "enough" space.
46
left_space = Float(-1)
48
# Available space to the right of the tooltip; if -1 there is "enough" space.
49
right_space = Float(-1)
51
# Available space below the tooltip; if -1 there is "enough" space.
52
below_space = Float(-1)
54
# Available space above the tooltip; if -1 there is "enough" space.
55
above_space = Float(-1)
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.
44
# Should the tooltip automatically reposition itself to remain visible
45
# and unclipped on its overlaid component?
46
auto_adjust = Bool(True)
62
48
# The tooltip is a fixed size. (Overrides PlotComponent.)
134
117
self.outer_bounds = outer_bounds
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]
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]
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
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
139
self.outer_position = new_pos
146
141
self._layout_needed = False
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
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()