Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. """Provides signalTap, a generic class to transform a pyqtSignal.
  2.  
  3. Usage example: Intercept a spinbox's valueChanged signal.
  4. ```python
  5. def __init__(self):
  6. valueChangedTap = signalTap(lambda val:
  7. (val * self.unitValue[self.siUnit],) )
  8. self.valueChanged.connect(valueChangedTap.emit)
  9. self.valueChanged = valueChangedTap
  10. ```
  11. """
  12.  
  13. class signalTap():
  14. """Generic class to transform a pyqtSignal."""
  15.  
  16. def __init__(self, transformer):
  17. """Create a new signal with a transformer function to be
  18. called on the real signal value before propagation."""
  19. self.callbacks = []
  20. self.transformer = transformer
  21.  
  22. def connect(self, fn):
  23. """Invoke the function `fn` when the signal is emitted."""
  24. self.callbacks.append(fn)
  25.  
  26. def disconnect(self, fn):
  27. """Do not call `fn` when the signal is emitted any more."""
  28. self.callbacks.remove(fn)
  29.  
  30. def emit(self, *args):
  31. """Emit the transformed signal, based on the
  32. untransformed input values. (Invokes transformer.)"""
  33. for callback in self.callbacks:
  34. callback(*self.transformer(*args))
  35.  
  36. def emitVerbatim(self, *args):
  37. """Emit the transformed signal, based on the
  38. pre-transformed input values. (Ignores transformer.)"""
  39. for callback in self.callbacks:
  40. callback(*args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement