Qt 4.8
qdeclarativecustomparser.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/qdeclarativecustomparser_p.h"
43 #include "private/qdeclarativecustomparser_p_p.h"
44 
45 #include "private/qdeclarativeparser_p.h"
46 #include "private/qdeclarativecompiler_p.h"
47 
48 #include <QtCore/qdebug.h>
49 
51 
52 using namespace QDeclarativeParser;
53 
73 /*
74  \fn QByteArray QDeclarativeCustomParser::compile(const QList<QDeclarativeCustomParserProperty> & properties)
75 
76  The custom parser processes \a properties, and returns
77  a QByteArray containing data meaningful only to the
78  custom parser; the type engine will pass this same data to
79  setCustomData() when making an instance of the data.
80 
81  Errors must be reported via the error() functions.
82 
83  The QByteArray may be cached between executions of the system, so
84  it must contain correctly-serialized data (not, for example,
85  pointers to stack objects).
86 */
87 
88 /*
89  \fn void QDeclarativeCustomParser::setCustomData(QObject *object, const QByteArray &data)
90 
91  This function sets \a object to have the properties defined
92  by \a data, which is a block of data previously returned by a call
93  to compile().
94 
95  Errors should be reported using qmlInfo(object).
96 
97  The \a object will be an instance of the TypeClass specified by QML_REGISTER_CUSTOM_TYPE.
98 */
99 
102 {
104  rootNode.d->name = root->typeName;
105  rootNode.d->location = root->location.start;
106 
107  for(QHash<QByteArray, Property *>::Iterator iter = root->properties.begin();
108  iter != root->properties.end();
109  ++iter) {
110 
111  Property *p = *iter;
112 
113  rootNode.d->properties << fromProperty(p);
114  }
115 
116  if (root->defaultProperty)
117  rootNode.d->properties << fromProperty(root->defaultProperty);
118 
119  return rootNode;
120 }
121 
124 {
126  prop.d->name = p->name;
127  prop.d->isList = (p->values.count() > 1);
128  prop.d->location = p->location.start;
129 
130  if (p->value) {
131  QDeclarativeCustomParserNode node = fromObject(p->value);
133  for (int ii = 0; ii < props.count(); ++ii)
134  prop.d->values << QVariant::fromValue(props.at(ii));
135  } else {
136  for(int ii = 0; ii < p->values.count(); ++ii) {
137  QDeclarativeParser::Value *v = p->values.at(ii);
139 
140  if(v->object) {
141  QDeclarativeCustomParserNode node = fromObject(v->object);
142  prop.d->values << QVariant::fromValue(node);
143  } else {
144  prop.d->values << QVariant::fromValue(v->value);
145  }
146 
147  }
148  }
149 
150  return prop;
151 }
152 
155 {
156 }
157 
160 {
161  *this = other;
162 }
163 
165 {
166  d->name = other.d->name;
167  d->properties = other.d->properties;
168  d->location = other.d->location;
169  return *this;
170 }
171 
173 {
174  delete d; d = 0;
175 }
176 
178 {
179  return d->name;
180 }
181 
183 {
184  return d->properties;
185 }
186 
188 {
189  return d->location;
190 }
191 
194 {
195 }
196 
199 {
200  *this = other;
201 }
202 
204 {
205  d->name = other.d->name;
206  d->isList = other.d->isList;
207  d->values = other.d->values;
208  d->location = other.d->location;
209  return *this;
210 }
211 
213 {
214  delete d; d = 0;
215 }
216 
218 {
219  return d->name;
220 }
221 
223 {
224  return d->isList;
225 }
226 
228 {
229  return d->location;
230 }
231 
233 {
234  return d->values;
235 }
236 
238 {
239  exceptions.clear();
240 }
241 
249 void QDeclarativeCustomParser::error(const QString& description)
250 {
251  Q_ASSERT(object);
253  QString exceptionDescription;
254  error.setLine(object->location.start.line);
255  error.setColumn(object->location.start.column);
256  error.setDescription(description);
257  exceptions << error;
258 }
259 
266 {
268  QString exceptionDescription;
269  error.setLine(prop.location().line);
270  error.setColumn(prop.location().column);
271  error.setDescription(description);
272  exceptions << error;
273 }
274 
281 {
283  QString exceptionDescription;
284  error.setLine(node.location().line);
285  error.setColumn(node.location().column);
286  error.setDescription(description);
287  exceptions << error;
288 }
289 
297 {
298  return compiler->evaluateEnum(script);
299 }
300 
306 {
307  return compiler->resolveType(name);
308 }
309 
316 {
317  return compiler->rewriteBinding(expression, name);
318 }
319 
double d
Definition: qnumeric_p.h:62
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
QList< QDeclarativeCustomParserProperty > properties() const
QDeclarativeBinding::Identifier rewriteBinding(const QString &, const QByteArray &)
Rewrites expression and returns an identifier that can be used to construct the binding later...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QList< QDeclarativeCustomParserProperty > properties
void setDescription(const QString &)
Sets the error description.
static QDeclarativeCustomParserNode fromObject(QDeclarativeParser::Object *)
QDeclarativeParser::Location location() const
const QMetaObject * resolveType(const QByteArray &) const
Resolves name to a type, or 0 if it is not a type.
#define error(msg)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setColumn(int)
Sets the error column number.
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QDeclarativeParser::Location location() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void error(const QString &description)
Reports an error with the given description.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QDeclarativeError class encapsulates a QML error.
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
void setLine(int)
Sets the error line number.
static QDeclarativeCustomParserProperty fromProperty(QDeclarativeParser::Property *)
QDeclarativeCustomParserProperty & operator=(const QDeclarativeCustomParserProperty &)
QDeclarativeCustomParserNode & operator=(const QDeclarativeCustomParserNode &)
QDeclarativeCustomParserPropertyPrivate * d
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
int evaluateEnum(const QByteArray &) const
If script is a simply enum expression (eg.
QDeclarativeCustomParserNodePrivate * d
QHash< QByteArray, Property * > properties