aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qml/qmlpuppet/qml2puppet/instances/quick3drenderablenodeinstance.cpp
blob: 3e799ed69e5e3c0e578b047a2ae444d1c4dc139d (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
130
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "quick3drenderablenodeinstance.h"
#include "qt5nodeinstanceserver.h"
#include "quickitemnodeinstance.h"
#include "../editor3d/generalhelper.h"

#ifdef QUICK3D_MODULE
#include <private/qquick3dobject_p.h>
#include <private/qquickstategroup_p.h>
#endif


namespace QmlDesigner {
namespace Internal {

const QRectF preview3dBoundingRect(0, 0, 640, 480);

Quick3DRenderableNodeInstance::Quick3DRenderableNodeInstance(QObject *node)
   : ObjectNodeInstance(node)
{
}

Quick3DRenderableNodeInstance::~Quick3DRenderableNodeInstance()
{
    delete m_dummyRootView;
}

void Quick3DRenderableNodeInstance::initialize(const ObjectNodeInstance::Pointer &objectNodeInstance,
                                     InstanceContainer::NodeFlags flags)
{
#ifdef QUICK3D_MODULE
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    // In case this is the scene root, we need to create a dummy View3D for the scene
    // in preview puppets
    if (instanceId() == 0 && (!nodeInstanceServer()->isInformationServer())) {
        nodeInstanceServer()->quickWindow()->setDefaultAlphaBuffer(true);
        nodeInstanceServer()->quickWindow()->setColor(Qt::transparent);

        auto helper = new QmlDesigner::Internal::GeneralHelper();
        engine()->rootContext()->setContextProperty("_generalHelper", helper);

        QQmlComponent component(engine());
        component.loadUrl(QUrl("qrc:/qtquickplugin/mockfiles/qt6/ModelNode3DImageView.qml"));
        m_dummyRootView = qobject_cast<QQuickItem *>(component.create());

        QMetaObject::invokeMethod(m_dummyRootView, m_dummyRootViewCreateFunction,
                                  Q_ARG(QVariant, QVariant::fromValue(object())));

        nodeInstanceServer()->setRootItem(m_dummyRootView);
    }
#endif // QT_VERSION
#endif // QUICK3D_MODULE
    ObjectNodeInstance::initialize(objectNodeInstance, flags);
}

QImage Quick3DRenderableNodeInstance::renderImage() const
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    if (!isRootNodeInstance() || !m_dummyRootView)
        return {};

    QSize size = preview3dBoundingRect.size().toSize();
    nodeInstanceServer()->quickWindow()->resize(size);
    m_dummyRootView->setSize(size);

    // Just render the window once to update spatial nodes
    nodeInstanceServer()->renderWindow();

    QMetaObject::invokeMethod(m_dummyRootView, "fitToViewPort", Qt::DirectConnection);

    QRectF renderBoundingRect = m_dummyRootView->boundingRect();
    QImage renderImage;

    if (QuickItemNodeInstance::unifiedRenderPath()) {
        renderImage = nodeInstanceServer()->grabWindow();
        renderImage = renderImage.copy(renderBoundingRect.toRect());
    } else {
        renderImage = nodeInstanceServer()->grabItem(m_dummyRootView);
    }

    // When grabbing an offscreen window the device pixel ratio is 1
    renderImage.setDevicePixelRatio(1);

    return renderImage;
#endif
    return {};
}

QImage Quick3DRenderableNodeInstance::renderPreviewImage(
    [[maybe_unused]] const QSize &previewImageSize) const
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    if (!isRootNodeInstance() || !m_dummyRootView)
        return {};

    nodeInstanceServer()->quickWindow()->resize(previewImageSize);
    m_dummyRootView->setSize(previewImageSize);

    // Just render the window once to update spatial nodes
    nodeInstanceServer()->renderWindow();

    QMetaObject::invokeMethod(m_dummyRootView, "fitToViewPort", Qt::DirectConnection);

    QRectF previewItemBoundingRect = boundingRect();

    if (previewItemBoundingRect.isValid()) {
        const QSize size = previewImageSize;
        if (m_dummyRootView->isVisible()) {
            QImage image;
            image = nodeInstanceServer()->grabWindow();
            image = image.copy(previewItemBoundingRect.toRect());
            image = image.scaledToWidth(size.width());
            return image;
        } else {
            QImage transparentImage(size, QImage::Format_ARGB32_Premultiplied);
            transparentImage.fill(Qt::transparent);
            return transparentImage;
        }
    }
#endif
    return {};
}

bool Quick3DRenderableNodeInstance::isRenderable() const
{
    return m_dummyRootView;
}

bool Quick3DRenderableNodeInstance::hasContent() const
{
    return true;
}

QRectF Quick3DRenderableNodeInstance::boundingRect() const
{
    //The information server has no m_dummyRootView therefore we use the hardcoded value
    if (nodeInstanceServer()->isInformationServer())
        return preview3dBoundingRect;

    if (m_dummyRootView)
        return m_dummyRootView->boundingRect();
    return ObjectNodeInstance::boundingRect();
}

QRectF Quick3DRenderableNodeInstance::contentItemBoundingBox() const
{
    return boundingRect();
}

QPointF Quick3DRenderableNodeInstance::position() const
{
    return QPointF(0, 0);
}

QSizeF Quick3DRenderableNodeInstance::size() const
{
    return boundingRect().size();
}

QList<ServerNodeInstance> Quick3DRenderableNodeInstance::stateInstances() const
{
    QList<ServerNodeInstance> instanceList;
#ifdef QUICK3D_MODULE
    if (auto obj3D = qobject_cast<QQuick3DObject *>(object())) {
        const QList<QQuickState *> stateList = QQuick3DObjectPrivate::get(obj3D)->_states()->states();
        for (QQuickState *state : stateList) {
            if (state && nodeInstanceServer()->hasInstanceForObject(state))
                instanceList.append(nodeInstanceServer()->instanceForObject(state));
        }
    }
#endif
    return instanceList;
}

QQuickItem *Quick3DRenderableNodeInstance::contentItem() const
{
    return m_dummyRootView;
}

void Quick3DRenderableNodeInstance::setPropertyVariant(const PropertyName &name,
                                                       const QVariant &value)
{
    if (m_dummyRootView && name == "isLibraryIcon")
        QMetaObject::invokeMethod(m_dummyRootView, "setIconMode", Q_ARG(QVariant, value));
    ObjectNodeInstance::setPropertyVariant(name, value);
}

Qt5NodeInstanceServer *Quick3DRenderableNodeInstance::qt5NodeInstanceServer() const
{
    return qobject_cast<Qt5NodeInstanceServer *>(nodeInstanceServer());
}

} // namespace Internal
} // namespace QmlDesigner