~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/canvas/data_source_button.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
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
7
 
 
8
 
DEBUG = False
9
 
 
10
 
class ButtonController(HasTraits):
11
 
    """ Tells buttons what to do
12
 
 
13
 
    Buttons defer to this when they are activated.
14
 
    """
15
 
 
16
 
    modifier = Enum("control", "shift", "alt")
17
 
 
18
 
    # The list of buttons that are currently "down"
19
 
    active_buttons = List
20
 
 
21
 
    # A reference to the Plot object that displays scatterplots of multiple
22
 
    # dataseries
23
 
    plot = Instance(Plot)
24
 
 
25
 
    # The transient plot overlay housing self.plot
26
 
    plot_overlay = Any
27
 
 
28
 
    # The name of the scatter plot in the Plot
29
 
    _scatterplot_name = "ButtonControllerPlot"
30
 
 
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
34
 
        actual mouse event.
35
 
        """
36
 
        #control_down = getattr(event, self.modifier + "_down", False)
37
 
        control_down = True
38
 
        if DEBUG:
39
 
            print "[notify]", button.plotname, type, "control:", control_down
40
 
        if type == "down":
41
 
            if control_down and button in self.active_buttons:
42
 
                self.button_deselected(button)
43
 
            else:
44
 
                if not control_down:
45
 
                    # Deselect all current buttons and select the new one
46
 
                    [self.button_deselected(b) for b in self.active_buttons \
47
 
                            if b is not button]
48
 
                self.button_selected(button)
49
 
        else:  # type == "up"
50
 
            #if not control_down:
51
 
            if 1:
52
 
                self.button_deselected(button)
53
 
        return
54
 
 
55
 
    def button_selected(self, button):
56
 
        if DEBUG:
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:
60
 
            return
61
 
        numbuttons = len(self.active_buttons)
62
 
        if numbuttons == 0:
63
 
            self.active_buttons.append(button)
64
 
            button.show_overlay()
65
 
        elif numbuttons == 1:
66
 
            self.active_buttons[0].hide_overlay()
67
 
            self.active_buttons.append(button)
68
 
            self.show_scatterplot(*self.active_buttons)
69
 
        elif numbuttons == 2:
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)
75
 
        else:
76
 
            return
77
 
        button.button_state = "down"
78
 
        return
79
 
 
80
 
    def button_deselected(self, button):
81
 
        if DEBUG:
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"
86
 
            return
87
 
        numbuttons = len(self.active_buttons)
88
 
        if numbuttons == 1:
89
 
            if button in self.active_buttons:
90
 
                self.active_buttons.remove(button)
91
 
            button.hide_overlay()
92
 
        elif numbuttons == 2:
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()
98
 
        else:
99
 
            return
100
 
        button.button_state = "up"
101
 
        return
102
 
 
103
 
    def show_scatterplot(self, b1, b2):
104
 
        if len(self.plot.plots) > 0:
105
 
            self.plot.delplot(*self.plot.plots.keys())
106
 
 
107
 
        cur_plot = self.plot.plot((b1.plotname+"_y", b2.plotname+"_y"),
108
 
                                  name=self._scatterplot_name,
109
 
                                  type="scatter",
110
 
                                  marker="square",
111
 
                                  color=tuple(choice(COLOR_PALETTE)),
112
 
                                  marker_size=8,
113
 
                                  )
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()
119
 
 
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
126
 
 
127
 
 
128
 
 
129
 
class DataSourceButton(PlotToolbarButton):
130
 
 
131
 
    # A TransientPlotOverlay containing the timeseries plot of this datasource
132
 
    plot_overlay = Any
133
 
 
134
 
    plotname = Str
135
 
 
136
 
    canvas = Any
137
 
 
138
 
    #overlay_bounds = List()
139
 
 
140
 
    # Can't call this "controller" because it conflicts with old tool dispatch
141
 
    button_controller = Instance(ButtonController)
142
 
 
143
 
    # Override inherited trait
144
 
    label_color = (0,0,0,1)
145
 
 
146
 
    resizable = ""
147
 
 
148
 
    cur_bid = Int(-1)
149
 
 
150
 
    # The overlay to display when the user holds the mouse down over us.
151
 
    #_overlay = Instance(AbstractOverlay)
152
 
 
153
 
    def normal_left_down(self, event):
154
 
        self.button_state = "down"
155
 
        self.button_controller.notify(self, "down", event)
156
 
        event.handled = True
157
 
        self.request_redraw()
158
 
 
159
 
    def normal_left_up(self, event):
160
 
        self.button_state = "up"
161
 
        self.button_controller.notify(self, "up", event)
162
 
        event.handled = True
163
 
        self.request_redraw()
164
 
 
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())
172
 
 
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)
177
 
            self.cur_bid = -1
178
 
            self.normal_left_up(event)
179
 
 
180
 
    def normal_mouse_leave(self, event):
181
 
        if event.left_down:
182
 
            return self.normal_left_up(event)
183
 
 
184
 
    def normal_mouse_enter(self, event):
185
 
        if event.left_down:
186
 
            return self.normal_left_down(event)
187
 
 
188
 
    def show_overlay(self):
189
 
        if self.plot_overlay is not None:
190
 
            self._do_layout()
191
 
            self.plot_overlay.visible = True
192
 
        self.request_redraw()
193
 
        return
194
 
 
195
 
    def hide_overlay(self):
196
 
        if self.plot_overlay is not None:
197
 
            self.plot_overlay.visible = False
198
 
        self.request_redraw()
199
 
        return
200
 
 
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()