aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qml/qmlpuppet/qml2puppet/instances/qt5previewnodeinstanceserver.cpp
blob: df6c7b27f5b21a8482dbc2067e19bcd04ca67ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "qt5previewnodeinstanceserver.h"

#include "changepreviewimagesizecommand.h"
#include "createscenecommand.h"
#include "nodeinstanceclientinterface.h"
#include "removesharedmemorycommand.h"
#include "statepreviewimagechangedcommand.h"

#include <QQuickView>
#include <QQuickItem>
#include <private/qquickdesignersupport_p.h>

namespace QmlDesigner {

Qt5PreviewNodeInstanceServer::Qt5PreviewNodeInstanceServer(NodeInstanceClientInterface *nodeInstanceClient) :
    Qt5NodeInstanceServer(nodeInstanceClient)
{
    setSlowRenderTimerInterval(100000000);
    setRenderTimerInterval(100);
}

void Qt5PreviewNodeInstanceServer::createScene(const CreateSceneCommand &command)
{
    initializeView();
    registerFonts(command.resourceUrl);
    setTranslationLanguage(command.language);
    setupScene(command);
    startRenderTimer();
}
void Qt5PreviewNodeInstanceServer::startRenderTimer()
{
    if (timerId() != 0)
        killTimer(timerId());

    int timerId = startTimer(renderTimerInterval());

    setTimerId(timerId);
}

void Qt5PreviewNodeInstanceServer::collectItemChangesAndSendChangeCommands()
{
    static bool inFunction = false;

    if (!rootNodeInstance().holdsGraphical())
        return;

    if (!inFunction && nodeInstanceClient()->bytesToWrite() < 10000) {
        inFunction = true;

        QQuickDesignerSupport::polishItems(quickWindow());

        QVector<ImageContainer> imageContainerVector;
        imageContainerVector.append(ImageContainer(0, renderPreviewImage(), -1));

          QList<ServerNodeInstance> stateInstances = rootNodeInstance().stateInstances();

          const QList<ServerNodeInstance> groupInstances = allGroupStateInstances();

          for (ServerNodeInstance instance : groupInstances) {
              stateInstances.append(instance.stateInstances());
          }

          for (ServerNodeInstance instance : qAsConst(stateInstances)) {
              instance.activateState();
              QImage previewImage = renderPreviewImage();
              if (!previewImage.isNull())
                  imageContainerVector.append(ImageContainer(instance.instanceId(),
                                                             renderPreviewImage(),
                                                             instance.instanceId()));
              instance.deactivateState();
          }

        nodeInstanceClient()->statePreviewImagesChanged(
            StatePreviewImageChangedCommand(imageContainerVector));

        slowDownRenderTimer();
        handleExtraRender();
        inFunction = false;
    }
}

void Qt5PreviewNodeInstanceServer::changeState(const ChangeStateCommand &/*command*/)
{

}

QImage Qt5PreviewNodeInstanceServer::renderPreviewImage()
{
    // Ensure the state preview image is always clipped properly to root item dimensions
    if (auto rootItem = qobject_cast<QQuickItem *>(rootNodeInstance().internalObject()))
        rootItem->setClip(true);

    rootNodeInstance().updateDirtyNodeRecursive();

    QRectF boundingRect = rootNodeInstance().boundingRect();

    QSize previewImageSize = boundingRect.size().toSize();

    if (m_previewSize.isValid() && !m_previewSize.isNull())
        previewImageSize.scale(m_previewSize, Qt::KeepAspectRatio);

    QImage previewImage = rootNodeInstance().renderPreviewImage(previewImageSize);

    return previewImage;
}

void QmlDesigner::Qt5PreviewNodeInstanceServer::removeSharedMemory(const QmlDesigner::RemoveSharedMemoryCommand &command)
{
    if (command.typeName() == "Image")
        ImageContainer::removeSharedMemorys(command.keyNumbers());
}

void Qt5PreviewNodeInstanceServer::changePreviewImageSize(
    const ChangePreviewImageSizeCommand &command)
{
    m_previewSize = command.size;

    collectItemChangesAndSendChangeCommands();
}

bool Qt5PreviewNodeInstanceServer::isPreviewServer() const
{
    return true;
}

} // namespace QmlDesigner