PythonQt
PythonQtSignalReceiver.h
Go to the documentation of this file.
1 #ifndef _PYTHONQTSIGNALRECEIVER_H
2 #define _PYTHONQTSIGNALRECEIVER_H
3 
4 /*
5  *
6  * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * Further, this software is distributed without any warranty that it is
19  * free of the rightful claim of any third person regarding infringement
20  * or the like. Any license provided herein, whether implied or
21  * otherwise, applies only to this software file. Patent licenses, if
22  * any, provided herein do not apply to combinations of this program with
23  * other software, or any other product whatsoever.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
30  * 28359 Bremen, Germany or:
31  *
32  * http://www.mevis.de
33  *
34  */
35 
36 //----------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------
44 
45 #include "PythonQtPythonInclude.h"
46 
47 #include "PythonQtSystem.h"
48 #include "PythonQtObjectPtr.h"
49 
50 class PythonQtMethodInfo;
51 class PythonQtClassInfo;
52 
54 
57 public:
59  _signalId = -1;
60  _methodInfo = nullptr;
61  _slotId = -1;
62  }
63 
64  PythonQtSignalTarget(int signalId,const PythonQtMethodInfo* methodInfo, int slotId, PyObject* callable)
65  {
66  _signalId = signalId;
67  _slotId = slotId;
68  _methodInfo = methodInfo;
69  _callable = callable;
70  };
71 
73  };
74 
76  int signalId() const { return _signalId; }
77 
79  int slotId() const { return _slotId; }
80 
82  const PythonQtMethodInfo* methodInfo() const { return _methodInfo; }
83 
85  void call(void **arguments) const;
86 
88  bool isSame(int signalId, PyObject* callable) const;
89 
91  static PyObject* call(PyObject* callable, const PythonQtMethodInfo* methodInfo, void **arguments, bool skipFirstArgumentOfMethodInfo = false);
92 
93 private:
94  int _signalId;
95  int _slotId;
96  const PythonQtMethodInfo* _methodInfo;
97  PythonQtSafeObjectPtr _callable;
98 };
99 
101 
103 class PythonQtSignalReceiverBase : public QObject {
104  Q_OBJECT
105 public:
106  PythonQtSignalReceiverBase(QObject* obj):QObject(obj) {};
107 };
108 
110 
113 
114 public:
115  PythonQtSignalReceiver(QObject* obj);
117 
119  bool addSignalHandler(const char* signal, PyObject* callable);
120 
122  bool removeSignalHandler(const char* signal, PyObject* callable = nullptr);
123 
125  int qt_metacall(QMetaObject::Call c, int id, void **arguments) override;
126 
127 private:
129  int getSignalIndex(const char* signal);
130 
131  QObject* _obj;
132  PythonQtClassInfo* _objClassInfo;
133  int _slotCount;
134  int _destroyedSignalCount;
135  // linear list may get slow on multiple targets, but I think typically we have many objects and just a few signals
136  QList<PythonQtSignalTarget> _targets;
137 
138  static int _destroyedSignal1Id;
139  static int _destroyedSignal2Id;
140 };
141 
142 
143 #endif
144 
struct _object PyObject
#define PYTHONQT_EXPORT
a class that stores all required information about a Qt object (and an optional associated C++ class ...
stores information about a specific signal/slot/method
a smart pointer that stores a PyObject pointer and that handles reference counting automatically
base class for signal receivers
receives all signals for one QObject
bool addSignalHandler(const char *signal, PyObject *callable)
add a signal handler
~PythonQtSignalReceiver() override
int qt_metacall(QMetaObject::Call c, int id, void **arguments) override
we implement this method to simulate a number of slots that match the ids in _targets
bool removeSignalHandler(const char *signal, PyObject *callable=nullptr)
remove a signal handler for given callable (or all callables on that signal if callable is NULL)
PythonQtSignalReceiver(QObject *obj)
stores information about a signal target
bool isSame(int signalId, PyObject *callable) const
check if it is the same signal target
int slotId() const
get the id that was assigned to this simulated slot
static PyObject * call(PyObject *callable, const PythonQtMethodInfo *methodInfo, void **arguments, bool skipFirstArgumentOfMethodInfo=false)
call the given callable with arguments described by PythonQtMethodInfo, returns a new reference as re...
void call(void **arguments) const
call the python callable with the given arguments (as defined in methodInfo)
int signalId() const
get the id of the original signal
const PythonQtMethodInfo * methodInfo() const
get the signals parameter info
PythonQtSignalTarget(int signalId, const PythonQtMethodInfo *methodInfo, int slotId, PyObject *callable)