nodemetainfo.cpp 35.1 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
30 31 32 33 34 35 36 37 38
**
**************************************************************************/

#include "nodemetainfo.h"
#include "model.h"
#include "widgetqueryview.h"
#include "invalidargumentexception.h"

#include "metainfo.h"
39 40
#include <model.h>
#include <rewriterview.h>
41

42 43 44 45 46 47 48
#include <QSharedData>
#include <QtDebug>
#include <QIcon>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>

49 50
#include <qmljs/qmljslookupcontext.h>
#include <qmljs/qmljsdocument.h>
51
#include <qmljs/qmljscontext.h>
52 53
#include <qmljs/qmljsbind.h>
#include <qmljs/parser/qmljsast_p.h>
54
#include <languageutils/fakemetaobject.h>
55
#include <private/qdeclarativemetatype_p.h>
56
#include <private/qdeclarativestringconverters_p.h>
57 58 59 60 61

namespace QmlDesigner {

namespace Internal {

62
struct TypeDescription
63
{
64
    QString className;
65
    int minorVersion;
66
    int majorVersion;
67 68
};

69
} //Internal
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

/*!
\class QmlDesigner::NodeMetaInfo
\ingroup CoreModel
\brief The NodeMetaInfo class provides meta information about a qml type.

A NodeMetaInfo object can be created via ModelNode::metaInfo, or MetaInfo::nodeMetaInfo.

The object can be invalid - you can check this by calling isValid().
The object is invalid if you ask for meta information for
an non-existing qml property. Also the node meta info can become invalid
if the enclosing type is deregistered from the meta type system (e.g.
a sub component qml file is deleted). Trying to call any accessor methods on an invalid
NodeMetaInfo object will result in an InvalidMetaInfoException being thrown.

\see QmlDesigner::MetaInfo, QmlDesigner::PropertyMetaInfo, QmlDesigner::EnumeratorMetaInfo
*/

88 89 90 91 92 93 94
namespace Internal {

using namespace QmlJS;

typedef QPair<QString, QString> PropertyInfo;

class PropertyMemberProcessor : public Interpreter::MemberProcessor
95
{
96 97 98 99 100
public:
    virtual bool processProperty(const QString &name, const Interpreter::Value *value)
    {
        const Interpreter::ASTPropertyReference *ref = dynamic_cast<const Interpreter::ASTPropertyReference*>(value);
        if (ref) {
101
            QString type = "unknown";
102 103 104 105
            if (ref->ast()->memberType)
                type = ref->ast()->memberType->asString();
            m_properties.append(qMakePair(name, type));
        } else {
106 107
            if (const Interpreter::QmlObjectValue * ov = dynamic_cast<const Interpreter::QmlObjectValue *>(value)) {
                QString qualifiedTypeName = ov->packageName().isEmpty() ? ov->className() : ov->packageName() + '.' + ov->className();
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
                m_properties.append(qMakePair(name, qualifiedTypeName));
            } else {
                Interpreter::TypeId typeId;
                QString typeName = typeId(value);
                if (typeName == QLatin1String("number")) {
                    if (value->asRealValue()) {
                        typeName = "real";
                    } else {
                    typeName = "int";
                    }
                }
                m_properties.append(qMakePair(name, typeName));
            }
        }
        return true;
    }
124

125 126 127 128 129 130 131
    QList<PropertyInfo> properties() const { return m_properties; }

private:
    QList<PropertyInfo> m_properties;
};

static inline bool isValueType(const QString &type)
132
{
133 134 135
    QStringList objectValuesList;
    objectValuesList << "QFont" << "QPoint" << "QPointF" << "QSize" << "QSizeF" << "QVector3D" << "QVector2D";
    return objectValuesList.contains(type);
136 137
}

138
const Interpreter::QmlObjectValue *findQmlPrototype(const Interpreter::ObjectValue *ov, LookupContext *context)
139
{
140 141 142 143 144 145 146 147
    if (!ov)
        return 0;

    const Interpreter::QmlObjectValue * qmlValue = dynamic_cast<const Interpreter::QmlObjectValue *>(ov);
    if (qmlValue)
        return qmlValue;

    return findQmlPrototype(ov->prototype(context->context()), context);
148 149
}

150
QStringList prototypes(const Interpreter::ObjectValue *ov, LookupContext::Ptr context, bool versions = false)
151
{
152 153 154 155 156 157 158 159
    QStringList list;
    if (!ov)
        return list;
    ov = ov->prototype(context->context());
    while (ov) {
        const Interpreter::QmlObjectValue * qmlValue = dynamic_cast<const Interpreter::QmlObjectValue *>(ov);
        if (qmlValue) {
            if (versions) {
160 161 162
                list << qmlValue->packageName() + '.' + qmlValue->className() +
                ' ' + QString::number(qmlValue->version().majorVersion()) +
                '.' + QString::number(qmlValue->version().minorVersion());
163
            } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
164
                list << qmlValue->packageName() + QLatin1Char('.') + qmlValue->className();
165 166 167 168 169 170 171 172 173 174 175
            }
        } else {
            if (versions) {
                list << ov->className() + " -1.-1";
            } else {
                list << ov->className();
            }
        }
        ov = ov->prototype(context->context());
    }
    return list;
176 177
}

178 179 180
QList<PropertyInfo> getObjectTypes(const Interpreter::ObjectValue *ov, LookupContext *context, bool local = false);

QList<PropertyInfo> getQmlTypes(const Interpreter::QmlObjectValue *ov, LookupContext *context, bool local = false)
181
{
182 183 184
    QList<PropertyInfo> list;
    if (!ov)
        return list;
185

186 187
    PropertyMemberProcessor processor;
    ov->processMembers(&processor);
188

189 190 191 192 193 194
    QList<PropertyInfo> newList = processor.properties();

    foreach (PropertyInfo property, newList) {
        QString name = property.first;
        if (!ov->isWritable(name) && ov->isPointer(name)) {
            //dot property
195
            const Interpreter::QmlObjectValue * qmlValue = dynamic_cast<const Interpreter::QmlObjectValue *>(ov->lookupMember(name, context->context()));
196 197 198 199 200 201 202 203 204 205 206
            if (qmlValue) {
                QList<PropertyInfo> dotProperties = getQmlTypes(qmlValue, context);
                foreach (const PropertyInfo &propertyInfo, dotProperties) {
                    QString dotName = propertyInfo.first;
                    QString type = propertyInfo.second;
                    dotName = name + '.' + dotName;
                    list.append(qMakePair(dotName, type));
                }
            }
        }
        if (isValueType(ov->propertyType(name))) {
207
            const Interpreter::ObjectValue *dotObjectValue = dynamic_cast<const Interpreter::ObjectValue *>(ov->lookupMember(name, context->context()));
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            if (dotObjectValue) {
                QList<PropertyInfo> dotProperties = getObjectTypes(dotObjectValue, context);
                foreach (const PropertyInfo &propertyInfo, dotProperties) {
                    QString dotName = propertyInfo.first;
                    QString type = propertyInfo.second;
                    dotName = name + '.' + dotName;
                    list.append(qMakePair(dotName, type));
                }
            }
        }
        QString type = property.second;
        if (!ov->isPointer(name) && !ov->isListProperty(name))
            type = ov->propertyType(name);
        list.append(qMakePair(name, type));
    }

    if (!local) {
        const Interpreter::ObjectValue* prototype = ov->prototype(context->context());

        const Interpreter::QmlObjectValue * qmlObjectValue = dynamic_cast<const Interpreter::QmlObjectValue *>(prototype);

        if (qmlObjectValue) {
            list << getQmlTypes(qmlObjectValue, context);
        } else {
            list << getObjectTypes(prototype, context);
        }
    }

    return list;
237 238
}

239
QList<PropertyInfo> getTypes(const Interpreter::ObjectValue *ov, LookupContext *context, bool local = false)
240
{
241 242 243 244 245 246 247 248
    QList<PropertyInfo> list;

    const Interpreter::QmlObjectValue * qmlObjectValue = dynamic_cast<const Interpreter::QmlObjectValue *>(ov);

    if (qmlObjectValue) {
        list << getQmlTypes(qmlObjectValue, context, local);
    } else {
        list << getObjectTypes(ov, context, local);
249 250
    }

251
    return list;
252 253
}

254
QList<PropertyInfo> getObjectTypes(const Interpreter::ObjectValue *ov, LookupContext *context, bool local)
255
{
256 257 258 259 260 261 262 263 264 265
    QList<PropertyInfo> list;
    if (!ov)
        return list;
    PropertyMemberProcessor processor;
    ov->processMembers(&processor);

    list << processor.properties();

    if (!local) {
        const Interpreter::ObjectValue* prototype = ov->prototype(context->context());
266

267
        const Interpreter::QmlObjectValue * qmlObjectValue = dynamic_cast<const Interpreter::QmlObjectValue *>(prototype);
268

269 270 271 272 273
        if (qmlObjectValue) {
            list << getQmlTypes(qmlObjectValue, context);
        } else {
            list << getObjectTypes(prototype, context);
        }
274 275
    }

276
    return list;
277 278
}

279
class NodeMetaInfoPrivate
280
{
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
public:
    typedef QSharedPointer<NodeMetaInfoPrivate> Pointer;
    NodeMetaInfoPrivate();
    ~NodeMetaInfoPrivate() {}

    bool isValid() const;

    bool isComponent() const
    {
        return m_isComponent;
    }

    QStringList properties() const
    {
        return m_properties;
296 297
    }

298 299 300 301 302 303 304
    QStringList localProperties() const
    {
        return m_localProperties;
    }

    QString defaultPropertyName() const
    {
305 306 307
        if (!m_defaultPropertyName.isEmpty())
            return m_defaultPropertyName;
        return QLatin1String("data");
308 309 310 311
    }

    QString propertyType(const QString &propertyName) const;

312
    void setupPrototypes();
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    QList<TypeDescription> prototypes() const;

    bool isPropertyWritable(const QString &propertyName) const;
    bool isPropertyPointer(const QString &propertyName) const;
    bool isPropertyList(const QString &propertyName) const;
    bool isPropertyEnum(const QString &propertyName) const;
    QString propertyEnumScope(const QString &propertyName) const;
    QStringList keysForEnum(const QString &enumName) const;
    bool cleverCheckType(const QString &otherType) const;
    QVariant::Type variantTypeId(const QString &properyName) const;

    int majorVersion() const
    { return m_majorVersion; }

    int minorVersion() const
    { return m_minorVersion; }

    QString qualfiedTypeName() const
    { return m_qualfiedTypeName; }

    Model *model() const
    { return m_model; }

    QString packageName() const;

    QString componentSource() const;
    QString componentFileName() const;

341 342
    static Pointer create(Model *model, const QString &type, int maj = -1, int min = -1);

343
     QSet<QString> &prototypeCachePositives()
344
     {
345 346 347 348 349 350
         return m_prototypeCachePositives;
     }

     QSet<QString> &prototypeCacheNegatives()
     {
         return m_prototypeCacheNegatives;
351
     }
352

353 354 355 356 357
     static void clearCache()
     {
         m_nodeMetaInfoCache.clear();
     }

358 359 360 361 362 363 364 365 366 367
private:
    NodeMetaInfoPrivate(Model *model, QString type, int maj = -1, int min = -1);

    const QmlJS::Interpreter::QmlObjectValue *getQmlObjectValue() const;
    const QmlJS::Interpreter::ObjectValue *getObjectValue() const;
    void setupPropertyInfo(QList<PropertyInfo> propertyInfos);
    void setupLocalPropertyInfo(QList<PropertyInfo> propertyInfos);
    QString lookupName() const;
    QStringList lookupNameComponent() const;
    const QmlJS::Interpreter::QmlObjectValue *getNearestQmlObjectValue() const;
368
    QString fullQualifiedImportAliasType() const;
369 370 371 372 373 374 375 376 377 378

    QString m_qualfiedTypeName;
    int m_majorVersion;
    int m_minorVersion;
    bool m_isValid;
    bool m_isComponent;
    QStringList m_properties;
    QStringList m_propertyTypes;
    QStringList m_localProperties;
    QString m_defaultPropertyName;
379
    QList<TypeDescription> m_prototypes;
380 381
    QSet<QString> m_prototypeCachePositives;
    QSet<QString> m_prototypeCacheNegatives;
382 383 384 385 386 387

    //storing the pointer would not be save
    QmlJS::LookupContext *lookupContext() const;
    QmlJS::Document *document() const;

    QPointer<Model> m_model;
388
    static QHash<QString, Pointer> m_nodeMetaInfoCache;
389 390
};

391 392
QHash<QString, NodeMetaInfoPrivate::Pointer> NodeMetaInfoPrivate::m_nodeMetaInfoCache;

393

394
static inline QString stringIdentifier( const QString &type, int maj, int min)
395
{
396
    return type + QString::number(maj) + '_' + QString::number(min);
397 398 399 400 401 402 403 404
}

NodeMetaInfoPrivate::Pointer NodeMetaInfoPrivate::create(Model *model, const QString &type, int maj, int min)
{
    if (m_nodeMetaInfoCache.contains(stringIdentifier(type, maj, min))) {
        const Pointer &info = m_nodeMetaInfoCache.value(stringIdentifier(type, maj, min));
        if (info->model() == model) {
            return info;
405
        } else {
406
            m_nodeMetaInfoCache.clear();
407 408
        }
    }
409

410 411
    Pointer newData(new NodeMetaInfoPrivate(model, type, maj, min));
    if (newData->isValid())
412
        m_nodeMetaInfoCache.insert(stringIdentifier(type, maj, min), newData);
413
    return newData;
414 415
}

416 417
NodeMetaInfoPrivate::NodeMetaInfoPrivate() : m_isValid(false)
{
418

419
}
420

Kai Koehne's avatar
Kai Koehne committed
421
NodeMetaInfoPrivate::NodeMetaInfoPrivate(Model *model, QString type, int maj, int min) :
422
                                        m_qualfiedTypeName(type), m_majorVersion(maj),
423
                                        m_minorVersion(min), m_isValid(false), m_isComponent(false),
Kai Koehne's avatar
Kai Koehne committed
424
                                        m_model(model)
425
{
426 427 428 429 430 431 432 433
    if (lookupContext()) {
        const Interpreter::QmlObjectValue *objectValue = getQmlObjectValue();
        if (objectValue) {
            setupPropertyInfo(getTypes(objectValue, lookupContext()));
            setupLocalPropertyInfo(getTypes(objectValue, lookupContext(), true));
            m_defaultPropertyName = objectValue->defaultPropertyName();
            setupPrototypes();
            m_isValid = true;
434
        } else {
435 436 437 438 439 440
            const Interpreter::ObjectValue *objectValue = getObjectValue();
            if (objectValue) {
                const Interpreter::QmlObjectValue *qmlValue = dynamic_cast<const Interpreter::QmlObjectValue *>(objectValue);
                if (qmlValue) {
                    m_majorVersion = qmlValue->version().majorVersion();
                    m_minorVersion = qmlValue->version().minorVersion();
441
                    m_qualfiedTypeName = qmlValue->packageName() + '.' + qmlValue->className();
442 443 444 445 446 447 448 449 450
                } else {
                    m_isComponent = true;
                }
                setupPropertyInfo(getTypes(objectValue, lookupContext()));
                setupLocalPropertyInfo(getTypes(objectValue, lookupContext(), true));
                m_defaultPropertyName = lookupContext()->context()->defaultPropertyName(objectValue);
                setupPrototypes();
                m_isValid = true;
            }
451 452 453 454
        }
    }
}

455 456 457 458 459 460 461 462 463 464 465 466
static inline QString getUrlFromType(const QString& typeName)
{
    QStringList nameComponents = typeName.split('.');
    QString result;

    for (int i = 0; i < (nameComponents.count() - 1); i++) {
        result += nameComponents.at(i);
    }

    return result;
}

467 468
const QmlJS::Interpreter::QmlObjectValue *NodeMetaInfoPrivate::getQmlObjectValue() const
{
469
    QmlJS::Interpreter::QmlObjectValue * value = lookupContext()->valueOwner()->cppQmlTypes().typeByQualifiedName(lookupName());
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    if (value)
        return value;

    //If no version was specified (-1,-1) the approach above does not work.
    //But we can look up the value "manually"
    //This is usefull to make something like QtQuick.Item -1 -1 work in all cases
    //and fix ambiguities with Qt 4.7.

    if (m_majorVersion != -1 ||   m_minorVersion != -1)
        return 0;   

    const QString old_qualfiedTypeName =  m_qualfiedTypeName;

    //This makes only sense if a package was specified.
    if (m_qualfiedTypeName.split(".").count() < 2)
        return 0;

    const QString package = getUrlFromType(m_qualfiedTypeName);
    const QString type = m_qualfiedTypeName.split('.').last();

490

491
    LanguageUtils::ComponentVersion version(9999, 9999);
492 493
    //get the correct version
    Document::Ptr doc = lookupContext()->document();
Christian Kamm's avatar
Christian Kamm committed
494
    const Interpreter::Context *context = lookupContext()->context().data();
495 496 497 498 499
    Interpreter::ImportInfo importInfo = context->imports(doc.data())->info(fullQualifiedImportAliasType(), context);

    if (importInfo.isValid())
        version = importInfo.version();

500
    QList<Interpreter::QmlObjectValue *> qmlObjectValues = lookupContext()->valueOwner()->cppQmlTypes().typesForImport(package, version);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    const Interpreter::QmlObjectValue *qmlValue = 0;
    foreach (Interpreter::QmlObjectValue *value, qmlObjectValues) {
        if (value->className() == type)
            qmlValue = value;
    }

    if (!qmlValue)
        return 0;

    //Now we have to check the different packages.
    const LanguageUtils::FakeMetaObject::Export exp =
            qmlValue->metaObject()->exportInPackage(package);
    const QString convertedName = exp.type;

    //Not available in the requested package
    if (convertedName.isNull())
        return 0;

    //Different name for requested package
    if (type != convertedName)
        return 0;

    return qmlValue;
524
}
525

526
const QmlJS::Interpreter::ObjectValue *NodeMetaInfoPrivate::getObjectValue() const
527
{
528 529
    return lookupContext()->context()->lookupType(document(), lookupNameComponent());
}
530

531 532 533 534 535 536 537
QmlJS::LookupContext *NodeMetaInfoPrivate::lookupContext() const
{
    if (m_model && m_model->rewriterView()) {
        return m_model->rewriterView()->lookupContext();
    }
    return 0;
}
538

539 540 541 542 543 544 545
QmlJS::Document *NodeMetaInfoPrivate::document() const
{
    if (m_model && m_model->rewriterView()) {
        return m_model->rewriterView()->document();
    }
    return 0;
}
546

547 548 549 550
void NodeMetaInfoPrivate::setupLocalPropertyInfo(QList<PropertyInfo> localPropertyInfos)
{
    foreach (const PropertyInfo &propertyInfo, localPropertyInfos) {
        m_localProperties.append(propertyInfo.first);
551 552 553
    }
}

554 555 556 557 558 559 560
void NodeMetaInfoPrivate::setupPropertyInfo(QList<PropertyInfo> propertyInfos)
{
    foreach (const PropertyInfo &propertyInfo, propertyInfos) {
        m_properties.append(propertyInfo.first);
        m_propertyTypes.append(propertyInfo.second);
    }
}
561

562
bool NodeMetaInfoPrivate::isPropertyWritable(const QString &propertyName) const
563
{
564 565
    if (!isValid())
        return false;
566

567
    if (propertyName.contains('.')) {
568 569 570
        const QStringList parts = propertyName.split('.');
        const QString objectName = parts.first();
        const QString rawPropertyName = parts.last();
571
        const QString objectType = propertyType(objectName);
572

573 574
        if (isValueType(objectType)) {
            return true;
575 576
        }

577 578 579 580 581
        QSharedPointer<NodeMetaInfoPrivate> objectInfo(create(m_model, objectType));
        if (objectInfo->isValid())
            return objectInfo->isPropertyWritable(rawPropertyName);
        else
            return true;
582
    }
583 584

    const QmlJS::Interpreter::QmlObjectValue *qmlObjectValue = getNearestQmlObjectValue();
585 586
    if (!qmlObjectValue)
        return true;
587 588 589 590
    if (qmlObjectValue->hasProperty(propertyName))
        return qmlObjectValue->isWritable(propertyName);
    else
        return true; //all properties of components are writable
591 592 593
}


594
bool NodeMetaInfoPrivate::isPropertyList(const QString &propertyName) const
595
{
596
    if (!isValid())
597
        return false;
598 599

    if (propertyName.contains('.')) {
600 601 602
        const QStringList parts = propertyName.split('.');
        const QString objectName = parts.first();
        const QString rawPropertyName = parts.last();
603 604 605 606 607 608 609 610 611 612 613
        const QString objectType = propertyType(objectName);

        if (isValueType(objectType)) {
            return false;
        }

        QSharedPointer<NodeMetaInfoPrivate> objectInfo(create(m_model, objectType));
        if (objectInfo->isValid())
            return objectInfo->isPropertyList(rawPropertyName);
        else
            return true;
614 615
    }

616 617 618 619
    const QmlJS::Interpreter::QmlObjectValue *qmlObjectValue = getNearestQmlObjectValue();
    if (!qmlObjectValue)
        return false;
    return qmlObjectValue->isListProperty(propertyName);
620
}
621

622 623 624 625
bool NodeMetaInfoPrivate::isPropertyPointer(const QString &propertyName) const
{
    if (!isValid())
        return false;
626

627
    if (propertyName.contains('.')) {
628 629 630
        const QStringList parts = propertyName.split('.');
        const QString objectName = parts.first();
        const QString rawPropertyName = parts.last();
631
        const QString objectType = propertyType(objectName);
632

633 634
        if (isValueType(objectType)) {
            return false;
635 636
        }

637 638 639 640 641
        QSharedPointer<NodeMetaInfoPrivate> objectInfo(create(m_model, objectType));
        if (objectInfo->isValid())
            return objectInfo->isPropertyPointer(rawPropertyName);
        else
            return true;
642 643
    }

644 645 646 647
    const QmlJS::Interpreter::QmlObjectValue *qmlObjectValue = getNearestQmlObjectValue();
    if (!qmlObjectValue)
        return false;
    return qmlObjectValue->isPointer(propertyName);
648
}
649

650
bool NodeMetaInfoPrivate::isPropertyEnum(const QString &propertyName) const
651
{
652
    if (!isValid())
653
        return false;
654 655

    if (propertyName.contains('.')) {
656 657 658
        const QStringList parts = propertyName.split('.');
        const QString objectName = parts.first();
        const QString rawPropertyName = parts.last();
659 660 661 662 663 664 665 666 667 668
        const QString objectType = propertyType(objectName);

        if (isValueType(objectType)) {
            return false;
        }

        QSharedPointer<NodeMetaInfoPrivate> objectInfo(create(m_model, objectType));
        if (objectInfo->isValid())
            return objectInfo->isPropertyEnum(rawPropertyName);
        else
669
            return false;
670 671
    }

672 673
    QList<const Interpreter::ObjectValue *> objects;
    objects = Interpreter::PrototypeIterator(getNearestQmlObjectValue(), lookupContext()->context()).all();
674

675 676 677
    //We have to run the prototype chain
    foreach (const Interpreter::ObjectValue *ov, objects) {
        if (const Interpreter::QmlObjectValue * qmlValue = dynamic_cast<const Interpreter::QmlObjectValue *>(ov)) {
678
            if (qmlValue->getEnum(propertyType(propertyName)).isValid())
679 680 681
                return true;
        }
    }
682 683 684 685

    return false;
}

686
QString NodeMetaInfoPrivate::propertyEnumScope(const QString &propertyName) const
687
{
688 689 690 691
    if (!isValid())
        return QString();

    if (propertyName.contains('.')) {
692 693 694
        const QStringList parts = propertyName.split('.');
        const QString objectName = parts.first();
        const QString rawPropertyName = parts.last();
695 696 697 698 699 700 701 702 703 704 705
        const QString objectType = propertyType(objectName);

        if (isValueType(objectType)) {
            return QString();
        }

        QSharedPointer<NodeMetaInfoPrivate> objectInfo(create(m_model, objectType));
        if (objectInfo->isValid())
            return objectInfo->propertyEnumScope(rawPropertyName);
        else
            return QString();
706
    }
707

708 709
    QList<const Interpreter::ObjectValue *> objects;
    objects = Interpreter::PrototypeIterator(getNearestQmlObjectValue(), lookupContext()->context()).all();
710

711 712 713
    //We have to run the prototype chain
    foreach (const Interpreter::ObjectValue *ov, objects) {
        if (const Interpreter::QmlObjectValue * qmlValue = dynamic_cast<const Interpreter::QmlObjectValue *>(ov)) {
714
            if (qmlValue->getEnum(propertyType(propertyName)).isValid())
715 716 717
                return qmlValue->className();
        }
    }
718

719 720
    return QString();
}
721

722
bool NodeMetaInfoPrivate::cleverCheckType(const QString &otherType) const
723
{
724 725 726 727 728
    if (otherType == qualfiedTypeName())
            return true;

    if (isComponent())
        return false;
729

730
    QStringList split = otherType.split('.');
731 732 733 734 735
    QString package;
    QString typeName = otherType;
    if (split.count() > 1) {
        package = split.first();
        typeName = split.at(1);
736
    }
737
    if (packageName() == package)
738
        return QString(package + '.' + typeName) == qualfiedTypeName();
739

740 741 742
    const LanguageUtils::FakeMetaObject::Export exp =
            getQmlObjectValue()->metaObject()->exportInPackage(package);
    const QString convertedName = exp.type;
743 744

    return typeName == convertedName;
745
}
746

747
QVariant::Type NodeMetaInfoPrivate::variantTypeId(const QString &properyName) const
748
{
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    QString typeName = propertyType(properyName);
    if (typeName == "string")
        return QVariant::String;

    if (typeName == "color")
        return QVariant::Color;

    if (typeName == "int")
        return QVariant::Int;

    if (typeName == "url")
        return QVariant::Url;

    if (typeName == "real")
        return QVariant::Double;
764

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    if (typeName == "bool")
        return QVariant::Bool;

    if (typeName == "boolean")
        return QVariant::Bool;

    if (typeName == "date")
        return QVariant::Date;

    if (typeName == "alias")
        return QVariant::UserType;

    if (typeName == "var")
        return QVariant::UserType;

    return QVariant::nameToType(typeName.toLatin1().data());
781 782 783
}


784 785 786 787
QStringList NodeMetaInfoPrivate::keysForEnum(const QString &enumName) const
{
    if (!isValid())
        return QStringList();
788

789
    return getNearestQmlObjectValue()->getEnum(enumName).keys();
790 791 792
}

QString NodeMetaInfoPrivate::packageName() const
793
{
794 795 796 797 798 799 800 801 802 803 804 805
    if (!isComponent())
        return getQmlObjectValue()->packageName();
    return QString();
}

QString NodeMetaInfoPrivate::componentSource() const
{
    if (isComponent()) {
        const Interpreter::ASTObjectValue * astObjectValue = dynamic_cast<const Interpreter::ASTObjectValue *>(getObjectValue());
        if (astObjectValue)
            return astObjectValue->document()->source().mid(astObjectValue->typeName()->identifierToken.begin(),
                                                            astObjectValue->initializer()->rbraceToken.end());
806
    }
807 808
    return QString();
}
809

810 811 812 813 814 815 816 817 818 819 820 821 822
QString NodeMetaInfoPrivate::componentFileName() const
{
    if (isComponent()) {
        const Interpreter::ASTObjectValue * astObjectValue = dynamic_cast<const Interpreter::ASTObjectValue *>(getObjectValue());
        if (astObjectValue) {
            QString fileName;
            int line;
            int column;
            if (astObjectValue->getSourceLocation(&fileName, &line, &column))
                return fileName;
        }
    }
    return QString();
823 824
}

825 826

QString NodeMetaInfoPrivate::lookupName() const
827
{
828 829
    QString className = m_qualfiedTypeName;
    QString packageName;
830

831
    QStringList packageClassName = m_qualfiedTypeName.split(QLatin1Char('.'));
832 833 834 835
    if (packageClassName.size() > 1) {
        className = packageClassName.takeLast();
        packageName = packageClassName.join(QLatin1String("."));
    }
836

837 838 839 840
    return Interpreter::CppQmlTypes::qualifiedName(
                packageName,
                className,
                LanguageUtils::ComponentVersion(m_majorVersion, m_minorVersion));
841 842
}

843
QStringList NodeMetaInfoPrivate::lookupNameComponent() const
844
{
845
        QString tempString = fullQualifiedImportAliasType();
846
        return tempString.split('.');
847 848
}

849

850
bool NodeMetaInfoPrivate::isValid() const
851
{
852 853 854 855 856 857
    return m_isValid && lookupContext() && document();
}

QString NodeMetaInfoPrivate::propertyType(const QString &propertyName) const
{
    if (!m_properties.contains(propertyName))
858
        return QString();
859 860 861
    return m_propertyTypes.at(m_properties.indexOf(propertyName));
}

862
void NodeMetaInfoPrivate::setupPrototypes()
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
{
    QList<const Interpreter::ObjectValue *> objects;
    if (m_isComponent)
        objects = Interpreter::PrototypeIterator(getObjectValue(), lookupContext()->context()).all();
    else
        objects = Interpreter::PrototypeIterator(getQmlObjectValue(), lookupContext()->context()).all();
    foreach (const Interpreter::ObjectValue *ov, objects) {
        TypeDescription description;
        description.className = ov->className();
        description.minorVersion = -1;
        description.majorVersion = -1;
        if (const Interpreter::QmlObjectValue * qmlValue = dynamic_cast<const Interpreter::QmlObjectValue *>(ov)) {
            description.minorVersion = qmlValue->version().minorVersion();
            description.majorVersion = qmlValue->version().majorVersion();
            if (!qmlValue->packageName().isEmpty())
878
                description.className = qmlValue->packageName() + '.' + description.className;
Marco Bubke's avatar
Marco Bubke committed
879
            m_prototypes.append(description);
880 881 882 883
        } else {
            if (lookupContext()->context()->lookupType(document(), QStringList() << ov->className()))
                m_prototypes.append(description);
        }
884
    }
885 886 887 888 889 890
}


QList<TypeDescription> NodeMetaInfoPrivate::prototypes() const
{
    return m_prototypes;
891
}
892

893 894 895 896 897
const QmlJS::Interpreter::QmlObjectValue *NodeMetaInfoPrivate::getNearestQmlObjectValue() const
{
    if (m_isComponent)
        return findQmlPrototype(getObjectValue(), lookupContext());
    return getQmlObjectValue();
898 899
}

900 901 902 903 904 905 906
QString NodeMetaInfoPrivate::fullQualifiedImportAliasType() const
{
    if (m_model && m_model->rewriterView())
        return model()->rewriterView()->convertTypeToImportAlias(m_qualfiedTypeName);
    return m_qualfiedTypeName;
}

907 908 909
} //namespace Internal

