1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 #include <QtCore/qdebug.h>
45 #include <QtCore/qfile.h>
46 #include <QtCore/qstringlist.h>
53 \brief The QmlError class encapsulates a QML error.
55 QmlError includes a textual description of the error, as well
56 as location information (the file, line, and column). The toString()
57 method creates a single-line, human-readable string containing all of
58 this information, for example:
60 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
63 You can use qDebug() or qWarning() to output errors to the console. This method
64 will attempt to open the file indicated by the error
65 and include additional contextual information.
67 file:///home/user/test.qml:7:8: Invalid property assignment: double expected
72 \sa QmlView::errors(), QmlComponent::errors()
85 QmlErrorPrivate::QmlErrorPrivate()
86 : line(-1), column(-1)
91 Creates an empty error object.
99 Creates a copy of \a other.
101 QmlError::QmlError(const QmlError &other)
108 Assigns \a other to this error object.
110 QmlError &QmlError::operator=(const QmlError &other)
116 if (!d) d = new QmlErrorPrivate;
117 d->url = other.d->url;
118 d->description = other.d->description;
119 d->line = other.d->line;
120 d->column = other.d->column;
128 QmlError::~QmlError()
134 Returns true if this error is valid, otherwise false.
136 bool QmlError::isValid() const
142 Returns the url for the file that caused this error.
144 QUrl QmlError::url() const
146 if (d) return d->url;
151 Sets the \a url for the file that caused this error.
153 void QmlError::setUrl(const QUrl &url)
155 if (!d) d = new QmlErrorPrivate;
160 Returns the error description.
162 QString QmlError::description() const
164 if (d) return d->description;
165 else return QString();
169 Sets the error \a description.
171 void QmlError::setDescription(const QString &description)
173 if (!d) d = new QmlErrorPrivate;
174 d->description = description;
178 Returns the error line number.
180 int QmlError::line() const
182 if (d) return d->line;
187 Sets the error \a line number.
189 void QmlError::setLine(int line)
191 if (!d) d = new QmlErrorPrivate;
196 Returns the error column number.
198 int QmlError::column() const
200 if (d) return d->column;
205 Sets the error \a column number.
207 void QmlError::setColumn(int column)
209 if (!d) d = new QmlErrorPrivate;
214 Returns the error as a human readable string.
216 QString QmlError::toString() const
219 if (url().isEmpty()) {
220 rv = QLatin1String("<Unknown File>");
221 } else if (line() != -1) {
222 rv = url().toString() + QLatin1Char(':') + QString::number(line());
224 rv += QLatin1Char(':') + QString::number(column());
226 rv = url().toString();
229 rv += QLatin1String(": ") + description();
236 \fn QDebug operator<<(QDebug debug, const QmlError &error)
238 Outputs a human readable version of \a error to \a debug.
241 QDebug operator<<(QDebug debug, const QmlError &error)
243 debug << qPrintable(error.toString());
245 QUrl url = error.url();
247 if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
248 QString file = url.toLocalFile();
250 if (f.open(QIODevice::ReadOnly)) {
251 QByteArray data = f.readAll();
252 QTextStream stream(data, QIODevice::ReadOnly);
253 #ifndef QT_NO_TEXTCODEC
254 stream.setCodec("UTF-8");
256 const QString code = stream.readAll();
257 const QStringList lines = code.split(QLatin1Char('\n'));
259 if (lines.count() >= error.line()) {
260 const QString &line = lines.at(error.line() - 1);
261 debug << "\n " << qPrintable(line);
263 if(error.column() > 0) {
264 int column = qMax(0, error.column() - 1);
265 column = qMin(column, line.length());
269 for (int i = 0; i < column; ++i) {
270 const QChar ch = line.at(i);
272 ind.append(ch.unicode());
277 debug << "\n " << ind.constData();