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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
class Signal:
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
    """Simple implementation of signal/slot concept.
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
    In signalling class, add attribute with "sig_" prefix:
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
        self.sig_clicked = Signal()
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
    In listening class, add normal method, e.g. "close()" and connect it, e.g:
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
        button.sig_clicked.connect(window.close)
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    When button gets clicked, it should call the signal:
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        self.sig_clicked()
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
    Then window.close() will be called.
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
    """
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    18
    def __init__(self, allow_stop=False):
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self._handlers = []
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    20
        #: Allow one of the handlers to stop processing signal
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    21
        #: The handler should return True value,
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    22
        #: then other handlers will not be called
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    23
        self.allow_stop = allow_stop
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
    def __call__(self, *args, **kwargs):
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        """Emit the signal to all connected handlers."""
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        for handler in self._handlers:
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    28
            res = handler(*args, **kwargs)
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    29
            if self.allow_stop and res:
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    30
                return True
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
    def connect(self, handler):
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        if not handler in self._handlers:
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
            self._handlers.append(handler)
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
    def disconnect(self, handler):
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        self._handlers.remove(handler)