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

33
34
35
36
37
38
#include "nodeinstanceserver.h"

#include <QDeclarativeEngine>
#include <QFileSystemWatcher>
#include <QUrl>
#include <QSet>
39
#include <QDir>
40
41
#include <QVariant>
#include <QMetaType>
42
#include <QDeclarativeComponent>
43
44
#include <QDeclarativeContext>
#include <private/qlistmodelinterface_p.h>
45
46
#include <QAbstractAnimation>
#include <private/qabstractanimation_p.h>
47
#include <QMutableVectorIterator>
48
49
50
51
52
53
54
55
56
57
58
59

#include "servernodeinstance.h"
#include "childrenchangeeventfilter.h"
#include "propertyabstractcontainer.h"
#include "propertybindingcontainer.h"
#include "propertyvaluecontainer.h"
#include "instancecontainer.h"
#include "createinstancescommand.h"
#include "changefileurlcommand.h"
#include "clearscenecommand.h"
#include "reparentinstancescommand.h"
#include "changevaluescommand.h"
60
#include "changeauxiliarycommand.h"
61
62
63
64
65
66
67
68
69
70
71
#include "changebindingscommand.h"
#include "changeidscommand.h"
#include "removeinstancescommand.h"
#include "nodeinstanceclientinterface.h"
#include "removepropertiescommand.h"
#include "valueschangedcommand.h"
#include "informationchangedcommand.h"
#include "pixmapchangedcommand.h"
#include "commondefines.h"
#include "childrenchangeeventfilter.h"
#include "changestatecommand.h"
72
#include "childrenchangedcommand.h"
73
74
#include "completecomponentcommand.h"
#include "componentcompletedcommand.h"
75
#include "createscenecommand.h"
76
#include "changenodesourcecommand.h"
77

78
79
#include "dummycontextobject.h"

80
81
82
83
84
85
86

