Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
[qt-netbsd.git] / src / testlib / qsignalspy.h
blob860414a6025ceb94aa04c9e6796e6abcd8c753b5
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #ifndef QSIGNALSPY_H
43 #define QSIGNALSPY_H
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>
51 QT_BEGIN_HEADER
53 QT_BEGIN_NAMESPACE
55 QT_MODULE(Test)
57 class QVariant;
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> >
63 public:
64 QSignalSpy(QObject *obj, const char *aSignal)
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);
74 if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE) {
75 qWarning("QSignalSpy: Not a valid signal, use the SIGNAL macro");
76 return;
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;
87 if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
88 Qt::DirectConnection, 0)) {
89 qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect.");
90 return;
92 sig = ba;
93 initArgs(mo->method(sigIndex));
96 inline bool isValid() const { return !sig.isEmpty(); }
97 inline QByteArray signal() const { return sig; }
100 int qt_metacall(QMetaObject::Call call, int methodId, void **a)
102 methodId = QObject::qt_metacall(call, methodId, a);
103 if (methodId < 0)
104 return methodId;
106 if (call == QMetaObject::InvokeMetaMethod) {
107 if (methodId == 0) {
108 appendArgs(a);
110 --methodId;
112 return methodId;
115 private:
116 void initArgs(const QMetaMethod &member)
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;
128 void appendArgs(void **a)
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]);
135 append(list);
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;
144 QT_END_NAMESPACE
146 QT_END_HEADER
148 #endif