Hi list,

I have just writen a new widget : a DragingCursor.

If anyone want and to try and use it ...

It is inspired from Cursor in widget module.

Samuel



-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Samuel Garcia
Universite Claude Bernard LYON 1
CNRS - UMR5020, Laboratoire des Neurosciences et Systemes Sensoriels
50, avenue Tony Garnier
69366 LYON Cedex 07
FRANCE
Tél : 04 37 28 74 64
Fax : 04 37 28 76 01
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import pylab

import new

#####################################################################
## DragingCursor
def on_move_canvas(self,event) :
	if event.inaxes is None :return
	for dragingcursor in self.list_dragingcursor :
		if dragingcursor.moving :
			#if event.inaxes != self.ax: return
			
			dragingcursor.linev.set_xdata((event.xdata, event.xdata))
			dragingcursor.lineh.set_ydata((event.ydata, event.ydata))
			dragingcursor.linev.set_visible(dragingcursor.vertOn)
			dragingcursor.lineh.set_visible(dragingcursor.horizOn)
			dragingcursor._update()

def on_button_press_event_canvas(self,event) :
	if event.inaxes is None :return
	ax = event.inaxes
	for dragingcursor in self.list_dragingcursor :
		if dragingcursor.moving :
			return
	for dragingcursor in self.list_dragingcursor :
		if dragingcursor.direction =='hor' :
			delta = (ax.get_ylim()[1]-ax.get_ylim()[0])/25
			coor = event.ydata
		elif dragingcursor.direction =='ver' :
			delta = (ax.get_xlim()[1]-ax.get_xlim()[0])/25
			coor = event.xdata
		if (dragingcursor.coor() > coor-delta) & (dragingcursor.coor() < coor+delta) :
			dragingcursor.moving = True
			return

def on_button_release_event_canvas(self,event) :
	if event.inaxes is None :return
	for dragingcursor in self.list_dragingcursor :
		if dragingcursor.moving :
			dragingcursor.moving = False

class DragingCursor  :
	def __init__(self , ax, direction = 'hor' , coor = 0. , useblit = False, **lineprops ) :
		self.ax = ax
		self.canvas = ax.figure.canvas
		self.direction = direction
		self.moving = False
		
		self.visible = True
		self.horizOn = False
		self.vertOn = False
		if direction =='ver' :
			self.vertOn = True
		if direction =='hor' :
			self.horizOn = True
		self.useblit = useblit
		self.lineh = ax.axhline(y=coor, visible=self.horizOn, **lineprops)
		self.linev = ax.axvline(x=coor, visible=self.vertOn, **lineprops)

		self.background = None
		self.needclear = False

		if 'list_dragingcursor' not in dir(self.canvas) :
			self.canvas.list_dragingcursor = []
			
			self.canvas.on_move = new.instancemethod(on_move_canvas ,
						self.canvas,
						self.canvas.__class__)
			self.canvas.on_button_press_event = new.instancemethod(on_button_press_event_canvas ,
						self.canvas,
						self.canvas.__class__)
			self.canvas.on_button_release_event = new.instancemethod(on_button_release_event_canvas ,
						self.canvas,
						self.canvas.__class__)
			self.canvas.mpl_connect('motion_notify_event', self.canvas.on_move)
			self.canvas.mpl_connect('button_press_event', self.canvas.on_button_press_event)
			self.canvas.mpl_connect('button_release_event', self.canvas.on_button_release_event)
		self.canvas.list_dragingcursor.append(self)
		#self.canvas.draw()
		#self._update()

	
	def coor(self) :
		if self.direction =='hor' :
			return self.lineh.get_ydata()[0]
		elif  self.direction =='ver' :
			return self.linev.get_xdata()[0]
		else :
			return None
	
	def _update(self):
		if self.useblit:
			if self.background is not None:
				self.canvas.restore_region(self.background)
			self.ax.draw_artist(self.linev)
			self.ax.draw_artist(self.lineh)
			self.canvas.blit(self.ax.bbox)
		else:
			self.canvas.draw_idle()
		return False

	def delete(self):
		if self.useblit:
			self.background = self.canvas.copy_from_bbox(self.ax.bbox)
		self.linev.set_visible(False)
		self.lineh.set_visible(False)
		self.canvas.list_dragingcursor.remove(self)
		
		



#####################################################################
## Example

fig = pylab.figure(figsize=(8,6))
ax = fig.add_axes([0.075, 0.25, 0.9, 0.725], axisbg='#FFFFCC')

x,y = 4*(pylab.rand(2,100)-.5)
ax.plot(x,y,'o')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)

# set useblit = True on gtkagg for enhanced performance    
#cursor = Cursor(ax, useblit=True, color='red', linewidth=2 )
a = DragingCursor(ax , coor = -0.5,direction = 'hor' , useblit=False, color='red', linewidth=2 )
b = DragingCursor(ax , coor = 0.5,direction = 'hor' , useblit=False, color='red', linewidth=2 )
c = DragingCursor(ax , coor = -0.5,direction = 'ver' , useblit=False, color='red', linewidth=2 )
d = DragingCursor(ax , coor = 0.5,direction = 'ver' , useblit=False, color='red', linewidth=2 )

pylab.show()
print a.coor(),b.coor(),c.coor(),d.coor()
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to