namespace QmlDesigner {

NodeInstanceServer::NodeInstanceServer(NodeInstanceClientInterface *nodeInstanceClient) :
    NodeInstanceServerInterface(),
    m_childrenChangeEventFilter(new Internal::ChildrenChangeEventFilter(this)),
    m_nodeInstanceClient(nodeInstanceClient),
87
    m_timer(0),
88
89
    m_renderTimerInterval(16),
    m_slowRenderTimer(false),
90
    m_slowRenderTimerInterval(200)
91
{
92
93
    qmlRegisterType<DummyContextObject>("QmlDesigner", 1, 0, "DummyContextObject");

94
95
96
97
98
99
100
    connect(m_childrenChangeEventFilter.data(), SIGNAL(childrenChanged(QObject*)), this, SLOT(emitParentChanged(QObject*)));
}

NodeInstanceServer::~NodeInstanceServer()
{
}

101
QList<ServerNodeInstance>  NodeInstanceServer::createInstances(const QVector<InstanceContainer> &containerVector)
102
{
103
    Q_ASSERT(declarativeView() || sgView());
104
    QList<ServerNodeInstance> instanceList;
105
    foreach (const InstanceContainer &instanceContainer, containerVector) {
106
107
108
109
110
111
        ServerNodeInstance instance;
        if (instanceContainer.nodeSourceType() == InstanceContainer::ComponentSource) {
            instance = ServerNodeInstance::create(this, instanceContainer, ServerNodeInstance::WrapAsComponent);
        } else {
            instance = ServerNodeInstance::create(this, instanceContainer, ServerNodeInstance::DoNotWrapAsComponent);
        }
112
113
114
115
116
        insertInstanceRelationship(instance);
        instanceList.append(instance);
        instance.internalObject()->installEventFilter(childrenChangeEventFilter());
        if (instanceContainer.instanceId() == 0) {
            m_rootNodeInstance = instance;
117
            resizeCanvasSizeToRootItemSize();
118
        }
119
120
121

        foreach (QDeclarativeContext* context, allSubContextsForObject(instance.internalObject()))
            setupDummysForContext(context);
122
123
    }

124
125
126
127
128
129
    return instanceList;
}

void NodeInstanceServer::createInstances(const CreateInstancesCommand &command)
{
    createInstances(command.instances());
130
    refreshBindings();
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
    startRenderTimer();
}

ServerNodeInstance NodeInstanceServer::instanceForId(qint32 id) const
{
    if (id < 0)
        return ServerNodeInstance();

    Q_ASSERT(m_idInstanceHash.contains(id));
    return m_idInstanceHash.value(id);
}

bool NodeInstanceServer::hasInstanceForId(qint32 id) const
{
    if (id < 0)
        return false;

    return m_idInstanceHash.contains(id);
}

ServerNodeInstance NodeInstanceServer::instanceForObject(QObject *object) const
{
    Q_ASSERT(m_objectInstanceHash.contains(object));
    return m_objectInstanceHash.value(object);
}

bool NodeInstanceServer::hasInstanceForObject(QObject *object) const
{
    if (object == 0)
        return false;

    return m_objectInstanceHash.contains(object);
}

165
166
167
168
169
170
171
172
173
174
void NodeInstanceServer::setRenderTimerInterval(int timerInterval)
{
    m_renderTimerInterval = timerInterval;
}

void NodeInstanceServer::setSlowRenderTimerInterval(int timerInterval)
{
    m_slowRenderTimerInterval = timerInterval;
}

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
void NodeInstanceServer::setTimerId(int timerId)
{
    m_timer = timerId;
}

int NodeInstanceServer::timerId() const
{
    return m_timer;
}

int NodeInstanceServer::renderTimerInterval() const
{
    return m_renderTimerInterval;
}

190
191
void NodeInstanceServer::startRenderTimer()
{
192
193
194
    if (m_slowRenderTimer)
        stopRenderTimer();

195
    if (m_timer == 0)
196
        m_timer = startTimer(m_renderTimerInterval);
197
198
199
200
201
202
203
204
205

    m_slowRenderTimer = false;
}

void NodeInstanceServer::slowDownRenderTimer()
{
    if (!m_slowRenderTimer)
        stopRenderTimer();

206
207
208
209
210
    if (m_timer != 0) {
        killTimer(m_timer);
        m_timer = 0;
    }

211
    if (m_timer == 0)
212
        m_timer = startTimer(m_slowRenderTimerInterval);
213
214

    m_slowRenderTimer = true;
215
216
217
218
219
220
221
222
223
224
}

void NodeInstanceServer::stopRenderTimer()
{
    if (m_timer) {
        killTimer(m_timer);
        m_timer = 0;
    }
}

225
void NodeInstanceServer::createScene(const CreateSceneCommand &command)
226
{
227
228
229
230
    initializeView(command.imports());
    QUnifiedTimer::instance()->setSlowdownFactor(0.00001);
    QUnifiedTimer::instance()->setSlowModeEnabled(true);

231
    QList<ServerNodeInstance> instanceList = setupScene(command);
232

233
234
    refreshBindings();

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    startRenderTimer();
}

void NodeInstanceServer::clearScene(const ClearSceneCommand &/*command*/)
{
    stopRenderTimer();

    removeAllInstanceRelationships();
    m_fileSystemWatcherHash.clear();
    m_rootNodeInstance.makeInvalid();
    m_changedPropertyList.clear();
    m_fileUrl.clear();
}

void NodeInstanceServer::removeInstances(const RemoveInstancesCommand &command)
{
251
    ServerNodeInstance oldState = activeStateInstance();
252
253
254
    if (activeStateInstance().isValid())
        activeStateInstance().deactivateState();

255
    foreach (qint32 instanceId, command.instanceIds()) {
256
257
258
        removeInstanceRelationsip(instanceId);
    }

259
260
    if (oldState.isValid())
        oldState.activateState();
261
262

    refreshBindings();
263
264
265
266
267
    startRenderTimer();
}

void NodeInstanceServer::removeProperties(const RemovePropertiesCommand &command)
{
268
    bool hasDynamicProperties = false;
269
    foreach (const PropertyAbstractContainer &container, command.properties()) {
270
        hasDynamicProperties |= container.isDynamic();
271
        resetInstanceProperty(container);
272
273
274
275
    }

    if (hasDynamicProperties)
        refreshBindings();
276
277
278
279

    startRenderTimer();
}

280
void NodeInstanceServer::reparentInstances(const QVector<ReparentContainer> &containerVector)
281
{
282
    foreach (const ReparentContainer &container, containerVector) {
283
284
285
286
287
        ServerNodeInstance instance = instanceForId(container.instanceId());
        if (instance.isValid()) {
            instance.reparent(instanceForId(container.oldParentInstanceId()), container.oldParentProperty(), instanceForId(container.newParentInstanceId()), container.newParentProperty());
        }
    }
288

289
290
291
292
293
}

void NodeInstanceServer::reparentInstances(const ReparentInstancesCommand &command)
{
    reparentInstances(command.reparentInstances());
294
    refreshBindings();
295
296
297
298
299
300
    startRenderTimer();
}

void NodeInstanceServer::changeState(const ChangeStateCommand &command)
{
    if (hasInstanceForId(command.stateInstanceId())) {
301
302
        if (activeStateInstance().isValid())
            activeStateInstance().deactivateState();
303
304
305
306
307
308
309
310
311
312
        ServerNodeInstance instance = instanceForId(command.stateInstanceId());
        instance.activateState();
    } else {
        if (activeStateInstance().isValid())
            activeStateInstance().deactivateState();
    }

    startRenderTimer();
}

313
314
void NodeInstanceServer::completeComponent(const CompleteComponentCommand &command)
{
315
316
    QList<ServerNodeInstance> instanceList;

317
    foreach (qint32 instanceId, command.instances()) {
318
319
320
        if (hasInstanceForId(instanceId)) {
            ServerNodeInstance instance = instanceForId(instanceId);
            instance.doComponentComplete();
321
            instanceList.append(instance);
322
323
        }
    }
324

325
    refreshBindings();
326
327

    startRenderTimer();
328
329
}

330
void NodeInstanceServer::changeNodeSource(const ChangeNodeSourceCommand &command)
331
332
333
{
    if (hasInstanceForId(command.instanceId())) {
        ServerNodeInstance instance = instanceForId(command.instanceId());
334
335
        if (instance.isValid())
            instance.setNodeSource(command.nodeSource());
336
337
338
339
340
    }

    startRenderTimer();
}

341
void NodeInstanceServer::setupImports(const QVector<AddImportContainer> &containerVector)
342
{
343
344
345
346
347
348
349
    foreach (const AddImportContainer &container, containerVector) {
        QString importStatement = QString("import ");

        if (!container.fileName().isEmpty())
            importStatement += '"' + container.fileName() + '"';
        else if (!container.url().isEmpty())
            importStatement += container.url().toString();
350

351
        if (!container.version().isEmpty())
352
            importStatement += ' ' + container.version();
353

354
355
        if (!container.alias().isEmpty())
            importStatement += " as " + container.alias();
356

357
        importStatement.append('\n');
358

359
360
361
        if (!m_importList.contains(importStatement))
            m_importList.append(importStatement);
    }
Marco Bubke's avatar
Marco Bubke committed
362

363
364
365
366
    delete m_importComponent.data();
    delete m_importComponentObject.data();

    m_importComponent = new QDeclarativeComponent(engine(), 0);
367
    QString componentString;
368
    foreach (const QString &importStatement, m_importList)
369
        componentString += QString("%1").arg(importStatement);
370

371
    componentString += QString("Item {}\n");
372

373
374
    m_importComponent->setData(componentString.toUtf8(), fileUrl());
    m_importComponentObject = m_importComponent->create();
375

376
377
    if (!m_importComponent->errorString().isEmpty())
        qDebug() << "QmlDesigner.NodeInstances: import wrong: " << m_importComponent->errorString();
378
379
}

380
381
void NodeInstanceServer::setupFileUrl(const QUrl &fileUrl)
{
382
    if (!fileUrl.isEmpty()) {
383
384
385
386
387
388
        engine()->setBaseUrl(fileUrl);
        m_fileUrl = fileUrl;
    }
}

void NodeInstanceServer::setupDummyData(const QUrl &fileUrl)
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
    if (!fileUrl.isEmpty()) {
        QStringList dummyDataDirectoryList = dummyDataDirectories(QFileInfo(fileUrl.toLocalFile()).path());
        foreach (const QString &dummyDataDirectory, dummyDataDirectoryList)
            loadDummyDataFiles(dummyDataDirectory);
    }
}

