tuikit/core/signal.py
author Radek Brich <radek.brich@devl.cz>
Sun, 22 Feb 2015 09:53:13 +0100
changeset 119 dd91747504dd
parent 109 105b1affc3c2
permissions -rw-r--r--
Redraw widgets on request. Add scrollbar demo.

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)