1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdeclarativewatcher_p.h"
44 #include "qdeclarativeexpression.h"
45 #include "qdeclarativecontext.h"
46 #include "qdeclarative.h"
48 #include <private/qdeclarativedebugservice_p.h>
49 #include "qdeclarativeproperty_p.h"
50 #include "qdeclarativevaluetype_p.h"
52 #include <QtCore/qmetaobject.h>
53 #include <QtCore/qdebug.h>
58 class QDeclarativeWatchProxy : public QObject
62 QDeclarativeWatchProxy(int id,
65 const QMetaProperty &prop,
66 QDeclarativeWatcher *parent = 0);
68 QDeclarativeWatchProxy(int id,
69 QDeclarativeExpression *exp,
71 QDeclarativeWatcher *parent = 0);
74 void notifyValueChanged();
77 friend class QDeclarativeWatcher;
79 QDeclarativeWatcher *m_watch;
82 QMetaProperty m_property;
84 QDeclarativeExpression *m_expr;
87 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
88 QDeclarativeExpression *exp,
90 QDeclarativeWatcher *parent)
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 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
99 const QMetaProperty &prop,
100 QDeclarativeWatcher *parent)
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 = QDeclarativeWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
107 if (prop.hasNotifySignal())
108 QDeclarativePropertyPrivate::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
111 void QDeclarativeWatchProxy::notifyValueChanged()
115 v = m_expr->evaluate();
116 else if (QDeclarativeValueTypeFactory::isValueType(m_property.userType()))
117 v = m_property.read(m_object);
119 emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
123 QDeclarativeWatcher::QDeclarativeWatcher(QObject *parent)
128 bool QDeclarativeWatcher::addWatch(int id, quint32 debugId)
130 QObject *object = QDeclarativeDebugService::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 QDeclarativeWatcher::addWatch(int id, quint32 debugId, const QByteArray &property)
142 QObject *object = QDeclarativeDebugService::objectForId(debugId);
144 int index = object->metaObject()->indexOfProperty(property.constData());
146 addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
153 bool QDeclarativeWatcher::addWatch(int id, quint32 objectId, const QString &expr)
155 QObject *object = QDeclarativeDebugService::objectForId(objectId);
156 QDeclarativeContext *context = qmlContext(object);
158 QDeclarativeExpression *exprObj = new QDeclarativeExpression(context, object, expr);
159 exprObj->setNotifyOnValueChanged(true);
160 QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, exprObj, objectId, this);
161 exprObj->setParent(proxy);
162 m_proxies[id].append(proxy);
163 proxy->notifyValueChanged();
169 void QDeclarativeWatcher::removeWatch(int id)
171 if (!m_proxies.contains(id))
174 QList<QPointer<QDeclarativeWatchProxy> > proxies = m_proxies.take(id);
178 void QDeclarativeWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
180 QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, object, debugId, property, this);
181 m_proxies[id].append(proxy);
183 proxy->notifyValueChanged();
188 #include <qdeclarativewatcher.moc>