Skip to content

Commit

Permalink
Merge pull request #1334 from NelleV/pep8_blocking_input
Browse files Browse the repository at this point in the history
PEP8 fixes on blocking_input.py
  • Loading branch information
dmcdougall committed Oct 8, 2012
2 parents f0fb4f7 + 292eb8e commit 28aa647
Showing 1 changed file with 67 additions and 60 deletions.
127 changes: 67 additions & 60 deletions lib/matplotlib/blocking_input.py
@@ -1,19 +1,24 @@
"""
This provides several classes used for blocking interaction with figure windows:
This provides several classes used for blocking interaction with figure
windows:
:class:`BlockingInput`
creates a callable object to retrieve events in a blocking way for interactive sessions
creates a callable object to retrieve events in a blocking way for
interactive sessions
:class:`BlockingKeyMouseInput`
creates a callable object to retrieve key or mouse clicks in a blocking way for interactive sessions.
creates a callable object to retrieve key or mouse clicks in a blocking
way for interactive sessions.
Note: Subclass of BlockingInput. Used by waitforbuttonpress
:class:`BlockingMouseInput`
creates a callable object to retrieve mouse clicks in a blocking way for interactive sessions.
creates a callable object to retrieve mouse clicks in a blocking way for
interactive sessions.
Note: Subclass of BlockingInput. Used by ginput
:class:`BlockingContourLabeler`
creates a callable object to retrieve mouse clicks in a blocking way that will then be used to place labels on a ContourSet
creates a callable object to retrieve mouse clicks in a blocking way that
will then be used to place labels on a ContourSet
Note: Subclass of BlockingMouseInput. Used by clabel
"""

Expand All @@ -22,14 +27,16 @@
from matplotlib.cbook import is_sequence_of_strings
import matplotlib.lines as mlines


class BlockingInput(object):
"""
Class that creates a callable object to retrieve events in a
blocking way.
"""
def __init__(self, fig, eventslist=()):
self.fig = fig
assert is_sequence_of_strings(eventslist), "Requires a sequence of event name strings"
assert is_sequence_of_strings(
eventslist), "Requires a sequence of event name strings"
self.eventslist = eventslist

def on_event(self, event):
Expand Down Expand Up @@ -60,13 +67,13 @@ def cleanup(self):
for cb in self.callbacks:
self.fig.canvas.mpl_disconnect(cb)

self.callbacks=[]
self.callbacks = []

def add_event(self,event):
def add_event(self, event):
"""For base class, this just appends an event to events."""
self.events.append(event)

def pop_event(self,index=-1):
def pop_event(self, index=-1):
"""
This removes an event from the event list. Defaults to
removing last event, but an index can be supplied. Note that
Expand All @@ -76,11 +83,11 @@ def pop_event(self,index=-1):
"""
self.events.pop(index)

def pop(self,index=-1):
def pop(self, index=-1):
self.pop_event(index)
pop.__doc__=pop_event.__doc__
pop.__doc__ = pop_event.__doc__

def __call__(self, n=1, timeout=30 ):
def __call__(self, n=1, timeout=30):
"""
Blocking call to retrieve n events
"""
Expand All @@ -96,18 +103,20 @@ def __call__(self, n=1, timeout=30 ):

# connect the events to the on_event function call
for n in self.eventslist:
self.callbacks.append( self.fig.canvas.mpl_connect(n, self.on_event) )
self.callbacks.append(
self.fig.canvas.mpl_connect(n, self.on_event))

try:
# Start event loop
self.fig.canvas.start_event_loop(timeout=timeout)
finally: # Run even on exception like ctrl-c
finally: # Run even on exception like ctrl-c
# Disconnect the callbacks
self.cleanup()

# Return the events in this case
return self.events


class BlockingMouseInput(BlockingInput):
"""
Class that creates a callable object to retrieve mouse clicks in a
Expand All @@ -118,25 +127,23 @@ class BlockingMouseInput(BlockingInput):
enter is like mouse button 2 and all others are like mouse button 1).
"""

button_add = 1
button_pop = 3
button_stop = 2
button_add = 1
button_pop = 3
button_stop = 2

def __init__(self, fig, mouse_add=1, mouse_pop=3, mouse_stop=2):
BlockingInput.__init__(self, fig=fig,
eventslist=('button_press_event',
'key_press_event') )
'key_press_event'))
self.button_add = mouse_add
self.button_pop = mouse_pop
self.button_stop= mouse_stop


self.button_stop = mouse_stop

def post_event(self):
"""
This will be called to process events
"""
assert len(self.events)>0, "No events yet"
assert len(self.events) > 0, "No events yet"

if self.events[-1].name == 'key_press_event':
self.key_event()
Expand Down Expand Up @@ -171,56 +178,57 @@ def key_event(self):

if key in ['backspace', 'delete']:
self.mouse_event_pop(event)
elif key in ['escape', 'enter']: # on windows XP and wxAgg, the enter key doesn't seem to register
elif key in ['escape', 'enter']:
# on windows XP and wxAgg, the enter key doesn't seem to register
self.mouse_event_stop(event)
else:
self.mouse_event_add(event)

def mouse_event_add( self, event ):
def mouse_event_add(self, event):
"""
Will be called for any event involving a button other than
button 2 or 3. This will add a click if it is inside axes.
"""
if event.inaxes:
self.add_click(event)
else: # If not a valid click, remove from event list
BlockingInput.pop(self,-1)
else: # If not a valid click, remove from event list
BlockingInput.pop(self, -1)

