itom  4.1.0
ito::PythonQtSignalMapper Class Reference

This class provides the possibility to redirect any signal emitted in an user-defined GUI to different python methods. More...

#include <pythonQtSignalMapper.h>

Inheritance diagram for ito::PythonQtSignalMapper:
ito::PythonQtSignalMapperBase

Public Member Functions

 PythonQtSignalMapper ()
 constructor More...
 
 ~PythonQtSignalMapper ()
 destructor More...
 
bool addSignalHandler (QObject *obj, const char *signal, int sigId, PyObject *callable, IntList &argTypeList, int minRepeatInterval)
 creates signal-slot connection between the signal of any widget and a python method as slot More...
 
bool removeSignalHandler (QObject *obj, int sigId, PyObject *callable)
 disconnects a certain connection More...
 
void removeSignalHandlers ()
 disconnects all signal-slot connections managed by this instane of PythonQtSignalMapper More...
 
virtual int qt_metacall (QMetaObject::Call c, int id, void **arguments)
 overwrites qt_metacall from PythonQtSignalMapperBase. More...
 

Private Types

typedef QMap< int, PythonQtSignalTargetTargetMap
 list with all virtual slot targets that are the destination for any registered signal-slot-connection
 

Private Attributes

TargetMap m_targets
 index of the last virtual slot managed by this instance (auto-incremented)
 
int m_slotCount
 

Detailed Description

This class provides the possibility to redirect any signal emitted in an user-defined GUI to different python methods.

Every user-defined dialog, main window or widget that is loaded by the python-class ui or that is loaded from a plugin, contains one instance of this class. Any signal from any item of this user interface, that is connected by a python script with any appropriate bounded or unbounded pyhton method, is handled by this instance. This works as follows: The Qt-signal-slot system obtains a virtual slot that fits to the requirements of the signal. Both are connected. Once the slot as a member of this class is called, the call is catched by the overwritten method qt_metacall. If anything is ok, the call is then redirected to the registered python method. Any python method that acts as slot for any signal of the dialog or window, is one instance of the class PythonQtSignalTarget.

Since the mapper usually is created in the python thread, while the signaling widgets are in the main thread, the necessary thread-change is already handled when connection the signal of the widget with the virtual slot of this mapper instance.

See also
PythonQtSignalMapperBase, PythonQtSignalTarget

This class must not define the Q_OBJECT macro, since the method qt_metacall, that is usually generated by the Qt moc-process (run, if Q_OBJECT is defined), is manually created within this class (this is the special thing). The method qt_metacall overwrites the moc-generated method qt_metacall of the baseclass. Since the baseclass has no slots or signals defined, its specific method qt_metacall is 'empty'.

Every python object that wants to define slots on a python level, that can be connected to real signals of Qt classes (like widgets or plugins), should create an object of PythonQtSignalMapper. It is also possible that several python objects share one instance of PythonQtSignalMapper (e.g. all widgets share the PythonQtSignalMapper object of their top-level-widget (the dialog, main window...)).

The PythonQtSignalMapper acts like a 'virtual' slot, can be bind to a signal of a QObject derived class. The slots are not defined in this class definition, but they are added at runtime and stored in the m_targets list. This list knows which python method or function should be called if a specific signal, indicated by its unique signal index, of a specific QObject instance (widget, plugin...) is emitted. The standard Qt signal-slot connection is hereby initialized in the addSignalHandler method. It is removed by removeSignalHandler or removeSignalHandlers.

See also
PythonQtSignalMapperBase

Constructor & Destructor Documentation

ito::PythonQtSignalMapper::PythonQtSignalMapper ( )

constructor

Creates an instance of PythonQtSignalMapper and initializes the slot counter with the given value. Usually this initial slot counter is set to the highest slot number of the graphical user interface this mapper is assigned to.

Parameters
initSlotCountshould be set to the methodOffset() value of the underlying QObject, in order to separate default signals and slots of the base class from new, virtually created slots.
Todo:
: probably, m_slotCount can also be set to methodOffset() of the QObject base class of PythonQtSignalMapper and should not be given as argument. it should not be offset of the object, that emits the signal but of this object, that has the virtual slot!
ito::PythonQtSignalMapper::~PythonQtSignalMapper ( )

destructor

Destroys this signal mapper and deletes the managed targets (virtual slots). The connected signals are automatically disconnected by Qt.

Member Function Documentation

bool ito::PythonQtSignalMapper::addSignalHandler ( QObject *  obj,
const char *  signal,
int  sigId,
PyObject *  callable,
IntList &  argTypeList,
int  minRepeatInterval 
)

creates signal-slot connection between the signal of any widget and a python method as slot

The connection is established as follows:

  1. An instance of PythonQtSignalTarget is created as virtual slot for the corresponding python method
  2. This instance is appended to the target list m_targets.
  3. Using Qt-methods, the widget's signal is connected to the slot of this virtual target (auto-connection).
  4. This virtual slot gets the index of the member m_slotCount, that is incremented afterwards
Parameters
[in]objis the instance derived from QObject that is the signaling instance
[in]signalis the signature of the signal (Qt-syntax)
[in]sigIdis the Qt-internal ID of the signal (obtained by QMetaObject-system)
[in]callableis a reference to the real python method, that should act as slot. This method can be bounded or unbounded.
[in]argTypeListis a list of integer values that describe the Qt-internal type number for all arguments of the signal (type number with respect to QMetaType)
[in]minRepeatIntervalis a minimum amount of time (in ms) which has to be passed until the same signal-slot-connection is accepted again (additional signal emissions are blocked), default: 0 (no timeout)
Returns
true if the connection could be established, else false.
int ito::PythonQtSignalMapper::qt_metacall ( QMetaObject::Call  c,
int  id,
void **  arguments 
)
virtual

overwrites qt_metacall from PythonQtSignalMapperBase.

method invoked by Qt if a connected signal is emitted

This method is overwritten from the method created by the Qt-moc process. It is called whenever a signal, connected to a slot or virtual (python) slot of this instance. At first, the instance of qt_metacall of the derived from QObject is called with the same parameters, in order to allow the usual Qt-communcation. If the slot-id could not be handled by the base implementation, all registered PythonQtSignalTarget instances are searched. If their internal slot-index corresponds to the index given as argument to this function, the PythonQtSignalTarget instance is called (method call) with the given arguments.

Parameters
[in]cprovide basic information about the call (only used for passing to the base implementation)
[in]idis the unique slot index of the slot to call.
[in]argumentsis an array of different argument variables, whose types corresponds to the type-number list, passed when the connection has been registered.
bool ito::PythonQtSignalMapper::removeSignalHandler ( QObject *  obj,
int  sigId,
PyObject *  callable 
)

disconnects a certain connection

Disconnects a certain signal-slot connection, that has previously been connected. This connection is described by the signaling object, the index of the signal (and its signature) and the python callable object (as virtual slot)

Parameters
[in]objis the instance derived from QObject that is the signaling instance
[in]sigIdis the Qt-internal ID of the signal (obtained by QMetaObject-system)
[in]callableis a reference to the real python method, that should act as slot. This method can be bounded or unbounded.
Returns
true if the connection could be disconnected, else false.
void ito::PythonQtSignalMapper::removeSignalHandlers ( )

disconnects all signal-slot connections managed by this instane of PythonQtSignalMapper

This disconnection is easily done by deleting the list of targets.


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