From: <pki...@us...> - 2008-07-24 22:30:02
|
Revision: 5853 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5853&view=rev Author: pkienzle Date: 2008-07-24 22:29:57 +0000 (Thu, 24 Jul 2008) Log Message: ----------- support mouse wheel in wx Modified Paths: -------------- trunk/matplotlib/examples/pylab_examples/image_slices_viewer.py trunk/matplotlib/lib/matplotlib/backend_bases.py trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py trunk/matplotlib/lib/matplotlib/backends/backend_wx.py Modified: trunk/matplotlib/examples/pylab_examples/image_slices_viewer.py =================================================================== --- trunk/matplotlib/examples/pylab_examples/image_slices_viewer.py 2008-07-24 21:56:57 UTC (rev 5852) +++ trunk/matplotlib/examples/pylab_examples/image_slices_viewer.py 2008-07-24 22:29:57 UTC (rev 5853) @@ -17,7 +17,7 @@ self.update() def onscroll(self, event): - print event.button + print event.button, event.step if event.button=='up': self.ind = numpy.clip(self.ind+1, 0, self.slices-1) else: Modified: trunk/matplotlib/lib/matplotlib/backend_bases.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backend_bases.py 2008-07-24 21:56:57 UTC (rev 5852) +++ trunk/matplotlib/lib/matplotlib/backend_bases.py 2008-07-24 22:29:57 UTC (rev 5853) @@ -768,7 +768,10 @@ *key* the key pressed: None, chr(range(255), 'shift', 'win', or 'control' + *step* + number of scroll steps (positive for 'up', negative for 'down') + Example usage:: def on_press(event): @@ -783,16 +786,18 @@ inaxes = None # the Axes instance if mouse us over axes xdata = None # x coord of mouse in data coords ydata = None # y coord of mouse in data coords + step = None # scroll steps for scroll events def __init__(self, name, canvas, x, y, button=None, key=None, - guiEvent=None): + step=0, guiEvent=None): """ x, y in figure coords, 0,0 = bottom, left - button pressed None, 1, 2, 3 + button pressed None, 1, 2, 3, 'up', 'down' """ LocationEvent.__init__(self, name, canvas, x, y, guiEvent=guiEvent) self.button = button self.key = key + self.step = step class PickEvent(Event): """ @@ -1050,7 +1055,7 @@ event = PickEvent(s, self, mouseevent, artist, **kwargs) self.callbacks.process(s, event) - def scroll_event(self, x, y, button, guiEvent=None): + def scroll_event(self, x, y, step, guiEvent=None): """ Backend derived classes should call this function on any scroll wheel event. x,y are the canvas coords: 0,0 is lower, @@ -1059,9 +1064,13 @@ This method will be call all functions connected to the 'scroll_event' with a :class:`MouseEvent` instance. """ - self._button = button + if step >= 0: + self._button = 'up' + else: + self._button = 'down' s = 'scroll_event' - mouseevent = MouseEvent(s, self, x, y, button, self._key, guiEvent=guiEvent) + mouseevent = MouseEvent(s, self, x, y, self._button, self._key, + step=step, guiEvent=guiEvent) self.callbacks.process(s, mouseevent) Modified: trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2008-07-24 21:56:57 UTC (rev 5852) +++ trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2008-07-24 22:29:57 UTC (rev 5853) @@ -181,10 +181,10 @@ # flipy so y=0 is bottom of canvas y = self.allocation.height - event.y if event.direction==gdk.SCROLL_UP: - direction = 'up' + step = 1 else: - direction = 'down' - FigureCanvasBase.scroll_event(self, x, y, direction) + step = -1 + FigureCanvasBase.scroll_event(self, x, y, step) return False # finish event propagation? def button_press_event(self, widget, event): Modified: trunk/matplotlib/lib/matplotlib/backends/backend_wx.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_wx.py 2008-07-24 21:56:57 UTC (rev 5852) +++ trunk/matplotlib/lib/matplotlib/backends/backend_wx.py 2008-07-24 22:29:57 UTC (rev 5853) @@ -1160,9 +1160,36 @@ FigureCanvasBase.button_release_event(self, x, y, 1, guiEvent=evt) def _onMouseWheel(self, evt): - # TODO: implement mouse wheel handler - pass + """Translate mouse wheel events into matplotlib events""" + # Determine mouse location + x = evt.GetX() + y = self.figure.bbox.height - evt.GetY() + + # Convert delta/rotation/rate into a floating point step size + delta = evt.GetWheelDelta() + rotation = evt.GetWheelRotation() + rate = evt.GetLinesPerAction() + #print "delta,rotation,rate",delta,rotation,rate + step = rate*float(rotation)/delta + + # Done handling event + evt.Skip() + + # Mac is giving two events for every wheel event + # Need to skip every second one + if wx.Platform == '__WXMAC__': + if not hasattr(self,'_skipwheelevent'): + self._skipwheelevent = True + elif self._skipwheelevent: + self._skipwheelevent = False + return # Return without processing event + else: + self._skipwheelevent = True + + # Convert to mpl event + FigureCanvasBase.scroll_event(self, x, y, step, guiEvent=evt) + def _onMotion(self, evt): """Start measuring on an axis.""" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |