1 | // Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | #ifndef SIGNALHANDLER_H |
4 | #define SIGNALHANDLER_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists for the convenience |
11 | // of other Qt classes. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <Qt3DCore/qt3dcore_global.h> |
18 | #include <QtCore/QDebug> |
19 | #include <QtCore/QHash> |
20 | #include <QtCore/QMetaMethod> |
21 | #include <QtCore/QObject> |
22 | #include <private/qglobal_p.h> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | namespace Qt3DCore { |
27 | |
28 | class Q_3DCORESHARED_EXPORT PropertyChangeHandlerBase : public QObject |
29 | { |
30 | Q_OBJECT |
31 | public: |
32 | PropertyChangeHandlerBase(QObject *parent = nullptr); |
33 | |
34 | /** |
35 | * Connect to the change signal of @p property in @p object. |
36 | */ |
37 | void connectToPropertyChange(const QObject *object, int propertyIndex); |
38 | |
39 | /** |
40 | * Disconnect from the change signal of @p property in @p object. |
41 | */ |
42 | void disconnectFromPropertyChange(const QObject *object, int propertyIndex); |
43 | }; |
44 | |
45 | /** |
46 | * The property change handler is similar to QSignalSpy, but geared towards the usecase of Qt3D. |
47 | * |
48 | * It allows connecting to any number of property change signals of the receiver object and forwards |
49 | * the signal invocations to the Receiver by calling its propertyChanged function. |
50 | */ |
51 | template<class Receiver> |
52 | class PropertyChangeHandler : public PropertyChangeHandlerBase |
53 | { |
54 | public: |
55 | PropertyChangeHandler(Receiver *receiver, QObject *parent = nullptr); |
56 | |
57 | /** |
58 | * @internal |
59 | * |
60 | * Custom implementation of qt_metacall which calls propertyChanged() on the receiver for |
61 | * connected signals. |
62 | */ |
63 | int qt_metacall(QMetaObject::Call call, int methodId, void **args) override; |
64 | |
65 | private: |
66 | Receiver *m_receiver; |
67 | }; |
68 | |
69 | template<class Receiver> |
70 | PropertyChangeHandler<Receiver>::PropertyChangeHandler(Receiver *receiver, QObject *parent) |
71 | : PropertyChangeHandlerBase(parent) |
72 | , m_receiver(receiver) |
73 | { |
74 | } |
75 | |
76 | template<class Receiver> |
77 | int PropertyChangeHandler<Receiver>::qt_metacall(QMetaObject::Call call, int methodId, void **args) |
78 | { |
79 | methodId = QObject::qt_metacall(call, methodId, args); |
80 | if (methodId < 0) |
81 | return methodId; |
82 | |
83 | if (call == QMetaObject::InvokeMetaMethod) { |
84 | m_receiver->propertyChanged(methodId); |
85 | return -1; |
86 | } |
87 | return methodId; |
88 | } |
89 | |
90 | } |
91 | |
92 | QT_END_NAMESPACE |
93 | |
94 | #endif // SIGNALHANDLER_H |
95 | |