Uranium
Application Framework
Loading...
Searching...
No Matches
UM.Signal.Signal Class Reference

Simple implementation of signals and slots. More...

Public Member Functions

None __init__ (self, int type=Auto)
 Initialize the instance.
 
 getName (self)
 
None __call__ (self)
 
int getType (self)
 Get type of the signal.
 
None emit (self, *Any args, **Any kwargs)
 Emit the signal which indirectly calls all of the connected slots.
 
None connect (self, Union['Signal', Callable[[], None]] connector)
 Connect to this signal.
 
 disconnect (self, connector)
 Disconnect from this signal.
 
 disconnectAll (self)
 Disconnect all connected slots.
 
 __getstate__ (self)
 To support Pickle.
 
 __deepcopy__ (self, memo)
 To properly handle deepcopy in combination with getstate
 

Static Public Attributes

int Direct = 1
 Signal types.
 
int Auto = 2
 
int Queued = 3
 

Protected Attributes

 _postpone_emit
 
 _postpone_thread
 
 _compress_postpone
 
 _postponed_emits
 

Static Protected Attributes

 _app = None
 private:
 
 _signalQueue = None
 

Detailed Description

Simple implementation of signals and slots.

Signals and slots can be used as a light weight event system. A class can define signals that other classes can connect functions or methods to, called slots. Whenever the signal is called, it will proceed to call the connected slots.

To create a signal, create an instance variable of type Signal. Other objects can then use that variable's connect() method to connect methods, callable objects or signals to the signal. To emit the signal, call emit() on the signal. Arguments can be passed along to the signal, but slots will be required to handle them. When connecting signals to other signals, the connected signal will be emitted whenever the signal is emitted.

Signal-slot connections are weak references and as such will not prevent objects from being destroyed. In addition, all slots will be implicitly disconnected when the signal is destroyed.

Warning
It is imperative that the signals are created as instance variables, otherwise emitting signals will get confused. To help with this, see the SignalEmitter class.

Loosely based on http://code.activestate.com/recipes/577980-improved-signalsslots-implementation-in-python/ #pylint: disable=wrong-spelling-in-comment

See also
SignalEmitter

Constructor & Destructor Documentation

◆ __init__()

None UM.Signal.Signal.__init__ (   self,
int   type = Auto 
)

Initialize the instance.

Parameters
kwargsKeyword arguments. Possible keywords:
  • type: The signal type. Defaults to Auto.

Member Function Documentation

◆ __call__()

None UM.Signal.Signal.__call__ (   self)
Exceptions
NotImplementedError

◆ __deepcopy__()

UM.Signal.Signal.__deepcopy__ (   self,
  memo 
)

To properly handle deepcopy in combination with getstate

Apparently deepcopy uses getstate internally, which is not documented. The reimplementation of getstate then breaks deepcopy. On the other hand, if we do not reimplement it like that, we break pickle. So instead make sure to also reimplement deepcopy.

◆ __getstate__()

UM.Signal.Signal.__getstate__ (   self)

To support Pickle.

Since Weak containers cannot be serialized by Pickle we just return an empty dict as state.

◆ connect()

None UM.Signal.Signal.connect (   self,
Union['Signal', Callable[[],None]]  connector 
)

Connect to this signal.

Parameters
connectorThe signal or slot (function) to connect.

◆ disconnect()

UM.Signal.Signal.disconnect (   self,
  connector 
)

Disconnect from this signal.

Parameters
connectorThe signal or slot (function) to disconnect.

◆ emit()

None UM.Signal.Signal.emit (   self,
*Any  args,
**Any  kwargs 
)

Emit the signal which indirectly calls all of the connected slots.

Parameters
argsThe positional arguments to pass along.
kwargsThe keyword arguments to pass along.
Note
If the Signal type is Queued and this is not called from the application thread the call will be posted as an event to the application main thread, which means the function will be called on the next application event loop tick.

◆ getType()

int UM.Signal.Signal.getType (   self)

Get type of the signal.

Returns
int Direct(1), Auto(2) or Queued(3)

Member Data Documentation

◆ Direct

int UM.Signal.Signal.Direct = 1
static

Signal types.

These indicate the type of a signal, that is, how the signal handles calling the connected slots.

  • Direct connections immediately call the connected slots from the thread that called emit().
  • Auto connections will push the call onto the event loop if the current thread is not the main thread, but make a direct call if it is.
  • Queued connections will always push the call on to the event loop.

The documentation for this class was generated from the following file: