tuikit/core/signal.py
author Radek Brich <radek.brich@devl.cz>
Wed, 03 Sep 2014 19:08:21 +0200
changeset 109 105b1affc3c2
parent 86 0978fb755d31
permissions -rw-r--r--
Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.

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, allow_stop=False):
        self._handlers = []
        #: Allow one of the handlers to stop processing signal
        #: The handler should return True value,
        #: then other handlers will not be called
        self.allow_stop = allow_stop

    def __call__(self, *args, **kwargs):
        """Emit the signal to all connected handlers."""
        for handler in self._handlers:
            res = handler(*args, **kwargs)
            if self.allow_stop and res:
                return True

    def connect(self, handler):
        if not handler in self._handlers:
            self._handlers.append(handler)

    def disconnect(self, handler):
        self._handlers.remove(handler)