1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qqmlwatcher.h"
41 
42 #include "qqmlexpression.h"
43 #include "qqmlcontext.h"
44 #include "qqml.h"
45 
46 #include <private/qqmldebugservice_p.h>
47 #include <private/qqmlproperty_p.h>
48 #include <private/qqmlvaluetype_p.h>
49 
50 #include <QtCore/qmetaobject.h>
51 #include <QtCore/qdebug.h>
52 
53 QT_BEGIN_NAMESPACE
54 
55 
56 class QQmlWatchProxy : public QObject
57 {
58     Q_OBJECT
59 public:
60     QQmlWatchProxy(int id,
61                   QObject *object,
62                   int debugId,
63                   const QMetaProperty &prop,
64                   QQmlWatcher *parent = nullptr);
65 
66     QQmlWatchProxy(int id,
67                   QQmlExpression *exp,
68                   int debugId,
69                   QQmlWatcher *parent = nullptr);
70 
71 public slots:
72     void notifyValueChanged(); // Needs to be a slot because of QQmlPropertyPrivate::connect()
73 
74 private:
75     friend class QQmlWatcher;
76     int m_id;
77     QQmlWatcher *m_watch;
78     QObject *m_object;
79     int m_debugId;
80     QMetaProperty m_property;
81 
82     QQmlExpression *m_expr;
83 };
84 
QQmlWatchProxy(int id,QQmlExpression * exp,int debugId,QQmlWatcher * parent)85 QQmlWatchProxy::QQmlWatchProxy(int id,
86                              QQmlExpression *exp,
87                              int debugId,
88                              QQmlWatcher *parent)
89 : QObject(parent), m_id(id), m_watch(parent), m_object(nullptr), m_debugId(debugId), m_expr(exp)
90 {
91     QObject::connect(m_expr, &QQmlExpression::valueChanged,
92                      this, &QQmlWatchProxy::notifyValueChanged);
93 }
94 
QQmlWatchProxy(int id,QObject * object,int debugId,const QMetaProperty & prop,QQmlWatcher * parent)95 QQmlWatchProxy::QQmlWatchProxy(int id,
96                              QObject *object,
97                              int debugId,
98                              const QMetaProperty &prop,
99                              QQmlWatcher *parent)
100 : QObject(parent), m_id(id), m_watch(parent), m_object(object), m_debugId(debugId), m_property(prop), m_expr(nullptr)
101 {
102     static int refreshIdx = -1;
103     if(refreshIdx == -1)
104         refreshIdx = QQmlWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
105 
106     if (prop.hasNotifySignal())
107         QQmlPropertyPrivate::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
108 }
109 
notifyValueChanged()110 void QQmlWatchProxy::notifyValueChanged()
111 {
112     const QVariant v = m_expr ? m_expr->evaluate() : m_property.read(m_object);
113     emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
114 }
115 
116 
QQmlWatcher(QObject * parent)117 QQmlWatcher::QQmlWatcher(QObject *parent)
118     : QObject(parent)
119 {
120 }
121 
addWatch(int id,quint32 debugId)122 bool QQmlWatcher::addWatch(int id, quint32 debugId)
123 {
124     QObject *object = QQmlDebugService::objectForId(debugId);
125     if (object) {
126         int propCount = object->metaObject()->propertyCount();
127         for (int ii=0; ii<propCount; ii++)
128             addPropertyWatch(id, object, debugId, object->metaObject()->property(ii));
129         return true;
130     }
131     return false;
132 }
133 
addWatch(int id,quint32 debugId,const QByteArray & property)134 bool QQmlWatcher::addWatch(int id, quint32 debugId, const QByteArray &property)
135 {
136     QObject *object = QQmlDebugService::objectForId(debugId);
137     if (object) {
138         int index = object->metaObject()->indexOfProperty(property.constData());
139         if (index >= 0) {
140             addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
141             return true;
142         }
143     }
144     return false;
145 }
146 
addWatch(int id,quint32 objectId,const QString & expr)147 bool QQmlWatcher::addWatch(int id, quint32 objectId, const QString &expr)
148 {
149     QObject *object = QQmlDebugService::objectForId(objectId);
150     QQmlContext *context = qmlContext(object);
151     if (context) {
152         QQmlExpression *exprObj = new QQmlExpression(context, object, expr);
153         exprObj->setNotifyOnValueChanged(true);
154         QQmlWatchProxy *proxy = new QQmlWatchProxy(id, exprObj, objectId, this);
155         exprObj->setParent(proxy);
156         m_proxies[id].append(proxy);
157         proxy->notifyValueChanged();
158         return true;
159     }
160     return false;
161 }
162 
removeWatch(int id)163 bool QQmlWatcher::removeWatch(int id)
164 {
165     if (!m_proxies.contains(id))
166         return false;
167 
168     QList<QPointer<QQmlWatchProxy> > proxies = m_proxies.take(id);
169     qDeleteAll(proxies);
170     return true;
171 }
172 
addPropertyWatch(int id,QObject * object,quint32 debugId,const QMetaProperty & property)173 void QQmlWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
174 {
175     QQmlWatchProxy *proxy = new QQmlWatchProxy(id, object, debugId, property, this);
176     m_proxies[id].append(proxy);
177 
178     proxy->notifyValueChanged();
179 }
180 
181 QT_END_NAMESPACE
182 
183 #include <qqmlwatcher.moc>
184 
185 #include "moc_qqmlwatcher.cpp"
186