objectnodeinstance.cpp 22.8 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "objectnodeinstance.h"

Kai Koehne's avatar
Kai Koehne committed
32
33
34
35
36
37
38
39
40
#include "qmlgraphicsitemnodeinstance.h"
#include "graphicsobjectnodeinstance.h"
#include "graphicsviewnodeinstance.h"
#include "graphicsscenenodeinstance.h"
#include "graphicswidgetnodeinstance.h"
#include "qmlviewnodeinstance.h"
#include "widgetnodeinstance.h"
#include "proxywidgetnodeinstance.h"

41
42
43
44
45
#include <invalidreparentingexception.h>
#include <invalidnodeinstanceexception.h>
#include <notimplementedexception.h>
#include <noanchoringpossibleexception.h>

Kai Koehne's avatar
Kai Koehne committed
46
47
#include <variantproperty.h>
#include <nodelistproperty.h>
48
49
#include <metainfo.h>
#include <propertymetainfo.h>
Kai Koehne's avatar
Kai Koehne committed
50
51
52

#include <QEvent>
#include <QGraphicsScene>
53
54
55
56
#include <QDeclarativeContext>
#include <QDeclarativeError>
#include <QDeclarativeEngine>
#include <QDeclarativeProperty>
57
#include <QSharedPointer>
58
59
60
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QPixmapCache>
Kai Koehne's avatar
Kai Koehne committed
61

62
#include <private/qdeclarativebinding_p.h>
63
64
#include <private/qdeclarativemetatype_p.h>
#include <private/qdeclarativevaluetype_p.h>
65
66
67
68

