src/declarative/qml/qdeclarativeinfo.cpp
changeset 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 
       
    57     \brief Prints warnings messages that include the file and line number for QML types.
       
    58 
       
    59     When QML types display warning messages, it improves traceability
       
    60     if they include the QML file and line number on which the
       
    61     particular instance was instantiated.
       
    62     
       
    63     To include the file and line number, an object must be passed.  If
       
    64     the file and line number is not available for that instance
       
    65     (either it was not instantiated by the QML engine or location
       
    66     information is disabled), "unknown location" will be used instead.
       
    67 
       
    68     For example, 
       
    69 
       
    70     \code
       
    71     qmlInfo(object) << tr("component property is a write-once property");
       
    72     \endcode
       
    73 
       
    74     prints
       
    75 
       
    76     \code
       
    77     QML MyCustomType (unknown location): component property is a write-once property
       
    78     \endcode
       
    79 */
       
    80 
       
    81 class QDeclarativeInfoPrivate
       
    82 {
       
    83 public:
       
    84     QDeclarativeInfoPrivate() : ref (1), object(0) {}
       
    85 
       
    86     int ref;
       
    87     const QObject *object;
       
    88     QString buffer;
       
    89     QList<QDeclarativeError> errors;
       
    90 };
       
    91 
       
    92 QDeclarativeInfo::QDeclarativeInfo(QDeclarativeInfoPrivate *p)
       
    93 : QDebug(&p->buffer), d(p)
       
    94 {
       
    95     nospace();
       
    96 }
       
    97 
       
    98 QDeclarativeInfo::QDeclarativeInfo(const QDeclarativeInfo &other)
       
    99 : QDebug(other), d(other.d)
       
   100 {
       
   101     d->ref++;
       
   102 }
       
   103 
       
   104 QDeclarativeInfo::~QDeclarativeInfo()
       
   105 {
       
   106     if (0 == --d->ref) {
       
   107         QList<QDeclarativeError> errors = d->errors;
       
   108 
       
   109         QDeclarativeEngine *engine = 0;
       
   110 
       
   111         if (!d->buffer.isEmpty()) {
       
   112             QDeclarativeError error;
       
   113 
       
   114             QObject *object = const_cast<QObject *>(d->object);
       
   115 
       
   116             if (object) {
       
   117                 engine = qmlEngine(d->object);
       
   118                 QString typeName;
       
   119                 QDeclarativeType *type = QDeclarativeMetaType::qmlType(object->metaObject());
       
   120                 if (type) {
       
   121                     typeName = QLatin1String(type->qmlTypeName());
       
   122                     int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
       
   123                     if (lastSlash != -1)
       
   124                         typeName = typeName.mid(lastSlash+1);
       
   125                 } else {
       
   126                     typeName = QString::fromUtf8(object->metaObject()->className());
       
   127                     int marker = typeName.indexOf(QLatin1String("_QMLTYPE_"));
       
   128                     if (marker != -1)
       
   129                         typeName = typeName.left(marker);
       
   130                 }
       
   131 
       
   132                 d->buffer.prepend(QLatin1String("QML ") + typeName + QLatin1String(": "));
       
   133 
       
   134                 QDeclarativeData *ddata = QDeclarativeData::get(object, false);
       
   135                 if (ddata && ddata->outerContext && !ddata->outerContext->url.isEmpty()) {
       
   136                     error.setUrl(ddata->outerContext->url);
       
   137                     error.setLine(ddata->lineNumber);
       
   138                     error.setColumn(ddata->columnNumber);
       
   139                 }
       
   140             }
       
   141 
       
   142             error.setDescription(d->buffer);
       
   143 
       
   144             errors.prepend(error);
       
   145         }
       
   146 
       
   147         QDeclarativeEnginePrivate::warning(engine, errors);
       
   148 
       
   149         delete d;
       
   150     }
       
   151 }
       
   152 
       
   153 QDeclarativeInfo qmlInfo(const QObject *me)
       
   154 {
       
   155     QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
       
   156     d->object = me;
       
   157     return QDeclarativeInfo(d);
       
   158 }
       
   159 
       
   160 QDeclarativeInfo qmlInfo(const QObject *me, const QDeclarativeError &error)
       
   161 {
       
   162     QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
       
   163     d->object = me;
       
   164     d->errors << error;
       
   165     return QDeclarativeInfo(d);
       
   166 }
       
   167 
       
   168 QDeclarativeInfo qmlInfo(const QObject *me, const QList<QDeclarativeError> &errors)
       
   169 {
       
   170     QDeclarativeInfoPrivate *d = new QDeclarativeInfoPrivate;
       
   171     d->object = me;
       
   172     d->errors = errors;
       
   173     return QDeclarativeInfo(d);
       
   174 }
       
   175 
       
   176 
       
   177 QT_END_NAMESPACE