nodeinstanceview.cpp 31.1 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 "nodeinstanceview.h"

32
33
#include <QDeclarativeEngine>
#include <QDeclarativeContext>
34
#include <private/qdeclarativeengine_p.h>
35
36
37
38
39
40

#include <QtDebug>
#include <QUrl>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsObject>
41
#include <QFileSystemWatcher>
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <model.h>
#include <modelnode.h>
#include <metainfo.h>

#include <typeinfo>
#include <iwidgetplugin.h>

#include "abstractproperty.h"
#include "variantproperty.h"
#include "bindingproperty.h"
#include "nodeabstractproperty.h"
#include "nodelistproperty.h"

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <nodeinstanceserverinterface.h>

#include "createscenecommand.h"
#include "createinstancescommand.h"
#include "clearscenecommand.h"
#include "changefileurlcommand.h"
#include "reparentinstancescommand.h"
#include "changevaluescommand.h"
#include "changebindingscommand.h"
#include "changeidscommand.h"
#include "removeinstancescommand.h"
#include "removepropertiescommand.h"
#include "valueschangedcommand.h"
#include "pixmapchangedcommand.h"
#include "informationchangedcommand.h"
#include "changestatecommand.h"
72
#include "addimportcommand.h"
73
#include "childrenchangedcommand.h"
74
75
#include "imagecontainer.h"
#include "statepreviewimagechangedcommand.h"
76
77
#include "completecomponentcommand.h"
#include "componentcompletedcommand.h"
78
79

#include "nodeinstanceserverproxy.h"
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
enum {
    debug = false
};

/*!
\defgroup CoreInstance
*/
/*!
\class QmlDesigner::NodeInstanceView
\ingroup CoreInstance
\brief Central class to create and manage instances of a ModelNode.

This view is used to instance the ModelNodes. Many AbstractViews hold a
NodeInstanceView to get values from tghe NodeInstances back.
For this purpose this view can be rendered offscreen.

\see NodeInstance ModelNode
*/

