nodeinstance.cpp 18 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
32
33
34
35
36
37
38
39
40
41
42
43
44
**
** 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 "nodeinstance.h"

#include "objectnodeinstance.h"
#include "metainfo.h"
#include "graphicswidgetnodeinstance.h"
#include "widgetnodeinstance.h"
#include "qmlgraphicsitemnodeinstance.h"
#include "graphicsscenenodeinstance.h"
#include "graphicsviewnodeinstance.h"
#include "nodeinstanceview.h"
#include "qmlviewnodeinstance.h"
#include "dummynodeinstance.h"
#include "componentnodeinstance.h"
#include "qmltransitionnodeinstance.h"
#include "qmlpropertychangesnodeinstance.h"
Marco Bubke's avatar
Marco Bubke committed
45
#include "behaviornodeinstance.h"
46
47
48
49
50
51
52
53
54
#include "qmlstatenodeinstance.h"
#include "nodeabstractproperty.h"
#include "variantproperty.h"

#include <invalidnodeinstanceexception.h>

#include <QHash>
#include <QSet>

55
#include <QtDeclarative/QDeclarativeEngine>
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

/*!
  \class QmlDesigner::NodeInstance
  \ingroup CoreInstance
  \brief NodeInstance is a common handle for the actual object representation of a ModelNode.

   NodeInstance abstracts away the differences e.g. in terms of position and size
   for QWidget, QGraphicsView, QLayout etc objects. Multiple NodeInstance objects can share
   the pointer to the same instance object. The actual instance will be deleted when
   the last NodeInstance object referencing to it is deleted. This can be disabled by
   setDeleteHeldInstance().

   \see QmlDesigner::NodeInstanceView
*/

