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