From fac29dc9d913bada4fe1590798958e2b9534ac94 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Tue, 27 Jul 2010 00:13:23 -0400 Subject: [PATCH] added signalproxy class --- SignalProxy.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 SignalProxy.py diff --git a/SignalProxy.py b/SignalProxy.py new file mode 100644 index 00000000..9bc0b098 --- /dev/null +++ b/SignalProxy.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +from PyQt4 import QtCore +from ptime import time + +def proxyConnect(source, signal, slot, delay=0.3): + """Connect a signal to a slot with delay. Returns the SignalProxy + object that was created. Be sure to store this object so it is not + garbage-collected immediately.""" + sp = SignalProxy(source, signal, delay) + sp.connect(sp, signal, slot) + return sp + +class SignalProxy(QtCore.QObject): + """Object which collects rapid-fire signals and condenses them + into a single signal. Used, for example, to prevent a SpinBox + from generating multiple signals when the mouse wheel is rolled + over it.""" + + def __init__(self, source, signal, delay=0.3): + QtCore.QObject.__init__(self) + source.connect(source, signal, self.signal) + self.delay = delay + self.waitUntil = 0 + self.args = None + self.timers = 0 + self.signal = signal + + def setDelay(self, delay): + self.delay = delay + + def flush(self): + """If there is a signal queued up, send it now.""" + if self.args is None: + return False + self.emit(self.signal, *self.args) + self.args = None + return True + + + def signal(self, *args): + """Received signal, queue to be forwarded later.""" + self.waitUntil = time() + self.delay + self.args = args + self.timers += 1 + QtCore.QTimer.singleShot((self.delay*1000)+1, self.tryEmit) + + def tryEmit(self): + """Emit signal if it has been long enougn since receiving the last signal.""" + if self.args is None: + return False + self.timers -= 1 + t = time() + if t >= self.waitUntil: + return self.flush() + else: + if self.timers == 0: + self.timers += 1 + QtCore.QTimer.singleShot((self.waitUntil - t) * 1000, self.tryEmit) + return True + + + \ No newline at end of file