namespace QmlDesigner {

/*! \brief Constructor

Tobias Hunger's avatar
Tobias Hunger committed
104
  The class will be rendered offscreen if not set otherwise.
105

106
107
\param Parent of this object. If this parent is d this instance is
d too.
108
109
110
111

\see ~NodeInstanceView setRenderOffScreen
*/
NodeInstanceView::NodeInstanceView(QObject *parent)
112
        : AbstractView(parent)
113
{
114
115
116
    m_resetTimer.setInterval(200);
    m_resetTimer.setSingleShot(true);
    connect(&m_resetTimer, SIGNAL(timeout()), this, SLOT(restartProcessDelayed()));
117
118
119
120
121
122
123
124
125
}


/*! \brief Destructor

*/
NodeInstanceView::~NodeInstanceView()
{
    removeAllInstanceNodeRelationships();
126
    delete nodeInstanceServer();
127
128
129
130
131
132
133
134
135
136
137
138
139
140
}

/*!   \name Overloaded Notifiers
 *  This methodes notify the view that something has happen in the model
 */
//\{
/*! \brief Notifing the view that it was attached to a model.

  For every ModelNode in the model a NodeInstance will be created.
\param model Model to which the view is attached
*/
void NodeInstanceView::modelAttached(Model *model)
{
    AbstractView::modelAttached(model);
141
142
143
144
145
146
147
    m_nodeInstanceServer = new NodeInstanceServerProxy(this);
    connect(m_nodeInstanceServer.data(), SIGNAL(processCrashed()), this, SLOT(restartProcess()));

    setBlockUpdates(true);
    nodeInstanceServer()->createScene(createCreateSceneCommand());

    nodeInstanceServer()->changeFileUrl(createChangeFileUrlCommand(model->fileUrl()));
148
149
    foreach(const Import &import, model->imports())
        nodeInstanceServer()->addImport(createImportCommand(import));
150
151

    loadNodes(allModelNodes());
152

153
    setBlockUpdates(false);
154
155
156
157
158
}

void NodeInstanceView::modelAboutToBeDetached(Model * model)
{
    removeAllInstanceNodeRelationships();
159
160
    nodeInstanceServer()->clearScene(createClearSceneCommand());
    delete nodeInstanceServer();
161
162
163
164
    AbstractView::modelAboutToBeDetached(model);
}


165
166
167
168
169
170
171
172
173
174
175
void NodeInstanceView::restartProcess()
{
    setBlockUpdates(true);
    Model *oldModel = model();
    if (oldModel) {
        oldModel->detachView(this);
        oldModel->attachView(this);
    }
    setBlockUpdates(false);
}

176
177
178
179
180
void NodeInstanceView::restartProcessDelayed()
{
    m_resetTimer.start();
}

181
182
183
184
185
186
/*! \brief Notifing the view that a node was created.
  A NodeInstance will be created for the new created ModelNode.
\param createdNode New created ModelNode.
*/
void NodeInstanceView::nodeCreated(const ModelNode &createdNode)
{
187
    NodeInstance instance = loadNode(createdNode);
188
189
    nodeInstanceServer()->createInstances(createCreateInstancesCommand(QList<NodeInstance>() << instance));
    nodeInstanceServer()->changePropertyValues(createChangeValueCommand(createdNode.variantProperties()));
190
    nodeInstanceServer()->completeComponent(createComponentCompleteCommand(QList<NodeInstance>() << instance));
191
192
193
194
195
196
197
}

/*! \brief Notifing the view that a node was created.
\param removedNode
*/
void NodeInstanceView::nodeAboutToBeRemoved(const ModelNode &removedNode)
{
198
    nodeInstanceServer()->removeInstances(createRemoveInstancesCommand(removedNode));
199
200
201
202
203
204
205
    removeInstanceAndSubInstances(removedNode);
}

void NodeInstanceView::nodeRemoved(const ModelNode &/*removedNode*/, const NodeAbstractProperty &/*parentProperty*/, PropertyChangeFlags /*propertyChange*/)
{
}

206
void NodeInstanceView::resetHorizontalAnchors(const ModelNode &modelNode)
207
{
208
209
    QList<BindingProperty> bindingList;
    QList<VariantProperty> valueList;
210

211
212
213
214
    if (modelNode.hasBindingProperty("x")) {
        bindingList.append(modelNode.bindingProperty("x"));
    } else if (modelNode.hasVariantProperty("x")) {
        valueList.append(modelNode.variantProperty("x"));
215
216
    }

217
218
219
220
    if (modelNode.hasBindingProperty("width")) {
        bindingList.append(modelNode.bindingProperty("width"));
    } else if (modelNode.hasVariantProperty("width")) {
        valueList.append(modelNode.variantProperty("width"));
221
222
    }

223
224
    if (!valueList.isEmpty())
        nodeInstanceServer()->changePropertyValues(createChangeValueCommand(valueList));
225

226
227
    if (!bindingList.isEmpty())
        nodeInstanceServer()->changePropertyBindings(createChangeBindingCommand(bindingList));
228

229
}
230

231
232
233
234
void NodeInstanceView::resetVerticalAnchors(const ModelNode &modelNode)
{
    QList<BindingProperty> bindingList;
    QList<VariantProperty> valueList;
235

236
237
238
239
240
    if (modelNode.hasBindingProperty("yx")) {
        bindingList.append(modelNode.bindingProperty("yx"));
    } else if (modelNode.hasVariantProperty("y")) {
        valueList.append(modelNode.variantProperty("y"));
    }
241

242
243
244
245
    if (modelNode.hasBindingProperty("height")) {
        bindingList.append(modelNode.bindingProperty("height"));
    } else if (modelNode.hasVariantProperty("height")) {
        valueList.append(modelNode.variantProperty("height"));
246
247
    }

248
249
    if (!valueList.isEmpty())
        nodeInstanceServer()->changePropertyValues(createChangeValueCommand(valueList));
250

251
252
    if (!bindingList.isEmpty())
        nodeInstanceServer()->changePropertyBindings(createChangeBindingCommand(bindingList));
253
254
}

255
void NodeInstanceView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& propertyList)
256
257
{

258
259
    QList<ModelNode> nodeList;
    QList<AbstractProperty> nonNodePropertyList;
260

261
262
263
264
265
    foreach (const AbstractProperty &property, propertyList) {
        if (property.isNodeAbstractProperty()) {
            nodeList.append(property.toNodeAbstractProperty().allSubNodes());
        } else {
            nonNodePropertyList.append(property);
266
267
268
        }
    }

269
270
    nodeInstanceServer()->removeInstances(createRemoveInstancesCommand(nodeList));
    nodeInstanceServer()->removeProperties(createRemovePropertiesCommand(nonNodePropertyList));
271

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    foreach (const AbstractProperty &property, propertyList) {
        const QString &name = property.name();
        if (name == "anchors.fill") {
            resetHorizontalAnchors(property.parentModelNode());
            resetVerticalAnchors(property.parentModelNode());
        } else if (name == "anchors.centerIn") {
            resetHorizontalAnchors(property.parentModelNode());
            resetVerticalAnchors(property.parentModelNode());
        } else if (name == "anchors.top") {
            resetVerticalAnchors(property.parentModelNode());
        } else if (name == "anchors.left") {
            resetHorizontalAnchors(property.parentModelNode());
        } else if (name == "anchors.right") {
            resetHorizontalAnchors(property.parentModelNode());
        } else if (name == "anchors.bottom") {
            resetVerticalAnchors(property.parentModelNode());
        } else if (name == "anchors.horizontalCenter") {
            resetHorizontalAnchors(property.parentModelNode());
        } else if (name == "anchors.verticalCenter") {
            resetVerticalAnchors(property.parentModelNode());
        } else if (name == "anchors.baseline") {
            resetVerticalAnchors(property.parentModelNode());
294
295
296
        }
    }

297
298
299
300
301
302
    foreach (const ModelNode &node, nodeList)
        removeInstanceNodeRelationship(node);
}