QList<ServerNodeInstance>  NodeInstanceServer::setupInstances(const CreateSceneCommand &command)
{
    QList<ServerNodeInstance> instanceList = createInstances(command.instances());
    reparentInstances(command.reparentInstances());

    foreach (const IdContainer &container, command.ids()) {
        if (hasInstanceForId(container.instanceId()))
            instanceForId(container.instanceId()).setId(container.id());
    }

    foreach (const PropertyValueContainer &container, command.valueChanges()) {
        if (container.isDynamic())
            setInstancePropertyVariant(container);
    }

    foreach (const PropertyValueContainer &container, command.valueChanges()) {
        if (!container.isDynamic())
            setInstancePropertyVariant(container);
    }

    foreach (const PropertyBindingContainer &container, command.bindingChanges()) {
        if (container.isDynamic())
            setInstancePropertyBinding(container);
    }

    foreach (const PropertyBindingContainer &container, command.bindingChanges()) {
        if (!container.isDynamic())
            setInstancePropertyBinding(container);
    }

    foreach (const PropertyValueContainer &container, command.auxiliaryChanges()) {
        setInstanceAuxiliaryData(container);
    }

    foreach (ServerNodeInstance instance, instanceList)
        instance.doComponentComplete();

    return instanceList;
435
436
}

