summaryrefslogtreecommitdiff
path: root/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp
blob: b10395e15bdcb6b88edc11eec422584933d3c7d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

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

#include "metainfo.h"
#include "propertymetainfo.h"

#include <QSharedData>
#include <QtDebug>
#include <QIcon>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>

#include <private/qdeclarativemetatype_p.h>

namespace QmlDesigner {

namespace Internal {

class NodeMetaInfoData : public QSharedData
{
public:
    typedef enum {
        No = -1,
        Unknown = 0,
        Yes = 1,
    } TristateBoolean;

public:
    NodeMetaInfoData(const MetaInfo &metaInfo) :
            metaInfo(metaInfo),
            isContainer(false),
            isFXItem(Unknown),
            icon(),
            category("misc")
    { }

    MetaInfo metaInfo;
    QString typeName;
    bool isContainer;
    TristateBoolean isFXItem;
    QHash<QString, PropertyMetaInfo> propertyMetaInfoHash;
    QIcon icon;
    QString category;
    QString qmlFile;
    QString defaultProperty;

    QString superClassType;
    int superClassMajorVersion;
    int superClassMinorVersion;

    int majorVersion;
    int minorVersion;
};

} // namespace Internal

/*!
\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
*/

NodeMetaInfo::NodeMetaInfo()
    : m_data(0)
{
    // create invalid node
}

NodeMetaInfo::NodeMetaInfo(const MetaInfo &metaInfo)
    : m_data(new Internal::NodeMetaInfoData(metaInfo))
{
}

NodeMetaInfo::~NodeMetaInfo()
{
}

/*!
  \brief Creates a copy of the handle.
  */
NodeMetaInfo::NodeMetaInfo(const NodeMetaInfo &other)
    : m_data(other.m_data)
{
}

/*!
  \brief Copies the handle.
  */
NodeMetaInfo &NodeMetaInfo::operator=(const NodeMetaInfo &other)
{
    if (this != &other)
        this->m_data = other.m_data;

    return *this;
}

/*!
  \brief Returns whether the meta information system knows about this type.
  */
bool NodeMetaInfo::isValid() const
{
    return (m_data.data() != 0);
}

MetaInfo NodeMetaInfo::metaInfo() const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return MetaInfo();
    }

    return m_data->metaInfo;
}

/*!
  \brief Returns all (direct and indirect) ancestor types.

  \throws InvalidMetaInfoException if the object is not valid
  */
QList<NodeMetaInfo> NodeMetaInfo::superClasses() const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return QList<NodeMetaInfo>();
    }

    QList<NodeMetaInfo> types;

    NodeMetaInfo superType = directSuperClass();
    if (superType.isValid()) {
        types += superType;
        types += superType.superClasses();
    }

    return types;
}

/*!
  \brief Returns direct ancestor type. An invalid type if there is none.
  */
NodeMetaInfo NodeMetaInfo::directSuperClass() const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return NodeMetaInfo();
    }

    if (!m_data->superClassType.isEmpty()) {
        int superClassMajorVersion = m_data->superClassMajorVersion;
        int superClassMinorVersion = m_data->superClassMinorVersion;
        if (superClassMajorVersion == -1) {
            // If we don't know the version of the super type, assume that it's the same like our version.
            superClassMajorVersion = majorVersion();
            superClassMinorVersion = minorVersion();
        }
        return m_data->metaInfo.nodeMetaInfo(m_data->superClassType,
                                             superClassMajorVersion, superClassMinorVersion);
    }
    return NodeMetaInfo();
}

/*!
  \brief Returns meta information for all properties, including properties inherited from base types.

  Returns a Hash with the name of the property as key and property meta information as value. Node
  In case there are multiple properties with the same name in the hierarchy the property defined
  in the more concrete subclass is chosen.

  \throws InvalidMetaInfoException if the object is not valid
  */
QHash<QString,PropertyMetaInfo> NodeMetaInfo::properties(bool resolveDotSyntax) const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return QHash<QString,PropertyMetaInfo>();
    }

    QHash<QString,PropertyMetaInfo> propertiesInfo;
    propertiesInfo = m_data->propertyMetaInfoHash;

    foreach (const NodeMetaInfo &nodeInfo, superClasses()) {
        QHash<QString,PropertyMetaInfo> superClassProperties = nodeInfo.properties();
        QHashIterator<QString,PropertyMetaInfo> iter(superClassProperties);
        while (iter.hasNext()) {
            iter.next();
            if (!propertiesInfo.contains(iter.key()))
                propertiesInfo.insert(iter.key(), iter.value());
        }
    }
    if (resolveDotSyntax) {
        QHashIterator<QString,PropertyMetaInfo> iter(dotProperties());
        while (iter.hasNext()) {
            iter.next();
            if (!propertiesInfo.contains(iter.key()))
                propertiesInfo.insert(iter.key(), iter.value());
        }
    }
    return propertiesInfo;
}

/*!
  \brief Returns meta information for all dot properties, including properties inherited from base types.

  */
