2
from chaco.tools.api import SelectTool
3
from traits.api import List
5
from legend import Legend
7
class SelectableLegend(Legend, SelectTool):
9
# A list of indices into self._cached_labels that indicates which labels
10
# should be rendered in the "selected" style
13
# A cached list of tuples (x,y,w,h) of each label's geometry
14
_cached_label_dims = List
16
#------------------------------------------------------------------------
18
#------------------------------------------------------------------------
21
Legend._do_layout(self)
22
self._compute_label_dims()
24
def _compute_label_dims(self):
26
edge_space = self.border_width + self.border_padding
27
icon_width, icon_height = self.icon_bounds
29
icon_x = self.x + edge_space
30
text_x = icon_x + icon_width + self.icon_spacing
31
y = self.y2 - edge_space
32
for i, label_name in enumerate(self._cached_label_names):
33
label_width, label_height = self._cached_label_sizes[i]
35
icon_y = y + (label_height - icon_height) / 2
36
dims.append((icon_x, icon_y, icon_width + self.icon_spacing + label_width,
38
y -= self.line_spacing
39
self._cached_label_dims = dims
41
#------------------------------------------------------------------------
42
# SelectTool interface
43
#------------------------------------------------------------------------
45
def _get_selection_state(self, event):
46
for ndx, dims in enumerate(self._cached_label_dims):
48
if (x <= event.x <= x+w) and (y <= event.y <= y+h):
49
return (ndx in self.selections), True
51
if len(self._cached_label_dims) > 0:
52
return (ndx in self.selections), False
56
def _get_selection_token(self, event):
57
for ndx, dims in enumerate(self._cached_label_dims):
59
if (x <= event.x <= x+w) and (y <= event.y <= y+h):
64
def _select(self, index, append=True):
66
self.selections.append(index)
68
self.selections = [index]
71
def _deselect(self, index=None):
72
if index in self.selections:
73
self.selections.remove(index)