437
438
439
440
441
442
443
void NodeInstanceServer::changeFileUrl(const ChangeFileUrlCommand &command)
{
    m_fileUrl = command.fileUrl();

    if (engine())
        engine()->setBaseUrl(m_fileUrl);

444
    refreshBindings();
445
446
447
448
449
    startRenderTimer();
}

void NodeInstanceServer::changePropertyValues(const ChangeValuesCommand &command)
{
450
    bool hasDynamicProperties = false;
451
    foreach (const PropertyValueContainer &container, command.valueChanges()) {
452
453
454
        hasDynamicProperties |= container.isDynamic();
        setInstancePropertyVariant(container);
    }
455

456
457
458
459
    if (hasDynamicProperties)
        refreshBindings();

    startRenderTimer();
460
461
}

462
463
464
465
466
467
468
469
void NodeInstanceServer::changeAuxiliaryValues(const ChangeAuxiliaryCommand &command)
{
    foreach (const PropertyValueContainer &container, command.auxiliaryChanges()) {
        setInstanceAuxiliaryData(container);
    }

    startRenderTimer();
}
470
471
472

void NodeInstanceServer::changePropertyBindings(const ChangeBindingsCommand &command)
{
473
    bool hasDynamicProperties = false;
474
    foreach (const PropertyBindingContainer &container, command.bindingChanges()) {
475
        hasDynamicProperties |= container.isDynamic();
476
        setInstancePropertyBinding(container);
477
478
479
480
    }

    if (hasDynamicProperties)
        refreshBindings();
481
482
483
484
485
486

    startRenderTimer();
}

void NodeInstanceServer::changeIds(const ChangeIdsCommand &command)
{
487
    foreach (const IdContainer &container, command.ids()) {
488
489
490
491
        if (hasInstanceForId(container.instanceId()))
            instanceForId(container.instanceId()).setId(container.id());
    }

492
    refreshBindings();
493
494
495
    startRenderTimer();
}

496
497
QDeclarativeContext *NodeInstanceServer::context() const
{
498
499
500
501
502
503
504
    if (m_importComponentObject) {
        QDeclarativeContext *importComponentContext = QDeclarativeEngine::contextForObject(m_importComponentObject.data());
        if (importComponentContext) // this should be the default
            return importComponentContext;
    }

    if (engine())
505
        return rootContext();
506
507

    return 0;
508
509
}

510
QDeclarativeContext *NodeInstanceServer::rootContext() const
511
{
512
    return engine()->rootContext();
513
514
515
516
517
518
519
520
521
522
523
524
}

const QVector<NodeInstanceServer::InstancePropertyPair> NodeInstanceServer::changedPropertyList() const
{
    return m_changedPropertyList;
}

void NodeInstanceServer::clearChangedPropertyList()
{
    m_changedPropertyList.clear();
}

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
void NodeInstanceServer::setupDummysForContext(QDeclarativeContext *context)
{
    foreach (const DummyPair& dummyPair, m_dummyObjectList) {
        if (dummyPair.second) {
            context->setContextProperty(dummyPair.first, dummyPair.second.data());
        }
    }
}


QList<QDeclarativeContext*> NodeInstanceServer::allSubContextsForObject(QObject *object)
{
    QList<QDeclarativeContext*> contextList;

    if (object) {
        foreach (QObject *subObject, allSubObjectsForObject(object)) {
            QDeclarativeContext *contextOfObject = QDeclarativeEngine::contextForObject(subObject);
            if (contextOfObject) {
                if (contextOfObject != context() && !contextList.contains(contextOfObject))
                    contextList.append(contextOfObject);
            }
        }
    }

    return contextList;
}

QList<QObject*> NodeInstanceServer::allSubObjectsForObject(QObject *object)
{
    QList<QObject*> subChildren;
    if (object) {
        subChildren = object->findChildren<QObject*>();
    }

    return subChildren;
}

