Merge branch 'qtquick2'
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativewatcher.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativewatcher_p.h"
43
44 #include "qdeclarativeexpression.h"
45 #include "qdeclarativecontext.h"
46 #include "qdeclarative.h"
47
48 #include <qdeclarativedebugservice_p.h>
49 #include "private/qdeclarativeproperty_p.h"
50
51 #include <QtCore/qmetaobject.h>
52 #include <QtCore/qdebug.h>
53
54 QT_BEGIN_NAMESPACE
55
56
57 class QDeclarativeWatchProxy : public QObject
58 {
59     Q_OBJECT
60 public:
61     QDeclarativeWatchProxy(int id,
62                   QObject *object,
63                   int debugId,
64                   const QMetaProperty &prop,
65                   QDeclarativeWatcher *parent = 0);
66
67     QDeclarativeWatchProxy(int id,
68                   QDeclarativeExpression *exp,
69                   int debugId,
70                   QDeclarativeWatcher *parent = 0);
71
72 public slots:
73     void notifyValueChanged();
74
75 private:
76     friend class QDeclarativeWatcher;
77     int m_id;
78     QDeclarativeWatcher *m_watch;
79     QObject *m_object;
80     int m_debugId;
81     QMetaProperty m_property;
82
83     QDeclarativeExpression *m_expr;
84 };
85
86 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
87                              QDeclarativeExpression *exp,
88                              int debugId,
89                              QDeclarativeWatcher *parent)
90 : QObject(parent), m_id(id), m_watch(parent), m_object(0), m_debugId(debugId), m_expr(exp)
91 {
92     QObject::connect(m_expr, SIGNAL(valueChanged()), this, SLOT(notifyValueChanged()));
93 }
94
95 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
96                              QObject *object,
97                              int debugId,
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)
101 {
102     static int refreshIdx = -1;
103     if(refreshIdx == -1)
104         refreshIdx = QDeclarativeWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
105
106     if (prop.hasNotifySignal())
107         QDeclarativePropertyPrivate::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
108 }
109
110 void QDeclarativeWatchProxy::notifyValueChanged()
111 {
112     QVariant v;
113     if (m_expr)
114         v = m_expr->evaluate();
115     else
116         v = m_property.read(m_object);
117
118     emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
119 }
120
121
122 QDeclarativeWatcher::QDeclarativeWatcher(QObject *parent)
123     : QObject(parent)
124 {
125 }
126
127 bool QDeclarativeWatcher::addWatch(int id, quint32 debugId)
128 {
129     QObject *object = QDeclarativeDebugService::objectForId(debugId);
130     if (object) {
131         int propCount = object->metaObject()->propertyCount();
132         for (int ii=0; ii<propCount; ii++)
133             addPropertyWatch(id, object, debugId, object->metaObject()->property(ii));
134         return true;
135     }
136     return false;
137 }
138
139 bool QDeclarativeWatcher::addWatch(int id, quint32 debugId, const QByteArray &property)
140 {
141     QObject *object = QDeclarativeDebugService::objectForId(debugId);
142     if (object) {
143         int index = object->metaObject()->indexOfProperty(property.constData());
144         if (index >= 0) {
145             addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
146             return true;
147         }
148     }
149     return false;
150 }
151
152 bool QDeclarativeWatcher::addWatch(int id, quint32 objectId, const QString &expr)
153 {
154     QObject *object = QDeclarativeDebugService::objectForId(objectId);
155     QDeclarativeContext *context = qmlContext(object);
156     if (context) {
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();
163         return true;
164     }
165     return false;
166 }
167
168 void QDeclarativeWatcher::removeWatch(int id)
169 {
170     if (!m_proxies.contains(id))
171         return;
172
173     QList<QPointer<QDeclarativeWatchProxy> > proxies = m_proxies.take(id);
174     qDeleteAll(proxies);
175 }
176
177 void QDeclarativeWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
178 {
179     QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, object, debugId, property, this);
180     m_proxies[id].append(proxy);
181
182     proxy->notifyValueChanged();
183 }
184
185 QT_END_NAMESPACE
186
187 #include <qdeclarativewatcher.moc>