void NodeInstanceView::propertiesRemoved(const QList<AbstractProperty>& /*propertyList*/)
{
303
304
}

305
306
307
308
309
310
void NodeInstanceView::removeInstanceAndSubInstances(const ModelNode &node)
{
    foreach(const ModelNode &subNode, node.allSubModelNodes()) {
        if (hasInstanceForNode(subNode))
            removeInstanceNodeRelationship(subNode);
    }
311
312
313

    if (hasInstanceForNode(node))
        removeInstanceNodeRelationship(node);
314
315
}

316
void NodeInstanceView::rootNodeTypeChanged(const QString &/*type*/, int /*majorVersion*/, int /*minorVersion*/)
317
{
318
    nodeInstanceServer()->clearScene(createClearSceneCommand());
319
    removeAllInstanceNodeRelationships();
320
321
322

    QList<ModelNode> nodeList;

323
    nodeList.append(allModelNodes());
324

325
    nodeInstanceServer()->createScene(createCreateSceneCommand());
326
327
328
329
330
    loadNodes(nodeList);
}

void NodeInstanceView::bindingPropertiesChanged(const QList<BindingProperty>& propertyList, PropertyChangeFlags /*propertyChange*/)
{
331
    nodeInstanceServer()->changePropertyBindings(createChangeBindingCommand(propertyList));
332
333
334
335
}

/*! \brief Notifing the view that a AbstractProperty value was changed to a ModelNode.

Tobias Hunger's avatar
Tobias Hunger committed
336
  The property will be set for the NodeInstance.
337

Tobias Hunger's avatar
Tobias Hunger committed
338
\param state ModelNode to which the Property belongs
339
340
341
342
343
344
345
346
\param property AbstractProperty which was changed
\param newValue New Value of the property
\param oldValue Old Value of the property
\see AbstractProperty NodeInstance ModelNode
*/

void NodeInstanceView::variantPropertiesChanged(const QList<VariantProperty>& propertyList, PropertyChangeFlags /*propertyChange*/)
{
347
    nodeInstanceServer()->changePropertyValues(createChangeValueCommand(propertyList));
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
}
/*! \brief Notifing the view that a ModelNode has a new Parent.

  Note that also the ModelNode::childNodes() list was changed. The
  NodeInstance tree will be changed to reflect the ModelNode tree change.

\param node ModelNode which parent was changed.
\param oldParent Old parent of the node.
\param newParent New parent of the node.

\see NodeInstance ModelNode
*/

void NodeInstanceView::nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
363
364
365
366
367
368
369
370
371
    nodeInstanceServer()->reparentInstances(createReparentInstancesCommand(node, newPropertyParent, oldPropertyParent));
