src/declarative/qml/qdeclarativewatcher.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    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 
       
    50 #include <QtCore/qmetaobject.h>
       
    51 #include <QtCore/qdebug.h>
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 
       
    56 class QDeclarativeWatchProxy : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 public:
       
    60     QDeclarativeWatchProxy(int id,
       
    61                   QObject *object,
       
    62                   int debugId,
       
    63                   const QMetaProperty &prop,
       
    64                   QDeclarativeWatcher *parent = 0);
       
    65 
       
    66     QDeclarativeWatchProxy(int id,
       
    67                   QDeclarativeExpression *exp,
       
    68                   int debugId,
       
    69                   QDeclarativeWatcher *parent = 0);
       
    70 
       
    71 public slots:
       
    72     void notifyValueChanged();
       
    73 
       
    74 private:
       
    75     friend class QDeclarativeWatcher;
       
    76     int m_id;
       
    77     QDeclarativeWatcher *m_watch;
       
    78     QObject *m_object;
       
    79     int m_debugId;
       
    80     QMetaProperty m_property;
       
    81 
       
    82     QDeclarativeExpression *m_expr;
       
    83 };
       
    84 
       
    85 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
       
    86                              QDeclarativeExpression *exp,
       
    87                              int debugId,
       
    88                              QDeclarativeWatcher *parent)
       
    89 : QObject(parent), m_id(id), m_watch(parent), m_object(0), m_debugId(debugId), m_expr(exp)
       
    90 {
       
    91     QObject::connect(m_expr, SIGNAL(valueChanged()), this, SLOT(notifyValueChanged()));
       
    92 }
       
    93 
       
    94 QDeclarativeWatchProxy::QDeclarativeWatchProxy(int id,
       
    95                              QObject *object,
       
    96                              int debugId,
       
    97                              const QMetaProperty &prop,
       
    98                              QDeclarativeWatcher *parent)
       
    99 : QObject(parent), m_id(id), m_watch(parent), m_object(object), m_debugId(debugId), m_property(prop), m_expr(0)
       
   100 {
       
   101     static int refreshIdx = -1;
       
   102     if(refreshIdx == -1)
       
   103         refreshIdx = QDeclarativeWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
       
   104 
       
   105     if (prop.hasNotifySignal())
       
   106         QMetaObject::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
       
   107 }
       
   108 
       
   109 void QDeclarativeWatchProxy::notifyValueChanged()
       
   110 {
       
   111     QVariant v;
       
   112     if (m_expr)
       
   113         v = m_expr->evaluate();
       
   114     else
       
   115         v = m_property.read(m_object);
       
   116 
       
   117     emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
       
   118 }
       
   119 
       
   120 
       
   121 QDeclarativeWatcher::QDeclarativeWatcher(QObject *parent)
       
   122     : QObject(parent)
       
   123 {
       
   124 }
       
   125 
       
   126 bool QDeclarativeWatcher::addWatch(int id, quint32 debugId)
       
   127 {
       
   128     QObject *object = QDeclarativeDebugService::objectForId(debugId);
       
   129     if (object) {
       
   130         int propCount = object->metaObject()->propertyCount();
       
   131         for (int ii=0; ii<propCount; ii++)
       
   132             addPropertyWatch(id, object, debugId, object->metaObject()->property(ii));
       
   133         return true;
       
   134     }
       
   135     return false;
       
   136 }
       
   137 
       
   138 bool QDeclarativeWatcher::addWatch(int id, quint32 debugId, const QByteArray &property)
       
   139 {
       
   140     QObject *object = QDeclarativeDebugService::objectForId(debugId);
       
   141     if (object) {
       
   142         int index = object->metaObject()->indexOfProperty(property.constData());
       
   143         if (index >= 0) {
       
   144             addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
       
   145             return true;
       
   146         }
       
   147     }
       
   148     return false;
       
   149 }
       
   150 
       
   151 bool QDeclarativeWatcher::addWatch(int id, quint32 objectId, const QString &expr)
       
   152 {
       
   153     QObject *object = QDeclarativeDebugService::objectForId(objectId);
       
   154     QDeclarativeContext *context = qmlContext(object);
       
   155     if (context) {
       
   156         QDeclarativeExpression *exprObj = new QDeclarativeExpression(context, object, expr);
       
   157         exprObj->setNotifyOnValueChanged(true);
       
   158         QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, exprObj, objectId, this);
       
   159         exprObj->setParent(proxy);
       
   160         m_proxies[id].append(proxy);
       
   161         proxy->notifyValueChanged();
       
   162         return true;
       
   163     }
       
   164     return false;
       
   165 }
       
   166 
       
   167 void QDeclarativeWatcher::removeWatch(int id)
       
   168 {
       
   169     if (!m_proxies.contains(id))
       
   170         return;
       
   171 
       
   172     QList<QPointer<QDeclarativeWatchProxy> > proxies = m_proxies.take(id);
       
   173     qDeleteAll(proxies);
       
   174 }
       
   175 
       
   176 void QDeclarativeWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
       
   177 {
       
   178     QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, object, debugId, property, this);
       
   179     m_proxies[id].append(proxy);
       
   180 
       
   181     proxy->notifyValueChanged();
       
   182 }
       
   183 
       
   184 QT_END_NAMESPACE
       
   185 
       
   186 #include <qdeclarativewatcher.moc>