|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtTest module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef QSIGNALSPY_H |
|
43 #define QSIGNALSPY_H |
|
44 |
|
45 #include <QtCore/qbytearray.h> |
|
46 #include <QtCore/qlist.h> |
|
47 #include <QtCore/qobject.h> |
|
48 #include <QtCore/qmetaobject.h> |
|
49 #include <QtCore/qvariant.h> |
|
50 |
|
51 QT_BEGIN_HEADER |
|
52 |
|
53 QT_BEGIN_NAMESPACE |
|
54 |
|
55 QT_MODULE(Test) |
|
56 |
|
57 class QVariant; |
|
58 |
|
59 /* ### Qt5: change the class to use regular BC mechanisms, such that we can |
|
60 * implement things like suggested in task 160192. */ |
|
61 class QSignalSpy: public QObject, public QList<QList<QVariant> > |
|
62 { |
|
63 public: |
|
64 QSignalSpy(QObject *obj, const char *aSignal) |
|
65 { |
|
66 #ifdef Q_CC_BOR |
|
67 const int memberOffset = QObject::staticMetaObject.methodCount(); |
|
68 #else |
|
69 static const int memberOffset = QObject::staticMetaObject.methodCount(); |
|
70 #endif |
|
71 Q_ASSERT(obj); |
|
72 Q_ASSERT(aSignal); |
|
73 |
|
74 if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE) { |
|
75 qWarning("QSignalSpy: Not a valid signal, use the SIGNAL macro"); |
|
76 return; |
|
77 } |
|
78 |
|
79 QByteArray ba = QMetaObject::normalizedSignature(aSignal + 1); |
|
80 const QMetaObject *mo = obj->metaObject(); |
|
81 int sigIndex = mo->indexOfMethod(ba.constData()); |
|
82 if (sigIndex < 0) { |
|
83 qWarning("QSignalSpy: No such signal: '%s'", ba.constData()); |
|
84 return; |
|
85 } |
|
86 |
|
87 if (!QMetaObject::connect(obj, sigIndex, this, memberOffset, |
|
88 Qt::DirectConnection, 0)) { |
|
89 qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect."); |
|
90 return; |
|
91 } |
|
92 sig = ba; |
|
93 initArgs(mo->method(sigIndex)); |
|
94 } |
|
95 |
|
96 inline bool isValid() const { return !sig.isEmpty(); } |
|
97 inline QByteArray signal() const { return sig; } |
|
98 |
|
99 |
|
100 int qt_metacall(QMetaObject::Call call, int methodId, void **a) |
|
101 { |
|
102 methodId = QObject::qt_metacall(call, methodId, a); |
|
103 if (methodId < 0) |
|
104 return methodId; |
|
105 |
|
106 if (call == QMetaObject::InvokeMetaMethod) { |
|
107 if (methodId == 0) { |
|
108 appendArgs(a); |
|
109 } |
|
110 --methodId; |
|
111 } |
|
112 return methodId; |
|
113 } |
|
114 |
|
115 private: |
|
116 void initArgs(const QMetaMethod &member) |
|
117 { |
|
118 QList<QByteArray> params = member.parameterTypes(); |
|
119 for (int i = 0; i < params.count(); ++i) { |
|
120 int tp = QMetaType::type(params.at(i).constData()); |
|
121 if (tp == QMetaType::Void) |
|
122 qWarning("Don't know how to handle '%s', use qRegisterMetaType to register it.", |
|
123 params.at(i).constData()); |
|
124 args << tp; |
|
125 } |
|
126 } |
|
127 |
|
128 void appendArgs(void **a) |
|
129 { |
|
130 QList<QVariant> list; |
|
131 for (int i = 0; i < args.count(); ++i) { |
|
132 QMetaType::Type type = static_cast<QMetaType::Type>(args.at(i)); |
|
133 list << QVariant(type, a[i + 1]); |
|
134 } |
|
135 append(list); |
|
136 } |
|
137 |
|
138 // the full, normalized signal name |
|
139 QByteArray sig; |
|
140 // holds the QMetaType types for the argument list of the signal |
|
141 QList<int> args; |
|
142 }; |
|
143 |
|
144 QT_END_NAMESPACE |
|
145 |
|
146 QT_END_HEADER |
|
147 |
|
148 #endif |