QHash<QString,PropertyMetaInfo> NodeMetaInfo::dotProperties() const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return QHash<QString,PropertyMetaInfo>();
    }

    QHash<QString,PropertyMetaInfo> propertiesInfo;

    foreach (const QString &propertyName, properties().keys()) {
        if (property(propertyName).hasDotSubProperties()) {
                QString propertyType = property(propertyName).type();
                if (propertyType.right(1) == "*")
                    propertyType = propertyType.left(propertyType.size() - 1).trimmed();

                NodeMetaInfo nodeInfo(m_data->metaInfo.nodeMetaInfo(m_data->metaInfo.fromQtTypes(propertyType), majorVersion(), minorVersion()));
                if (nodeInfo.isValid()) {
                    QHashIterator<QString,PropertyMetaInfo> iter(nodeInfo.properties());
                    while (iter.hasNext()) {
                        iter.next();
                        if (!propertiesInfo.contains(iter.key()) && iter.key() != "objectName")
                            propertiesInfo.insert(propertyName + '.' + iter.key(), iter.value());
                    }
            }
        }
    }
    return propertiesInfo;
}

/*!
  \brief Returns meta information for a property. An invalid PropertyMetaInfo object if the given property name is unknown.

  \throws InvalidMetaInfoException if the object is not valid
  */
PropertyMetaInfo NodeMetaInfo::property(const QString &propertyName, bool resolveDotSyntax) const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return PropertyMetaInfo();
    }

    if (resolveDotSyntax && propertyName.contains('.')) {
        const QStringList nameParts = propertyName.split('.');
        NodeMetaInfo nodeInfo = *this;
        const int partCount = nameParts.size();
        for (int i = 0; i < partCount; ++i) {
            const QString namePart(nameParts.at(i));
            const PropertyMetaInfo propInfo = nodeInfo.property(namePart, false);

            if (!propInfo.isValid())
                break;

            if (i + 1 == partCount)
                return propInfo;

            QString propertyType = propInfo.type();
            if (propertyType.right(1) == "*")
                propertyType = propertyType.left(propertyType.size() - 1).trimmed();
            nodeInfo = m_data->metaInfo.nodeMetaInfo(m_data->metaInfo.fromQtTypes(propertyType), majorVersion(), minorVersion());
            if (!nodeInfo.isValid()) {
                qWarning() << "no type info available for" << propertyType;
                break;
            }
        }

        return PropertyMetaInfo();
    } else {
        PropertyMetaInfo propertyMetaInfo;

        if (hasLocalProperty(propertyName)) {
            propertyMetaInfo = m_data->propertyMetaInfoHash.value(propertyName, PropertyMetaInfo());
        } else {
            foreach (const NodeMetaInfo &superTypeMetaInfo, superClasses()) {
                Q_ASSERT(superTypeMetaInfo.isValid());
                propertyMetaInfo = superTypeMetaInfo.property(propertyName);
                if (propertyMetaInfo.isValid())
                    break;
            }
        }

        return propertyMetaInfo;
    }
}

/*!
  \brief Returns whether the type has a (not inherited) property.

  \throws InvalidMetaInfoException if the object is not valid
  */
bool NodeMetaInfo::hasLocalProperty(const QString &propertyName, bool resolveDotSyntax) const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return false;
    }

    if (resolveDotSyntax && propertyName.contains('.')) {
        const QStringList nameParts = propertyName.split('.');
        NodeMetaInfo nodeInfo = *this;
        const int partCount = nameParts.size();
        for (int i = 0; i < partCount; ++i) {
            QString namePart(nameParts.at(i));
            const PropertyMetaInfo propInfo = nodeInfo.property(namePart, false);

            if (!propInfo.isValid())
                break;

            if (i + 1 == partCount)
                return true;

            QString propertyType = propInfo.type();
            if (propertyType.right(1) == "*")
                propertyType = propertyType.left(propertyType.size() - 1).trimmed();
            nodeInfo = m_data->metaInfo.nodeMetaInfo(m_data->metaInfo.fromQtTypes(propertyType), majorVersion(), minorVersion());
            if (!nodeInfo.isValid()) {
                qWarning() << "no type info available for" << propertyType;
                break;
            }
        }

        return false;
    } else {
        return m_data->propertyMetaInfoHash.contains(propertyName);
    }
}

/*!
  \brief Returns whether the type has a (inherited or not inherited) property.

  \throws InvalidMetaInfoException if the object is not valid
  */
bool NodeMetaInfo::hasProperty(const QString &propertyName, bool resolveDotSyntax) const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return false;
    }

    if (hasLocalProperty(propertyName, resolveDotSyntax))
        return true;

    if (directSuperClass().isValid() && directSuperClass().hasProperty(propertyName, resolveDotSyntax))
        return true;

    return false;
}

void NodeMetaInfo::addProperty(const PropertyMetaInfo &property)
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return;
    }

    m_data->propertyMetaInfoHash.insert(property.name(), property);
}

/*!
  \brief Returns the name of the qml type.

  This is not necessarily the class name: E.g. the class defining "Item" is QDeclarativeItem.

  \throws InvalidMetaInfoException if the object is not valid
  */
