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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
""" Defines the PlotLabel class.
"""
from __future__ import with_statement
from enthought.enable.font_metrics_provider import font_metrics_provider
from enthought.traits.api import DelegatesTo, Enum, Instance, Str, Trait
from abstract_overlay import AbstractOverlay
from label import Label
LabelDelegate = DelegatesTo("_label")
class PlotLabel(AbstractOverlay):
""" A label used by plots.
This class wraps a simple Label instance, and delegates some traits to it.
"""
# The text of the label.
text = LabelDelegate
# The color of the label text.
color = DelegatesTo("_label")
# The font for the label text.
font = LabelDelegate
# The angle of rotation of the label.
angle = DelegatesTo("_label", "rotate_angle")
bgcolor = LabelDelegate
border_width = LabelDelegate
border_color = LabelDelegate
border_visible = LabelDelegate
margin = LabelDelegate
line_spacing = LabelDelegate
#------------------------------------------------------------------------
# Layout-related traits
#------------------------------------------------------------------------
# Horizontal justification used if the label has more horizontal space
# than it needs.
hjustify = Enum("center", "left", "right")
# Vertical justification used if the label has more vertical space than it
# needs.
vjustify = Enum("center", "bottom", "top")
# The position of this label relative to the object it is overlaying.
# Can be "top", "left", "right", "bottom", and optionally can be preceeded
# by the words "inside" or "outside", separated by a space. If "inside"
# and "outside" are not provided, then defaults to "outside".
# Examples:
# inside top
# outside right
overlay_position = Trait("outside top", Str, None)
# Should this PlotLabel modify the padding on its underlying component
# if there is not enough room to lay out the text?
# FIXME: This could cause cycles in layout, so not implemented for now
#modify_component = Bool(True)
# By default, this acts like a component and will render on the main
# "plot" layer unless its **component** attribute gets set.
draw_layer = "plot"
#------------------------------------------------------------------------
# Private traits
#------------------------------------------------------------------------
# The label has a fixed height and can be resized horizontally. (Overrides
# PlotComponent.)
resizable = "h"
# The Label instance this plot label is wrapping.
_label = Instance(Label, args=())
def __init__(self, text="", *args, **kw):
super(PlotLabel, self).__init__(*args, **kw)
self.text = text
return
def overlay(self, component, gc, view_bounds=None, mode="normal"):
""" Draws this label overlaid on another component.
Overrides AbstractOverlay.
"""
self._draw_overlay(gc, view_bounds, mode)
return
def get_preferred_size(self):
""" Returns the label's preferred size.
Overrides PlotComponent.
"""
dummy_gc = font_metrics_provider()
size = self._label.get_bounding_box(dummy_gc)
return size
def do_layout(self):
""" Tells this component to do layout.
Overrides PlotComponent.
"""
if self.component is not None:
self._layout_as_overlay()
else:
self._layout_as_component()
return
def _draw_overlay(self, gc, view_bounds=None, mode="normal"):
""" Draws the overlay layer of a component.
Overrides PlotComponent.
"""
# Perform justification and compute the correct offsets for
# the label position
width, height = self._label.get_bounding_box(gc)
if self.hjustify == "left":
x_offset = 0
elif self.hjustify == "right":
x_offset = self.width - width
elif self.hjustify == "center":
x_offset = int((self.width - width) / 2)
if self.vjustify == "bottom":
y_offset = 0
elif self.vjustify == "top":
y_offset = self.height - height
elif self.vjustify == "center":
y_offset = int((self.height - height) / 2)
with gc:
# XXX: Uncomment this after we fix kiva GL backend's clip stack
#gc.clip_to_rect(self.x, self.y, self.width, self.height)
# We have to translate to our position because the label
# tries to draw at (0,0).
gc.translate_ctm(self.x + x_offset, self.y + y_offset)
self._label.draw(gc)
return
def _draw_plot(self, gc, view_bounds=None, mode="normal"):
if self.component is None:
# We are not overlaying anything else, so we should render
# on this layer
self._draw_overlay(gc, view_bounds, mode)
def _layout_as_component(self, size=None, force=False):
pass
def _layout_as_overlay(self, size=None, force=False):
""" Lays out the label as an overlay on another component.
"""
if self.component is not None:
orientation = self.overlay_position
outside = True
if "inside" in orientation:
tmp = orientation.split()
tmp.remove("inside")
orientation = tmp[0]
outside = False
elif "outside" in orientation:
tmp = orientation.split()
tmp.remove("outside")
orientation = tmp[0]
if orientation in ("left", "right"):
self.y = self.component.y
self.height = self.component.height
if not outside:
gc = font_metrics_provider()
self.width = self._label.get_bounding_box(gc)[0]
if orientation == "left":
if outside:
self.x = self.component.outer_x
self.width = self.component.padding_left
else:
self.outer_x = self.component.x
elif orientation == "right":
if outside:
self.x = self.component.x2 + 1
self.width = self.component.padding_right
else:
self.x = self.component.x2 - self.outer_width
elif orientation in ("bottom", "top"):
self.x = self.component.x
self.width = self.component.width
if not outside:
gc = font_metrics_provider()
self.height = self._label.get_bounding_box(gc)[1]
if orientation == "bottom":
if outside:
self.y = self.component.outer_y
self.height = self.component.padding_bottom
else:
self.outer_y = self.component.y
elif orientation == "top":
if outside:
self.y = self.component.y2 + 1
self.height = self.component.padding_top
else:
self.y = self.component.y2 - self.outer_height
else:
# Leave the position alone
pass
return
def _text_changed(self, old, new):
self._label.text = new
self.do_layout()
return
def _font_changed(self, old, new):
self._label.font = new
self.do_layout()
return
def _angle_changed(self, old, new):
self._label.rotate_angle = new
self.do_layout()
return
def _overlay_position_changed(self):
self.do_layout()
def _component_changed(self, old, new):
if new:
self.draw_layer = "overlay"
else:
self.draw_layer = "plot"
return
# EOF
|