562
563
564
565
void NodeInstanceServer::removeAllInstanceRelationships()
{
    // prevent destroyed() signals calling back

566
    foreach (ServerNodeInstance instance, m_objectInstanceHash) {
567
568
569
570
571
572
573
574
575
576
577
        if (instance.isValid())
            instance.setId(QString());
    }

    //first  the root object
    if (rootNodeInstance().internalObject())
        rootNodeInstance().internalObject()->disconnect();

    rootNodeInstance().makeInvalid();


578
    foreach (ServerNodeInstance instance, m_objectInstanceHash) {
579
580
581
582
583
584
585
586
587
        if (instance.internalObject())
            instance.internalObject()->disconnect();
        instance.makeInvalid();
    }

    m_idInstanceHash.clear();
    m_objectInstanceHash.clear();
}

588
589
590
591
592
593
594
595
596
QFileSystemWatcher *NodeInstanceServer::dummydataFileSystemWatcher()
{
    if (m_dummdataFileSystemWatcher.isNull()) {
        m_dummdataFileSystemWatcher = new QFileSystemWatcher(this);
        connect(m_dummdataFileSystemWatcher.data(), SIGNAL(fileChanged(QString)), this, SLOT(refreshDummyData(QString)));
    }

    return m_dummdataFileSystemWatcher.data();
}
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614

QFileSystemWatcher *NodeInstanceServer::fileSystemWatcher()
{
    if (m_fileSystemWatcher.isNull()) {
        m_fileSystemWatcher = new QFileSystemWatcher(this);
        connect(m_fileSystemWatcher.data(), SIGNAL(fileChanged(QString)), this, SLOT(refreshLocalFileProperty(QString)));
    }

    return m_fileSystemWatcher.data();
}

Internal::ChildrenChangeEventFilter *NodeInstanceServer::childrenChangeEventFilter() const
{
    return m_childrenChangeEventFilter.data();
}

void NodeInstanceServer::addFilePropertyToFileSystemWatcher(QObject *object, const QString &propertyName, const QString &path)
{
615
616
617
618
    if (!m_fileSystemWatcherHash.contains(path)) {
        m_fileSystemWatcherHash.insert(path, ObjectPropertyPair(object, propertyName));
        fileSystemWatcher()->addPath(path);
    }
619
620
621
622
}

void NodeInstanceServer::removeFilePropertyFromFileSystemWatcher(QObject *object, const QString &propertyName, const QString &path)
{
623
624
625
626
    if (m_fileSystemWatcherHash.contains(path)) {
        fileSystemWatcher()->removePath(path);
        m_fileSystemWatcherHash.remove(path, ObjectPropertyPair(object, propertyName));
    }
627
628
629
630
631
}

void NodeInstanceServer::refreshLocalFileProperty(const QString &path)
{
    if (m_fileSystemWatcherHash.contains(path)) {
632
        foreach (const ObjectPropertyPair &objectPropertyPair, m_fileSystemWatcherHash) {
633
634
635
636
637
638
639
640
641
642
            QObject *object = objectPropertyPair.first.data();
            QString propertyName = objectPropertyPair.second;

            if (hasInstanceForObject(object)) {
                instanceForObject(object).refreshProperty(propertyName);
            }
        }
    }
}

643
644
645
void NodeInstanceServer::refreshDummyData(const QString &path)
{
    engine()->clearComponentCache();
646
647
648
649
650
651
    QFileInfo filePath(path);
    if (filePath.completeBaseName().contains("_dummycontext")) {
        loadDummyContextObjectFile(filePath);
    } else {
        loadDummyDataFile(filePath);
    }
652
653

    refreshBindings();
654
655
656
    startRenderTimer();
}

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
void NodeInstanceServer::addChangedProperty(const InstancePropertyPair &property)
{
    if (!m_changedPropertyList.contains(property))
        m_changedPropertyList.append(property);
}

void NodeInstanceServer::emitParentChanged(QObject *child)
{
    if (hasInstanceForObject(child)) {
        addChangedProperty(InstancePropertyPair(instanceForObject(child), "parent"));
    }
}

Internal::ChildrenChangeEventFilter *NodeInstanceServer::childrenChangeEventFilter()
{
    if (m_childrenChangeEventFilter.isNull()) {
        m_childrenChangeEventFilter = new Internal::ChildrenChangeEventFilter(this);
        connect(m_childrenChangeEventFilter.data(), SIGNAL(childrenChanged(QObject*)), this, SLOT(emitParentChanged(QObject*)));
    }

    return m_childrenChangeEventFilter.data();
}

