1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmlcustomparser_p.h"
43 #include "qqmlcustomparser_p_p.h"
45 #include "qqmlcompiler_p.h"
47 #include <QtCore/qdebug.h>
51 using namespace QQmlScript;
54 \class QQmlCustomParser
55 \brief The QQmlCustomParser class allows you to add new arbitrary types to QML.
58 By subclassing QQmlCustomParser, you can add a parser for
59 building a particular type.
61 The subclass must implement compile() and setCustomData(), and register
62 itself in the meta type system by calling the macro:
65 QML_REGISTER_CUSTOM_TYPE(Module, MajorVersion, MinorVersion, Name, TypeClass, ParserClass)
70 \fn QByteArray QQmlCustomParser::compile(const QList<QQmlCustomParserProperty> & properties)
72 The custom parser processes \a properties, and returns
73 a QByteArray containing data meaningful only to the
74 custom parser; the type engine will pass this same data to
75 setCustomData() when making an instance of the data.
77 Errors must be reported via the error() functions.
79 The QByteArray may be cached between executions of the system, so
80 it must contain correctly-serialized data (not, for example,
81 pointers to stack objects).
85 \fn void QQmlCustomParser::setCustomData(QObject *object, const QByteArray &data)
87 This function sets \a object to have the properties defined
88 by \a data, which is a block of data previously returned by a call
91 Errors should be reported using qmlInfo(object).
93 The \a object will be an instance of the TypeClass specified by QML_REGISTER_CUSTOM_TYPE.
97 QQmlCustomParserNodePrivate::fromObject(QQmlScript::Object *root)
99 QQmlCustomParserNode rootNode;
100 if (root->typeReference)
101 rootNode.d->name = root->typeReference->name;
102 rootNode.d->location = root->location.start;
104 for (Property *p = root->properties.first(); p; p = root->properties.next(p)) {
105 rootNode.d->properties << fromProperty(p);
108 if (root->defaultProperty)
109 rootNode.d->properties << fromProperty(root->defaultProperty);
114 QQmlCustomParserProperty
115 QQmlCustomParserNodePrivate::fromProperty(QQmlScript::Property *p)
117 QQmlCustomParserProperty prop;
118 prop.d->name = p->name().toString();
119 prop.d->isList = p->values.isMany();
120 prop.d->location = p->location.start;
123 QQmlCustomParserNode node = fromObject(p->value);
124 QList<QQmlCustomParserProperty> props = node.properties();
125 for (int ii = 0; ii < props.count(); ++ii)
126 prop.d->values << QVariant::fromValue(props.at(ii));
128 for (QQmlScript::Value *v = p->values.first(); v; v = p->values.next(v)) {
129 v->type = QQmlScript::Value::Literal;
132 QQmlCustomParserNode node = fromObject(v->object);
133 prop.d->values << QVariant::fromValue(node);
135 prop.d->values << QVariant::fromValue(v->value);
144 QQmlCustomParserNode::QQmlCustomParserNode()
145 : d(new QQmlCustomParserNodePrivate)
149 QQmlCustomParserNode::QQmlCustomParserNode(const QQmlCustomParserNode &other)
150 : d(new QQmlCustomParserNodePrivate)
155 QQmlCustomParserNode &QQmlCustomParserNode::operator=(const QQmlCustomParserNode &other)
157 d->name = other.d->name;
158 d->properties = other.d->properties;
159 d->location = other.d->location;
163 QQmlCustomParserNode::~QQmlCustomParserNode()
168 QString QQmlCustomParserNode::name() const
173 QList<QQmlCustomParserProperty> QQmlCustomParserNode::properties() const
175 return d->properties;
178 QQmlScript::Location QQmlCustomParserNode::location() const
183 QQmlCustomParserProperty::QQmlCustomParserProperty()
184 : d(new QQmlCustomParserPropertyPrivate)
188 QQmlCustomParserProperty::QQmlCustomParserProperty(const QQmlCustomParserProperty &other)
189 : d(new QQmlCustomParserPropertyPrivate)
194 QQmlCustomParserProperty &QQmlCustomParserProperty::operator=(const QQmlCustomParserProperty &other)
196 d->name = other.d->name;
197 d->isList = other.d->isList;
198 d->values = other.d->values;
199 d->location = other.d->location;
203 QQmlCustomParserProperty::~QQmlCustomParserProperty()
208 QString QQmlCustomParserProperty::name() const
213 bool QQmlCustomParserProperty::isList() const
218 QQmlScript::Location QQmlCustomParserProperty::location() const
223 QList<QVariant> QQmlCustomParserProperty::assignedValues() const
228 void QQmlCustomParser::clearErrors()
234 Reports an error with the given \a description.
236 This can only be used during the compile() step. For errors during setCustomData(), use qmlInfo().
238 An error is generated referring to the position of the element in the source file.
240 void QQmlCustomParser::error(const QString& description)
244 QString exceptionDescription;
245 error.setLine(object->location.start.line);
246 error.setColumn(object->location.start.column);
247 error.setDescription(description);
252 Reports an error in parsing \a prop, with the given \a description.
254 An error is generated referring to the position of \a node in the source file.
256 void QQmlCustomParser::error(const QQmlCustomParserProperty& prop, const QString& description)
259 QString exceptionDescription;
260 error.setLine(prop.location().line);
261 error.setColumn(prop.location().column);
262 error.setDescription(description);
267 Reports an error in parsing \a node, with the given \a description.
269 An error is generated referring to the position of \a node in the source file.
271 void QQmlCustomParser::error(const QQmlCustomParserNode& node, const QString& description)
274 QString exceptionDescription;
275 error.setLine(node.location().line);
276 error.setColumn(node.location().column);
277 error.setDescription(description);
282 If \a script is a simple enumeration expression (eg. Text.AlignLeft),
283 returns the integer equivalent (eg. 1), and sets \a ok to true.
285 Otherwise sets \a ok to false.
287 A valid \a ok must be provided, or the function will assert.
289 int QQmlCustomParser::evaluateEnum(const QByteArray& script, bool *ok) const
291 Q_ASSERT_X(ok, "QQmlCustomParser::evaluateEnum", "ok must not be a null pointer");
293 int dot = script.indexOf('.');
297 return compiler->evaluateEnum(QString::fromUtf8(script.left(dot)), script.mid(dot+1), ok);
301 Resolves \a name to a type, or 0 if it is not a type. This can be used
302 to type-check object nodes.
304 const QMetaObject *QQmlCustomParser::resolveType(const QString& name) const
306 return compiler->resolveType(name);
310 Rewrites \a value and returns an identifier that can be
311 used to construct the binding later. \a name
312 is used as the name of the rewritten function.
314 QQmlBinding::Identifier QQmlCustomParser::rewriteBinding(const QQmlScript::Variant &value, const QString& name)
316 return compiler->rewriteBinding(value, name);
320 Returns a rewritten \a handler. \a name
321 is used as the name of the rewritten function.
323 QString QQmlCustomParser::rewriteSignalHandler(const QQmlScript::Variant &value, const QString &name)
325 return compiler->rewriteSignalHandler(value , name);