//    NodeInstance nodeInstance(instanceForNode(node));
//    NodeInstance oldParentInstance;
//    if (hasInstanceForNode(oldPropertyParent.parentModelNode()))
//        oldParentInstance = instanceForNode(oldPropertyParent.parentModelNode());
//    NodeInstance newParentInstance;
//    if (hasInstanceForNode(newPropertyParent.parentModelNode()))
//        newParentInstance = instanceForNode(newPropertyParent.parentModelNode());
//    nodeInstance.reparent(oldParentInstance, oldPropertyParent.name(), newParentInstance, newPropertyParent.name());
372
373
}

374
375
376
377
378
void NodeInstanceView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
}


379
void NodeInstanceView::fileUrlChanged(const QUrl &/*oldUrl*/, const QUrl &newUrl)
380
381
{
    // TODO: We have to probably reload everything, so that images etc are updated!!!
382
383
384
385
    //engine()->setBaseUrl(model()->fileUrl());

    //TODO reload the whole scene
    nodeInstanceServer()->changeFileUrl(createChangeFileUrlCommand(newUrl));
386
387
}

388
void NodeInstanceView::nodeIdChanged(const ModelNode& node, const QString& /*newId*/, const QString& /*oldId*/)
389
390
391
{
    if (hasInstanceForNode(node)) {
        NodeInstance instance = instanceForNode(node);
392
        nodeInstanceServer()->changeIds(createChangeIdsCommand(QList<NodeInstance>() << instance));
393
394
395
    }
}

396
397
void NodeInstanceView::nodeOrderChanged(const NodeListProperty & listProperty,
                                        const ModelNode & /*movedNode*/, int /*oldIndex*/)
398
{
399
400
401
402
403
404
405
    QVector<ReparentContainer> containerList;
    QString propertyName = listProperty.name();
    qint32 containerInstanceId = -1;
    ModelNode containerNode = listProperty.parentModelNode();
    if (hasInstanceForNode(containerNode))
        containerInstanceId = instanceForNode(containerNode).instanceId();

406
    foreach(const ModelNode &node, listProperty.toModelNodeList()) {
407
408
409
410
411
412
        qint32 instanceId = -1;
        if (hasInstanceForNode(node)) {
            instanceId = instanceForNode(node).instanceId();
            ReparentContainer container(instanceId, containerInstanceId, propertyName, containerInstanceId, propertyName);
            containerList.append(container);
        }
413
    }
414
415

    nodeInstanceServer()->reparentInstances(ReparentInstancesCommand(containerList));
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
}

/*! \brief Notifing the view that the selection has been changed.

  Do nothing.

\param selectedNodeList List of ModelNode which has been selected
\param lastSelectedNodeList List of ModelNode which was selected

\see ModelNode NodeInstance
*/
void NodeInstanceView::selectedNodesChanged(const QList<ModelNode> &/*selectedNodeList*/,
                                              const QList<ModelNode> &/*lastSelectedNodeList*/)
{
}

Marco Bubke's avatar
Marco Bubke committed
432
433
434
435
void NodeInstanceView::scriptFunctionsChanged(const ModelNode &/*node*/, const QStringList &/*scriptFunctionList*/)
{

}
436

437
438
439
440
441
void NodeInstanceView::instancePropertyChange(const QList<QPair<ModelNode, QString> > &/*propertyList*/)
{

}

442
443
444
445
void NodeInstanceView::instancesCompleted(const QVector<ModelNode> &/*completedNodeList*/)
{
}

446
447
void NodeInstanceView::importAdded(const Import &import)
{
448
    restartProcessDelayed();
449
}
450

451
452
void NodeInstanceView::importRemoved(const Import &/*import*/)
{
453
    restartProcessDelayed();
454
455
}

456
457
//\}

458

459
460
void NodeInstanceView::loadNodes(const QList<ModelNode> &nodeList)
{
461
462
    QList<NodeInstance> instanceList;

463
    foreach (const ModelNode &node, nodeList)
464
        instanceList.append(loadNode(node));
465

466
467
468

    QList<VariantProperty> variantPropertyList;
    QList<BindingProperty> bindingPropertyList;
469
470

    foreach (const ModelNode &node, nodeList) {
471
472
        variantPropertyList.append(node.variantProperties());
        bindingPropertyList.append(node.bindingProperties());
473
    }
474

475
476
477
478
479
    nodeInstanceServer()->createInstances(createCreateInstancesCommand(instanceList));
    nodeInstanceServer()->reparentInstances(createReparentInstancesCommand(instanceList));
    nodeInstanceServer()->changeIds(createChangeIdsCommand(instanceList));
    nodeInstanceServer()->changePropertyValues(createChangeValueCommand(variantPropertyList));
    nodeInstanceServer()->changePropertyBindings(createChangeBindingCommand(bindingPropertyList));
480
    nodeInstanceServer()->completeComponent(createComponentCompleteCommand(instanceList));
481
482
483
484
485
486
}

