Qt 4.8
qdeclarativeerror.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative 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 "qdeclarativeerror.h"
43 
44 #include <QtCore/qdebug.h>
45 #include <QtCore/qfile.h>
46 #include <QtCore/qstringlist.h>
47 
49 
78 {
79 public:
81 
84  int line;
85  int column;
86 };
87 
89 : line(-1), column(-1)
90 {
91 }
92 
97 : d(0)
98 {
99 }
100 
105 : d(0)
106 {
107  *this = other;
108 }
109 
114 {
115  if (!other.d) {
116  delete d;
117  d = 0;
118  } else {
119  if (!d) d = new QDeclarativeErrorPrivate;
120  d->url = other.d->url;
121  d->description = other.d->description;
122  d->line = other.d->line;
123  d->column = other.d->column;
124  }
125  return *this;
126 }
127 
132 {
133  delete d; d = 0;
134 }
135 
140 {
141  return d != 0;
142 }
143 
148 {
149  if (d) return d->url;
150  else return QUrl();
151 }
152 
157 {
158  if (!d) d = new QDeclarativeErrorPrivate;
159  d->url = url;
160 }
161 
166 {
167  if (d) return d->description;
168  else return QString();
169 }
170 
175 {
176  if (!d) d = new QDeclarativeErrorPrivate;
178 }
179 
184 {
185  if (d) return d->line;
186  else return -1;
187 }
188 
193 {
194  if (!d) d = new QDeclarativeErrorPrivate;
195  d->line = line;
196 }
197 
202 {
203  if (d) return d->column;
204  else return -1;
205 }
206 
211 {
212  if (!d) d = new QDeclarativeErrorPrivate;
213  d->column = column;
214 }
215 
220 {
221  QString rv;
222  if (url().isEmpty()) {
223  rv = QLatin1String("<Unknown File>");
224  } else if (line() != -1) {
225  rv = url().toString() + QLatin1Char(':') + QString::number(line());
226  if(column() != -1)
227  rv += QLatin1Char(':') + QString::number(column());
228  } else {
229  rv = url().toString();
230  }
231 
232  rv += QLatin1String(": ") + description();
233 
234  return rv;
235 }
236 
248 {
249  debug << qPrintable(error.toString());
250 
251  QUrl url = error.url();
252 
253  if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
254  QString file = url.toLocalFile();
255  QFile f(file);
256  if (f.open(QIODevice::ReadOnly)) {
257  QByteArray data = f.readAll();
259 #ifndef QT_NO_TEXTCODEC
260  stream.setCodec("UTF-8");
261 #endif
262  const QString code = stream.readAll();
263  const QStringList lines = code.split(QLatin1Char('\n'));
264 
265  if (lines.count() >= error.line()) {
266  const QString &line = lines.at(error.line() - 1);
267  debug << "\n " << qPrintable(line);
268 
269  if(error.column() > 0) {
270  int column = qMax(0, error.column() - 1);
271  column = qMin(column, line.length());
272 
273  QByteArray ind;
274  ind.reserve(column);
275  for (int i = 0; i < column; ++i) {
276  const QChar ch = line.at(i);
277  if (ch.isSpace())
278  ind.append(ch.unicode());
279  else
280  ind.append(' ');
281  }
282  ind.append('^');
283  debug << "\n " << ind.constData();
284  }
285  }
286  }
287  }
288  return debug;
289 }
290 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
QUrl url() const
Returns the url for the file that caused this error.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setDescription(const QString &)
Sets the error description.
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
QDeclarativeError()
Creates an empty error object.
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
#define error(msg)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
bool isValid() const
Returns true if this error is valid, otherwise false.
void setCodec(QTextCodec *codec)
Sets the codec for this stream to codec.
void setColumn(int)
Sets the error column number.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
bool isSpace() const
Returns true if the character is a separator character (Separator_* categories); otherwise returns fa...
Definition: qchar.cpp:609
QString toString() const
Returns the error as a human readable string.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static FILE * stream
static bool isEmpty(const char *str)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QString description() const
Returns the error description.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static const char * data(const QByteArray &arr)
The QDeclarativeError class encapsulates a QML error.
int line() const
Returns the error line number.
void setLine(int)
Sets the error line number.
QDeclarativeError & operator=(const QDeclarativeError &)
Assigns other to this error object.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
void setUrl(const QUrl &)
Sets the url for the file that caused this error.
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
int column() const
Returns the error column number.
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
QDeclarativeErrorPrivate * d
QString readAll()
Reads the entire content of the stream, and returns it as a QString.
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
void reserve(int size)
Attempts to allocate memory for at least size bytes.
Definition: qbytearray.h:449
#define qPrintable(string)
Definition: qglobal.h:1750
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QDebug operator<<(QDebug debug, const QDeclarativeError &error)
Outputs a human readable version of error to debug.