##// END OF EJS Templates
added error printing for connect/disconnect...
florianlink -
r158:04c639a56aef
parent child
Show More
@@ -1,305 +1,315
1 /*
1 /*
2 *
2 *
3 * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
3 * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
4 *
4 *
5 * This library is free software; you can redistribute it and/or
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
8 * version 2.1 of the License, or (at your option) any later version.
9 *
9 *
10 * This library is distributed in the hope that it will be useful,
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
13 * Lesser General Public License for more details.
14 *
14 *
15 * Further, this software is distributed without any warranty that it is
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
20 * other software, or any other product whatsoever.
21 *
21 *
22 * You should have received a copy of the GNU Lesser General Public
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
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
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
25 *
26 * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
26 * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
27 * 28359 Bremen, Germany or:
27 * 28359 Bremen, Germany or:
28 *
28 *
29 * http://www.mevis.de
29 * http://www.mevis.de
30 *
30 *
31 */
31 */
32
32
33 //----------------------------------------------------------------------------------
33 //----------------------------------------------------------------------------------
34 /*!
34 /*!
35 // \file PythonQtStdDecorators.cpp
35 // \file PythonQtStdDecorators.cpp
36 // \author Florian Link
36 // \author Florian Link
37 // \author Last changed by $Author: florian $
37 // \author Last changed by $Author: florian $
38 // \date 2007-04
38 // \date 2007-04
39 */
39 */
40 //----------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------
41
41
42 #include "PythonQtStdDecorators.h"
42 #include "PythonQtStdDecorators.h"
43 #include "PythonQt.h"
43 #include "PythonQt.h"
44 #include "PythonQtClassInfo.h"
44 #include "PythonQtClassInfo.h"
45 #include <QCoreApplication>
45 #include <QCoreApplication>
46
46
47 bool PythonQtStdDecorators::connect(QObject* sender, const QByteArray& signal, PyObject* callable)
47 bool PythonQtStdDecorators::connect(QObject* sender, const QByteArray& signal, PyObject* callable)
48 {
48 {
49 bool result = false;
49 QByteArray signalTmp;
50 QByteArray signalTmp;
50 char first = signal.at(0);
51 char first = signal.at(0);
51 if (first>='0' && first<='9') {
52 if (first>='0' && first<='9') {
52 signalTmp = signal;
53 signalTmp = signal;
53 } else {
54 } else {
54 signalTmp = "2" + signal;
55 signalTmp = "2" + signal;
55 }
56 }
56
57
57 if (sender) {
58 if (sender) {
58 return PythonQt::self()->addSignalHandler(sender, signalTmp, callable);
59 result = PythonQt::self()->addSignalHandler(sender, signalTmp, callable);
59 } else {
60 if (!result) {
60 return false;
61 if (sender->metaObject()->indexOfSignal(QMetaObject::normalizedSignature(signalTmp.constData()+1)) == -1) {
62 qWarning("PythonQt: QObject::connect() signal '%s' does not exist on %s", signal.constData(), sender->metaObject()->className());
63 }
64 }
61 }
65 }
66 return result;
62 }
67 }
63
68
64 bool PythonQtStdDecorators::connect(QObject* sender, const QByteArray& signal, QObject* receiver, const QByteArray& slot)
69 bool PythonQtStdDecorators::connect(QObject* sender, const QByteArray& signal, QObject* receiver, const QByteArray& slot)
65 {
70 {
66 bool r = false;
71 bool r = false;
67 if (sender && receiver) {
72 if (sender && receiver) {
68 QByteArray signalTmp;
73 QByteArray signalTmp;
69 char first = signal.at(0);
74 char first = signal.at(0);
70 if (first>='0' && first<='9') {
75 if (first>='0' && first<='9') {
71 signalTmp = signal;
76 signalTmp = signal;
72 } else {
77 } else {
73 signalTmp = "2" + signal;
78 signalTmp = "2" + signal;
74 }
79 }
75
80
76 QByteArray slotTmp;
81 QByteArray slotTmp;
77 first = slot.at(0);
82 first = slot.at(0);
78 if (first>='0' && first<='9') {
83 if (first>='0' && first<='9') {
79 slotTmp = slot;
84 slotTmp = slot;
80 } else {
85 } else {
81 slotTmp = "1" + slot;
86 slotTmp = "1" + slot;
82 }
87 }
83 r = QObject::connect(sender, signalTmp, receiver, slotTmp);
88 r = QObject::connect(sender, signalTmp, receiver, slotTmp);
84 }
89 }
85 return r;
90 return r;
86 }
91 }
87
92
88 bool PythonQtStdDecorators::disconnect(QObject* sender, const QByteArray& signal, PyObject* callable)
93 bool PythonQtStdDecorators::disconnect(QObject* sender, const QByteArray& signal, PyObject* callable)
89 {
94 {
95 bool result = false;
90 QByteArray signalTmp;
96 QByteArray signalTmp;
91 char first = signal.at(0);
97 char first = signal.at(0);
92 if (first>='0' && first<='9') {
98 if (first>='0' && first<='9') {
93 signalTmp = signal;
99 signalTmp = signal;
94 } else {
100 } else {
95 signalTmp = "2" + signal;
101 signalTmp = "2" + signal;
96 }
102 }
97 if (sender) {
103 if (sender) {
98 return PythonQt::self()->removeSignalHandler(sender, signalTmp, callable);
104 result = PythonQt::self()->removeSignalHandler(sender, signalTmp, callable);
99 } else {
105 if (!result) {
100 return false;
106 if (sender->metaObject()->indexOfSignal(QMetaObject::normalizedSignature(signalTmp.constData()+1)) == -1) {
107 qWarning("PythonQt: QObject::disconnect() signal '%s' does not exist on %s", signal.constData(), sender->metaObject()->className());
108 }
109 }
101 }
110 }
111 return result;
102 }
112 }
103
113
104 bool PythonQtStdDecorators::disconnect(QObject* sender, const QByteArray& signal, QObject* receiver, const QByteArray& slot)
114 bool PythonQtStdDecorators::disconnect(QObject* sender, const QByteArray& signal, QObject* receiver, const QByteArray& slot)
105 {
115 {
106 bool r = false;
116 bool r = false;
107 if (sender && receiver) {
117 if (sender && receiver) {
108 QByteArray signalTmp;
118 QByteArray signalTmp;
109 char first = signal.at(0);
119 char first = signal.at(0);
110 if (first>='0' && first<='9') {
120 if (first>='0' && first<='9') {
111 signalTmp = signal;
121 signalTmp = signal;
112 } else {
122 } else {
113 signalTmp = "2" + signal;
123 signalTmp = "2" + signal;
114 }
124 }
115
125
116 QByteArray slotTmp;
126 QByteArray slotTmp;
117 first = slot.at(0);
127 first = slot.at(0);
118 if (first>='0' && first<='9') {
128 if (first>='0' && first<='9') {
119 slotTmp = slot;
129 slotTmp = slot;
120 } else {
130 } else {
121 slotTmp = "1" + slot;
131 slotTmp = "1" + slot;
122 }
132 }
123
133
124 r = QObject::disconnect(sender, signalTmp, receiver, slotTmp);
134 r = QObject::disconnect(sender, signalTmp, receiver, slotTmp);
125 }
135 }
126 return r;
136 return r;
127 }
137 }
128
138
129 QObject* PythonQtStdDecorators::parent(QObject* o) {
139 QObject* PythonQtStdDecorators::parent(QObject* o) {
130 return o->parent();
140 return o->parent();
131 }
141 }
132
142
133 void PythonQtStdDecorators::setParent(QObject* o, QObject* parent)
143 void PythonQtStdDecorators::setParent(QObject* o, QObject* parent)
134 {
144 {
135 o->setParent(parent);
145 o->setParent(parent);
136 }
146 }
137
147
138 const QObjectList* PythonQtStdDecorators::children(QObject* o)
148 const QObjectList* PythonQtStdDecorators::children(QObject* o)
139 {
149 {
140 return &o->children();
150 return &o->children();
141 }
151 }
142
152
143 bool PythonQtStdDecorators::setProperty(QObject* o, const char* name, const QVariant& value)
153 bool PythonQtStdDecorators::setProperty(QObject* o, const char* name, const QVariant& value)
144 {
154 {
145 return o->setProperty(name, value);
155 return o->setProperty(name, value);
146 }
156 }
147
157
148 QVariant PythonQtStdDecorators::property(QObject* o, const char* name)
158 QVariant PythonQtStdDecorators::property(QObject* o, const char* name)
149 {
159 {
150 return o->property(name);
160 return o->property(name);
151 }
161 }
152
162
153 QString PythonQtStdDecorators::tr(QObject* obj, const QByteArray& text, const QByteArray& ambig, int n)
163 QString PythonQtStdDecorators::tr(QObject* obj, const QByteArray& text, const QByteArray& ambig, int n)
154 {
164 {
155 return QCoreApplication::translate(obj->metaObject()->className(), text.constData(), ambig.constData(), QCoreApplication::CodecForTr, n);
165 return QCoreApplication::translate(obj->metaObject()->className(), text.constData(), ambig.constData(), QCoreApplication::CodecForTr, n);
156 }
166 }
157
167
158 QObject* PythonQtStdDecorators::findChild(QObject* parent, PyObject* type, const QString& name)
168 QObject* PythonQtStdDecorators::findChild(QObject* parent, PyObject* type, const QString& name)
159 {
169 {
160 const QMetaObject* meta = NULL;
170 const QMetaObject* meta = NULL;
161 const char* typeName = NULL;
171 const char* typeName = NULL;
162
172
163 if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
173 if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
164 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
174 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
165 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
175 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
166 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
176 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
167 } else if (PyString_Check(type)) {
177 } else if (PyString_Check(type)) {
168 typeName = PyString_AsString(type);
178 typeName = PyString_AsString(type);
169 }
179 }
170
180
171 if (!typeName && !meta)
181 if (!typeName && !meta)
172 return NULL;
182 return NULL;
173
183
174 return findChild(parent, typeName, meta, name);
184 return findChild(parent, typeName, meta, name);
175 }
185 }
176
186
177 QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QString& name)
187 QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QString& name)
178 {
188 {
179 const QMetaObject* meta = NULL;
189 const QMetaObject* meta = NULL;
180 const char* typeName = NULL;
190 const char* typeName = NULL;
181
191
182 if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
192 if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
183 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
193 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
184 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
194 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
185 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
195 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
186 } else if (PyString_Check(type)) {
196 } else if (PyString_Check(type)) {
187 typeName = PyString_AsString(type);
197 typeName = PyString_AsString(type);
188 }
198 }
189
199
190 QList<QObject*> list;
200 QList<QObject*> list;
191
201
192 if (!typeName && !meta)
202 if (!typeName && !meta)
193 return list;
203 return list;
194
204
195 findChildren(parent, typeName, meta, name, list);
205 findChildren(parent, typeName, meta, name, list);
196
206
197 return list;
207 return list;
198 }
208 }
199
209
200 QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegExp& regExp)
210 QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegExp& regExp)
201 {
211 {
202 const QMetaObject* meta = NULL;
212 const QMetaObject* meta = NULL;
203 const char* typeName = NULL;
213 const char* typeName = NULL;
204
214
205 if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
215 if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
206 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
216 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
207 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
217 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
208 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
218 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
209 } else if (PyString_Check(type)) {
219 } else if (PyString_Check(type)) {
210 typeName = PyString_AsString(type);
220 typeName = PyString_AsString(type);
211 }
221 }
212
222
213 QList<QObject*> list;
223 QList<QObject*> list;
214
224
215 if (!typeName && !meta)
225 if (!typeName && !meta)
216 return list;
226 return list;
217
227
218 findChildren(parent, typeName, meta, regExp, list);
228 findChildren(parent, typeName, meta, regExp, list);
219
229
220 return list;
230 return list;
221 }
231 }
222
232
223 QObject* PythonQtStdDecorators::findChild(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name)
233 QObject* PythonQtStdDecorators::findChild(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name)
224 {
234 {
225 const QObjectList &children = parent->children();
235 const QObjectList &children = parent->children();
226
236
227 int i;
237 int i;
228 for (i = 0; i < children.size(); ++i) {
238 for (i = 0; i < children.size(); ++i) {
229 QObject* obj = children.at(i);
239 QObject* obj = children.at(i);
230
240
231 if (!obj)
241 if (!obj)
232 return NULL;
242 return NULL;
233
243
234 // Skip if the name doesn't match.
244 // Skip if the name doesn't match.
235 if (!name.isNull() && obj->objectName() != name)
245 if (!name.isNull() && obj->objectName() != name)
236 continue;
246 continue;
237
247
238 if ((typeName && obj->inherits(typeName)) ||
248 if ((typeName && obj->inherits(typeName)) ||
239 (meta && meta->cast(obj)))
249 (meta && meta->cast(obj)))
240 return obj;
250 return obj;
241 }
251 }
242
252
243 for (i = 0; i < children.size(); ++i) {
253 for (i = 0; i < children.size(); ++i) {
244 QObject* obj = findChild(children.at(i), typeName, meta, name);
254 QObject* obj = findChild(children.at(i), typeName, meta, name);
245
255
246 if (obj != NULL)
256 if (obj != NULL)
247 return obj;
257 return obj;
248 }
258 }
249
259
250 return NULL;
260 return NULL;
251 }
261 }
252
262
253 int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name, QList<QObject*>& list)
263 int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name, QList<QObject*>& list)
254 {
264 {
255 const QObjectList& children = parent->children();
265 const QObjectList& children = parent->children();
256 int i;
266 int i;
257
267
258 for (i = 0; i < children.size(); ++i) {
268 for (i = 0; i < children.size(); ++i) {
259 QObject* obj = children.at(i);
269 QObject* obj = children.at(i);
260
270
261 if (!obj)
271 if (!obj)
262 return -1;
272 return -1;
263
273
264 // Skip if the name doesn't match.
274 // Skip if the name doesn't match.
265 if (!name.isNull() && obj->objectName() != name)
275 if (!name.isNull() && obj->objectName() != name)
266 continue;
276 continue;
267
277
268 if ((typeName && obj->inherits(typeName)) ||
278 if ((typeName && obj->inherits(typeName)) ||
269 (meta && meta->cast(obj))) {
279 (meta && meta->cast(obj))) {
270 list += obj;
280 list += obj;
271 }
281 }
272
282
273 if (findChildren(obj, typeName, meta, name, list) < 0)
283 if (findChildren(obj, typeName, meta, name, list) < 0)
274 return -1;
284 return -1;
275 }
285 }
276
286
277 return 0;
287 return 0;
278 }
288 }
279
289
280 int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegExp& regExp, QList<QObject*>& list)
290 int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegExp& regExp, QList<QObject*>& list)
281 {
291 {
282 const QObjectList& children = parent->children();
292 const QObjectList& children = parent->children();
283 int i;
293 int i;
284
294
285 for (i = 0; i < children.size(); ++i) {
295 for (i = 0; i < children.size(); ++i) {
286 QObject* obj = children.at(i);
296 QObject* obj = children.at(i);
287
297
288 if (!obj)
298 if (!obj)
289 return -1;
299 return -1;
290
300
291 // Skip if the name doesn't match.
301 // Skip if the name doesn't match.
292 if (regExp.indexIn(obj->objectName()) == -1)
302 if (regExp.indexIn(obj->objectName()) == -1)
293 continue;
303 continue;
294
304
295 if ((typeName && obj->inherits(typeName)) ||
305 if ((typeName && obj->inherits(typeName)) ||
296 (meta && meta->cast(obj))) {
306 (meta && meta->cast(obj))) {
297 list += obj;
307 list += obj;
298 }
308 }
299
309
300 if (findChildren(obj, typeName, meta, regExp, list) < 0)
310 if (findChildren(obj, typeName, meta, regExp, list) < 0)
301 return -1;
311 return -1;
302 }
312 }
303
313
304 return 0;
314 return 0;
305 }
315 }
General Comments 0
You need to be logged in to leave comments. Login now