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