void NodeInstanceServer::resetInstanceProperty(const PropertyAbstractContainer &propertyContainer)
{
    if (hasInstanceForId(propertyContainer.instanceId())) { // TODO ugly workaround
        ServerNodeInstance instance = instanceForId(propertyContainer.instanceId());
        Q_ASSERT(instance.isValid());

        const QString name = propertyContainer.name();

688
        if (activeStateInstance().isValid() && !instance.isSubclassOf("QtQuick/PropertyChanges")) {
689
690
691
692
693
694
            bool statePropertyWasReseted = activeStateInstance().resetStateProperty(instance, name, instance.resetVariant(name));
            if (!statePropertyWasReseted)
                instance.resetProperty(name);
        } else {
            instance.resetProperty(name);
        }
695
696

        if (propertyContainer.isDynamic() && propertyContainer.instanceId() == 0 && engine())
697
            rootContext()->setContextProperty(name, QVariant());
698
699
700
701
702
703
704
705
706
707
708
709
710
    }
}


void NodeInstanceServer::setInstancePropertyBinding(const PropertyBindingContainer &bindingContainer)
{
    if (hasInstanceForId(bindingContainer.instanceId())) {
        ServerNodeInstance instance = instanceForId(bindingContainer.instanceId());

        const QString name = bindingContainer.name();
        const QString expression = bindingContainer.expression();


711
        if (activeStateInstance().isValid() && !instance.isSubclassOf("QtQuick/PropertyChanges")) {
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
            bool stateBindingWasUpdated = activeStateInstance().updateStateBinding(instance, name, expression);
            if (!stateBindingWasUpdated) {
                if (bindingContainer.isDynamic())
                    instance.setPropertyDynamicBinding(name, bindingContainer.dynamicTypeName(), expression);
                else
                    instance.setPropertyBinding(name, expression);
            }
        } else {
            if (bindingContainer.isDynamic())
                instance.setPropertyDynamicBinding(name, bindingContainer.dynamicTypeName(), expression);
            else
                instance.setPropertyBinding(name, expression);
        }
    }
}


void NodeInstanceServer::removeProperties(const QList<PropertyAbstractContainer> &propertyList)
{
    foreach (const PropertyAbstractContainer &property, propertyList)
        resetInstanceProperty(property);
}

void NodeInstanceServer::setInstancePropertyVariant(const PropertyValueContainer &valueContainer)
{
    if (hasInstanceForId(valueContainer.instanceId())) {
        ServerNodeInstance instance = instanceForId(valueContainer.instanceId());


        const QString name = valueContainer.name();
        const QVariant value = valueContainer.value();


745
        if (activeStateInstance().isValid() && !instance.isSubclassOf("QtQuick/PropertyChanges")) {
746
747
748
749
750
751
752
753
754
755
756
757
758
759
            bool stateValueWasUpdated = activeStateInstance().updateStateVariant(instance, name, value);
            if (!stateValueWasUpdated) {
                if (valueContainer.isDynamic())
                    instance.setPropertyDynamicVariant(name, valueContainer.dynamicTypeName(), value);
                else
                    instance.setPropertyVariant(name, value);
            }
        } else { //base state
            if (valueContainer.isDynamic())
                instance.setPropertyDynamicVariant(name, valueContainer.dynamicTypeName(), value);
            else
                instance.setPropertyVariant(name, value);
        }

760
        if (valueContainer.isDynamic() && valueContainer.instanceId() == 0 && engine())
761
            rootContext()->setContextProperty(name, value);
762
763
764
    }
}

765
766
767
768
769
770
771
772
773
774
void NodeInstanceServer::setInstanceAuxiliaryData(const PropertyValueContainer &auxiliaryContainer)
{
    //instanceId() == 0: the item is root
    if (auxiliaryContainer.instanceId() == 0 && (auxiliaryContainer.name() == QLatin1String("width") ||
                                        auxiliaryContainer.name() == QLatin1String("height"))) {

        setInstancePropertyVariant(auxiliaryContainer);
    }
}

775
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

QUrl NodeInstanceServer::fileUrl() const
{
    return m_fileUrl;
}

ServerNodeInstance NodeInstanceServer::activeStateInstance() const
{
    return m_activeStateInstance;
}