QString NodeMetaInfo::typeName() const
{
    if (!isValid()) {
        return QString();
    }
    return m_data->typeName;
}

/*!
  \brief Returns the name of the major number of the qml type.

  \throws InvalidMetaInfoException if the object is not valid
*/
int NodeMetaInfo::majorVersion() const
{
    if (!isValid()) {
        return -1;
    }

    return m_data->majorVersion;
}


/*!
  \brief Returns the name of the minor number of the qml type to which the type is used.

  \throws InvalidMetaInfoException if the object is not valid
*/
int NodeMetaInfo::minorVersion() const
{
    if (!isValid()) {
        return -1;
    }

    return m_data->minorVersion;
}

bool NodeMetaInfo::availableInVersion(int majorVersion, int minorVersion) const
{
    return ((majorVersion > m_data->majorVersion)
            || (majorVersion == m_data->majorVersion && minorVersion >= m_data->minorVersion))
            || (majorVersion == -1 && minorVersion == -1);
}

bool NodeMetaInfo::hasDefaultProperty() const
{
    if (!isValid()) {
        return false;
    }

    return !m_data->defaultProperty.isNull();
}

QString NodeMetaInfo::defaultProperty() const
{
    if (!isValid()) {
        return QString();
    }

    return m_data->defaultProperty;
}

void NodeMetaInfo::setDefaultProperty(const QString &defaultProperty)
{
     if (!isValid()) {
        return;
    }

    m_data->defaultProperty = defaultProperty;
}

void NodeMetaInfo::setSuperClass(const QString &typeName, int majorVersion, int minorVersion)
{
    if (!isValid()) {
        return;
    }
    m_data->superClassType = typeName;
    m_data->superClassMajorVersion = majorVersion;
    m_data->superClassMinorVersion = minorVersion;
}

void NodeMetaInfo::setInvalid()
{
    if (!isValid())
        return;

    m_data = 0;
}

void NodeMetaInfo::setType(const QString &typeName, int majorVersion, int minorVersion)
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return;
    }
    m_data->typeName = typeName;
    m_data->majorVersion = majorVersion;
    m_data->minorVersion = minorVersion;
}

uint qHash(const NodeMetaInfo &nodeMetaInfo)
{
    if (!nodeMetaInfo.isValid())
        return 0;
    return qHash(nodeMetaInfo.m_data->typeName);
}

bool operator==(const NodeMetaInfo &firstNodeInfo,
                const NodeMetaInfo &secondNodeInfo)
{
    if (!firstNodeInfo.isValid() || !secondNodeInfo.isValid())
        return false;
    return firstNodeInfo.m_data->typeName == secondNodeInfo.m_data->typeName;
}

/*!
  \brief Returns whether objects of these type can have children.

  \throws InvalidMetaInfoException if the object is not valid
  */
bool NodeMetaInfo::isContainer() const
{
    // TODO KAI: Is this too generic?
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return false;
    }
    return m_data->isContainer;
}

void NodeMetaInfo::setIsContainer(bool isContainer)
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return;
    }
    m_data->isContainer = isContainer;
}

QString NodeMetaInfo::componentString() const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return QString();
    }
    return m_data->qmlFile;
}

QIcon NodeMetaInfo::icon() const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return QIcon();
    }
    return m_data->icon;
}

void NodeMetaInfo::setIcon(const QIcon &icon)
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return;
    }
    m_data->icon = icon;
}

bool NodeMetaInfo::isComponent() const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return false;
    }

    return !m_data->qmlFile.isEmpty();
}

/*!
  \brief Returns whether the type inherits from a type.

  \throws InvalidMetaInfoException if the object is not valid
  */
bool NodeMetaInfo::isSubclassOf(const QString &type, int majorVersion, int minorVersion) const
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return false;
    }

    if (typeName() == type
        && availableInVersion(majorVersion, minorVersion))
        return true;

    foreach (const NodeMetaInfo &superClass, superClasses()) {
        if (superClass.typeName() == type
            && superClass.availableInVersion(majorVersion, minorVersion))
            return true;
    }
    return false;
}

void NodeMetaInfo::setQmlFile(const QString &filePath)
{
    if (!isValid()) {
        qWarning() << "NodeMetaInfo is invalid";
        return;
    }
    m_data->qmlFile = filePath;
}

//QDataStream& operator<<(QDataStream& stream, const NodeMetaInfo& nodeMetaInfo)
//{
//    stream << nodeMetaInfo.typeName();
//    stream << nodeMetaInfo.majorVersion();
//    stream << nodeMetaInfo.minorVersionTo();
//
//    return stream;
//}
//
//QDataStream& operator>>(QDataStream& stream, NodeMetaInfo& nodeMetaInfo)
//{
//    QString typeName;
//    int minorVersion;
//    int majorVersion;
//
//    stream >> minorVersion;
//    stream >> majorVersion;
//    stream >> typeName;
//
//    nodeMetaInfo = MetaInfo::global().nodeMetaInfo(typeName/*, majorVersion ,minorVersion*/);
//
//    return stream;
//}

} // namespace QmlDesigner