itom  4.1.0
pythonQtSignalMapper.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef PYTHONQTSIGNALMAPPER_H
24 #define PYTHONQTSIGNALMAPPER_H
25 
26 #ifndef Q_MOC_RUN
27  #define PY_ARRAY_UNIQUE_SYMBOL itom_ARRAY_API
28 
29  //python
30  // see http://vtk.org/gitweb?p=VTK.git;a=commitdiff;h=7f3f750596a105d48ea84ebfe1b1c4ca03e0bab3
31  #if (defined _DEBUG) && (defined WIN32)
32  #undef _DEBUG
33  #include "python/pythonWrapper.h"
34  #define _DEBUG
35  #else
36  #include "python/pythonWrapper.h"
37  #endif
38 #endif
39 
40 #include "../global.h"
41 
42 #include <qvariant.h>
43 #include <qobject.h>
44 #include <qelapsedtimer.h>
45 #include <qhash.h>
46 
47 namespace ito
48 {
49 
51 {
52 public:
53 
56 
58  PythonQtSignalTarget(IntList &argTypeList, int slotId, int signalId, PyObject* callable, const char *signal, int minRepeatInterval);
59 
62 
65 
68 
70  inline int signalId() const { return m_signalId; }
71 
73  inline int slotId() const { return m_slotId; }
74 
75  // call the python callable with the given arguments (docs see source file)
76  void call(void ** arguments);
77 
79  inline IntList argTypeList() const { return m_argTypeList; };
80 
82  bool isSame(int signalId, PyObject* callable) const;
83 
84 private:
85  int m_slotId;
86  int m_signalId;
87  IntList m_argTypeList;
88 
90  {
92 
95 
98 
100  Callable_CFunction
101  };
102 
103  /* If the target is a bounded method, m_boundedMethod is true and
104  this member holds a Python weak reference to the method, that acts as slot.
105  m_boundedInstance is != NULL then.
106 
107  If the target is an unbounded function, m_boundedMethod is false and
108  this member holds a new reference to the function itself (that acts as slot).
109  m_boundedInstance is NULL then. */
110  PyObject *m_function;
111  PyObject *m_boundedInstance;
113  QString m_signalName;
114  QElapsedTimer m_elapsedTimer;
115 
116  /*
117  if > 0, every call of a certain slot by a certain signal with restart m_elapsedTimer. If the same signal-slot-connection is
118  called another time, while less than m_minRepeatInterval ms have expired, this new invoke will be ignored.
119 
120  This can be used to prevent that very fast signal emissions will overflow the call queue for the connected callable python method.
121  */
122  int m_minRepeatInterval;
123 };
124 
141 class PythonQtSignalMapperBase : public QObject
142 {
143  Q_OBJECT
144 public:
146 };
147 
170 {
171 
172 public:
175 
176  bool addSignalHandler(QObject *obj, const char* signal, int sigId, PyObject* callable, IntList &argTypeList, int minRepeatInterval);
177  bool removeSignalHandler(QObject *obj, int sigId, PyObject* callable);
178  void removeSignalHandlers();
179 
181  virtual int qt_metacall(QMetaObject::Call c, int id, void **arguments);
182 
183 private:
184  typedef QMap<int, PythonQtSignalTarget> TargetMap;
185 
187  /* This list is generated as map, that maps the slotId of the PythonQtSignalTarget
188  to the target itself (for a faster indexing).
189  */
190  TargetMap m_targets;
191 
193  int m_slotCount;
194 
195 };
196 
197 } //end namespace ito
198 
199 #endif
TargetMap m_targets
index of the last virtual slot managed by this instance (auto-incremented)
Definition: pythonQtSignalMapper.h:190
IntList m_argTypeList
type id's from QMetaType::type("..."), describing the arguments of the function-call ...
Definition: pythonQtSignalMapper.h:87
int signalId() const
gets the id of the original signal
Definition: pythonQtSignalMapper.h:70
Definition: pythonQtSignalMapper.h:50
unbounded python method, the function is stored in m_function, m_boundedInstance is NULL ...
Definition: pythonQtSignalMapper.h:94
QMap< int, PythonQtSignalTarget > TargetMap
list with all virtual slot targets that are the destination for any registered signal-slot-connection...
Definition: pythonQtSignalMapper.h:184
void removeSignalHandlers()
disconnects all signal-slot connections managed by this instane of PythonQtSignalMapper ...
Definition: pythonQtSignalMapper.cpp:178
virtual int qt_metacall(QMetaObject::Call c, int id, void **arguments)
overwrites qt_metacall from PythonQtSignalMapperBase.
Definition: pythonQtSignalMapper.cpp:199
CallableType m_callableType
type of the python callable (see CallableType)
Definition: pythonQtSignalMapper.h:112
bool isSame(int signalId, PyObject *callable) const
Compares this signal target with given values.
Definition: pythonQtSignalMapper.cpp:346
Definition: apiFunctionsGraph.cpp:39
void call(void **arguments)
invokes the python method or function
Definition: pythonQtSignalMapper.cpp:382
PythonQtSignalMapper()
constructor
Definition: pythonQtSignalMapper.cpp:69
~PythonQtSignalTarget()
destructor
Definition: pythonQtSignalMapper.cpp:330
CallableType
Definition: pythonQtSignalMapper.h:89
IntList argTypeList() const
returns list of type-numbers of arguments
Definition: pythonQtSignalMapper.h:79
int slotId() const
gets the id that was assigned to this simulated slot
Definition: pythonQtSignalMapper.h:73
int m_slotId
index of this slot
Definition: pythonQtSignalMapper.h:85
PythonQtSignalTarget()
empty constructor
Definition: pythonQtSignalMapper.cpp:219
int m_signalId
index of the connected signal
Definition: pythonQtSignalMapper.h:86
PythonQtSignalTarget & operator=(const PythonQtSignalTarget &rhs)
assignment operator
Definition: pythonQtSignalMapper.cpp:306
bool removeSignalHandler(QObject *obj, int sigId, PyObject *callable)
disconnects a certain connection
Definition: pythonQtSignalMapper.cpp:146
~PythonQtSignalMapper()
destructor
Definition: pythonQtSignalMapper.cpp:80
the callable is invalid
Definition: pythonQtSignalMapper.h:91
PyObject * m_boundedInstance
weak reference to the python-class instance of the function (if the function is bounded) or NULL if t...
Definition: pythonQtSignalMapper.h:111
QElapsedTimer m_elapsedTimer
see m_minRepeatInterval
Definition: pythonQtSignalMapper.h:114
QString m_signalName
signature of the signal (mainly used for debugging reasons)
Definition: pythonQtSignalMapper.h:113
base class for PythonQtSignalMapper
Definition: pythonQtSignalMapper.h:141
function, written in C, stored in m_function. m_boundedInstance is NULL, since the potential self obj...
Definition: pythonQtSignalMapper.h:97
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 ...
Definition: pythonQtSignalMapper.cpp:107
This class provides the possibility to redirect any signal emitted in an user-defined GUI to differen...
Definition: pythonQtSignalMapper.h:169