ServerNodeInstance NodeInstanceServer::rootNodeInstance() const
{
    return m_rootNodeInstance;
}

void NodeInstanceServer::setStateInstance(const ServerNodeInstance &stateInstance)
{
    m_activeStateInstance = stateInstance;
}

void NodeInstanceServer::clearStateInstance()
{
    m_activeStateInstance = ServerNodeInstance();
}

void NodeInstanceServer::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == m_timer) {
804
        collectItemChangesAndSendChangeCommands();
805
806
807
808
809
810
811
812
813
814
    }

    NodeInstanceServerInterface::timerEvent(event);
}

NodeInstanceClientInterface *NodeInstanceServer::nodeInstanceClient() const
{
    return m_nodeInstanceClient;
}

815
816
817
818
ChildrenChangedCommand NodeInstanceServer::createChildrenChangedCommand(const ServerNodeInstance &parentInstance, const QList<ServerNodeInstance> &instanceList) const
{
    QVector<qint32> instanceVector;

819
    foreach (const ServerNodeInstance &instance, instanceList)
820
821
822
823
824
        instanceVector.append(instance.instanceId());

    return ChildrenChangedCommand(parentInstance.instanceId(), instanceVector);
}

825
826
827
828
InformationChangedCommand NodeInstanceServer::createAllInformationChangedCommand(const QList<ServerNodeInstance> &instanceList, bool initial) const
{
    QVector<InformationContainer> informationVector;

829
    foreach (const ServerNodeInstance &instance, instanceList) {
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
        informationVector.append(InformationContainer(instance.instanceId(), Position, instance.position()));
        informationVector.append(InformationContainer(instance.instanceId(), Transform, instance.transform()));
        informationVector.append(InformationContainer(instance.instanceId(), SceneTransform, instance.sceneTransform()));
        informationVector.append(InformationContainer(instance.instanceId(), Size, instance.size()));
        informationVector.append(InformationContainer(instance.instanceId(), BoundingRect, instance.boundingRect()));
        informationVector.append(InformationContainer(instance.instanceId(), Transform, instance.transform()));
        informationVector.append(InformationContainer(instance.instanceId(), HasContent, instance.hasContent()));
        informationVector.append(InformationContainer(instance.instanceId(), IsMovable, instance.isMovable()));
        informationVector.append(InformationContainer(instance.instanceId(), IsResizable, instance.isResizable()));
        informationVector.append(InformationContainer(instance.instanceId(), IsInPositioner, instance.isInPositioner()));
        informationVector.append(InformationContainer(instance.instanceId(), PenWidth, instance.penWidth()));
        informationVector.append(InformationContainer(instance.instanceId(), IsAnchoredByChildren, instance.isAnchoredByChildren()));
        informationVector.append(InformationContainer(instance.instanceId(), IsAnchoredBySibling, instance.isAnchoredBySibling()));

        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.fill"), instance.hasAnchor("anchors.fill")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.centerIn"), instance.hasAnchor("anchors.centerIn")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.right"), instance.hasAnchor("anchors.right")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.top"), instance.hasAnchor("anchors.top")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.left"), instance.hasAnchor("anchors.left")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.bottom"), instance.hasAnchor("anchors.bottom")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.horizontalCenter"), instance.hasAnchor("anchors.horizontalCenter")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.verticalCenter"), instance.hasAnchor("anchors.verticalCenter")));
        informationVector.append(InformationContainer(instance.instanceId(), HasAnchor, QString("anchors.baseline"), instance.hasAnchor("anchors.baseline")));

        QPair<QString, ServerNodeInstance> anchorPair = instance.anchor("anchors.fill");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.fill"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.centerIn");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.centerIn"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.right");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.right"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.top");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.top"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.left");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.left"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.bottom");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.bottom"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.horizontalCenter");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.horizontalCenter"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.verticalCenter");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.verticalCenter"), anchorPair.first, anchorPair.second.instanceId()));

        anchorPair = instance.anchor("anchors.baseline");
        informationVector.append(InformationContainer(instance.instanceId(), Anchor, QString("anchors.baseline"), anchorPair.first, anchorPair.second.instanceId()));

        QStringList propertyNames = instance.propertyNames();

        if (initial) {
            foreach (const QString &propertyName,propertyNames)
                informationVector.append(InformationContainer(instance.instanceId(), InstanceTypeForProperty, propertyName, instance.instanceType(propertyName)));
        }

        foreach (const QString &propertyName,instance.propertyNames()) {
            bool hasChanged = false;
            bool hasBinding = instance.hasBindingForProperty(propertyName, &hasChanged);
            if (hasChanged)
                informationVector.append(InformationContainer(instance.instanceId(), HasBindingForProperty, propertyName, hasBinding));
        }

    }

    return InformationChangedCommand(informationVector);
}

