Qt 4.8
qdeclarativewatcher.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
49 #include "private/qdeclarativeproperty_p.h"
50 #include "private/qdeclarativevaluetype_p.h"
51 
52 #include <QtCore/qmetaobject.h>
53 #include <QtCore/qdebug.h>
54 
56 
57 
59 {
60  Q_OBJECT
61 public:
63  QObject *object,
64  int debugId,
65  const QMetaProperty &prop,
67 
70  int debugId,
72 
73 public slots:
74  void notifyValueChanged();
75 
76 private:
77  friend class QDeclarativeWatcher;
78  int m_id;
81  int m_debugId;
83 
85 };
86 
89  int debugId,
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 
97  QObject *object,
98  int debugId,
99  const QMetaProperty &prop,
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())
109 }
110 
112 {
113  QVariant v;
114  if (m_expr)
115  v = m_expr->evaluate();
117  v = m_property.read(m_object);
118 
120 }
121 
122 
124  : QObject(parent)
125 {
126 }
127 
129 {
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 
141 {
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 {
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 
170 {
171  if (!m_proxies.contains(id))
172  return;
173 
175  qDeleteAll(proxies);
176 }
177 
179 {
180  QDeclarativeWatchProxy *proxy = new QDeclarativeWatchProxy(id, object, debugId, property, this);
181  m_proxies[id].append(proxy);
182 
183  proxy->notifyValueChanged();
184 }
185 
187 
188 #include <qdeclarativewatcher.moc>
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QDeclarativeWatcher * m_watch
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QObject * objectForId(int)
Returns the object for unique id.
int notifySignalIndex() const
Returns the index of the property change notifying signal if one was specified, otherwise returns -1...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
Definition: qobject.h:128
void addPropertyWatch(int id, QObject *object, quint32 objectId, const QMetaProperty &property)
static bool connect(QObject *sender, int signal_index, const QObject *receiver, int method_index, int type=0, int *types=0)
Connect sender signal_index to receiver method_index with the specified type and types.
QVariant evaluate(bool *valueIsUndefined=0)
Evaulates the expression, returning the result of the evaluation, or an invalid QVariant if the expre...
The QString class provides a Unicode character string.
Definition: qstring.h:83
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it...
Definition: qhash.h:807
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
Q_DECLARATIVE_EXPORT QDeclarativeContext * qmlContext(const QObject *)
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
QHash< int, QList< QPointer< QDeclarativeWatchProxy > > > m_proxies
bool addWatch(int id, quint32 objectId)
void setNotifyOnValueChanged(bool)
Sets whether the valueChanged() signal is emitted when the expression&#39;s evaluated value changes...
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int userType() const
Returns this property&#39;s user type.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define emit
Definition: qobjectdefs.h:76
#define Q_OBJECT
Definition: qobjectdefs.h:157
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QDeclarativeExpression * m_expr
int indexOfMethod(const char *method) const
Finds method and returns its index; otherwise returns -1.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
unsigned int quint32
Definition: qglobal.h:938
void propertyChanged(int id, int objectId, const QMetaProperty &property, const QVariant &value)
QVariant read(const QObject *obj) const
Reads the property&#39;s value from the given object.
friend class QDeclarativeWatchProxy
The QDeclarativeContext class defines a context within a QML engine.
quint16 index
QVariant property(const char *name) const
Returns the value of the object&#39;s name property.
Definition: qobject.cpp:3807
The QMetaProperty class provides meta-data about a property.
Definition: qmetaobject.h:176
QDeclarativeWatchProxy(int id, QObject *object, int debugId, const QMetaProperty &prop, QDeclarativeWatcher *parent=0)
The QDeclarativeExpression class evaluates JavaScript in a QML context.
#define slots
Definition: qobjectdefs.h:68
bool hasNotifySignal() const
Returns true if this property has a corresponding change notify signal; otherwise returns false...
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62