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