NodeMetaInfo::NodeMetaInfo() : m_privateData(new Internal::NodeMetaInfoPrivate())
910 911 912 913
{

}

914
NodeMetaInfo::NodeMetaInfo(Model *model, QString type, int maj, int min) : m_privateData(Internal::NodeMetaInfoPrivate::create(model, type, maj, min))
915
{
916

917 918
}

919
NodeMetaInfo::~NodeMetaInfo()
920
{
921
}
922

923 924 925
NodeMetaInfo::NodeMetaInfo(const NodeMetaInfo &other)
    : m_privateData(other.m_privateData)
{
926 927
}

928
NodeMetaInfo &NodeMetaInfo::operator=(const NodeMetaInfo &other)
929
{
930 931 932 933
    if (this != &other)
        this->m_privateData = other.m_privateData;

    return *this;
934 935
}

936
bool NodeMetaInfo::isValid() const
937
{
938
    return m_privateData->isValid();
939 940
}

941
bool NodeMetaInfo::isComponent() const
942
{
943
    return m_privateData->isComponent();
944 945
}

946 947 948 949
bool NodeMetaInfo::hasProperty(const QString &propertyName) const
{
    return propertyNames().contains(propertyName);
}
950

951
QStringList NodeMetaInfo::propertyNames() const
952
{
953
    return m_privateData->properties();
954 955
}

956
QStringList NodeMetaInfo::directPropertyNames() const
957
{
958
    return m_privateData->localProperties();
959 960
}

961
QString NodeMetaInfo::defaultPropertyName() const
962
{