Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtSignalReceiver.cpp
3206 views
1
/*
2
*
3
* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4
*
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
9
*
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
14
*
15
* Further, this software is distributed without any warranty that it is
16
* free of the rightful claim of any third person regarding infringement
17
* or the like. Any license provided herein, whether implied or
18
* otherwise, applies only to this software file. Patent licenses, if
19
* any, provided herein do not apply to combinations of this program with
20
* other software, or any other product whatsoever.
21
*
22
* You should have received a copy of the GNU Lesser General Public
23
* License along with this library; if not, write to the Free Software
24
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
*
26
* Contact information: MeVis Research GmbH, Universitaetsallee 29,
27
* 28359 Bremen, Germany or:
28
*
29
* http://www.mevis.de
30
*
31
*/
32
33
//----------------------------------------------------------------------------------
34
/*!
35
// \file PythonQtSignalReceiver.cpp
36
// \author Florian Link
37
// \author Last changed by $Author: florian $
38
// \date 2006-05
39
*/
40
//----------------------------------------------------------------------------------
41
42
#include "PythonQtSignalReceiver.h"
43
#include "PythonQtClassInfo.h"
44
#include "PythonQtMethodInfo.h"
45
#include "PythonQtConversion.h"
46
#include <QMetaObject>
47
#include <QMetaMethod>
48
49
void PythonQtSignalTarget::call(void **arguments) const
50
{
51
const PythonQtMethodInfo* m = methodInfo();
52
// paramterCount includes return value:
53
int count = m->parameterCount();
54
55
PyObject* pargs = NULL;
56
if (count>1) {
57
pargs = PyTuple_New(count-1);
58
}
59
bool err = false;
60
// transform Qt values to Python
61
const QList<PythonQtMethodInfo::ParameterInfo>& params = m->parameters();
62
for (int i = 1; i < count; i++) {
63
const PythonQtMethodInfo::ParameterInfo& param = params.at(i);
64
PyObject* arg = PythonQtConv::ConvertQtValueToPython(param, arguments[i]);
65
if (arg) {
66
// steals reference, no unref
67
PyTuple_SetItem(pargs, i-1,arg);
68
} else {
69
err = true;
70
break;
71
}
72
}
73
74
if (!err) {
75
PyErr_Clear();
76
PyObject* result = PyObject_CallObject(_callable, pargs);
77
if (result) {
78
// ok
79
Py_DECREF(result);
80
} else {
81
PythonQt::self()->handleError();
82
}
83
}
84
if (pargs) {
85
// free the arguments again
86
Py_DECREF(pargs);
87
}
88
}
89
90
//------------------------------------------------------------------------------
91
92
PythonQtSignalReceiver::PythonQtSignalReceiver(QObject* obj):PythonQtSignalReceiverBase(obj)
93
{
94
_obj = obj;
95
_slotCount = staticMetaObject.methodOffset();
96
}
97
98
PythonQtSignalReceiver::~PythonQtSignalReceiver()
99
{
100
}
101
102
103
bool PythonQtSignalReceiver::addSignalHandler(const char* signal, PyObject* callable)
104
{
105
bool flag = false;
106
int sigId = getSignalIndex(signal);
107
if (sigId>=0) {
108
// create PythonQtMethodInfo from signal
109
QMetaMethod meta = _obj->metaObject()->method(sigId);
110
const PythonQtMethodInfo* signalInfo = PythonQtMethodInfo::getCachedMethodInfo(meta);
111
PythonQtSignalTarget t(sigId, signalInfo, _slotCount, callable);
112
_targets.append(t);
113
// now connect to ourselves with the new slot id
114
QMetaObject::connect(_obj, sigId, this, _slotCount, Qt::AutoConnection, 0);
115
116
_slotCount++;
117
flag = true;
118
}
119
return flag;
120
}
121
122
bool PythonQtSignalReceiver::removeSignalHandler(const char* signal, PyObject* callable)
123
{
124
bool found = false;
125
int sigId = getSignalIndex(signal);
126
if (sigId>=0) {
127
QMutableListIterator<PythonQtSignalTarget> i(_targets);
128
while (i.hasNext()) {
129
if (i.next().isSame(sigId, callable)) {
130
i.remove();
131
found = true;
132
break;
133
}
134
}
135
}
136
return found;
137
}
138
139
void PythonQtSignalReceiver::removeSignalHandlers()
140
{
141
_targets.clear();
142
}
143
144
int PythonQtSignalReceiver::getSignalIndex(const char* signal)
145
{
146
int sigId = _obj->metaObject()->indexOfSignal(signal+1);
147
if (sigId<0) {
148
QByteArray tmpSig = QMetaObject::normalizedSignature(signal+1);
149
sigId = _obj->metaObject()->indexOfSignal(tmpSig);
150
}
151
return sigId;
152
}
153
154
int PythonQtSignalReceiver::qt_metacall(QMetaObject::Call c, int id, void **arguments)
155
{
156
// mlabDebugConst("PythonQt", "PythonQtSignalReceiver invoke " << _obj->className() << " " << _obj->name() << " " << id);
157
if (c != QMetaObject::InvokeMetaMethod) {
158
QObject::qt_metacall(c, id, arguments);
159
}
160
161
bool found = false;
162
foreach(const PythonQtSignalTarget& t, _targets) {
163
if (t.slotId() == id) {
164
found = true;
165
t.call(arguments);
166
break;
167
}
168
}
169
return 0;
170
}
171
172
173