void NodeInstanceView::removeAllInstanceNodeRelationships()
{
    // prevent destroyed() signals calling back

487
488
489
490
//    foreach (NodeInstance instance, m_objectInstanceHash.values()) {
//        if (instance.isValid())
//            instance.setId(QString());
//    }
491

492
493
494
//    //first  the root object
//    if (rootNodeInstance().internalObject())
//        rootNodeInstance().internalObject()->disconnect();
495

496
//    rootNodeInstance().makeInvalid();
497
498


499
500
501
    foreach (NodeInstance instance, m_idInstanceHash.values()) {
//        if (instance.internalObject())
//            instance.internalObject()->disconnect();
502
        instance.makeInvalid();
503
        }
504
505

    m_nodeInstanceHash.clear();
506
    m_idInstanceHash.clear();
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
}

/*! \brief Returns a List of all NodeInstances

\see NodeInstance
*/

QList<NodeInstance> NodeInstanceView::instances() const
{
    return m_nodeInstanceHash.values();
}

/*! \brief Returns the NodeInstance for this ModelNode

  Returns a invalid NodeInstance if no NodeInstance for this ModelNode exists.

\param node ModelNode must be valid.
\returns  NodeStance for ModelNode.
\see NodeInstance
*/
527
NodeInstance NodeInstanceView::instanceForNode(const ModelNode &node) const
528
529
530
531
532
533
534
{
    Q_ASSERT(node.isValid());
    Q_ASSERT(m_nodeInstanceHash.contains(node));
    Q_ASSERT(m_nodeInstanceHash.value(node).modelNode() == node);
    return m_nodeInstanceHash.value(node);
}

535
bool NodeInstanceView::hasInstanceForNode(const ModelNode &node) const
536
537
538
539
{
    return m_nodeInstanceHash.contains(node);
}

540
NodeInstance NodeInstanceView::instanceForId(qint32 id) const
541
{
542
    if (id < 0)
543
544
        return NodeInstance();

545
    return m_idInstanceHash.value(id);
546
547
}

548
bool NodeInstanceView::hasInstanceForId(qint32 id) const
549
{
550
    if (id < 0)
551
552
        return false;

553
    return m_idInstanceHash.contains(id);
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
}


/*! \brief Returns the root NodeInstance of this view.


\returns  Root NodeIntance for this view.
\see NodeInstance
*/
NodeInstance NodeInstanceView::rootNodeInstance() const
{
    return m_rootNodeInstance;
}

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

  This can be the root NodeInstance if it is specified in the qml file.
\code
    QGraphicsView {
         QGraphicsScene {
             Item {}
         }
    }
\endcode

    If there is node view in the qml file:
 \code

    Item {}

\endcode
    Than there will be a new NodeInstance for this QGraphicsView
    generated which is not the root instance of this NodeInstanceView.

    This is the way to get this QGraphicsView NodeInstance.

\returns  Root NodeIntance for this view.
\see NodeInstance
*/



596
void NodeInstanceView::insertInstanceRelationships(const NodeInstance &instance)
597
{
598
599
600
601
602
    Q_ASSERT(instance.instanceId() >=0);
    Q_ASSERT(!m_nodeInstanceHash.contains(instance.modelNode()));
    Q_ASSERT(!m_idInstanceHash.contains(instance.instanceId()));
    m_nodeInstanceHash.insert(instance.modelNode(), instance);
    m_idInstanceHash.insert(instance.instanceId(), instance);
603
604
605
606
607
608
}

void NodeInstanceView::removeInstanceNodeRelationship(const ModelNode &node)
{
    Q_ASSERT(m_nodeInstanceHash.contains(node));
    NodeInstance instance = instanceForNode(node);
609
610
611
//    if (instance.isValid())
//        instance.setId(QString());
    m_idInstanceHash.remove(instanceForNode(node).instanceId());
612
613
614
615
    m_nodeInstanceHash.remove(node);
    instance.makeInvalid();
}

