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

« back to all changes in this revision

Viewing changes to chaco/tools/better_zoom.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:
33
33
 
34
34
    # if the mouse pointer should be used to control the center
35
35
    # of the zoom action
36
 
    zoom_to_mouse = Bool(False)
 
36
    zoom_to_mouse = Bool(True)
 
37
 
 
38
    # if the mouse pointer should be used to control the center
 
39
    # of the zoom action even for key events based zoom
 
40
    keys_zoom_to_mouse = Bool(True)
37
41
 
38
42
    # The axis to which the selection made by this tool is perpendicular. This
39
43
    # only applies in 'range' mode.
59
63
    # inherited from ToolHistoryMixin, but requires instances of ZoomState
60
64
    _history = List(ToolState, [ZoomState((1.0, 1.0), (1.0, 1.0))])
61
65
 
 
66
    def _do_zoom(self, new_index_factor, new_value_factor):
 
67
        if self.zoom_to_mouse:
 
68
            location = self.position
 
69
            x_map = self._get_x_mapper()
 
70
            y_map = self._get_y_mapper()
 
71
 
 
72
            cx = (x_map.range.high + x_map.range.low)/2
 
73
            if self._index_factor == new_index_factor:
 
74
                nextx = cx
 
75
            else:
 
76
                x = x_map.map_data(location[0])
 
77
                nextx = x + (cx - x)*(self._index_factor/new_index_factor)
 
78
 
 
79
            cy = (y_map.range.high + y_map.range.low)/2
 
80
            if self._value_factor == new_value_factor:
 
81
                nexty = cy
 
82
            else:
 
83
                y = y_map.map_data(location[1])
 
84
                nexty = y + (cy - y)*(self._value_factor/new_value_factor)
 
85
 
 
86
            pan_state = PanState((cx,cy), (nextx, nexty))
 
87
            zoom_state = ZoomState((self._index_factor, self._value_factor),
 
88
                                   (new_index_factor, new_value_factor))
 
89
 
 
90
            states = GroupedToolState([pan_state, zoom_state])
 
91
            states.apply(self)
 
92
            self._append_state(states)
 
93
 
 
94
        else:
 
95
 
 
96
            zoom_state = ZoomState((self._index_factor, self._value_factor),
 
97
                                   (new_index_factor, new_value_factor))
 
98
 
 
99
            zoom_state.apply(self)
 
100
            self._append_state(zoom_state)
 
101
 
62
102
    #--------------------------------------------------------------------------
63
103
    #  public interface
64
104
    #--------------------------------------------------------------------------
85
125
                return
86
126
            if self._zoom_limit_reached(new_value_factor, 'x'):
87
127
                return
88
 
 
89
 
        if self.zoom_to_mouse:
90
 
            location = self.position
91
 
 
92
 
            x_map = self._get_x_mapper()
93
 
            y_map = self._get_y_mapper()
94
 
 
95
 
            next = (x_map.map_data(location[0]),
96
 
                    y_map.map_data(location[1]))
97
 
            prev = (x_map.map_data(self.component.bounds[0]/2),
98
 
                    y_map.map_data(self.component.bounds[1]/2))
99
 
 
100
 
            pan_state = PanState(prev, next)
101
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
102
 
                                   (new_index_factor, new_value_factor))
103
 
 
104
 
            states = GroupedToolState([pan_state, zoom_state])
105
 
            states.apply(self)
106
 
            self._append_state(states)
107
 
 
108
 
        else:
109
 
 
110
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
111
 
                                   (new_index_factor, new_value_factor))
112
 
 
113
 
            zoom_state.apply(self)
114
 
            self._append_state(zoom_state)
 
128
        self._do_zoom(new_index_factor, new_value_factor)
115
129
 
116
130
    def zoom_out(self, factor=0):
117
131
        if factor == 0:
136
150
            if self._zoom_limit_reached(new_value_factor, 'x'):
137
151
                return
138
152
 
139
 
        if self.zoom_to_mouse:
140
 
            location = self.position
141
 
 
142
 
            x_map = self._get_x_mapper()
143
 
            y_map = self._get_y_mapper()
144
 
 
145
 
            next = (x_map.map_data(location[0]),
146
 
                    y_map.map_data(location[1]))
147
 
            prev = (x_map.map_data(self.component.bounds[0]/2),
148
 
                    y_map.map_data(self.component.bounds[1]/2))
149
 
 
150
 
            pan_state = PanState(prev, next)
151
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
152
 
                                   (new_index_factor, new_value_factor))
153
 
 
154
 
            states = GroupedToolState([pan_state, zoom_state])
155
 
            states.apply(self)
156
 
            self._append_state(states)
157
 
 
158
 
        else:
159
 
 
160
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
161
 
                                   (new_index_factor, new_value_factor))
162
 
 
163
 
            zoom_state.apply(self)
164
 
            self._append_state(zoom_state)
 
153
        self._do_zoom(new_index_factor, new_value_factor)
165
154
 
166
155
    def zoom_in_x(self, factor=0):
167
156
        if factor == 0:
178
167
            if self._zoom_limit_reached(new_value_factor, 'x'):
179
168
                return
180
169
 
181
 
        if self.zoom_to_mouse:
182
 
            location = self.position
183
 
 
184
 
            x_map = self._get_x_mapper()
185
 
            y_map = self._get_y_mapper()
186
 
 
187
 
            next = (x_map.map_data(location[0]),
188
 
                    y_map.map_data(location[1]))
189
 
            prev = (x_map.map_data(self.component.bounds[0]/2),
190
 
                    y_map.map_data(self.component.bounds[1]/2))
191
 
 
192
 
            pan_state = PanState(prev, next)
193
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
194
 
                                   (new_index_factor, new_value_factor))
