Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeinfo.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativeinfo.h"
43
44 #include "private/qdeclarativedata_p.h"
45 #include "qdeclarativecontext.h"
46 #include "private/qdeclarativecontext_p.h"
47 #include "private/qdeclarativemetatype_p.h"
48 #include "private/qdeclarativeengine_p.h"
49
50 #include <QCoreApplication>
51
52 QT_BEGIN_NAMESPACE
53
54 /*!
55     \fn QDeclarativeInfo qmlInfo(const QObject *object)
56     \relates QDeclarativeEngine
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 QDeclarativeInfoPrivate
84 {
85 public:
86     QDeclarativeInfoPrivate() : ref (1), object(0) {}
87
88     int ref;
89     const QObject *object;
90     QString buffer;
91     QList<QDeclarativeError> errors;
92 };
93
94 QDeclarativeInfo::QDeclarativeInfo(QDeclarativeInfoPrivate *p)
95 : QDebug(&p->buffer), d(p)
96 {
97     nospace();
98 }
99
100 QDeclarativeInfo::QDeclarativeInfo(const QDeclarativeInfo &other)
101 : QDebug(other), d(other.d)
102 {
103     d->ref++;
104 }
105
106 QDeclarativeInfo::~QDeclarativeInfo()
107 {
108     if (0 == --d->ref) {
109         QList<QDeclarativeError> errors = d->errors;
110
111         QDeclarativeEngine *engine = 0;
112
113         if (!d->buffer.isEmpty()) {
114             QDeclarativeError error;
115
116             QObject *object = const_cast<QObject *>(d->object);
117
118             if (object) {
119                 engine = qmlEngine(d->object);
120                 QString typeName;
121                 QDeclarativeType *type = QDeclarativeMetaType::qmlType(object->metaObject());
122                 if (type) {
123                     typeName = QLatin1String(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
134                 d->buffer.prepend(QLatin1String("QML ") + typeName + QLatin1String(": "));
135
136                 QDeclarativeData *ddata = QDeclarativeData::get(object, false);
137                 if (ddata && ddata->outerContext && !ddata->outerContext->url.isEmpty()) {
138                     error.setUrl(ddata->outerContext->url);
139                     error.setLine(ddata->lineNumber);
140                     error.setColumn(ddata->columnNumber);
141                 }
142             }
143
144             error.setDescription(d->buffer);
145
146             errors.prepend(error);
147         }
148
149         QDeclarativeEnginePrivate::warning(engine, errors);
150
151         delete d;
152     }
153 }
154
155 QDeclarativeInfo qmlInfo(const QObject *me)
156 {
157     QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
158     d->object = me;
159     return QDeclarativeInfo(d);
160 }
161
162 QDeclarativeInfo qmlInfo(const QObject *me, const QDeclarativeError &error)
163 {
164     QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
165     d->object = me;
166     d->errors << error;
167     return QDeclarativeInfo(d);
168 }
169
170 QDeclarativeInfo qmlInfo(const QObject *me, const QList<QDeclarativeError> &errors)
171 {
172     QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
173     d->object = me;
174     d->errors = errors;
175     return QDeclarativeInfo(d);
176 }
177
178
179 QT_END_NAMESPACE