changeset 164:5b04a7d3ded6

typo fix some missing event (de)registrations ability to capture one event multiple times (instead of only having one slot, we have groups and each group has an own slot, the default group is "default" so we are backward compatible
author spq@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 15 Oct 2008 14:57:04 +0000
parents 31718fa356f8
children fbc55c6f57cf
files engine/extensions/pychan/events.py engine/extensions/pychan/widgets.py
diffstat 2 files changed, 28 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/engine/extensions/pychan/events.py	Wed Oct 15 09:13:59 2008 +0000
+++ b/engine/extensions/pychan/events.py	Wed Oct 15 14:57:04 2008 +0000
@@ -42,7 +42,7 @@
 	if "mouse" in name:
 		return MOUSE_EVENT
 	if "key" in name:
-		return MOUSE_EVENT
+		return KEY_EVENT
 	return ACTION_EVENT
 
 
@@ -81,7 +81,8 @@
 		event = self.translateEvent(getEventType(name), event)
 		if name in self.events:
 			if self.debug: print "-"*self.indent, name
-			self.events[name]( event )
+			for f in self.events[name].itervalues():
+				f( event )
 		self.indent -= 4
 
 	def translateEvent(self,event_type,event):
@@ -143,6 +144,7 @@
 		if not self.listener.events:
 			return
 		if self.debug: print "Attach:",self
+		self.widget.real_widget.addKeyListener( self.listener )
 		self.widget.real_widget.addMouseListener( self.listener )
 		self.widget.real_widget.addActionListener( self.listener )
 		self.is_attached = True
@@ -156,23 +158,22 @@
 		if not self.is_attached:
 			return
 		if self.debug: print "Detach:",self
+		self.widget.real_widget.removeKeyListener( self.listener )
 		self.widget.real_widget.removeMouseListener( self.listener )
 		self.widget.real_widget.removeActionListener( self.listener )
 		self.is_attached = False
 
-	def capture(self,event_name,callback):
+	def capture(self,event_name,callback,group_name):
 		if event_name not in EVENTS:
 			raise exceptions.RuntimeError("Unknown eventname: " + event_name)
 
-		if callback is None and not self.isCaptured(event_name):
-			if self.debug:
-				print CALLBACK_NONE_MESSAGE % str(self.widget)
-			return
-
 		if callback is None:
-			del self.listener.events[event_name]
-			if not self.listener.events:
-				self.detach()
+			if self.isCaptured(event_name):
+				del self.listener.events[event_name][group_name]
+				if not self.listener.events:
+					self.detach()
+			elif self.debug:
+					print CALLBACK_NONE_MESSAGE % str(self.widget)
 			return
 
 		if not callable(callback):
@@ -181,11 +182,14 @@
 		def captured_f(event):
 			tools.applyOnlySuitable(callback,event=event,widget=self.widget)
 
-		self.listener.events[event_name] = captured_f
+		if event_name not in self.listener.events:
+			self.listener.events[event_name] = {group_name : captured_f}
+		else:
+			self.listener.events[event_name][group_name] = captured_f
 		self.attach()
 
-	def isCaptured(self,event_name):
-		return event_name in self.listener.events
+	def isCaptured(self,event_name,group_name):
+		return event_name in self.listener.events and group_name in self.listener.events[event_name]
 
 	def getCapturedEvents(self):
 		return self.listener.events.keys()
@@ -194,11 +198,13 @@
 def splitEventDescriptor(name):
 	""" Utility function to split "widgetName/eventName" descriptions into tuples. """
 	L = name.split("/")
+	if len(L) not in (1,2,3):
+		raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name)
 	if len(L) == 1:
 		L = L[0],"action"
-	if len(L) != 2:
-		raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name)
-	if L[1] not in EVENTS:
+	elif L[1] not in EVENTS:
 		raise exceptions.RuntimeError("Unknown event name: " + name)
+	if len(L) == 2:
+		L = L[0],L[1],"default"
 	return L
 
--- a/engine/extensions/pychan/widgets.py	Wed Oct 15 09:13:59 2008 +0000
+++ b/engine/extensions/pychan/widgets.py	Wed Oct 15 14:57:04 2008 +0000
@@ -166,7 +166,7 @@
 				return False
 		return True
 
-	def capture(self, callback, event_name="action"):
+	def capture(self, callback, event_name="action", group_name="default"):
 		"""
 		Add a callback to be executed when the widget event occurs on this widget.
 		
@@ -180,7 +180,7 @@
 		@param callback: Event callback - may accept keyword arguments event and widget.
 		@paran event_name: The event to capture - may be one of L{events.EVENTS} and defaults to "action"
 		"""
-		self.event_mapper.capture( event_name, callback )
+		self.event_mapper.capture( event_name, callback, group_name )
 
 	def isCaptured(self):
 		"""
@@ -340,11 +340,11 @@
 
 		"""
 		for descr,func in eventMap.items():
-			name, event_name = events.splitEventDescriptor(descr)
-			print name, event_name
+			name, event_name, group_name = events.splitEventDescriptor(descr)
+			print name, event_name, group_name
 			widget = self.findChild(name=name)
 			if widget:
-				widget.capture( func, event_name = event_name )
+				widget.capture( func, event_name = event_name, group_name = group_name )
 			elif not ignoreMissing:
 				raise RuntimeError("No widget with the name: %s" % name)