itom
|
This class provides the possibility to redirect any signal emitted in an user-defined GUI to different python methods. More...
#include <pythonQtSignalMapper.h>
Public Member Functions | |
PythonQtSignalMapper () | |
constructor | |
~PythonQtSignalMapper () | |
destructor | |
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 | |
bool | removeSignalHandler (QObject *obj, int sigId, PyObject *callable) |
disconnects a certain connection | |
void | removeSignalHandlers () |
disconnects all signal-slot connections managed by this instance of PythonQtSignalMapper | |
virtual int | qt_metacall (QMetaObject::Call c, int id, void **arguments) |
overwrites qt_metacall from PythonQtSignalMapperBase. | |
Public Member Functions inherited from ito::PythonQtSignalMapperBase |
Private Types | |
typedef QMap< int, PythonQtSignalTarget > | TargetMap |
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 |
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 python 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 caught 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.
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.
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.
initSlotCount | should 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. |
ito::PythonQtSignalMapper::~PythonQtSignalMapper | ( | ) |
destructor
Destroys this signal mapper and deletes the managed targets (virtual slots). The connected signals are automatically disconnected by Qt.
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:
[in] | obj | is the instance derived from QObject that is the signaling instance |
[in] | signal | is the signature of the signal (Qt-syntax) |
[in] | sigId | is the Qt-internal ID of the signal (obtained by QMetaObject-system) |
[in] | callable | is a reference to the real python method, that should act as slot. This method can be bounded or unbounded. |
[in] | argTypeList | is 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] | minRepeatInterval | is 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) |
|
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.
[in] | c | provide basic information about the call (only used for passing to the base implementation) |
[in] | id | is the unique slot index of the slot to call. |
[in] | arguments | is 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)
[in] | obj | is the instance derived from QObject that is the signaling instance |
[in] | sigId | is the Qt-internal ID of the signal (obtained by QMetaObject-system) |
[in] | callable | is a reference to the real python method, that should act as slot. This method can be bounded or unbounded. |
void ito::PythonQtSignalMapper::removeSignalHandlers | ( | ) |
disconnects all signal-slot connections managed by this instance of PythonQtSignalMapper
This disconnection is easily done by deleting the list of targets.