Qt 4.8
qdeclarativetypenamescriptclass.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/qdeclarativetypenamescriptclass_p.h"
43 
44 #include "private/qdeclarativeengine_p.h"
45 #include "private/qdeclarativetypenamecache_p.h"
46 
48 
53  }
56  }
57 
62 };
63 
65 : QScriptDeclarativeClass(QDeclarativeEnginePrivate::getScriptEngine(bindEngine)),
66  engine(bindEngine), object(0), type(0)
67 {
68 }
69 
71 {
72 }
73 
75 {
77 
78  return QScriptDeclarativeClass::newObject(scriptEngine, this, new TypeNameData(object, type, mode));
79 }
80 
82 {
84 
85  return QScriptDeclarativeClass::newObject(scriptEngine, this, new TypeNameData(object, ns, mode));
86 }
87 
88 QScriptClass::QueryFlags
90  QScriptClass::QueryFlags flags)
91 {
92  Q_UNUSED(flags);
93 
94  TypeNameData *data = (TypeNameData *)obj;
95 
96  object = 0;
97  type = 0;
99 
100  if (data->typeNamespace) {
101 
103  if (d && d->type) {
104  type = d->type;
106  } else {
107  return 0;
108  }
109 
110  } else if (data->type) {
111 
112  if (startsWithUpper(name)) {
113  QString strName = toString(name);
114  // Must be an enum
115  if (data->mode == IncludeEnums) {
116  // ### Optimize
117  QByteArray enumName = strName.toUtf8();
118  const QMetaObject *metaObject = data->type->baseMetaObject();
119  for (int ii = metaObject->enumeratorCount() - 1; ii >= 0; --ii) {
120  QMetaEnum e = metaObject->enumerator(ii);
121  int value = e.keyToValue(enumName.constData());
122  if (value != -1) {
123  enumValue = value;
125  }
126  }
127  }
128  return 0;
129  } else if (data->object) {
130  // Must be an attached property
132  if (!object) return 0;
133  return ep->objectClass->queryProperty(object, name, flags, 0);
134  }
135 
136  }
137 
138  return 0;
139 }
140 
143 {
146  if (type) {
147  return Value(scriptEngine, newObject(((TypeNameData *)obj)->object, type, ((TypeNameData *)obj)->mode));
148  } else if (object) {
149  return ep->objectClass->property(object, name);
150  } else {
151  return Value(scriptEngine, enumValue);
152  }
153 }
154 
156 {
157  Q_ASSERT(object);
158  Q_ASSERT(!type);
159 
161  ep->objectClass->setProperty(object, n, v, context());
162 }
163 
165 
static QScriptEngine * getScriptEngine(QDeclarativeEngine *e)
double d
Definition: qnumeric_p.h:62
TypeNameData(QObject *o, QDeclarativeTypeNameCache *n, QDeclarativeTypeNameScriptClass::TypeNameMode m)
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
TypeNameData(QObject *o, QDeclarativeType *t, QDeclarativeTypeNameScriptClass::TypeNameMode m)
virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags flags)
The QMetaEnum class provides meta-data about an enumerator.
Definition: qmetaobject.h:147
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool startsWithUpper(const Identifier &)
static QScriptValue newObject(QScriptEngine *, QScriptDeclarativeClass *, Object *)
QScriptClass::QueryFlags queryProperty(QObject *, const Identifier &, QScriptClass::QueryFlags flags, QDeclarativeContextData *evalContext, QueryHints hints=0)
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
virtual void setProperty(Object *, const Identifier &name, const QScriptValue &)
const QMetaObject * baseMetaObject() const
int attachedPropertiesId() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
#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)
QDeclarativeTypeNameScriptClass::TypeNameMode mode
The QScriptDeclarativeClass::Value class acts as a container for JavaScript data types.
Q_DECLARATIVE_EXPORT QObject * qmlAttachedPropertiesObjectById(int, const QObject *, bool create=true)
The QScriptEngine class provides an environment for evaluating Qt Script code.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QScriptValue newObject(QObject *, QDeclarativeType *, TypeNameMode=IncludeEnums)
const char * name
QScriptEngine * engine() const
static const char * data(const QByteArray &arr)
int enumeratorCount() const
Returns the number of enumerators in this class.
QDeclarativeTypeNameCache * typeNamespace
QDeclarativeObjectScriptClass * objectClass
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
virtual Value property(Object *, const Identifier &)
The QDeclarativeEngine class provides an environment for instantiating QML components.
QMetaEnum enumerator(int index) const
Returns the meta-data for the enumerator with the given index.
int keyToValue(const char *key) const
Returns the integer value of the given enumeration key, or -1 if key is not defined.
void setProperty(QObject *, const Identifier &name, const QScriptValue &, QScriptContext *context, QDeclarativeContextData *evalContext=0)
Data * data(const QString &) const
QScriptContext * context() const
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QString toString(const Identifier &)
Value property(QObject *, const Identifier &)