2010-07-27 04:13:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-03-02 02:55:32 +00:00
|
|
|
from Qt import QtCore
|
2010-07-27 04:13:23 +00:00
|
|
|
from ptime import time
|
2012-03-17 15:48:21 +00:00
|
|
|
import ThreadsafeTimer
|
2010-07-27 04:13:23 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
__all__ = ['SignalProxy']
|
|
|
|
|
2010-07-27 04:13:23 +00:00
|
|
|
class SignalProxy(QtCore.QObject):
|
|
|
|
"""Object which collects rapid-fire signals and condenses them
|
2012-04-03 05:01:33 +00:00
|
|
|
into a single signal or a rate-limited stream of signals.
|
|
|
|
Used, for example, to prevent a SpinBox from generating multiple
|
|
|
|
signals when the mouse wheel is rolled over it.
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
Emits sigDelayed after input signals have stopped for a certain period of time.
|
|
|
|
"""
|
|
|
|
|
|
|
|
sigDelayed = QtCore.Signal(object)
|
2010-07-27 04:13:23 +00:00
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
def __init__(self, signal, delay=0.3, rateLimit=0, slot=None):
|
2011-04-05 14:35:50 +00:00
|
|
|
"""Initialization arguments:
|
2012-03-02 02:55:32 +00:00
|
|
|
signal - a bound Signal or pyqtSignal instance
|
|
|
|
delay - Time (in seconds) to wait for signals to stop before emitting (default 0.3s)
|
|
|
|
slot - Optional function to connect sigDelayed to.
|
2012-04-03 05:01:33 +00:00
|
|
|
rateLimit - (signals/sec) if greater than 0, this allows signals to stream out at a
|
|
|
|
steady rate while they are being received.
|
2012-03-02 02:55:32 +00:00
|
|
|
"""
|
2011-04-05 14:35:50 +00:00
|
|
|
|
2010-07-27 04:13:23 +00:00
|
|
|
QtCore.QObject.__init__(self)
|
2012-03-02 02:55:32 +00:00
|
|
|
signal.connect(self.signalReceived)
|
|
|
|
self.signal = signal
|
2010-07-27 04:13:23 +00:00
|
|
|
self.delay = delay
|
2012-04-03 05:01:33 +00:00
|
|
|
self.rateLimit = rateLimit
|
2010-07-27 04:13:23 +00:00
|
|
|
self.args = None
|
2012-03-17 15:48:21 +00:00
|
|
|
self.timer = ThreadsafeTimer.ThreadsafeTimer()
|
2011-04-05 14:35:50 +00:00
|
|
|
self.timer.timeout.connect(self.flush)
|
2010-11-22 03:50:04 +00:00
|
|
|
self.block = False
|
2012-03-02 02:55:32 +00:00
|
|
|
self.slot = slot
|
2012-04-03 05:01:33 +00:00
|
|
|
self.lastFlushTime = None
|
2012-03-02 02:55:32 +00:00
|
|
|
if slot is not None:
|
|
|
|
self.sigDelayed.connect(slot)
|
2010-07-27 04:13:23 +00:00
|
|
|
|
|
|
|
def setDelay(self, delay):
|
|
|
|
self.delay = delay
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
def signalReceived(self, *args):
|
|
|
|
"""Received signal. Cancel previous timer and store args to be forwarded later."""
|
2010-11-22 03:50:04 +00:00
|
|
|
if self.block:
|
|
|
|
return
|
2010-07-27 04:13:23 +00:00
|
|
|
self.args = args
|
2012-04-03 05:01:33 +00:00
|
|
|
if self.rateLimit == 0:
|
|
|
|
self.timer.stop()
|
|
|
|
self.timer.start((self.delay*1000)+1)
|
|
|
|
else:
|
|
|
|
now = time()
|
|
|
|
if self.lastFlushTime is None:
|
|
|
|
leakTime = 0
|
|
|
|
else:
|
|
|
|
lastFlush = self.lastFlushTime
|
|
|
|
leakTime = max(0, (lastFlush + (1.0 / self.rateLimit)) - now)
|
|
|
|
|
|
|
|
self.timer.stop()
|
|
|
|
self.timer.start((min(leakTime, self.delay)*1000)+1)
|
|
|
|
|
2010-07-27 04:13:23 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
def flush(self):
|
|
|
|
"""If there is a signal queued up, send it now."""
|
2010-11-22 03:50:04 +00:00
|
|
|
if self.args is None or self.block:
|
2010-07-27 04:13:23 +00:00
|
|
|
return False
|
2012-03-02 02:55:32 +00:00
|
|
|
#self.emit(self.signal, *self.args)
|
|
|
|
self.sigDelayed.emit(self.args)
|
2011-04-05 14:35:50 +00:00
|
|
|
self.args = None
|
2012-03-02 02:55:32 +00:00
|
|
|
self.timer.stop()
|
2012-04-03 05:01:33 +00:00
|
|
|
self.lastFlushTime = time()
|
2010-07-27 04:13:23 +00:00
|
|
|
return True
|
|
|
|
|
2010-11-22 03:50:04 +00:00
|
|
|
def disconnect(self):
|
|
|
|
self.block = True
|
2012-03-02 02:55:32 +00:00
|
|
|
try:
|
|
|
|
self.signal.disconnect(self.signalReceived)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.sigDelayed.disconnect(self.slot)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
#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)
|
|
|
|
#if source is None:
|
|
|
|
#sp.connect(sp, QtCore.SIGNAL('signal'), slot)
|
|
|
|
#else:
|
|
|
|
#sp.connect(sp, signal, slot)
|
|
|
|
#return sp
|
2011-04-05 14:35:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2012-03-02 02:55:32 +00:00
|
|
|
from Qt import QtGui
|
2011-04-05 14:35:50 +00:00
|
|
|
app = QtGui.QApplication([])
|
|
|
|
win = QtGui.QMainWindow()
|
|
|
|
spin = QtGui.QSpinBox()
|
|
|
|
win.setCentralWidget(spin)
|
|
|
|
win.show()
|
|
|
|
|
|
|
|
def fn(*args):
|
2012-03-02 02:55:32 +00:00
|
|
|
print "Raw signal:", args
|
|
|
|
def fn2(*args):
|
|
|
|
print "Delayed signal:", args
|
2011-04-05 14:35:50 +00:00
|
|
|
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
spin.valueChanged.connect(fn)
|
|
|
|
#proxy = proxyConnect(spin, QtCore.SIGNAL('valueChanged(int)'), fn)
|
|
|
|
proxy = SignalProxy(spin.valueChanged, delay=0.5, slot=fn2)
|
2011-04-05 14:35:50 +00:00
|
|
|
|