616
void NodeInstanceView::setBlockUpdates(bool block)
617
{
618
619
    if (m_blockUpdates == 0 && block == true)
        m_nodeInstanceServer->setBlockUpdates(true);
620

621
622
623
624
625
    if (block) {
        m_blockUpdates++;
    } else if (m_blockUpdates > 0) {
        m_blockUpdates--;
    }
626
627
}

628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
void NodeInstanceView::setStateInstance(const NodeInstance &stateInstance)
{
    m_activeStateInstance = stateInstance;
}

void NodeInstanceView::clearStateInstance()
{
    m_activeStateInstance = NodeInstance();
}

NodeInstance NodeInstanceView::activeStateInstance() const
{
    return m_activeStateInstance;
}

643
NodeInstanceServerInterface *NodeInstanceView::nodeInstanceServer() const
644
{
645
    return m_nodeInstanceServer.data();
646
647
}

648
649

NodeInstance NodeInstanceView::loadNode(const ModelNode &node)
650
{
651
    NodeInstance instance(NodeInstance::create(node));
652
653

    insertInstanceRelationships(instance);
654
655
656
657
658
659
660
661

    if (node.isRootNode()) {
        m_rootNodeInstance = instance;
    }

    return instance;
}

662
663
void NodeInstanceView::activateState(const NodeInstance &instance)
{
664
665
666
667
    nodeInstanceServer()->changeState(ChangeStateCommand(instance.instanceId()));
//    activateBaseState();
//    NodeInstance stateInstance(instance);
//    stateInstance.activateState();
668
669
670
671
}

void NodeInstanceView::activateBaseState()
{
672
673
674
    nodeInstanceServer()->changeState(ChangeStateCommand(-1));
//    if (activeStateInstance().isValid())
//        activeStateInstance().deactivateState();
675
676
}

677
678
void NodeInstanceView::removeRecursiveChildRelationship(const ModelNode &removedNode)
{
679
680
681
//    if (hasInstanceForNode(removedNode)) {
//        instanceForNode(removedNode).setId(QString());
//    }
682

683
684
685
686
687
688
689
690
    foreach (const ModelNode &childNode, removedNode.allDirectSubModelNodes())
        removeRecursiveChildRelationship(childNode);

    removeInstanceNodeRelationship(removedNode);
}

QRectF NodeInstanceView::sceneRect() const
{
691
    if (rootNodeInstance().isValid())
692
       return rootNodeInstance().boundingRect();
693
694
695
696

    return QRectF();
}

697
698
699
700
701
702
703
704
CreateSceneCommand NodeInstanceView::createCreateSceneCommand() const
{
    return CreateSceneCommand();
}
ClearSceneCommand NodeInstanceView::createClearSceneCommand() const
{
    return ClearSceneCommand();
}
705

706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
CompleteComponentCommand NodeInstanceView::createComponentCompleteCommand(const QList<NodeInstance> &instanceList) const
{
    QVector<qint32> containerList;
    foreach(const NodeInstance &instance, instanceList) {
        if (instance.instanceId() >= 0)
            containerList.append(instance.instanceId());
    }

    return CompleteComponentCommand(containerList);
}

ComponentCompletedCommand NodeInstanceView::createComponentCompletedCommand(const QList<NodeInstance> &instanceList) const
{
    QVector<qint32> containerList;
    foreach(const NodeInstance &instance, instanceList) {
        if (instance.instanceId() >= 0)
            containerList.append(instance.instanceId());
    }

    return ComponentCompletedCommand(containerList);
}

728
CreateInstancesCommand NodeInstanceView::createCreateInstancesCommand(const QList<NodeInstance> &instanceList) const
729
{
730
731
    QVector<InstanceContainer> containerList;
    foreach(const NodeInstance &instance, instanceList) {
732
        InstanceContainer container(instance.instanceId(), instance.modelNode().type(), instance.modelNode().majorVersion(), instance.modelNode().minorVersion(), instance.modelNode().metaInfo().componentFileName());
733
        containerList.append(container);
734
735
    }

736
    return CreateInstancesCommand(containerList);
737
738
}