namespace QmlDesigner {

/*!
\brief Constructor - creates a invalid NodeInstance


\see NodeInstanceView
*/
NodeInstance::NodeInstance()
{
}

/*!
\brief Destructor

*/
NodeInstance::~NodeInstance()
{
}

/*!
\brief Constructor - creates a valid NodeInstance

*/
NodeInstance::NodeInstance(const Internal::ObjectNodeInstance::Pointer &abstractInstance)
  : m_nodeInstance(abstractInstance)
{

}


NodeInstance::NodeInstance(const NodeInstance &other)
  : m_nodeInstance(other.m_nodeInstance)
{
}

NodeInstance &NodeInstance::operator=(const NodeInstance &other)
{
    m_nodeInstance = other.m_nodeInstance;
    return *this;
}

/*!
\brief Paints the NodeInstance with this painter.
\param painter used QPainter
*/
void NodeInstance::paint(QPainter *painter) const
{
    m_nodeInstance->paint(painter);
}

/*!
\brief Creates a new NodeInstace for this NodeMetaInfo

\param metaInfo MetaInfo for which a Instance should be created
126
\param context QDeclarativeContext which should be used
127
128
129
130
\returns Internal Pointer of a NodeInstance
\see NodeMetaInfo
*/
Internal::ObjectNodeInstance::Pointer NodeInstance::createInstance(const NodeMetaInfo &metaInfo,
131
                                                                     QDeclarativeContext *context, QObject *objectToBeWrapped)
132
133
134
{
    Internal::ObjectNodeInstance::Pointer instance;

135
136
    if (!metaInfo.isValid())
        instance = Internal::DummyNodeInstance::create();
137
    else if (metaInfo.isSubclassOf("QtWebKit/WebView", 1, 0))
138
        instance = Internal::DummyNodeInstance::create();
139
    else if (metaInfo.isSubclassOf("Qt/QWidget", 4, 7))
140
        instance = Internal::DummyNodeInstance::create();
141
    else if (metaInfo.isSubclassOf("Qt/QGraphicsView", 4, 7))
142
        instance = Internal::GraphicsViewNodeInstance::create(metaInfo, context, objectToBeWrapped);
143
    else if (metaInfo.isSubclassOf("Qt/QDeclarativeView", 4, 7))
144
        instance = Internal::QDeclarativeViewNodeInstance::create(metaInfo, context, objectToBeWrapped);
145
    else if (metaInfo.isSubclassOf("Qt/QGraphicsWidget", 4, 7))
146
        instance = Internal::GraphicsWidgetNodeInstance::create(metaInfo, context, objectToBeWrapped);
147
    else if (metaInfo.isSubclassOf("Qt/Item", 4, 7))
148
        instance = Internal::QmlGraphicsItemNodeInstance::create(metaInfo, context, objectToBeWrapped);
149
    else if (metaInfo.isSubclassOf("Qt/QGraphicsScene", 4, 7))
150
        instance = Internal::GraphicsSceneNodeInstance::create(metaInfo, context, objectToBeWrapped);
151
    else if (metaInfo.isSubclassOf("Qt/Component", 4, 7))
152
        instance = Internal::ComponentNodeInstance::create(metaInfo, context, objectToBeWrapped);
153
    else if (metaInfo.isSubclassOf("Qt/PropertyChanges", 4, 7))
154
        instance = Internal::QmlPropertyChangesNodeInstance::create(metaInfo, context, objectToBeWrapped);
155
    else if (metaInfo.isSubclassOf("Qt/State", 4, 7))
156
        instance = Internal::QmlStateNodeInstance::create(metaInfo, context, objectToBeWrapped);
157
    else if (metaInfo.isSubclassOf("Qt/Transition", 4, 7))
158
        instance = Internal::QmlTransitionNodeInstance::create(metaInfo, context, objectToBeWrapped);
159
    else if (metaInfo.isSubclassOf("Qt/Behavior", 4, 7))
Marco Bubke's avatar
Marco Bubke committed
160
        instance = Internal::BehaviorNodeInstance::create(metaInfo, context, objectToBeWrapped);
161
    else if (metaInfo.isSubclassOf("Qt/QtObject", 4, 7))
162
        instance = Internal::ObjectNodeInstance::create(metaInfo, context, objectToBeWrapped);
163
164
    else
        instance = Internal::DummyNodeInstance::create();
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180


    return instance;
}



NodeInstance NodeInstance::create(NodeInstanceView *nodeInstanceView, const ModelNode &node, QObject *objectToBeWrapped)
{
    Q_ASSERT(node.isValid());
    Q_ASSERT(nodeInstanceView);

    // For the moment just use the root context of the engine
    // for all items. However, this is a hack ... ideally we should
    // rebuild the same context hierarchy as the qml compiler does

181
    QDeclarativeContext *context = nodeInstanceView->engine()->rootContext();
182
183
184
185
186

    NodeInstance instance(createInstance(node.metaInfo(), context, objectToBeWrapped));

    instance.m_nodeInstance->setModelNode(node);

187
    instance.m_nodeInstance->setNodeInstanceView(nodeInstanceView);
188

189
190
    instance.m_nodeInstance->initializePropertyWatcher(instance.m_nodeInstance);

191
192
    instance.setId(node.id());

Marco Bubke's avatar
Marco Bubke committed
193
194
    QObject::connect(instance.internalObject(), SIGNAL(destroyed(QObject*)), nodeInstanceView, SLOT(removeIdFromContext(QObject*)));

195
196
197
198
199
200
201
202
203
204
    foreach (const VariantProperty &property, node.variantProperties()) {
        if (property.isDynamic())
            instance.setPropertyDynamicVariant(property.name(), property.dynamicTypeName(), property.value());
        else
            instance.setPropertyVariant(property.name(), property.value());
    }

    return instance;
}

205
NodeInstance NodeInstance::create(NodeInstanceView *nodeInstanceView, const NodeMetaInfo &metaInfo, QDeclarativeContext *context)
206
207
{
    NodeInstance instance(createInstance(metaInfo, context, 0));
208
    instance.m_nodeInstance->setNodeInstanceView(nodeInstanceView);
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

    return instance;
}

/*!
\brief Returns the ModelNode of this NodeInstance.
\returns ModelNode of this NodeState
*/
ModelNode NodeInstance::modelNode() const
{
    if (m_nodeInstance.isNull())
        return ModelNode();

    return m_nodeInstance->modelNode();
}


/*!
\brief Changes the NodeState of the ModelNode of this NodeInstance.
    All properties are updated.
\param state NodeState of this NodeInstance
*/
void NodeInstance::setModelNode(const ModelNode &node)
{
    Q_ASSERT(node.isValid());
    if (m_nodeInstance->modelNode() == node)
        return;

    m_nodeInstance->setModelNode(node);
}

/*!
\brief Returns if the NodeInstance is a top level item.
\returns true if this NodeInstance is a top level item
*/
bool NodeInstance::isTopLevel() const
{
    return m_nodeInstance->isTopLevel();
}

void NodeInstance::reparent(const NodeInstance &oldParentInstance, const QString &oldParentProperty, const NodeInstance &newParentInstance, const QString &newParentProperty)
{
    m_nodeInstance->reparent(oldParentInstance, oldParentProperty, newParentInstance, newParentProperty);
}

/*!
\brief Returns the parent NodeInstance of this NodeInstance.

    If there is not parent than the parent is invalid.

\returns Parent NodeInstance.
*/
NodeInstance NodeInstance::parent() const
{
263
    return m_nodeInstance->nodeInstanceParentForObject(m_nodeInstance->parent());
264
265
266
267
}

bool NodeInstance::hasParent() const
{
268
    return m_nodeInstance->parent();
269
270
271
}

/*!
272
273
\brief Returns if the NodeInstance is a QDeclarativeItem.
\returns true if this NodeInstance is a QDeclarativeItem
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
*/
bool NodeInstance::isQmlGraphicsItem() const
{
    return m_nodeInstance->isQmlGraphicsItem();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsScene.
\returns true if this NodeInstance is a QGraphicsScene
*/
bool NodeInstance::isGraphicsScene() const
{
    return m_nodeInstance->isGraphicsScene();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsView.
\returns true if this NodeInstance is a QGraphicsView
*/
bool NodeInstance::isGraphicsView() const
{
    return m_nodeInstance->isGraphicsView();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsWidget.
\returns true if this NodeInstance is a QGraphicsWidget
*/
bool NodeInstance::isGraphicsWidget() const
{
    return m_nodeInstance->isGraphicsWidget();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsProxyWidget.
\returns true if this NodeInstance is a QGraphicsProxyWidget
*/
bool NodeInstance::isProxyWidget() const
{
    return m_nodeInstance->isProxyWidget();
}

/*!
\brief Returns if the NodeInstance is a QWidget.
\returns true if this NodeInstance is a QWidget
*/
bool NodeInstance::isWidget() const
{
    return m_nodeInstance->isWidget();
}

/*!
326
327
\brief Returns if the NodeInstance is a QDeclarativeView.
\returns true if this NodeInstance is a QDeclarativeView
328
*/
329
bool NodeInstance::isQDeclarativeView() const
330
{
331
    return m_nodeInstance->isQDeclarativeView();
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
}

bool NodeInstance::isGraphicsObject() const
{
    return m_nodeInstance->isGraphicsObject();
}

bool NodeInstance::isTransition() const
{
    return m_nodeInstance->isTransition();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsItem.
\returns true if this NodeInstance is a QGraphicsItem
*/
bool NodeInstance::equalGraphicsItem(QGraphicsItem *item) const
{
    return m_nodeInstance->equalGraphicsItem(item);
}

/*!
\brief Returns the bounding rect of the NodeInstance.
\returns QRectF of the NodeInstance
*/
QRectF NodeInstance::boundingRect() const
{
    QRectF boundingRect(m_nodeInstance->boundingRect());

//
//    if (modelNode().isValid()) { // TODO implement recursiv stuff
//        if (qFuzzyIsNull(boundingRect.width()))
//            boundingRect.setWidth(nodeState().property("width").value().toDouble());
//
//        if (qFuzzyIsNull(boundingRect.height()))
//            boundingRect.setHeight(nodeState().property("height").value().toDouble());
//    }

    return boundingRect;
}

void NodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
{
    m_nodeInstance->setPropertyVariant(name, value);
376

377
378
}

379
void NodeInstance::setPropertyDynamicVariant(const QString &name, const QString &typeName, const QVariant &value)
380
{
381
382
    m_nodeInstance->createDynamicProperty(name, typeName);
    m_nodeInstance->setPropertyVariant(name, value);
383
384
385
386
387
388
389
}

void NodeInstance::setPropertyBinding(const QString &name, const QString &expression)
{
    m_nodeInstance->setPropertyBinding(name, expression);
}

390
void NodeInstance::setPropertyDynamicBinding(const QString &name, const QString &typeName, const QString &expression)
391
{
392
393
    m_nodeInstance->createDynamicProperty(name, typeName);
    m_nodeInstance->setPropertyBinding(name, expression);
394
395
396
397
398
399
400
}

void NodeInstance::resetProperty(const QString &name)
{
    m_nodeInstance->resetProperty(name);
}

401
402
403
404
405
void NodeInstance::refreshProperty(const QString &name)
{
    m_nodeInstance->refreshProperty(name);
}

406
407
408
409
410
411
412
413
414
415
416
417
418
419
void NodeInstance::setId(const QString &id)
{
    m_nodeInstance->setId(id);
}

/*!
\brief Returns the property value of the property of this NodeInstance.
\returns QVariant value
*/
QVariant NodeInstance::property(const QString &name) const
{
    return m_nodeInstance->property(name);
}

Thomas Hartmann's avatar
Thomas Hartmann committed
420
421
422
423
424
bool NodeInstance::hasBindingForProperty(const QString &name) const
{
    return m_nodeInstance->hasBindingForProperty(name);
}

425
426
427
428
429
430
431
432
433
/*!
\brief Returns the property default value of the property of this NodeInstance.
\returns QVariant default value which is the reset value to
*/
QVariant NodeInstance::defaultValue(const QString &name) const
{
    return m_nodeInstance->resetValue(name);
}

434
435
436
437
438
439
440
441
/*!
\brief Returns the type of the property of this NodeInstance.
*/
QString NodeInstance::instanceType(const QString &name) const
{
    return m_nodeInstance->instanceType(name);
}

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
/*!
\brief Returns if the NodeInstance is valid.
\returns true if the NodeInstance is valid
*/
bool NodeInstance::isValid() const
{
    return m_nodeInstance && internalObject();
}

void NodeInstance::makeInvalid()
{
    if (m_nodeInstance)
        m_nodeInstance->destroy();
    m_nodeInstance.clear();
}

bool NodeInstance::hasContent() const
{
    return m_nodeInstance->hasContent();
}

bool NodeInstance::hasAnchor(const QString &name) const
{
    return m_nodeInstance->hasAnchor(name);
}

Marco Bubke's avatar
Marco Bubke committed
468
469
470
471
472
int NodeInstance::penWidth() const
{
    return m_nodeInstance->penWidth();
}

473
bool NodeInstance::isAnchoredBySibling() const
474
{
475
476
477
478
479
480
    return m_nodeInstance->isAnchoredBySibling();
}

bool NodeInstance::isAnchoredByChildren() const
{
    return m_nodeInstance->isAnchoredByChildren();
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
}

QPair<QString, NodeInstance> NodeInstance::anchor(const QString &name) const
{
    return m_nodeInstance->anchor(name);
}

uint qHash(const NodeInstance &instance)
{
    return ::qHash(instance.m_nodeInstance.data());
}

bool operator==(const NodeInstance &first, const NodeInstance &second)
{
    return first.m_nodeInstance.data() == second.m_nodeInstance.data();
}

498
bool NodeInstance::isWrappingThisObject(QObject *object) const
499
{
500
    return internalObject() && internalObject() == object;
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
}

/*!
\brief Returns the position in parent coordiantes.
\returns QPointF of the position of the instance.
*/
QPointF NodeInstance::position() const
{
    return m_nodeInstance->position();
}

/*!
\brief Returns the size in local coordiantes.
\returns QSizeF of the size of the instance.
*/
QSizeF NodeInstance::size() const
{
    QSizeF instanceSize = m_nodeInstance->size();

//    if (nodeState().isValid()) {
//        if (qFuzzyIsNull(instanceSize.width()))
//            instanceSize.setWidth(nodeState().property("width").value().toDouble());
//
//        if (qFuzzyIsNull(instanceSize.height()))
//            instanceSize.setHeight(nodeState().property("height").value().toDouble());
//    }
    return instanceSize;
}

QTransform NodeInstance::transform() const
{
    return m_nodeInstance->transform();
}

/*!
\brief Returns the transform matrix of the instance.
\returns QTransform of the instance.
*/
QTransform NodeInstance::customTransform() const
{
    return m_nodeInstance->customTransform();
}

QTransform NodeInstance::sceneTransform() const
{
    return m_nodeInstance->sceneTransform();
}

double NodeInstance::rotation() const
{
    return m_nodeInstance->rotation();
}

double NodeInstance::scale() const
{
    return m_nodeInstance->scale();
}

QList<QGraphicsTransform *> NodeInstance::transformations() const
{
    return m_nodeInstance->transformations();
}

QPointF NodeInstance::transformOriginPoint() const
{
    return m_nodeInstance->transformOriginPoint();
}

double NodeInstance::zValue() const
{
    return m_nodeInstance->zValue();
}

/*!
\brief Returns the opacity of the instance.
\returns 0.0 mean transparent and 1.0 opaque.
*/
double NodeInstance::opacity() const
{
    return m_nodeInstance->opacity();
}


void NodeInstance::setDeleteHeldInstance(bool deleteInstance)
{
    m_nodeInstance->setDeleteHeldInstance(deleteInstance);
}


void NodeInstance::paintUpdate()
{
    m_nodeInstance->paintUpdate();
}


Internal::QmlGraphicsItemNodeInstance::Pointer NodeInstance::qmlGraphicsItemNodeInstance() const
{
    return m_nodeInstance.dynamicCast<Internal::QmlGraphicsItemNodeInstance>();
}

QObject *NodeInstance::internalObject() const
{
    if (m_nodeInstance.isNull())
        return 0;

    return m_nodeInstance->object();
}

Marco Bubke's avatar
Marco Bubke committed
609
610
611
612
613
614
615
616
617
618
void NodeInstance::activateState()
{
    m_nodeInstance->activateState();
}

void NodeInstance::deactivateState()
{
    m_nodeInstance->deactivateState();
}

619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
bool NodeInstance::updateStateVariant(const NodeInstance &target, const QString &propertyName, const QVariant &value)
{
    return m_nodeInstance->updateStateVariant(target, propertyName, value);
}

bool NodeInstance::updateStateBinding(const NodeInstance &target, const QString &propertyName, const QString &expression)
{
    return m_nodeInstance->updateStateBinding(target, propertyName, expression);
}

QVariant NodeInstance::resetVariant(const QString &propertyName) const
{
    return m_nodeInstance->resetValue(propertyName);
}

bool NodeInstance::resetStateProperty(const NodeInstance &target, const QString &propertyName, const QVariant &resetValue)
Marco Bubke's avatar
Marco Bubke committed
635
{
636
    return m_nodeInstance->resetStateProperty(target, propertyName, resetValue);
Marco Bubke's avatar
Marco Bubke committed
637
}
638
639
640
641
642
643

/*!
 Makes types used in node instances known to the Qml engine. To be called once at initialization time.
*/
void NodeInstance::registerDeclarativeTypes()
{
Marco Bubke's avatar
Marco Bubke committed
644
//    qmlRegisterType<QmlDesigner::Internal::QmlPropertyChangesObject>();
645
}
646

647
648
void NodeInstance::doComponentComplete()
{
649
    m_nodeInstance->doComponentComplete();
650
651
}

652
653
654
655
656
QString NodeInstance::id() const
{
    return m_nodeInstance->id();
}

657
#ifdef QTCREATOR_TEST
658
659
660
661
QObject* NodeInstance::testHandle() const
{
    return internalObject();
}
662
663
664
665
666
Internal::ObjectNodeInstance* NodeInstance::internalInstance() const
{
    return m_nodeInstance.data();
}

667
668
#endif

669
} // namespace QmlDesigner