1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "private/qdeclarativewatcher_p.h"
44 #include "qdeclarativeexpression.h"
45 #include "qdeclarativecontext.h"
46 #include "qdeclarative.h"
48 #include <qdeclarativedebugservice_p.h>
49 #include "private/qdeclarativeproperty_p.h"
51 #include <QtCore/qmetaobject.h>
52 #include <QtCore/qdebug.h>
57 class QDeclarativeWatchProxy : public QObject
61 QDeclarativeWatchProxy(int id,
64 const QMetaProperty &prop,
65 QDeclarativeWatcher *parent = 0);
67 QDeclarativeWatchProxy(int id,
68 QDeclarativeExpression *exp,
70 QDeclarativeWatcher *parent = 0);
73 void notifyValueChanged();
76 friend class QDeclarativeWatcher;
78 QDeclarativeWatcher *m_watch;
81 QMetaProperty m_property;
83 QDeclarativeExpression *m_expr;
86 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
87 QDeclarativeExpression *exp,
89 QDeclarativeWatcher *parent)
90 : QObject(parent), m_id(id), m_watch(parent), m_object(0), m_debugId(debugId), m_expr(exp)
92 QObject::connect(m_expr, SIGNAL(valueChanged()), this, SLOT(notifyValueChanged()));
95 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
98 const QMetaProperty &prop,
99 QDeclarativeWatcher *parent)
100 : QObject(parent), m_id(id), m_watch(parent), m_object(object), m_debugId(debugId), m_property(prop), m_expr(0)
102 static int refreshIdx = -1;
104 refreshIdx = QDeclarativeWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
106 if (prop.hasNotifySignal())
107 QDeclarativePropertyPrivate::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
110 void QDeclarativeWatchProxy::notifyValueChanged()
114 v = m_expr->evaluate();
116 v = m_property.read(m_object);
118 emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
122 QDeclarativeWatcher::QDeclarativeWatcher(QObject *parent)
127 bool QDeclarativeWatcher::addWatch(int id, quint32 debugId)
129 QObject *object = QDeclarativeDebugService::objectForId(debugId);
131 int propCount = object->metaObject()->propertyCount();
132 for (int ii=0; ii<propCount; ii++)
133 addPropertyWatch(id, object, debugId, object->metaObject()->property(ii));
139 bool QDeclarativeWatcher::addWatch(int id, quint32 debugId, const QByteArray &property)
141 QObject *object = QDeclarativeDebugService::objectForId(debugId);
143 int index = object->metaObject()->indexOfProperty(property.constData());
145 addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
152 bool QDeclarativeWatcher::addWatch(int id, quint32 objectId, const QString &expr)
154 QObject *object = QDeclarativeDebugService::objectForId(objectId);
155 QDeclarativeContext *context = qmlContext(object);
157 QDeclarativeExpression *exprObj = new QDeclarativeExpression(context, object, expr);
158 exprObj->setNotifyOnValueChanged(true);
159 QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, exprObj, objectId, this);
160 exprObj->setParent(proxy);
161 m_proxies[id].append(proxy);
162 proxy->notifyValueChanged();
168 void QDeclarativeWatcher::removeWatch(int id)
170 if (!m_proxies.contains(id))
173 QList<QPointer<QDeclarativeWatchProxy> > proxies = m_proxies.take(id);
177 void QDeclarativeWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
179 QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, object, debugId, property, this);
180 m_proxies[id].append(proxy);
182 proxy->notifyValueChanged();
187 #include <qdeclarativewatcher.moc>