739
ReparentInstancesCommand NodeInstanceView::createReparentInstancesCommand(const QList<NodeInstance> &instanceList) const
740
{
741
742
743
744
745
746
747
748
    QVector<ReparentContainer> containerList;
    foreach(const NodeInstance &instance, instanceList) {
        if (instance.modelNode().hasParentProperty()) {
            NodeAbstractProperty parentProperty = instance.modelNode().parentProperty();
            ReparentContainer container(instance.instanceId(), -1, QString(), instanceForNode(parentProperty.parentModelNode()).instanceId(), parentProperty.name());
            containerList.append(container);
        }
    }
749

750
    return ReparentInstancesCommand(containerList);
751
752
}

753
ReparentInstancesCommand NodeInstanceView::createReparentInstancesCommand(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent) const
754
{
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
    QVector<ReparentContainer> containerList;

    qint32 newParentInstanceId = -1;
    qint32 oldParentInstanceId = -1;

    if (newPropertyParent.isValid() && hasInstanceForNode(newPropertyParent.parentModelNode()))
        newParentInstanceId = instanceForNode(newPropertyParent.parentModelNode()).instanceId();


    if (oldPropertyParent.isValid() && hasInstanceForNode(oldPropertyParent.parentModelNode()))
        oldParentInstanceId = instanceForNode(oldPropertyParent.parentModelNode()).instanceId();


    ReparentContainer container(instanceForNode(node).instanceId(), oldParentInstanceId, oldPropertyParent.name(), newParentInstanceId, newPropertyParent.name());

    containerList.append(container);

    return ReparentInstancesCommand(containerList);
773
774
}

775
ChangeFileUrlCommand NodeInstanceView::createChangeFileUrlCommand(const QUrl &fileUrl) const
776
{
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
    return ChangeFileUrlCommand(fileUrl);
}

ChangeValuesCommand NodeInstanceView::createChangeValueCommand(const QList<VariantProperty>& propertyList) const
{
    QVector<PropertyValueContainer> containerList;

    foreach(const VariantProperty &property, propertyList) {
        ModelNode node = property.parentModelNode();
        if (node.isValid() && hasInstanceForNode(node)) {
            NodeInstance instance = instanceForNode(node);
            PropertyValueContainer container(instance.instanceId(), property.name(), property.value(), property.dynamicTypeName());
            containerList.append(container);
        }

    }

    return ChangeValuesCommand(containerList);
}

ChangeBindingsCommand NodeInstanceView::createChangeBindingCommand(const QList<BindingProperty> &propertyList) const
{
    QVector<PropertyBindingContainer> containerList;

    foreach(const BindingProperty &property, propertyList) {
        ModelNode node = property.parentModelNode();
        if (node.isValid() && hasInstanceForNode(node)) {
            NodeInstance instance = instanceForNode(node);
            PropertyBindingContainer container(instance.instanceId(), property.name(), property.expression(), property.dynamicTypeName());
            containerList.append(container);
        }

    }

    return ChangeBindingsCommand(containerList);
}

ChangeIdsCommand NodeInstanceView::createChangeIdsCommand(const QList<NodeInstance> &instanceList) const
{
    QVector<IdContainer> containerList;
    foreach(const NodeInstance &instance, instanceList) {
        QString id = instance.modelNode().id();
        if (!id.isEmpty()) {
            IdContainer container(instance.instanceId(), id);
            containerList.append(container);
        }
    }

    return ChangeIdsCommand(containerList);
}

828

829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

RemoveInstancesCommand NodeInstanceView::createRemoveInstancesCommand(const QList<ModelNode> &nodeList) const
{
    QVector<qint32> idList;
    foreach(const ModelNode &node, nodeList) {
        if (node.isValid() && hasInstanceForNode(node)) {
            NodeInstance instance = instanceForNode(node);

            if (instance.instanceId() >= 0) {
                idList.append(instance.instanceId());
            }
        }
    }

    return RemoveInstancesCommand(idList);
}

RemoveInstancesCommand NodeInstanceView::createRemoveInstancesCommand(const ModelNode &node) const
{
    QVector<qint32> idList;

    if (node.isValid() && hasInstanceForNode(node))
        idList.append(instanceForNode(node).instanceId());

    return RemoveInstancesCommand(idList);
}

