Improve documentation.
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlinfo.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml 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 "qqmlinfo.h"
43
44 #include "qqmldata_p.h"
45 #include "qqmlcontext.h"
46 #include "qqmlcontext_p.h"
47 #include "qqmlmetatype_p.h"
48 #include "qqmlengine_p.h"
49
50 #include <QCoreApplication>
51
52 QT_BEGIN_NAMESPACE
53
54 /*!
55     \fn QQmlInfo qmlInfo(const QObject *object)
56     \relates QQmlEngine
57
58     Prints warning messages that include the file and line number for the
59     specified QML \a object.
60
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.
64     
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.
69
70     For example, 
71
72     \code
73     qmlInfo(object) << tr("component property is a write-once property");
74     \endcode
75
76     prints
77
78     \code
79     QML MyCustomType (unknown location): component property is a write-once property
80     \endcode
81 */
82
83 class QQmlInfoPrivate
84 {
85 public:
86     QQmlInfoPrivate() : ref (1), object(0) {}
87
88     int ref;
89     const QObject *object;
90     QString buffer;
91     QList<QQmlError> errors;
92 };
93
94 QQmlInfo::QQmlInfo(QQmlInfoPrivate *p)
95 : QDebug(&p->buffer), d(p)
96 {
97     nospace();
98 }
99
100 QQmlInfo::QQmlInfo(const QQmlInfo &other)
101 : QDebug(other), d(other.d)
102 {
103     d->ref++;
104 }
105
106 QQmlInfo::~QQmlInfo()
107 {
108     if (0 == --d->ref) {
109         QList<QQmlError> errors = d->errors;
110
111         QQmlEngine *engine = 0;
112
113         if (!d->buffer.isEmpty()) {
114             QQmlError error;
115
116             QObject *object = const_cast<QObject *>(d->object);
117
118             if (object) {
119                 engine = qmlEngine(d->object);
120                 QString typeName;
121                 QQmlType *type = QQmlMetaType::qmlType(object->metaObject());
122                 if (type) {
123                     typeName = type->qmlTypeName();
124                     int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
125                     if (lastSlash != -1)
126                         typeName = typeName.mid(lastSlash+1);
127                 } else {
128                     typeName = QString::fromUtf8(object->metaObject()->className());
129                     int marker = typeName.indexOf(QLatin1String("_QMLTYPE_"));
130                     if (marker != -1)
131                         typeName = typeName.left(marker);
132
133                     marker = typeName.indexOf(QLatin1String("_QML_"));
134                     if (marker != -1) {
135                         typeName = typeName.left(marker);
136                         typeName += QLatin1Char('*');
137                         type = QQmlMetaType::qmlType(QMetaType::type(typeName.toLatin1()));
138                         if (type) {
139                             typeName = type->qmlTypeName();
140                             int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
141                             if (lastSlash != -1)
142                                 typeName = typeName.mid(lastSlash+1);
143                         }
144                     }
145                 }
146
147                 d->buffer.prepend(QLatin1String("QML ") + typeName + QLatin1String(": "));
148
149                 QQmlData *ddata = QQmlData::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);
154                 }
155             }
156
157             error.setDescription(d->buffer);
158
159             errors.prepend(error);
160         }
161
162         QQmlEnginePrivate::warning(engine, errors);
163
164         delete d;
165     }
166 }
167
168 QQmlInfo qmlInfo(const QObject *me)
169 {
170     QQmlInfoPrivate *d = new QQmlInfoPrivate;
171     d->object = me;
172     return QQmlInfo(d);
173 }
174
175 QQmlInfo qmlInfo(const QObject *me, const QQmlError &error)
176 {
177     QQmlInfoPrivate *d = new QQmlInfoPrivate;
178     d->object = me;
179     d->errors << error;
180     return QQmlInfo(d);
181 }
182
183 QQmlInfo qmlInfo(const QObject *me, const QList<QQmlError> &errors)
184 {
185     QQmlInfoPrivate *d = new QQmlInfoPrivate;
186     d->object = me;
187     d->errors = errors;
188     return QQmlInfo(d);
189 }
190
191
192 QT_END_NAMESPACE