900
901
902
903
904
static bool supportedVariantType(QVariant::Type type)
{
    return type < QVariant::UserType && type != QVariant::Icon;
}

905
906
907
908
ValuesChangedCommand NodeInstanceServer::createValuesChangedCommand(const QList<ServerNodeInstance> &instanceList) const
{
    QVector<PropertyValueContainer> valueVector;

909
910
    foreach (const ServerNodeInstance &instance, instanceList) {
        foreach (const QString &propertyName, instance.propertyNames()) {
911
            QVariant propertyValue = instance.property(propertyName);
912
            if (supportedVariantType(propertyValue.type()))
913
914
915
916
917
918
919
                valueVector.append(PropertyValueContainer(instance.instanceId(), propertyName, propertyValue, QString()));
        }
    }

    return ValuesChangedCommand(valueVector);
}

920
921
922
923
924
925
926
927
928
929
930
ComponentCompletedCommand NodeInstanceServer::createComponentCompletedCommand(const QList<ServerNodeInstance> &instanceList)
{
    QVector<qint32> idVector;
    foreach (const ServerNodeInstance &instance, instanceList) {
        if (instance.instanceId() >= 0)
            idVector.append(instance.instanceId());
    }

    return ComponentCompletedCommand(idVector);
}

931
932
933
934
935
936
937
938
ValuesChangedCommand NodeInstanceServer::createValuesChangedCommand(const QVector<InstancePropertyPair> &propertyList) const
{
    QVector<PropertyValueContainer> valueVector;

    foreach (const InstancePropertyPair &property, propertyList) {
        const QString propertyName = property.second;
        const ServerNodeInstance instance = property.first;

939
        if ( instance.isValid()) {
940
            QVariant propertyValue = instance.property(propertyName);
941
            if (supportedVariantType(propertyValue.type()))
942
943
944
945
946
947
948
                valueVector.append(PropertyValueContainer(instance.instanceId(), propertyName, propertyValue, QString()));
        }
    }

    return ValuesChangedCommand(valueVector);
}

Marco Bubke's avatar
Marco Bubke committed
949
950
951
952
953
QStringList NodeInstanceServer::imports() const
{
    return m_importList;
}

954
955
956
957
958
void NodeInstanceServer::addImportString(const QString &import)
{
    m_importList.append(import);
}

959
960
961
962
963
QObject *NodeInstanceServer::dummyContextObject() const
{
    return m_dummyContextObject.data();
}

964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
void NodeInstanceServer::notifyPropertyChange(qint32 instanceid, const QString &propertyName)
{
    if (hasInstanceForId(instanceid))
        addChangedProperty(InstancePropertyPair(instanceForId(instanceid), propertyName));
}

void NodeInstanceServer::insertInstanceRelationship(const ServerNodeInstance &instance)
{
    Q_ASSERT(instance.isValid());
    Q_ASSERT(!m_idInstanceHash.contains(instance.instanceId()));
    Q_ASSERT(!m_objectInstanceHash.contains(instance.internalObject()));
    m_objectInstanceHash.insert(instance.internalObject(), instance);
    m_idInstanceHash.insert(instance.instanceId(), instance);
}

void NodeInstanceServer::removeInstanceRelationsip(qint32 instanceId)
{
    if (hasInstanceForId(instanceId)) {
        ServerNodeInstance instance = instanceForId(instanceId);
        if (instance.isValid())
            instance.setId(QString());
        m_idInstanceHash.remove(instanceId);
        m_objectInstanceHash.remove(instance.internalObject());
        instance.makeInvalid();
    }
}

991
PixmapChangedCommand NodeInstanceServer::createPixmapChangedCommand(const QList<ServerNodeInstance> &instanceList) const
992
{
993
994
    QVector<ImageContainer> imageVector;

995
996
997
998
    foreach (const ServerNodeInstance &instance, instanceList) {
        if (instance.isValid())
            imageVector.append(ImageContainer(instance.instanceId(), instance.renderImage()));
    }
999
1000

    return PixmapChangedCommand(imageVector);
For faster browsing, not all history is shown. View entire blame