合并1.6.1版本的 EventEngine
This commit is contained in:
parent
dae0bdfe67
commit
f0b18e08ec
@ -4,6 +4,7 @@
|
|||||||
from Queue import Queue, Empty
|
from Queue import Queue, Empty
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
# 第三方模块
|
# 第三方模块
|
||||||
from PyQt4.QtCore import QTimer
|
from PyQt4.QtCore import QTimer
|
||||||
@ -68,7 +69,10 @@ class EventEngine(object):
|
|||||||
|
|
||||||
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
||||||
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
||||||
self.__handlers = {}
|
self.__handlers = defaultdict(list)
|
||||||
|
|
||||||
|
# __generalHandlers是一个列表,用来保存通用回调函数(所有事件均调用)
|
||||||
|
self.__generalHandlers = []
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def __run(self):
|
def __run(self):
|
||||||
@ -92,6 +96,10 @@ class EventEngine(object):
|
|||||||
#for handler in self.__handlers[event.type_]:
|
#for handler in self.__handlers[event.type_]:
|
||||||
#handler(event)
|
#handler(event)
|
||||||
|
|
||||||
|
# 调用通用处理函数进行处理
|
||||||
|
if self.__generalHandlers:
|
||||||
|
[handler(event) for handler in self.__generalHandlers]
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def __onTimer(self):
|
def __onTimer(self):
|
||||||
"""向事件队列中存入计时器事件"""
|
"""向事件队列中存入计时器事件"""
|
||||||
@ -102,8 +110,11 @@ class EventEngine(object):
|
|||||||
self.put(event)
|
self.put(event)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def start(self):
|
def start(self, timer=True):
|
||||||
"""引擎启动"""
|
"""
|
||||||
|
引擎启动
|
||||||
|
timer:是否要启动计时器
|
||||||
|
"""
|
||||||
# 将引擎设为启动
|
# 将引擎设为启动
|
||||||
self.__active = True
|
self.__active = True
|
||||||
|
|
||||||
@ -111,7 +122,8 @@ class EventEngine(object):
|
|||||||
self.__thread.start()
|
self.__thread.start()
|
||||||
|
|
||||||
# 启动计时器,计时器事件间隔默认设定为1秒
|
# 启动计时器,计时器事件间隔默认设定为1秒
|
||||||
self.__timer.start(1000)
|
if timer:
|
||||||
|
self.__timer.start(1000)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def stop(self):
|
def stop(self):
|
||||||
@ -128,12 +140,8 @@ class EventEngine(object):
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def register(self, type_, handler):
|
def register(self, type_, handler):
|
||||||
"""注册事件处理函数监听"""
|
"""注册事件处理函数监听"""
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无则创建
|
# 尝试获取该事件类型对应的处理函数列表,若无defaultDict会自动创建新的list
|
||||||
try:
|
handlerList = self.__handlers[type_]
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
except KeyError:
|
|
||||||
handlerList = []
|
|
||||||
self.__handlers[type_] = handlerList
|
|
||||||
|
|
||||||
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
||||||
if handler not in handlerList:
|
if handler not in handlerList:
|
||||||
@ -143,24 +151,34 @@ class EventEngine(object):
|
|||||||
def unregister(self, type_, handler):
|
def unregister(self, type_, handler):
|
||||||
"""注销事件处理函数监听"""
|
"""注销事件处理函数监听"""
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
||||||
try:
|
handlerList = self.__handlers[type_]
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
|
|
||||||
# 如果该函数存在于列表中,则移除
|
|
||||||
if handler in handlerList:
|
|
||||||
handlerList.remove(handler)
|
|
||||||
|
|
||||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
# 如果该函数存在于列表中,则移除
|
||||||
if not handlerList:
|
if handler in handlerList:
|
||||||
del self.__handlers[type_]
|
handlerList.remove(handler)
|
||||||
except KeyError:
|
|
||||||
pass
|
# 如果函数列表为空,则从引擎中移除该事件类型
|
||||||
|
if not handlerList:
|
||||||
|
del self.__handlers[type_]
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def put(self, event):
|
def put(self, event):
|
||||||
"""向事件队列中存入事件"""
|
"""向事件队列中存入事件"""
|
||||||
self.__queue.put(event)
|
self.__queue.put(event)
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
def registerGeneralHandler(self, handler):
|
||||||
|
"""注册通用事件处理函数监听"""
|
||||||
|
if handler not in self.__generalHandlers:
|
||||||
|
self.__generalHandlers.append(handler)
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
def unregisterGeneralHandler(self, handler):
|
||||||
|
"""注销通用事件处理函数监听"""
|
||||||
|
if handler in self.__generalHandlers:
|
||||||
|
self.__generalHandlers.remove(handler)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
class EventEngine2(object):
|
class EventEngine2(object):
|
||||||
@ -187,7 +205,10 @@ class EventEngine2(object):
|
|||||||
|
|
||||||
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
||||||
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
||||||
self.__handlers = {}
|
self.__handlers = defaultdict(list)
|
||||||
|
|
||||||
|
# __generalHandlers是一个列表,用来保存通用回调函数(所有事件均调用)
|
||||||
|
self.__generalHandlers = []
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def __run(self):
|
def __run(self):
|
||||||
@ -211,6 +232,10 @@ class EventEngine2(object):
|
|||||||
#for handler in self.__handlers[event.type_]:
|
#for handler in self.__handlers[event.type_]:
|
||||||
#handler(event)
|
#handler(event)
|
||||||
|
|
||||||
|
# 调用通用处理函数进行处理
|
||||||
|
if self.__generalHandlers:
|
||||||
|
[handler(event) for handler in self.__generalHandlers]
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def __runTimer(self):
|
def __runTimer(self):
|
||||||
"""运行在计时器线程中的循环函数"""
|
"""运行在计时器线程中的循环函数"""
|
||||||
@ -225,8 +250,11 @@ class EventEngine2(object):
|
|||||||
sleep(self.__timerSleep)
|
sleep(self.__timerSleep)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def start(self):
|
def start(self, timer=True):
|
||||||
"""引擎启动"""
|
"""
|
||||||
|
引擎启动
|
||||||
|
timer:是否要启动计时器
|
||||||
|
"""
|
||||||
# 将引擎设为启动
|
# 将引擎设为启动
|
||||||
self.__active = True
|
self.__active = True
|
||||||
|
|
||||||
@ -234,8 +262,9 @@ class EventEngine2(object):
|
|||||||
self.__thread.start()
|
self.__thread.start()
|
||||||
|
|
||||||
# 启动计时器,计时器事件间隔默认设定为1秒
|
# 启动计时器,计时器事件间隔默认设定为1秒
|
||||||
self.__timerActive = True
|
if timer:
|
||||||
self.__timer.start()
|
self.__timerActive = True
|
||||||
|
self.__timer.start()
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def stop(self):
|
def stop(self):
|
||||||
@ -253,12 +282,8 @@ class EventEngine2(object):
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def register(self, type_, handler):
|
def register(self, type_, handler):
|
||||||
"""注册事件处理函数监听"""
|
"""注册事件处理函数监听"""
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无则创建
|
# 尝试获取该事件类型对应的处理函数列表,若无defaultDict会自动创建新的list
|
||||||
try:
|
handlerList = self.__handlers[type_]
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
except KeyError:
|
|
||||||
handlerList = []
|
|
||||||
self.__handlers[type_] = handlerList
|
|
||||||
|
|
||||||
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
||||||
if handler not in handlerList:
|
if handler not in handlerList:
|
||||||
@ -268,24 +293,33 @@ class EventEngine2(object):
|
|||||||
def unregister(self, type_, handler):
|
def unregister(self, type_, handler):
|
||||||
"""注销事件处理函数监听"""
|
"""注销事件处理函数监听"""
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
||||||
try:
|
handlerList = self.__handlers[type_]
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
|
# 如果该函数存在于列表中,则移除
|
||||||
# 如果该函数存在于列表中,则移除
|
if handler in handlerList:
|
||||||
if handler in handlerList:
|
handlerList.remove(handler)
|
||||||
handlerList.remove(handler)
|
|
||||||
|
# 如果函数列表为空,则从引擎中移除该事件类型
|
||||||
|
if not handlerList:
|
||||||
|
del self.__handlers[type_]
|
||||||
|
|
||||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
|
||||||
if not handlerList:
|
|
||||||
del self.__handlers[type_]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def put(self, event):
|
def put(self, event):
|
||||||
"""向事件队列中存入事件"""
|
"""向事件队列中存入事件"""
|
||||||
self.__queue.put(event)
|
self.__queue.put(event)
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
def registerGeneralHandler(self, handler):
|
||||||
|
"""注册通用事件处理函数监听"""
|
||||||
|
if handler not in self.__generalHandlers:
|
||||||
|
self.__generalHandlers.append(handler)
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
def unregisterGeneralHandler(self, handler):
|
||||||
|
"""注销通用事件处理函数监听"""
|
||||||
|
if handler in self.__generalHandlers:
|
||||||
|
self.__generalHandlers.remove(handler)
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
class Event:
|
class Event:
|
||||||
@ -311,7 +345,8 @@ def test():
|
|||||||
app = QCoreApplication(sys.argv)
|
app = QCoreApplication(sys.argv)
|
||||||
|
|
||||||
ee = EventEngine2()
|
ee = EventEngine2()
|
||||||
ee.register(EVENT_TIMER, simpletest)
|
#ee.register(EVENT_TIMER, simpletest)
|
||||||
|
ee.registerGeneralHandler(simpletest)
|
||||||
ee.start()
|
ee.start()
|
||||||
|
|
||||||
app.exec_()
|
app.exec_()
|
||||||
|
Loading…
Reference in New Issue
Block a user