Qt 4.8
qdeclarativevaluetypescriptclass.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/qdeclarativevaluetypescriptclass_p.h"
43 
44 #include "private/qdeclarativebinding_p.h"
45 #include "private/qdeclarativeproperty_p.h"
46 #include "private/qdeclarativeengine_p.h"
47 #include "private/qdeclarativeguard_p.h"
48 
49 #include <QtScript/qscriptcontextinfo.h>
50 
52 
54  enum Type { Reference, Copy };
58 };
59 
63  int property;
64 };
65 
69 };
70 
72 : QScriptDeclarativeClass(QDeclarativeEnginePrivate::getScriptEngine(bindEngine)), engine(bindEngine)
73 {
74 }
75 
77 {
78 }
79 
81 {
83  ref->type = type;
84  ref->object = object;
85  ref->property = coreIndex;
87  return QScriptDeclarativeClass::newObject(scriptEngine, this, ref);
88 }
89 
91 {
93  copy->type = type;
94  copy->value = v;
96  return QScriptDeclarativeClass::newObject(scriptEngine, this, copy);
97 }
98 
99 QScriptClass::QueryFlags
101  QScriptClass::QueryFlags)
102 {
104 
105  m_lastIndex = -1;
106 
107  QByteArray propName = toString(name).toUtf8();
108 
109  m_lastIndex = o->type->metaObject()->indexOfProperty(propName.constData());
110  if (m_lastIndex == -1)
111  return 0;
112 
113  QScriptClass::QueryFlags rv = 0;
114 
117 
118  if (!ref->object)
119  return 0;
120 
122 
124  if (prop.isWritable())
126  } else {
128  }
129 
130  return rv;
131 }
132 
134 {
136 
137  QVariant rv;
140 
142  ref->type->read(ref->object, ref->property);
143  rv = p.read(ref->type);
144  } else {
145  QDeclarativeValueTypeCopy *copy = static_cast<QDeclarativeValueTypeCopy *>(obj);
146 
148  copy->type->setValue(copy->value);
149  rv = p.read(copy->type);
150  }
151 
153  return Value(scriptEngine, static_cast<QDeclarativeEnginePrivate *>(QObjectPrivate::get(engine))->scriptValueFromVariant(rv));
154 }
155 
157  const QScriptValue &value)
158 {
160 
162 
165 
166  ref->type->read(ref->object, ref->property);
168 
169  QDeclarativeBinding *newBinding = 0;
170  if (value.isFunction() && !value.isRegExp()) {
172 
175  cacheData.propType = ref->object->metaObject()->property(ref->property).userType();
176  cacheData.coreIndex = ref->property;
177 
179  valueTypeData.valueTypeCoreIdx = m_lastIndex;
180  valueTypeData.valueTypePropType = p.userType();
181 
182  newBinding = new QDeclarativeBinding(value, ref->object, ctxt);
183  QScriptContextInfo ctxtInfo(context());
184  newBinding->setSourceLocation(ctxtInfo.fileName(), ctxtInfo.functionStartLineNumber());
185  QDeclarativeProperty prop = QDeclarativePropertyPrivate::restore(cacheData, valueTypeData, ref->object, ctxt);
186  newBinding->setTarget(prop);
187  if (newBinding->expression().contains(QLatin1String("this")))
189  }
190 
191  QDeclarativeAbstractBinding *delBinding =
193  if (delBinding)
194  delBinding->destroy();
195 
196  if (p.isEnumType() && (QMetaType::Type)v.type() == QMetaType::Double)
197  v = v.toInt();
198  p.write(ref->type, v);
199  ref->type->write(ref->object, ref->property, 0);
200 
201  } else {
202  QDeclarativeValueTypeCopy *copy = static_cast<QDeclarativeValueTypeCopy *>(obj);
203  copy->type->setValue(copy->value);
205  p.write(copy->type, v);
206  copy->value = copy->type->value();
207  }
208 }
209 
211 {
213 
216 
217  if (ok) *ok = true;
218 
219  if (ref->object) {
220  ref->type->read(ref->object, ref->property);
221  return ref->type->value();
222  }
223  } else {
224  QDeclarativeValueTypeCopy *copy = static_cast<QDeclarativeValueTypeCopy *>(obj);
225 
226  if (ok) *ok = true;
227 
228  return copy->value;
229  }
230 
231  return QVariant();
232 }
233 
235 {
236  Q_ASSERT(scriptClass(value) == this);
237 
238  return toVariant(object(value), 0);
239 }
240 
242 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
static QScriptEngine * getScriptEngine(QDeclarativeEngine *e)
virtual void setProperty(Object *, const Identifier &name, const QScriptValue &)
virtual QVariant value()=0
int type
Definition: qmetatype.cpp:239
bool isWritable() const
Returns true if this property is writable; otherwise returns false.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual Value property(Object *, const Identifier &)
bool isFunction() const
Returns true if this QScriptValue is a function; otherwise returns false.
static QScriptValue newObject(QScriptEngine *, QScriptDeclarativeClass *, Object *)
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setTarget(const QDeclarativeProperty &)
virtual void destroy(DestroyMode mode=DisconnectBinding)
Destroy the binding.
virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags flags)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static Object * object(const QScriptValue &)
virtual void write(QObject *, int, QDeclarativePropertyPrivate::WriteFlags flags)=0
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static QDeclarativeEnginePrivate * get(QDeclarativeEngine *e)
int indexOfProperty(const char *name) const
Finds property name and returns its index; otherwise returns -1.
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
The QScriptDeclarativeClass::Value class acts as a container for JavaScript data types.
QVariant scriptValueToVariant(const QScriptValue &, int hint=QVariant::Invalid)
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
QString fileName() const
Returns the name of the file where the code being executed was defined, if available; otherwise retur...
The QScriptEngine class provides an environment for evaluating Qt Script code.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual QVariant toVariant(Object *, bool *ok=0)
bool isEnumType() const
Returns true if the property&#39;s type is an enumeration value; otherwise returns false.
int userType() const
Returns this property&#39;s user type.
virtual void setValue(QVariant)=0
const char * name
QScriptEngine * engine() const
Type
These are the built-in types supported by QMetaType:
Definition: qmetatype.h:64
static QDeclarativeAbstractBinding * setBinding(QObject *, int coreIndex, int valueTypeIndex, QDeclarativeAbstractBinding *, WriteFlags flags=DontRemoveBinding)
void setEvaluateFlags(EvaluateFlags flags)
int functionStartLineNumber() const
Returns the line number where the definition of the called function starts, or -1 if the line number ...
QScriptValue newObject(QObject *object, int coreIndex, QDeclarativeValueType *)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
The QScriptContextInfo class provides additional information about a QScriptContext.
The QDeclarativeEngine class provides an environment for instantiating QML components.
bool isRegExp() const
Returns true if this QScriptValue is an object of the RegExp class; otherwise returns false...
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
static QDeclarativeProperty restore(const QByteArray &, QObject *, QDeclarativeContextData *)
virtual QString expression() const
QVariant read(const QObject *obj) const
Reads the property&#39;s value from the given object.
void setSourceLocation(const QString &fileName, int line)
Set the location of this expression to line of url.
The QDeclarativeProperty class abstracts accessing properties on objects created from QML...
static QScriptDeclarativeClass * scriptClass(const QScriptValue &)
The QMetaProperty class provides meta-data about a property.
Definition: qmetaobject.h:176
bool write(QObject *obj, const QVariant &value) const
Writes value as the property&#39;s value to the given object.
QScriptContext * context() const
QDeclarativeContextData * getContext(QScriptContext *)
Returns the QDeclarativeContext for the executing QScript ctxt.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QString toString(const Identifier &)
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.
EvaluateFlags evaluateFlags() const
virtual void read(QObject *, int)=0