RemovePropertiesCommand NodeInstanceView::createRemovePropertiesCommand(const QList<AbstractProperty> &propertyList) const
{
    QVector<PropertyAbstractContainer> containerList;

    foreach(const AbstractProperty &property, propertyList) {
        ModelNode node = property.parentModelNode();
        if (node.isValid() && hasInstanceForNode(node)) {
            NodeInstance instance = instanceForNode(node);
            PropertyAbstractContainer container(instance.instanceId(), property.name(), property.dynamicTypeName());
            containerList.append(container);
        }

    }

    return RemovePropertiesCommand(containerList);
}

873
874
875
876
877
AddImportCommand NodeInstanceView::createImportCommand(const Import &import)
{
    return AddImportCommand(import.url(), import.file(), import.version(), import.alias(), import.importPaths());
}

878
879
void NodeInstanceView::valuesChanged(const ValuesChangedCommand &command)
{
880
881
882
    if (!model())
        return;

883
884
    QList<QPair<ModelNode, QString> > valuePropertyChangeList;

885
886
887
888
889
    foreach(const PropertyValueContainer &container, command.valueChanges()) {
        if (hasInstanceForId(container.instanceId())) {
            NodeInstance instance = instanceForId(container.instanceId());
            if (instance.isValid()) {
                instance.setProperty(container.name(), container.value());
890
                valuePropertyChangeList.append(qMakePair(instance.modelNode(), container.name()));
891
892
893
894
            }
        }
    }

895
896
    if (!valuePropertyChangeList.isEmpty())
        emitInstancePropertyChange(valuePropertyChangeList);
897
898
899
}

void NodeInstanceView::pixmapChanged(const PixmapChangedCommand &command)
900
901
902
903
{
      if (!model())
        return;

904
    QSet<ModelNode> renderImageChangeSet;
905

906
907
908
909
910
911
912
    foreach (const ImageContainer &container, command.images()) {
        if (hasInstanceForId(container.instanceId())) {
            NodeInstance instance = instanceForId(container.instanceId());
            if (instance.isValid()) {
                instance.setRenderImage(container.image());
                renderImageChangeSet.insert(instance.modelNode());
            }
913
914
915
        }
    }

916
917
    if (!renderImageChangeSet.isEmpty())
         emitCustomNotification("__instance render pixmap changed__", renderImageChangeSet.toList());
918
919
920
921
}

void NodeInstanceView::informationChanged(const InformationChangedCommand &command)
{
922
923
924
    if (!model())
        return;

925
926
    QList<ModelNode> informationChangedList;

927
928
929
930
931
    foreach(const InformationContainer &container, command.informations()) {
        if (hasInstanceForId(container.instanceId())) {
            NodeInstance instance = instanceForId(container.instanceId());
            if (instance.isValid()) {
                instance.setInformation(container.name(), container.information(), container.secondInformation(), container.thirdInformation());
932
                informationChangedList.append(instance.modelNode());
933
934
935
            }
        }
    }
936

937
938
    if (!informationChangedList.isEmpty())
        emitCustomNotification("__instance information changed__", informationChangedList);
939
940
}

941
942
943
944
945
void NodeInstanceView::statePreviewImagesChanged(const StatePreviewImageChangedCommand &/*command*/)
{

}

946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
void NodeInstanceView::componentCompleted(const ComponentCompletedCommand &command)
{
    if (!model())
        return;

    QVector<ModelNode> nodeVector;

    foreach(const qint32 &instanceId, command.instances()) {
        if (hasModelNodeForInternalId(instanceId)) {
            nodeVector.append(modelNodeForInternalId(instanceId));
        }
    }

    if (!nodeVector.isEmpty())
        emitInstancesCompleted(nodeVector);
}

963
964
void NodeInstanceView::childrenChanged(const ChildrenChangedCommand &command)
{
965
966
967
     if (!model())
        return;

968
969
970
971
972
973
974
975
976
977
978
979
980
    QList<ModelNode> childNodeList;

    foreach(qint32 instanceId, command.childrenInstances()) {
        if (hasInstanceForId(instanceId)) {
            NodeInstance instance = instanceForId(instanceId);
            instance.setParentId(command.parentInstanceId());
            childNodeList.append(instance.modelNode());
        }
    }

    if (!childNodeList.isEmpty())
        emitCustomNotification("__instance children changed__", childNodeList);
}
981

982
}