195
 
 
196
 
            states = GroupedToolState([pan_state, zoom_state])
197
 
            states.apply(self)
198
 
            self._append_state(states)
199
 
 
200
 
        else:
201
 
 
202
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
203
 
                                   (new_index_factor, new_value_factor))
204
 
 
205
 
            zoom_state.apply(self)
206
 
            self._append_state(zoom_state)
 
170
        self._do_zoom(new_index_factor, new_value_factor)
207
171
 
208
172
    def zoom_out_x(self, factor=0):
209
173
        if factor == 0:
219
183
            new_value_factor = self._value_factor / factor
220
184
            if self._zoom_limit_reached(new_value_factor, 'x'):
221
185
                return
222
 
 
223
 
        if self.zoom_to_mouse:
224
 
            location = self.position
225
 
 
226
 
            x_map = self._get_x_mapper()
227
 
            y_map = self._get_y_mapper()
228
 
 
229
 
            next = (x_map.map_data(location[0]),
230
 
                    y_map.map_data(location[1]))
231
 
            prev = (x_map.map_data(self.component.bounds[0]/2),
232
 
                    y_map.map_data(self.component.bounds[1]/2))
233
 
 
234
 
            pan_state = PanState(prev, next)
235
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
236
 
                                   (new_index_factor, new_value_factor))
237
 
 
238
 
            states = GroupedToolState([pan_state, zoom_state])
239
 
            states.apply(self)
240
 
            self._append_state(states)
241
 
 
242
 
        else:
243
 
 
244
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
245
 
                                   (new_index_factor, new_value_factor))
246
 
 
247
 
            zoom_state.apply(self)
248
 
            self._append_state(zoom_state)
 
186
        self._do_zoom(new_index_factor, new_value_factor)
 
187
 
249
188
 
250
189
    def zoom_in_y(self, factor=0):
251
190
        if factor == 0:
262
201
            if self._zoom_limit_reached(new_value_factor, 'y'):
263
202
                return
264
203
 
265
 
        if self.zoom_to_mouse:
266
 
            location = self.position
267
 
 
268
 
            x_map = self._get_x_mapper()
269
 
            y_map = self._get_y_mapper()
270
 
 
271
 
            next = (x_map.map_data(location[0]),
272
 
                    y_map.map_data(location[1]))
273
 
            prev = (x_map.map_data(self.component.bounds[0]/2),
274
 
                    y_map.map_data(self.component.bounds[1]/2))
275
 
 
276
 
            pan_state = PanState(prev, next)
277
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
278
 
                                   (new_index_factor, new_value_factor))
279
 
 
280
 
            states = GroupedToolState([pan_state, zoom_state])
281
 
            states.apply(self)
282
 
            self._append_state(states)
283
 
 
284
 
        else:
285
 
 
286
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
287
 
                                   (new_index_factor, new_value_factor))
288
 
 
289
 
            zoom_state.apply(self)
290
 
            self._append_state(zoom_state)
 
204
        self._do_zoom(new_index_factor, new_value_factor)
291
205
 
292
206
    def zoom_out_y(self, factor=0):
293
207
        if factor == 0:
304
218
            if self._zoom_limit_reached(new_value_factor, 'y'):
305
219
                return
306
220
 
307
 
        if self.zoom_to_mouse:
308
 
            location = self.position
309
 
 
310
 
            x_map = self._get_x_mapper()
311
 
            y_map = self._get_y_mapper()
312
 
 
313
 
            next = (x_map.map_data(location[0]),
314
 
                    y_map.map_data(location[1]))
315
 
            prev = (x_map.map_data(self.component.bounds[0]/2),
316
 
                    y_map.map_data(self.component.bounds[1]/2))
317
 
 
318
 
            pan_state = PanState(prev, next)
319
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
320
 
                                   (new_index_factor, new_value_factor))
321
 
 
322
 
            states = GroupedToolState([pan_state, zoom_state])
323
 
            states.apply(self)
324
 
            self._append_state(states)
325
 
 
326
 
        else:
327
 
 
328
 
            zoom_state = ZoomState((self._index_factor, self._value_factor),
329
 
                                   (new_index_factor, new_value_factor))
330
 
 
331
 
            zoom_state.apply(self)
332
 
            self._append_state(zoom_state)
 
221
        self._do_zoom(new_index_factor, new_value_factor)
333
222
 
334
223
    #--------------------------------------------------------------------------
335
224
    #  BaseTool interface
339
228
        """ Handles a key being pressed when the tool is in the 'normal'
340
229
        state.
341
230
        """
 
231
        if not self.keys_zoom_to_mouse:
 
232
            self.position = self._center_screen()
342
233
        if self.zoom_in_key.match(event):
343
 
            self.position = self._center_screen()
344
234
            self.zoom_in()
345
235
            event.handled = True
346
236
        elif self.zoom_out_key.match(event):
347
 
            self.position = self._center_screen()
348
237
            self.zoom_out()
349
238
            event.handled = True
350
239
        elif self.zoom_in_x_key.match(event):
351
 
            self.position = self._center_screen()
352
240
            self.zoom_in_x(self.zoom_factor)
353
241
            event.handled = True
354
242
        elif self.zoom_out_x_key.match(event):
355
 
            self.position = self._center_screen()
356
243
            self.zoom_out_x(self.zoom_factor)
357
244
            event.handled = True
358
245
        elif self.zoom_in_y_key.match(event):
359
 
            self.position = self._center_screen()
360
246
            self.zoom_in_y(self.zoom_factor)
361
247
            event.handled = True
362
248
        elif self.zoom_out_y_key.match(event):
363
 
            self.position = self._center_screen()
364
249
            self.zoom_out_y(self.zoom_factor)
365
250
            event.handled = True
366
251