def mouse_event_stop( self, event ):
def mouse_event_stop(self, event):
"""
Will be called for any event involving button 2.
Button 2 ends blocking input.
"""

# Remove last event just for cleanliness
BlockingInput.pop(self,-1)
BlockingInput.pop(self, -1)

# This will exit even if not in infinite mode. This is
# consistent with MATLAB and sometimes quite useful, but will
# require the user to test how many points were actually
# returned before using data.
self.fig.canvas.stop_event_loop()

def mouse_event_pop( self, event ):
def mouse_event_pop(self, event):
"""
Will be called for any event involving button 3.
Button 3 removes the last click.
"""
# Remove this last event
BlockingInput.pop(self,-1)
BlockingInput.pop(self, -1)

# Now remove any existing clicks if possible
if len(self.events)>0:
self.pop(event,-1)
if len(self.events) > 0:
self.pop(event, -1)

def add_click(self,event):
def add_click(self, event):
"""
This add the coordinates of an event to the list of clicks
"""
self.clicks.append((event.xdata,event.ydata))
self.clicks.append((event.xdata, event.ydata))

verbose.report("input %i: %f,%f" %
(len(self.clicks),event.xdata, event.ydata))
(len(self.clicks), event.xdata, event.ydata))

# If desired plot up click
if self.show_clicks:
Expand All @@ -230,9 +238,7 @@ def add_click(self,event):
self.marks.append(line)
self.fig.canvas.draw()



def pop_click(self,event,index=-1):
def pop_click(self, event, index=-1):
"""
This removes a click from the list of clicks. Defaults to
removing the last click.
Expand All @@ -249,17 +255,16 @@ def pop_click(self,event,index=-1):
# for the keyboard backspace event on windows XP wxAgg.
# maybe event.inaxes here is a COPY of the actual axes?


def pop(self,event,index=-1):
def pop(self, event, index=-1):
"""
This removes a click and the associated event from the object.
Defaults to removing the last click, but any index can be
supplied.
"""
self.pop_click(event,index)
BlockingInput.pop(self,index)
self.pop_click(event, index)
BlockingInput.pop(self, index)

def cleanup(self,event=None):
def cleanup(self, event=None):
# clean the figure
if self.show_clicks:

Expand All @@ -278,28 +283,29 @@ def __call__(self, n=1, timeout=30, show_clicks=True):
clicks.
"""
self.show_clicks = show_clicks
self.clicks = []
self.marks = []
BlockingInput.__call__(self,n=n,timeout=timeout)
self.clicks = []
self.marks = []
BlockingInput.__call__(self, n=n, timeout=timeout)

return self.clicks

class BlockingContourLabeler( BlockingMouseInput ):

class BlockingContourLabeler(BlockingMouseInput):
"""
Class that creates a callable object that uses mouse clicks or key
clicks on a figure window to place contour labels.
"""
def __init__(self,cs):
def __init__(self, cs):
self.cs = cs
BlockingMouseInput.__init__(self, fig=cs.ax.figure )
BlockingMouseInput.__init__(self, fig=cs.ax.figure)

def add_click(self, event):
self.button1(event)

def pop_click(self, event, index=-1):
self.button3(event)

def button1(self,event):
def button1(self, event):
"""
This will be called if an event involving a button other than
2 or 3 occcurs. This will add a label to a contour.
Expand All @@ -311,10 +317,10 @@ def button1(self,event):
inline_spacing=self.inline_spacing,
transform=False)
self.fig.canvas.draw()
else: # Remove event if not valid
else: # Remove event if not valid
BlockingInput.pop(self)

def button3(self,event):
def button3(self, event):
"""
This will be called if button 3 is clicked. This will remove
a label if not in inline mode. Unfortunately, if one is doing
Expand All @@ -329,26 +335,28 @@ def button3(self,event):
self.cs.pop_label()
self.cs.ax.figure.canvas.draw()

def __call__(self,inline,inline_spacing=5,n=-1,timeout=-1):
self.inline=inline
self.inline_spacing=inline_spacing
def __call__(self, inline, inline_spacing=5, n=-1, timeout=-1):
self.inline = inline
self.inline_spacing = inline_spacing

BlockingMouseInput.__call__(self,n=n,timeout=timeout,
BlockingMouseInput.__call__(self, n=n, timeout=timeout,
show_clicks=False)


class BlockingKeyMouseInput(BlockingInput):
"""
Class that creates a callable object to retrieve a single mouse or
keyboard click
"""
def __init__(self, fig):
BlockingInput.__init__(self, fig=fig, eventslist=('button_press_event','key_press_event') )
BlockingInput.__init__(self, fig=fig, eventslist=(
'button_press_event', 'key_press_event'))

def post_event(self):
"""
Determines if it is a key event
"""
assert len(self.events)>0, "No events yet"
assert len(self.events) > 0, "No events yet"

self.keyormouse = self.events[-1].name == 'key_press_event'

Expand All @@ -358,7 +366,6 @@ def __call__(self, timeout=30):
Returns True if key click, False if mouse, or None if timeout
"""
self.keyormouse = None
BlockingInput.__call__(self,n=1,timeout=timeout)
BlockingInput.__call__(self, n=1, timeout=timeout)

return self.keyormouse

0 comments on commit 28aa647

Please sign in to comment.