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 QtDeclarative 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 "qdeclarativeinfo.h"
44 #include "qdeclarativedata_p.h"
45 #include "qdeclarativecontext.h"
46 #include "qdeclarativecontext_p.h"
47 #include "qdeclarativemetatype_p.h"
48 #include "qdeclarativeengine_p.h"
50 #include <QCoreApplication>
55 \fn QDeclarativeInfo qmlInfo(const QObject *object)
56 \relates QDeclarativeEngine
58 Prints warning messages that include the file and line number for the
59 specified QML \a object.
61 When QML types display warning messages, it improves traceability
62 if they include the QML file and line number on which the
63 particular instance was instantiated.
65 To include the file and line number, an object must be passed. If
66 the file and line number is not available for that instance
67 (either it was not instantiated by the QML engine or location
68 information is disabled), "unknown location" will be used instead.
73 qmlInfo(object) << tr("component property is a write-once property");
79 QML MyCustomType (unknown location): component property is a write-once property
83 class QDeclarativeInfoPrivate
86 QDeclarativeInfoPrivate() : ref (1), object(0) {}
89 const QObject *object;
91 QList<QDeclarativeError> errors;
94 QDeclarativeInfo::QDeclarativeInfo(QDeclarativeInfoPrivate *p)
95 : QDebug(&p->buffer), d(p)
100 QDeclarativeInfo::QDeclarativeInfo(const QDeclarativeInfo &other)
101 : QDebug(other), d(other.d)
106 QDeclarativeInfo::~QDeclarativeInfo()
109 QList<QDeclarativeError> errors = d->errors;
111 QDeclarativeEngine *engine = 0;
113 if (!d->buffer.isEmpty()) {
114 QDeclarativeError error;
116 QObject *object = const_cast<QObject *>(d->object);
119 engine = qmlEngine(d->object);
121 QDeclarativeType *type = QDeclarativeMetaType::qmlType(object->metaObject());
123 typeName = type->qmlTypeName();
124 int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
126 typeName = typeName.mid(lastSlash+1);
128 typeName = QString::fromUtf8(object->metaObject()->className());
129 int marker = typeName.indexOf(QLatin1String("_QMLTYPE_"));
131 typeName = typeName.left(marker);
133 marker = typeName.indexOf(QLatin1String("_QML_"));
135 typeName = typeName.left(marker);
136 typeName += QLatin1Char('*');
137 type = QDeclarativeMetaType::qmlType(QMetaType::type(typeName.toLatin1()));
139 typeName = type->qmlTypeName();
140 int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
142 typeName = typeName.mid(lastSlash+1);
147 d->buffer.prepend(QLatin1String("QML ") + typeName + QLatin1String(": "));
149 QDeclarativeData *ddata = QDeclarativeData::get(object, false);
150 if (ddata && ddata->outerContext && !ddata->outerContext->url.isEmpty()) {
151 error.setUrl(ddata->outerContext->url);
152 error.setLine(ddata->lineNumber);
153 error.setColumn(ddata->columnNumber);
157 error.setDescription(d->buffer);
159 errors.prepend(error);
162 QDeclarativeEnginePrivate::warning(engine, errors);
168 QDeclarativeInfo qmlInfo(const QObject *me)
170 QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
172 return QDeclarativeInfo(d);
175 QDeclarativeInfo qmlInfo(const QObject *me, const QDeclarativeError &error)
177 QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
180 return QDeclarativeInfo(d);
183 QDeclarativeInfo qmlInfo(const QObject *me, const QList<QDeclarativeError> &errors)
185 QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
188 return QDeclarativeInfo(d);