2
from random import choice
3
from traits.api import Any, Enum, HasTraits, Instance, Int, List, Str
4
from chaco.example_support import COLOR_PALETTE
5
from chaco.api import Plot
6
from chaco.plot_canvas_toolbar import PlotToolbarButton
10
class ButtonController(HasTraits):
11
""" Tells buttons what to do
13
Buttons defer to this when they are activated.
16
modifier = Enum("control", "shift", "alt")
18
# The list of buttons that are currently "down"
21
# A reference to the Plot object that displays scatterplots of multiple
25
# The transient plot overlay housing self.plot
28
# The name of the scatter plot in the Plot
29
_scatterplot_name = "ButtonControllerPlot"
31
def notify(self, button, type, event):
32
""" Informs the controller that the particular **button** just
33
got an event. **Type** is either "up" or "down". Event is the
36
#control_down = getattr(event, self.modifier + "_down", False)
39
print "[notify]", button.plotname, type, "control:", control_down
41
if control_down and button in self.active_buttons:
42
self.button_deselected(button)
45
# Deselect all current buttons and select the new one
46
[self.button_deselected(b) for b in self.active_buttons \
48
self.button_selected(button)
52
self.button_deselected(button)
55
def button_selected(self, button):
57
print "active:", [b.plotname for b in self.active_buttons]
58
print "new button selected:", button.plotname
59
if button in self.active_buttons:
61
numbuttons = len(self.active_buttons)
63
self.active_buttons.append(button)
66
self.active_buttons[0].hide_overlay()
67
self.active_buttons.append(button)
68
self.show_scatterplot(*self.active_buttons)
70
# Replace the last active button with the new one
71
self.active_buttons[1].button_state = "up"
72
self.active_buttons[1] = button
73
self.hide_scatterplot()
74
self.show_scatterplot(*self.active_buttons)
77
button.button_state = "down"
80
def button_deselected(self, button):
82
print "active:", [b.plotname for b in self.active_buttons]
83
print "new button deselected:", button.plotname
84
if button not in self.active_buttons:
85
button.button_state = "up"
87
numbuttons = len(self.active_buttons)
89
if button in self.active_buttons:
90
self.active_buttons.remove(button)
93
if button in self.active_buttons:
94
self.active_buttons.remove(button)
95
self.hide_scatterplot()
96
remaining_button = self.active_buttons[0]
97
remaining_button.show_overlay()
100
button.button_state = "up"
103
def show_scatterplot(self, b1, b2):
104
if len(self.plot.plots) > 0:
105
self.plot.delplot(*self.plot.plots.keys())
107
cur_plot = self.plot.plot((b1.plotname+"_y", b2.plotname+"_y"),
108
name=self._scatterplot_name,
111
color=tuple(choice(COLOR_PALETTE)),
114
self.plot.index_axis.title = b1.plotname
115
#self.plot.value_axis.title = b2.plotname
116
self.plot.title = b1.plotname + " vs. " + b2.plotname
117
self.plot_overlay.visible = True
118
self.plot.request_redraw()
120
def hide_scatterplot(self):
121
if self._scatterplot_name in self.plot.plots:
122
self.plot.delplot(self._scatterplot_name)
123
self.plot.index_range.set_bounds("auto", "auto")
124
self.plot.value_range.set_bounds("auto", "auto")
125
self.plot_overlay.visible = False
129
class DataSourceButton(PlotToolbarButton):
131
# A TransientPlotOverlay containing the timeseries plot of this datasource
138
#overlay_bounds = List()
140
# Can't call this "controller" because it conflicts with old tool dispatch
141
button_controller = Instance(ButtonController)
143
# Override inherited trait
144
label_color = (0,0,0,1)
150
# The overlay to display when the user holds the mouse down over us.
151
#_overlay = Instance(AbstractOverlay)
153
def normal_left_down(self, event):
154
self.button_state = "down"
155
self.button_controller.notify(self, "down", event)
157
self.request_redraw()
159
def normal_left_up(self, event):
160
self.button_state = "up"
161
self.button_controller.notify(self, "up", event)
163
self.request_redraw()
165
def normal_blob_down(self, event):
166
if self.cur_bid == -1:
167
self.cur_bid = event.bid
168
self.normal_left_down(event)
169
if hasattr(event, "bid"):
170
event.window.capture_blob(self, event.bid,
171
event.net_transform())
173
def normal_blob_up(self, event):
174
if event.bid == self.cur_bid:
175
if hasattr(event, "bid"):
176
event.window.release_blob(event.bid)
178
self.normal_left_up(event)
180
def normal_mouse_leave(self, event):
182
return self.normal_left_up(event)
184
def normal_mouse_enter(self, event):
186
return self.normal_left_down(event)
188
def show_overlay(self):
189
if self.plot_overlay is not None:
191
self.plot_overlay.visible = True
192
self.request_redraw()
195
def hide_overlay(self):
196
if self.plot_overlay is not None:
197
self.plot_overlay.visible = False
198
self.request_redraw()
201
def _do_layout(self):
202
if self.canvas is not None:
203
boff = self.canvas.bounds_offset
204
self.plot_overlay.offset = (boff[0],
205
boff[1] + self.y - self.container.y + self.height/2)
206
self.plot_overlay.do_layout()