tuikit/core/signal.py
changeset 86 0978fb755d31
child 109 105b1affc3c2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tuikit/core/signal.py	Mon Mar 17 20:40:04 2014 +0100
@@ -0,0 +1,31 @@
+class Signal:
+
+    """Simple implementation of signal/slot concept.
+
+    In signalling class, add attribute with "sig_" prefix:
+        self.sig_clicked = Signal()
+
+    In listening class, add normal method, e.g. "close()" and connect it, e.g:
+        button.sig_clicked.connect(window.close)
+
+    When button gets clicked, it should call the signal:
+        self.sig_clicked()
+
+    Then window.close() will be called.
+
+    """
+
+    def __init__(self):
+        self._handlers = []
+
+    def __call__(self, *args, **kwargs):
+        """Emit the signal to all connected handlers."""
+        for handler in self._handlers:
+            handler(*args, **kwargs)
+
+    def connect(self, handler):
+        if not handler in self._handlers:
+            self._handlers.append(handler)
+
+    def disconnect(self, handler):
+        self._handlers.remove(handler)