Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlwatcher.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#include "qqmlwatcher.h"
6
8#include "qqmlcontext.h"
9#include "qqml.h"
10
11#include <private/qqmldebugservice_p.h>
12#include <private/qqmlproperty_p.h>
13#include <private/qqmlvaluetype_p.h>
14
15#include <QtCore/qmetaobject.h>
16#include <QtCore/qdebug.h>
17
19
20
21class QQmlWatchProxy : public QObject
22{
24public:
27 int debugId,
28 const QMetaProperty &prop,
29 QQmlWatcher *parent = nullptr);
30
32 QQmlExpression *exp,
33 int debugId,
34 QQmlWatcher *parent = nullptr);
35
36public slots:
37 void notifyValueChanged(); // Needs to be a slot because of QQmlPropertyPrivate::connect()
38
39private:
40 friend class QQmlWatcher;
41 int m_id;
42 QQmlWatcher *m_watch;
43 QObject *m_object;
44 int m_debugId;
45 QMetaProperty m_property;
46
47 QQmlExpression *m_expr;
48};
49
50QQmlWatchProxy::QQmlWatchProxy(int id, QQmlExpression *exp, int debugId, QQmlWatcher *parent)
51 : QObject(parent),
52 m_id(id),
53 m_watch(parent),
54 m_object(nullptr),
55 m_debugId(debugId),
56 m_expr(exp)
57{
58 QObject::connect(m_expr, &QQmlExpression::valueChanged,
59 this, &QQmlWatchProxy::notifyValueChanged);
60}
61
62QQmlWatchProxy::QQmlWatchProxy(int id, QObject *object, int debugId, const QMetaProperty &prop,
63 QQmlWatcher *parent)
64 : QObject(parent),
65 m_id(id),
66 m_watch(parent),
67 m_object(object),
68 m_debugId(debugId),
69 m_property(prop),
70 m_expr(nullptr)
71{
72 static int refreshIdx = -1;
73 if(refreshIdx == -1)
74 refreshIdx = QQmlWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
75
76 if (prop.hasNotifySignal())
77 QQmlPropertyPrivate::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
78}
79
80void QQmlWatchProxy::notifyValueChanged()
81{
82 QVariant v;
83 if (m_expr)
84 v = m_expr->evaluate();
85 else
86 v = m_property.read(m_object);
87 emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
88}
89
90
91QQmlWatcher::QQmlWatcher(QObject *parent)
92 : QObject(parent)
93{
94}
95
96bool QQmlWatcher::addWatch(int id, quint32 debugId)
97{
98 QObject *object = QQmlDebugService::objectForId(debugId);
99 if (object) {
100 int propCount = object->metaObject()->propertyCount();
101 for (int ii=0; ii<propCount; ii++)
102 addPropertyWatch(id, object, debugId, object->metaObject()->property(ii));
103 return true;
104 }
105 return false;
106}
107
108bool QQmlWatcher::addWatch(int id, quint32 debugId, const QByteArray &property)
109{
110 QObject *object = QQmlDebugService::objectForId(debugId);
111 if (object) {
112 int index = object->metaObject()->indexOfProperty(property.constData());
113 if (index >= 0) {
114 addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
115 return true;
116 }
117 }
118 return false;
119}
120
121bool QQmlWatcher::addWatch(int id, quint32 objectId, const QString &expr)
122{
123 QObject *object = QQmlDebugService::objectForId(objectId);
124 QQmlContext *context = qmlContext(object);
125 if (context) {
126 QQmlExpression *exprObj = new QQmlExpression(context, object, expr);
127 exprObj->setNotifyOnValueChanged(true);
128 QQmlWatchProxy *proxy = new QQmlWatchProxy(id, exprObj, objectId, this);
129 exprObj->setParent(proxy);
130 m_proxies[id].append(proxy);
131 proxy->notifyValueChanged();
132 return true;
133 }
134 return false;
135}
136
138{
139 if (!m_proxies.contains(id))
140 return false;
141
142 QList<QPointer<QQmlWatchProxy> > proxies = m_proxies.take(id);
143 qDeleteAll(proxies);
144 return true;
145}
146
147void QQmlWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
148{
149 QQmlWatchProxy *proxy = new QQmlWatchProxy(id, object, debugId, property, this);
150 m_proxies[id].append(proxy);
151
152 proxy->notifyValueChanged();
153}
154
155QT_END_NAMESPACE
156
157#include <qqmlwatcher.moc>
158
159#include "moc_qqmlwatcher.cpp"
QQmlWatchProxy(int id, QQmlExpression *exp, int debugId, QQmlWatcher *parent=nullptr)
bool removeWatch(int id)
bool addWatch(int id, quint32 objectId)
bool addWatch(int id, quint32 objectId, const QByteArray &property)
Combined button and popup list for selecting options.