1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qqmlwatcher_p.h"
44 #include "qqmlexpression.h"
45 #include "qqmlcontext.h"
48 #include <private/qqmldebugservice_p.h>
49 #include "qqmlproperty_p.h"
50 #include "qqmlvaluetype_p.h"
52 #include <QtCore/qmetaobject.h>
53 #include <QtCore/qdebug.h>
58 class QQmlWatchProxy : public QObject
62 QQmlWatchProxy(int id,
65 const QMetaProperty &prop,
66 QQmlWatcher *parent = 0);
68 QQmlWatchProxy(int id,
71 QQmlWatcher *parent = 0);
74 void notifyValueChanged();
77 friend class QQmlWatcher;
82 QMetaProperty m_property;
84 QQmlExpression *m_expr;
87 QQmlWatchProxy::QQmlWatchProxy(int id,
91 : QObject(parent), m_id(id), m_watch(parent), m_object(0), m_debugId(debugId), m_expr(exp)
93 QObject::connect(m_expr, SIGNAL(valueChanged()), this, SLOT(notifyValueChanged()));
96 QQmlWatchProxy::QQmlWatchProxy(int id,
99 const QMetaProperty &prop,
101 : QObject(parent), m_id(id), m_watch(parent), m_object(object), m_debugId(debugId), m_property(prop), m_expr(0)
103 static int refreshIdx = -1;
105 refreshIdx = QQmlWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
107 if (prop.hasNotifySignal())
108 QQmlPropertyPrivate::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
111 void QQmlWatchProxy::notifyValueChanged()
115 v = m_expr->evaluate();
116 else if (QQmlValueTypeFactory::isValueType(m_property.userType()))
117 v = m_property.read(m_object);
119 emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
123 QQmlWatcher::QQmlWatcher(QObject *parent)
128 bool QQmlWatcher::addWatch(int id, quint32 debugId)
130 QObject *object = QQmlDebugService::objectForId(debugId);
132 int propCount = object->metaObject()->propertyCount();
133 for (int ii=0; ii<propCount; ii++)
134 addPropertyWatch(id, object, debugId, object->metaObject()->property(ii));
140 bool QQmlWatcher::addWatch(int id, quint32 debugId, const QByteArray &property)
142 QObject *object = QQmlDebugService::objectForId(debugId);
144 int index = object->metaObject()->indexOfProperty(property.constData());
146 addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
153 bool QQmlWatcher::addWatch(int id, quint32 objectId, const QString &expr)
155 QObject *object = QQmlDebugService::objectForId(objectId);
156 QQmlContext *context = qmlContext(object);
158 QQmlExpression *exprObj = new QQmlExpression(context, object, expr);
159 exprObj->setNotifyOnValueChanged(true);
160 QQmlWatchProxy *proxy = new QQmlWatchProxy(id, exprObj, objectId, this);
161 exprObj->setParent(proxy);
162 m_proxies[id].append(proxy);
163 proxy->notifyValueChanged();
169 bool QQmlWatcher::removeWatch(int id)
171 if (!m_proxies.contains(id))
174 QList<QPointer<QQmlWatchProxy> > proxies = m_proxies.take(id);
179 void QQmlWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
181 QQmlWatchProxy *proxy = new QQmlWatchProxy(id, object, debugId, property, this);
182 m_proxies[id].append(proxy);
184 proxy->notifyValueChanged();
189 #include <qqmlwatcher.moc>