namespace QmlDesigner {
namespace Internal {

69

70
71
72
73
74
75
ChildrenChangeEventFilter::ChildrenChangeEventFilter(QObject *parent)
    : QObject(parent)
{
}


76
bool ChildrenChangeEventFilter::eventFilter(QObject * /*object*/, QEvent *event)
77
78
79
{
    switch (event->type()) {
        case QEvent::ChildAdded:
80
81
82
83
84
        case QEvent::ChildRemoved:
            {
                QChildEvent *childEvent = static_cast<QChildEvent*>(event);
                emit childrenChanged(childEvent->child()); break;
            }
85
86
87
88
89
90
91
92
        default: break;
    }

    return false;
}

ObjectNodeInstance::ObjectNodeInstance(QObject *object)
    : m_deleteHeldInstance(true),
93
94
    m_object(object),
    m_metaObject(0)
95
{
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
}

ObjectNodeInstance::~ObjectNodeInstance()
{
    destroy();
}

static bool isChildrenProperty(const QString &name)
{
    return name == "data" || name == "children" || name == "resources";
}

static void specialRemoveParentForQmlGraphicsItemChildren(QObject *object)
{
111
    QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(object);
112
113
114
115
116
117
118
119
    if (item && item->scene())
        item->scene()->removeItem(item);

    object->setParent(0);
}

void ObjectNodeInstance::destroy()
{
Marco Bubke's avatar
Marco Bubke committed
120
121
122
123
124
125
126
    if (deleteHeldInstance()) {
        // Remove from old property
        if (object() && modelNode().isValid() && modelNode().parentProperty().isValid()) {
            NodeAbstractProperty parentProperty = modelNode().parentProperty();
            ModelNode parentNode = parentProperty.parentModelNode();
            if (parentNode.isValid() && nodeInstanceView()->hasInstanceForNode(parentNode)) {
                NodeInstance parentInstance = nodeInstanceView()->instanceForNode(parentNode);
127
                reparent(parentInstance, parentProperty.name(), NodeInstance() , QString());
128
129
130
            }
        }

Marco Bubke's avatar
Marco Bubke committed
131
132
133
134
135
136
137
138
        if (!m_id.isEmpty()) {
            context()->engine()->rootContext()->setContextProperty(m_id, 0);
        }

        if (object()) {
            QObject *obj = object();
            m_object.clear();
            delete obj;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        }
    }
}

ModelNode ObjectNodeInstance::modelNode() const
{
    return m_modelNode;
}

void ObjectNodeInstance::setModelNode(const ModelNode &node)
{
    m_modelNode = node;
}

NodeInstanceView *ObjectNodeInstance::nodeInstanceView() const
{
    return m_nodeInstanceView.data();
}

158
void ObjectNodeInstance::setNodeInstanceView(NodeInstanceView *view)
159
{
160
161
    Q_ASSERT(!m_nodeInstanceView.data());

162
163
164
    m_nodeInstanceView = view;
}

165
166
167
168
169
170
171
172
173
174
175
176
static bool hasPropertiesWitoutNotifications(const QMetaObject *metaObject)
{
    for(int propertyIndex = QObject::staticMetaObject.propertyCount(); propertyIndex < metaObject->propertyCount(); propertyIndex++) {
        if (!metaObject->property(propertyIndex).hasNotifySignal())
            return true;
    }

    return false;
}

void ObjectNodeInstance::initializePropertyWatcher(const ObjectNodeInstance::Pointer &objectNodeInstance)
{
177
178
    if (!objectNodeInstance->modelNode().metaInfo().isComponent()) { // TODO: this is a nasty workaround which needs to be removed
        const QMetaObject *metaObject = objectNodeInstance->object()->metaObject();
Kai Koehne's avatar
Kai Koehne committed
179
        m_metaObject = new NodeInstanceMetaObject(objectNodeInstance, nodeInstanceView()->engine());
180
181
182
183
        for(int propertyIndex = QObject::staticMetaObject.propertyCount(); propertyIndex < metaObject->propertyCount(); propertyIndex++) {
            if (QDeclarativeMetaType::isQObject(metaObject->property(propertyIndex).userType())) {
                QObject *propertyObject = QDeclarativeMetaType::toQObject(metaObject->property(propertyIndex).read(objectNodeInstance->object()));
                if (propertyObject && hasPropertiesWitoutNotifications(propertyObject->metaObject())) {
Kai Koehne's avatar
Kai Koehne committed
184
                    new NodeInstanceMetaObject(objectNodeInstance, propertyObject, metaObject->property(propertyIndex).name(), nodeInstanceView()->engine());
185
                }
186
187
            }
        }
188
189
    } else {
        qWarning() << "dynamic properties are not supported for components";
190
191
192
193
194
    }

    m_signalSpy.setObjectNodeInstance(objectNodeInstance);
}

195
196
void ObjectNodeInstance::setId(const QString &id)
{
Marco Bubke's avatar
Marco Bubke committed
197
198
    if (!m_id.isEmpty()) {
        context()->engine()->rootContext()->setContextProperty(m_id, 0);
199
200
    }

201
202
203
    if (!id.isEmpty()) {
        context()->engine()->rootContext()->setContextProperty(id, object()); // will also force refresh of all bindings
    }
Marco Bubke's avatar
Marco Bubke committed
204

Marco Bubke's avatar
Marco Bubke committed
205
    m_id = id;
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
}

bool ObjectNodeInstance::isQmlGraphicsItem() const
{
    return false;
}

bool ObjectNodeInstance::isGraphicsScene() const
{
    return false;
}

bool ObjectNodeInstance::isGraphicsView() const
{
    return false;
}

bool ObjectNodeInstance::isGraphicsWidget() const
{
    return false;
}

bool ObjectNodeInstance::isProxyWidget() const
{
    return false;
}

bool ObjectNodeInstance::isWidget() const
{
    return false;
}

238
bool ObjectNodeInstance::isQDeclarativeView() const
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
{
    return false;
}

bool ObjectNodeInstance::isGraphicsObject() const
{
    return false;
}

bool ObjectNodeInstance::isTransition() const
{
    return false;
}

bool ObjectNodeInstance::equalGraphicsItem(QGraphicsItem * /*item*/) const
{
    return false;
}

QTransform ObjectNodeInstance::transform() const
{
    return QTransform();
}

QTransform ObjectNodeInstance::customTransform() const
{
    return QTransform();
}

QTransform ObjectNodeInstance::sceneTransform() const
{
    return QTransform();
}

double ObjectNodeInstance::rotation() const
{
    return 0.0;
}

double ObjectNodeInstance::scale() const
{
    return 1.0;
}

QList<QGraphicsTransform *> ObjectNodeInstance::transformations() const
{
    QList<QGraphicsTransform *> transformationsList;

    return transformationsList;
}

QPointF ObjectNodeInstance::transformOriginPoint() const
{
    return QPoint();
}

double ObjectNodeInstance::zValue() const
{
    return 0.0;
}

double ObjectNodeInstance::opacity() const
{
    return 1.0;
}

bool ObjectNodeInstance::hasAnchor(const QString &/*name*/) const
{
    return false;
}

bool ObjectNodeInstance::isAnchoredBy() const
{
    return false;
}

QPair<QString, NodeInstance> ObjectNodeInstance::anchor(const QString &/*name*/) const
{
    return qMakePair(QString(), NodeInstance());
}


321
static bool isList(const QDeclarativeProperty &metaProperty)
322
{
323
    return metaProperty.propertyTypeCategory() == QDeclarativeProperty::List;
324
325
}

326
static bool isObject(const QDeclarativeProperty &metaProperty)
327
{
328
    return metaProperty.propertyTypeCategory() == QDeclarativeProperty::Object;
329
330
331
332
}

static QVariant objectToVariant(QObject *object)
{
333
    return QVariant::fromValue(object);
334
335
}

336
337
338
339
340
static bool hasFullImplementedListInterface(const QDeclarativeListReference &list)
{
    return list.isValid() && list.canCount() && list.canAt() && list.canAppend() && list.canClear();
}

341
static void removeObjectFromList(const QDeclarativeProperty &metaProperty, QObject *objectToBeRemoved, QDeclarativeEngine * engine)
342
{
343
    QDeclarativeListReference listReference(metaProperty.object(), metaProperty.name().toLatin1(), engine);
344
345
346
347
348
349

#ifndef QT_DEBUG
    if (!hasFullImplementedListInterface(listReference))
        return;
#endif

350
351
352
353
    int count = listReference.count();

    QObjectList objectList;

354
355
    Q_ASSERT(listReference.canCount());
    Q_ASSERT(listReference.canAt());
356
357
358
359
360
361
    for(int i = 0; i < count; i ++) {
        QObject *listItem = listReference.at(i);
        if (listItem != objectToBeRemoved)
            objectList.append(listItem);
    }

362
    Q_ASSERT(listReference.canClear());
363
364
    listReference.clear();

365
    Q_ASSERT(listReference.canAppend());
366
367
    foreach(QObject *object, objectList)
        listReference.append(object);
368
369
370
371
}

void ObjectNodeInstance::removeFromOldProperty(QObject *object, QObject *oldParent, const QString &oldParentProperty)
{
372
    QDeclarativeProperty metaProperty(oldParent, oldParentProperty, context());
373

Marco Bubke's avatar
Marco Bubke committed
374
375
376
    if (!metaProperty.isValid())
        return;

377
378
379
380
381
382
383
384
385
386
387
    if (isList(metaProperty)) {
        removeObjectFromList(metaProperty, object, nodeInstanceView()->engine());
    } else if (isObject(metaProperty)) {
        resetProperty(object, oldParentProperty);
    }

    object->setParent(0);
}

void ObjectNodeInstance::addToNewProperty(QObject *object, QObject *newParent, const QString &newParentProperty)
{
388
    QDeclarativeProperty metaProperty(newParent, newParentProperty, context());
389
390

    if (isList(metaProperty)) {
391
        QDeclarativeListReference list = qvariant_cast<QDeclarativeListReference>(metaProperty.read());
392
393
394
395
396
397

#ifndef QT_DEBUG
        if (!hasFullImplementedListInterface(listReference))
            return;
#endif

398
        Q_ASSERT(list.canAppend());
399
        list.append(object);
400
401
402
403
404
405
406
407
    } else if (isObject(metaProperty)) {
        metaProperty.write(objectToVariant(object));
    }
    object->setParent(newParent);

    Q_ASSERT(objectToVariant(object).isValid());
}

408
static void specialSetParentForQmlGraphicsItemChildren(QObject *object, QDeclarativeItem *parent)
409
{
410
    QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(object);
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
    if (item)
        item->setParentItem(parent);
    else
        object->setParent(parent);
}

void ObjectNodeInstance::reparent(const NodeInstance &oldParentInstance, const QString &oldParentProperty, const NodeInstance &newParentInstance, const QString &newParentProperty)
{
    if (oldParentInstance.isValid()) {
        if (oldParentInstance.isQmlGraphicsItem() && isChildrenProperty(oldParentProperty))
            specialRemoveParentForQmlGraphicsItemChildren(object());
        else
            removeFromOldProperty(object(), oldParentInstance.internalObject(), oldParentProperty);
    }

    if (newParentInstance.isValid()) {
        if (newParentInstance.isQmlGraphicsItem() && isChildrenProperty(newParentProperty))
428
            specialSetParentForQmlGraphicsItemChildren(object(), qobject_cast<QDeclarativeItem*>(newParentInstance.internalObject()));
429
430
431
432
433
434
435
        else
            addToNewProperty(object(), newParentInstance.internalObject(), newParentProperty);
    }

    refreshBindings(context()->engine()->rootContext());
}

436
void ObjectNodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
437
{
Marco Bubke's avatar
Marco Bubke committed
438
    QDeclarativeProperty property(object(), name, context());
439

Marco Bubke's avatar
Marco Bubke committed
440
441
442
    if (!property.isValid())
        return;

443
444
445
446
447
448
449
450
451
    QVariant oldValue = property.read();
    if (oldValue.type() == QVariant::Url) {
        QUrl url = oldValue.toUrl();
        QString path = url.toLocalFile();
        if (QFileInfo(path).exists() && nodeInstanceView() && !path.isEmpty())
            nodeInstanceView()->removeFilePropertyFromFileSystemWatcher(object(), name, path);
    }


Marco Bubke's avatar
Marco Bubke committed
452
    property.write(value);
453
454
455
456
457
458
459
460
461
462

    QVariant newValue = property.read();
    if (newValue.type() == QVariant::Url) {
        QUrl url = newValue.toUrl();
        QString path = url.toLocalFile();
        if (QFileInfo(path).exists() && nodeInstanceView() && !path.isEmpty())
            nodeInstanceView()->addFilePropertyToFileSystemWatcher(object(), name, path);
    }


463
464
465
466
}

void ObjectNodeInstance::setPropertyBinding(const QString &name, const QString &expression)
{
467
    QDeclarativeProperty metaProperty(object(), name, context());
Marco Bubke's avatar
Marco Bubke committed
468
469
470
471
472
473

    if (!metaProperty.isValid())
        return;

    if (metaProperty.isProperty()) {
        QDeclarativeBinding *binding = new QDeclarativeBinding(expression, object(), context());
474
475
476
        binding->setTarget(metaProperty);
        binding->setNotifyOnValueChanged(true);
        QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::setBinding(metaProperty, binding);
477
478
        if (oldBinding)
            oldBinding->destroy();
479
        binding->update();
480
481
482
483
484
485
    } else {
        qWarning() << "Cannot set binding for property" << name << ": property is unknown for type"
                   << (modelNode().isValid() ? modelNode().type() : "unknown");
    }
}

486
void ObjectNodeInstance::deleteObjectsInList(const QDeclarativeProperty &metaProperty)
487
488
{
    QObjectList objectList;
489
    QDeclarativeListReference list = qvariant_cast<QDeclarativeListReference>(metaProperty.read());
490

491
492
493
494
495
#ifndef QT_DEBUG
    if (!hasFullImplementedListInterface(list))
        return;
#endif

496
497
498
    Q_ASSERT(list.canCount());
    Q_ASSERT(list.canAt());

499
500
    for(int i = 0; i < list.count(); i++) {
        objectList += list.at(i);
501
502
    }

503
504
    Q_ASSERT(list.canClear());

505
    list.clear();
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
}

void ObjectNodeInstance::resetProperty(const QString &name)
{
    resetProperty(object(), name);

    if (name == "font.pixelSize")
        resetProperty(object(), "font.pointSize");

    if (name == "font.pointSize")
        resetProperty(object(), "font.pixelSize");
}

NodeInstance ObjectNodeInstance::instanceForNode(const ModelNode &node, const QString &fullname)
{
    if (nodeInstanceView()->hasInstanceForNode(node)) {
        return nodeInstanceView()->instanceForNode(node);
    } else {
        NodeInstance instance(nodeInstanceView()->loadNode(node));
        m_modelAbstractPropertyHash.insert(fullname, instance);
        return instance;
    }
}

530
531
532
533
void ObjectNodeInstance::refreshProperty(const QString &name)
{
    QDeclarativeProperty property(object(), name, context());

Marco Bubke's avatar
Marco Bubke committed
534
535
536
    if (!property.isValid())
        return;

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    QVariant oldValue(property.read());

    if (property.isResettable())
        property.reset();
    else
        property.write(resetValue(name));

    if (oldValue.type() == QVariant::Url) {
        QByteArray key = oldValue.toUrl().toEncoded(QUrl::FormattingOption(0x100));
        QString pixmapKey = QString::fromLatin1(key.constData(), key.count());
        QPixmapCache::remove(pixmapKey);
    }

    property.write(oldValue);
}

553
554
555
556
void ObjectNodeInstance::resetProperty(QObject *object, const QString &propertyName)
{
    m_modelAbstractPropertyHash.remove(propertyName);

557
558
    QDeclarativeProperty property(object, propertyName, context());

Marco Bubke's avatar
Marco Bubke committed
559
560
561
    if (!property.isValid())
        return;

562
563
564
565
566
567
568
569
    QVariant oldValue = property.read();
    if (oldValue.type() == QVariant::Url) {
        QUrl url = oldValue.toUrl();
        QString path = url.toLocalFile();
        if (QFileInfo(path).exists() && nodeInstanceView())
            nodeInstanceView()->removeFilePropertyFromFileSystemWatcher(object, propertyName, path);
    }

570

571
    QDeclarativeAbstractBinding *binding = QDeclarativePropertyPrivate::binding(property);
572
573
    if (binding) {
        binding->setEnabled(false, 0);
574
        binding->destroy();
575
576
    }

577
578
579
580
    if (property.isResettable()) {
        property.reset();
    } else if (property.isWritable()) {
        if (property.read() == resetValue(propertyName))
581
            return;
582
583
        property.write(resetValue(propertyName));
    } else if (property.propertyTypeCategory() == QDeclarativeProperty::List) {
584
585
586
587
588
589
        QDeclarativeListReference list = qvariant_cast<QDeclarativeListReference>(property.read());
#ifndef QT_DEBUG
        if (!hasFullImplementedListInterface(list))
            return;
#endif
        list.clear();
590
591
592
593
594
595
596
597
598
599
600
    }
}

QVariant ObjectNodeInstance::property(const QString &name) const
{
    if (m_modelAbstractPropertyHash.contains(name))
        return QVariant::fromValue(m_modelAbstractPropertyHash.value(name));


    // TODO: handle model nodes

601
    QDeclarativeProperty metaProperty(object(), name, context());
602
603
604
605
606
607
608
609
610
611
612
    if (metaProperty.property().isEnumType()) {
        QVariant value = object()->property(name.toLatin1());
        return metaProperty.property().enumerator().valueToKey(value.toInt());
    }

    if (metaProperty.propertyType() == QVariant::Url) {
        QUrl url = metaProperty.read().toUrl();
        if (url.isEmpty())
            return QVariant();

        if (url.scheme() == "file") {
613
            int basePathLength = nodeInstanceView()->model()->fileUrl().toLocalFile().lastIndexOf('/');
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
            return QUrl(url.toLocalFile().mid(basePathLength + 1));
        }
    }

    return metaProperty.read();
}


void ObjectNodeInstance::setDeleteHeldInstance(bool deleteInstance)
{
    m_deleteHeldInstance = deleteInstance;
}

bool ObjectNodeInstance::deleteHeldInstance() const
{
    return m_deleteHeldInstance;
}

632
ObjectNodeInstance::Pointer ObjectNodeInstance::create(const NodeMetaInfo &nodeMetaInfo, QDeclarativeContext *context, QObject *objectToBeWrapped)
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
{
    QObject *object = 0;
    if (objectToBeWrapped)
        object = objectToBeWrapped;
    else
        object = createObject(nodeMetaInfo, context);

    Pointer instance(new ObjectNodeInstance(object));

    if (objectToBeWrapped)
        instance->setDeleteHeldInstance(false); // the object isn't owned

    instance->populateResetValueHash();

    return instance;
}

650
QObject* ObjectNodeInstance::createObject(const NodeMetaInfo &metaInfo, QDeclarativeContext *context)
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
{
    QObject *object = metaInfo.createInstance(context);

    if (object == 0)
        throw InvalidNodeInstanceException(__LINE__, __FUNCTION__, __FILE__);

    return object;
}

QObject *ObjectNodeInstance::object() const
{
    return m_object.data();
}

bool ObjectNodeInstance::hasContent() const
{
    return false;
}

void ObjectNodeInstance::updateAnchors()
{
}

674
QDeclarativeContext *ObjectNodeInstance::context() const
675
{
676
    QDeclarativeContext *context = QDeclarativeEngine::contextForObject(object());
677
678
    if (context)
        return context;
679
    else if (nodeInstanceView())
680
681
682
683
684
685
686
687
688
        return nodeInstanceView()->engine()->rootContext();

    return 0;
}

void ObjectNodeInstance::paintUpdate()
{
}

Marco Bubke's avatar
Marco Bubke committed
689
690
691
692
693
694
695
696
void ObjectNodeInstance::activateState()
{
}

void ObjectNodeInstance::deactivateState()
{
}

697
698
699
700
701
702
703
704
QStringList propertyNameForWritableProperties(QObject *object, const QString &baseName = QString())
{
    QStringList propertyNameList;

    const QMetaObject *metaObject = object->metaObject();
    for (int index = 0; index < metaObject->propertyCount(); ++index) {
        QMetaProperty metaProperty = metaObject->property(index);
        if (metaProperty.isReadable() && !metaProperty.isWritable()) {
705
            QObject *childObject = QDeclarativeMetaType::toQObject(metaProperty.read(object));
706
            if (childObject)
707
                propertyNameList.append(propertyNameForWritableProperties(childObject, baseName +  QString::fromUtf8(metaProperty.name()) + '.'));
708
709
        } else if (QDeclarativeValueTypeFactory::valueType(metaProperty.userType())) {
            QDeclarativeValueType *valueType = QDeclarativeValueTypeFactory::valueType(metaProperty.userType());
710
            valueType->setValue(metaProperty.read(object));
711
            propertyNameList.append(propertyNameForWritableProperties(valueType, baseName +  QString::fromUtf8(metaProperty.name()) + '.'));
712
713
714
715
716
717
718
719
720
721
722
723
724
        } else if (metaProperty.isReadable() && metaProperty.isWritable()) {
            propertyNameList.append(baseName + QString::fromUtf8(metaProperty.name()));
        }
    }

    return propertyNameList;
}

void ObjectNodeInstance::populateResetValueHash()
{
    QStringList propertyNameList = propertyNameForWritableProperties(object());

    foreach(const QString &propertyName, propertyNameList) {
725
        QDeclarativeProperty metaProperty(object(), propertyName, context());
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
        if (metaProperty.isWritable())
            m_resetValueHash.insert(propertyName, metaProperty.read());
    }
}

QVariant ObjectNodeInstance::resetValue(const QString &propertyName) const
{
    return m_resetValueHash.value(propertyName);
}

void ObjectNodeInstance::paint(QPainter * /*painter*/) const
{
}

bool ObjectNodeInstance::isTopLevel() const
{
    return false;
}

QObject *ObjectNodeInstance::parent() const
{
    if (!object())
        return 0;

    return object()->parent();
}

QRectF ObjectNodeInstance::boundingRect() const
{
    return QRect();
}

QPointF ObjectNodeInstance::position() const
{
    return QPointF();
}

QSizeF ObjectNodeInstance::size() const
{
    return QSizeF();
}

Marco Bubke's avatar
Marco Bubke committed
768
769
770
771
772
int ObjectNodeInstance::penWidth() const
{
    return 0;
}

773
static bool metaObjectHasNotPropertyName(NodeInstanceMetaObject *metaObject, const QString &propertyName)
774
{
775
776
777
778
779
780
781
782
783
784
    for (int i = 0; i < metaObject->count(); i++) {
        if (metaObject->name(i) == propertyName)
            return false;
    }

    return true;
}

void ObjectNodeInstance::createDynamicProperty(const QString &name, const QString &/*typeName*/)
{
785
786
787
788
789
790
    if (m_metaObject == 0) {
        qWarning() << "dynamic properties are not supported for components";
        return;
    }


791
792
    if (metaObjectHasNotPropertyName(m_metaObject, name))
        m_metaObject->createNewProperty(name);
793
794
795
796
797
}

/**
  Force all bindings in this or a sub context to be re-evaluated.
  */
798
void ObjectNodeInstance::refreshBindings(QDeclarativeContext *context)
799
800
{
    // TODO: Maybe do this via a timer to prevent update flooding
801
802
803

    static int i = 0;
    context->setContextProperty(QString("__dummy_%1").arg(i++), true);
804
805
}

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
bool ObjectNodeInstance::updateStateVariant(const NodeInstance &/*target*/, const QString &/*propertyName*/, const QVariant &/*value*/)
{
    return false;
}

bool ObjectNodeInstance::updateStateBinding(const NodeInstance &/*target*/, const QString &/*propertyName*/, const QString &/*expression*/)
{
    return false;
}

bool ObjectNodeInstance::resetStateProperty(const NodeInstance &/*target*/, const QString &/*propertyName*/, const QVariant &/*resetValue*/)
{
    return false;
}

821
822
823
}
}