1/****************************************************************************
2**
3** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <Qt3DQuickExtras/qt3dquickwindow.h>
52#include <Qt3DExtras/Qt3DWindow>
53#include "qt3dquickwindow_p.h"
54#include <Qt3DQuick/QQmlAspectEngine>
55#include <Qt3DQuickExtras/qt3dquickwindow.h>
56#include <Qt3DInput/qinputaspect.h>
57#include <Qt3DInput/qinputsettings.h>
58#include <Qt3DLogic/qlogicaspect.h>
59#include <Qt3DRender/qcamera.h>
60#include <Qt3DRender/qrenderaspect.h>
61#include <Qt3DRender/qrendersurfaceselector.h>
62#include <QtGui/QGuiApplication>
63#include <QtGui/QScreen>
64#include <qopenglcontext.h>
65#include <QtQml/QQmlContext>
66#include <QtQml/qqmlincubator.h>
67
68#include <Qt3DQuickExtras/private/qt3dquickwindowlogging_p.h>
69#include <Qt3DRender/private/qrendersurfaceselector_p.h>
70#include <Qt3DRender/private/qrenderaspect_p.h>
71
72QT_BEGIN_NAMESPACE
73
74namespace Qt3DExtras {
75
76namespace Quick {
77
78namespace {
79
80class Qt3DQuickWindowIncubationController : public QObject, public QQmlIncubationController
81{
82 Q_OBJECT
83public:
84 explicit Qt3DQuickWindowIncubationController(QObject *parent = nullptr)
85 : QObject(parent)
86 , m_incubationTime(std::max(a: 1, b: int(1000 / QGuiApplication::primaryScreen()->refreshRate()) / 3))
87 {
88 startTimer(interval: QGuiApplication::primaryScreen()->refreshRate());
89 }
90
91 void timerEvent(QTimerEvent *) override
92 {
93 incubateFor(msecs: m_incubationTime);
94 }
95
96private:
97 const int m_incubationTime;
98};
99
100} // anonymous
101
102Qt3DQuickWindowPrivate::Qt3DQuickWindowPrivate()
103 : m_engine(nullptr)
104 , m_renderAspect(nullptr)
105 , m_inputAspect(nullptr)
106 , m_logicAspect(nullptr)
107 , m_initialized(false)
108 , m_cameraAspectRatioMode(Qt3DQuickWindow::AutomaticAspectRatio)
109 , m_incubationController(nullptr)
110{
111}
112
113Qt3DQuickWindow::Qt3DQuickWindow(QWindow *parent)
114 : QWindow(*new Qt3DQuickWindowPrivate(), parent)
115{
116 Q_D(Qt3DQuickWindow);
117
118 resize(w: 1024, h: 768);
119
120 Qt3DExtras::setupWindowSurface(window: this, Qt3DRender::API::OpenGL);
121
122 d->m_renderAspect = new Qt3DRender::QRenderAspect;
123 if (parent && parent->screen())
124 static_cast<Qt3DRender::QRenderAspectPrivate*>(Qt3DRender::QRenderAspectPrivate::get(q: d->m_renderAspect))->m_screen = parent->screen();
125 d->m_inputAspect = new Qt3DInput::QInputAspect;
126 d->m_logicAspect = new Qt3DLogic::QLogicAspect;
127 d->m_engine = new Qt3DCore::Quick::QQmlAspectEngine;
128
129 d->m_engine->aspectEngine()->registerAspect(aspect: d->m_renderAspect);
130 d->m_engine->aspectEngine()->registerAspect(aspect: d->m_inputAspect);
131 d->m_engine->aspectEngine()->registerAspect(aspect: d->m_logicAspect);
132}
133
134Qt3DQuickWindow::~Qt3DQuickWindow()
135{
136 Q_D(Qt3DQuickWindow);
137 delete d->m_engine;
138}
139
140void Qt3DQuickWindow::registerAspect(Qt3DCore::QAbstractAspect *aspect)
141{
142 Q_ASSERT(!isVisible());
143 Q_D(Qt3DQuickWindow);
144 d->m_engine->aspectEngine()->registerAspect(aspect);
145}
146
147void Qt3DQuickWindow::registerAspect(const QString &name)
148{
149 Q_ASSERT(!isVisible());
150 Q_D(Qt3DQuickWindow);
151 d->m_engine->aspectEngine()->registerAspect(name);
152}
153
154void Qt3DQuickWindow::setSource(const QUrl &source)
155{
156 Q_D(Qt3DQuickWindow);
157 d->m_source = source;
158}
159
160Qt3DCore::Quick::QQmlAspectEngine *Qt3DQuickWindow::engine() const
161{
162 Q_D(const Qt3DQuickWindow);
163 return d->m_engine;
164}
165
166void Qt3DQuickWindow::setCameraAspectRatioMode(CameraAspectRatioMode mode)
167{
168 Q_D(Qt3DQuickWindow);
169 if (d->m_cameraAspectRatioMode == mode)
170 return;
171
172 d->m_cameraAspectRatioMode = mode;
173 setCameraAspectModeHelper();
174 emit cameraAspectRatioModeChanged(mode);
175}
176
177Qt3DQuickWindow::CameraAspectRatioMode Qt3DQuickWindow::cameraAspectRatioMode() const
178{
179 Q_D(const Qt3DQuickWindow);
180 return d->m_cameraAspectRatioMode;
181}
182
183void Qt3DQuickWindow::showEvent(QShowEvent *e)
184{
185 Q_D(Qt3DQuickWindow);
186 if (!d->m_initialized) {
187
188 // Connect to the QQmlAspectEngine's statusChanged signal so that when the QML is loaded
189 // and th eobjects hav ebeen instantiated, but before we set them on the QAspectEngine we
190 // can swoop in and set the window surface and camera on the framegraph and ensure the camera
191 // respects the window's aspect ratio
192 connect(sender: d->m_engine, signal: &Qt3DCore::Quick::QQmlAspectEngine::sceneCreated,
193 receiver: this, slot: &Qt3DQuickWindow::onSceneCreated);
194
195 d->m_engine->setSource(d->m_source);
196
197 // Set the QQmlIncubationController on the window
198 // to benefit from asynchronous incubation
199 if (!d->m_incubationController)
200 d->m_incubationController = new Qt3DQuickWindowIncubationController(this);
201
202 d->m_engine->qmlEngine()->setIncubationController(d->m_incubationController);
203
204 d->m_initialized = true;
205 }
206 QWindow::showEvent(e);
207}
208
209void Qt3DQuickWindow::onSceneCreated(QObject *rootObject)
210{
211 Q_ASSERT(rootObject);
212 Q_D(Qt3DQuickWindow);
213
214 setWindowSurface(rootObject);
215
216 if (d->m_cameraAspectRatioMode == AutomaticAspectRatio) {
217 // Set aspect ratio of first camera to match the window
218 QList<Qt3DRender::QCamera *> cameras
219 = rootObject->findChildren<Qt3DRender::QCamera *>();
220 if (cameras.isEmpty()) {
221 qCDebug(QuickWindow) << "No camera found";
222 } else {
223 d->m_camera = cameras.first();
224 setCameraAspectModeHelper();
225 }
226 }
227
228 // Set ourselves up as a source of input events for the input aspect
229 Qt3DInput::QInputSettings *inputSettings = rootObject->findChild<Qt3DInput::QInputSettings *>();
230 if (inputSettings) {
231 inputSettings->setEventSource(this);
232 } else {
233 qCDebug(QuickWindow) << "No Input Settings found, keyboard and mouse events won't be handled";
234 }
235}
236
237void Qt3DQuickWindow::setWindowSurface(QObject *rootObject)
238{
239 Qt3DRender::QRenderSurfaceSelector *surfaceSelector = Qt3DRender::QRenderSurfaceSelectorPrivate::find(rootObject);
240 if (surfaceSelector)
241 surfaceSelector->setSurface(this);
242}
243
244void Qt3DQuickWindow::setCameraAspectModeHelper()
245{
246 Q_D(Qt3DQuickWindow);
247 switch (d->m_cameraAspectRatioMode) {
248 case AutomaticAspectRatio:
249 connect(sender: this, signal: &QWindow::widthChanged, receiver: this, slot: &Qt3DQuickWindow::updateCameraAspectRatio);
250 connect(sender: this, signal: &QWindow::heightChanged, receiver: this, slot: &Qt3DQuickWindow::updateCameraAspectRatio);
251 // Update the aspect ratio the first time the surface is set
252 updateCameraAspectRatio();
253 break;
254 case UserAspectRatio:
255 disconnect(sender: this, signal: &QWindow::widthChanged, receiver: this, slot: &Qt3DQuickWindow::updateCameraAspectRatio);
256 disconnect(sender: this, signal: &QWindow::heightChanged, receiver: this, slot: &Qt3DQuickWindow::updateCameraAspectRatio);
257 break;
258 }
259}
260
261void Qt3DQuickWindow::updateCameraAspectRatio()
262{
263 Q_D(Qt3DQuickWindow);
264 if (d->m_camera) {
265 d->m_camera->setAspectRatio(static_cast<float>(width()) /
266 std::max(a: 1.f, b: static_cast<float>(height())));
267 }
268}
269
270} // Quick
271
272} // Qt3DExtras
273
274QT_END_NAMESPACE
275
276#include "qt3dquickwindow.moc"
277

source code of qt3d/src/quick3d/quick3dextras/qt3dquickwindow.cpp