/usr/lib/python2.7/dist-packages/chaco/tools/legend_highlighter.py is in python-chaco 4.4.1-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import operator
# ETS imports
from chaco.tools.api import LegendTool
from traits.api import List, Float
def get_hit_plots(legend, event):
if legend is None or not legend.is_in(event.x, event.y):
return []
try:
# FIXME: The size of the legend is not being computed correctly, so
# always look at the front of the label where we know we'll get a hit.
label = legend.get_label_at(legend.x + 20, event.y)
except:
raise
label = None
if label is None:
return []
try:
ndx = legend._cached_labels.index(label)
label_name = legend._cached_label_names[ndx]
renderers = legend.plots[label_name]
return renderers
except (ValueError, KeyError):
return []
class LegendHighlighter(LegendTool):
""" A tool for legends that allows clicking on the legend to show
or hide certain plots.
"""
#: Which mousebutton to use to move the legend
drag_button = "right"
#: What to divide the alpha value by when plot is not selected
dim_factor = Float(3.0)
#: How much to scale the line when it is selected or deselected
line_scale = Float(2.0)
# The currently selected renderers
_selected_renderers = List
def normal_left_down(self, event):
if not self.component.is_in(event.x, event.y):
return
plots = get_hit_plots(self.component, event)
if len(plots) > 0:
plot = plots[0]
if event.shift_down:
# User in multi-select mode by using [shift] key.
if plot in self._selected_renderers:
self._selected_renderers.remove(plot)
else:
self._selected_renderers.append(plot)
else:
# User in single-select mode.
add_plot = plot not in self._selected_renderers
self._selected_renderers = []
if add_plot:
self._selected_renderers.append(plot)
if self._selected_renderers:
self._set_states(self.component.plots)
else:
self._reset_selects(self.component.plots)
plot.request_redraw()
event.handled = True
def _reset_selects(self, plots):
""" Set all renderers to their default values. """
for plot in reduce(operator.add, plots.values()):
if not hasattr(plot, '_orig_alpha'):
plot._orig_alpha = plot.alpha
plot._orig_line_width = plot.line_width
plot.alpha = plot._orig_alpha
plot.line_width = plot._orig_line_width
return
def _set_states(self, plots):
""" Decorates a plot to indicate it is selected """
for plot in reduce(operator.add, plots.values()):
if not hasattr(plot, '_orig_alpha'):
# FIXME: These attributes should be put into the class def.
plot._orig_alpha = plot.alpha
plot._orig_line_width = plot.line_width
if plot in self._selected_renderers:
plot.line_width = plot._orig_line_width * self.line_scale
plot.alpha = plot._orig_alpha
else:
plot.alpha = plot._orig_alpha / self.dim_factor
plot.line_width = plot._orig_line_width / self.line_scale
# Move the selected renderers to the front
if len(self._selected_renderers) > 0:
container = self._selected_renderers[0].container
components = container.components[:]
for renderer in self._selected_renderers:
components.remove(renderer)
components += self._selected_renderers
container._components = components
|