summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2016-01-18 15:45:06 +0000
committerSean Harmer <sean.harmer@kdab.com>2016-01-18 16:05:44 +0000
commitce8e5ed4a6b2c4b1e1b26a33b74e92c3edc8bfd1 (patch)
tree9ea71fe157c6fd3df7034387f8e8e8f31dc98d0a /examples
parente2fddf5f24b2c23c8188a3508e75e32db7f9ca57 (diff)
Allow for window to manage camera aspect ratio
Change-Id: I465811b8cce6b26614493f844829f9e1f0b1f3c9 Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/qt3d/examples-common/qt3dquickwindow.cpp58
-rw-r--r--examples/qt3d/examples-common/qt3dquickwindow.h21
2 files changed, 78 insertions, 1 deletions
diff --git a/examples/qt3d/examples-common/qt3dquickwindow.cpp b/examples/qt3d/examples-common/qt3dquickwindow.cpp
index 4e633fc55..759405541 100644
--- a/examples/qt3d/examples-common/qt3dquickwindow.cpp
+++ b/examples/qt3d/examples-common/qt3dquickwindow.cpp
@@ -36,6 +36,7 @@
#include "qt3dquickwindow.h"
#include <Qt3DQuick/QQmlAspectEngine>
+#include <Qt3DRender/qcamera.h>
#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DRender/qframegraph.h>
#include <Qt3DRender/qrendersurfaceselector.h>
@@ -55,6 +56,7 @@ Qt3DQuickWindow::Qt3DQuickWindow(QWindow *parent)
, m_inputAspect(new Qt3DInput::QInputAspect)
, m_logicAspect(new Qt3DLogic::QLogicAspect)
, m_initialized(false)
+ , m_cameraAspectRatioMode(AutomaticAspectRatio)
{
setSurfaceType(QSurface::OpenGLSurface);
@@ -102,6 +104,21 @@ Qt3DCore::Quick::QQmlAspectEngine *Qt3DQuickWindow::engine() const
return m_engine.data();
}
+void Qt3DQuickWindow::setCameraAspectRatioMode(CameraAspectRatioMode mode)
+{
+ if (m_cameraAspectRatioMode == mode)
+ return;
+
+ m_cameraAspectRatioMode = mode;
+ setCameraAspectModeHelper();
+ emit cameraAspectRatioModeChanged(mode);
+}
+
+Qt3DQuickWindow::CameraAspectRatioMode Qt3DQuickWindow::cameraAspectRatioMode() const
+{
+ return m_cameraAspectRatioMode;
+}
+
void Qt3DQuickWindow::showEvent(QShowEvent *e)
{
if (!m_initialized) {
@@ -134,6 +151,25 @@ void Qt3DQuickWindow::onSceneCreated(QObject *rootObject)
{
Q_ASSERT(rootObject);
+ setWindowSurface(rootObject);
+
+ if (m_cameraAspectRatioMode == AutomaticAspectRatio) {
+ // Set aspect ratio of first camera to match the window
+ QList<Qt3DRender::QCamera *> cameras
+ = rootObject->findChildren<Qt3DRender::QCamera *>();
+ if (cameras.isEmpty()) {
+ qWarning() << "No camera found";
+ } else {
+ m_camera = cameras.first();
+ setCameraAspectModeHelper();
+ }
+ }
+
+ // TODO: Set ourselves up as a source of input events for the input aspect
+}
+
+void Qt3DQuickWindow::setWindowSurface(QObject *rootObject)
+{
// Find surface selector in framegraph and set ourselves up as the
// render surface there
Qt3DRender::QFrameGraph *frameGraphComponent
@@ -157,8 +193,28 @@ void Qt3DQuickWindow::onSceneCreated(QObject *rootObject)
}
surfaceSelector->setWindow(this);
+}
- // TODO: Set ourselves up as a source of input events for the input aspect
+void Qt3DQuickWindow::setCameraAspectModeHelper()
+{
+ switch (m_cameraAspectRatioMode) {
+ case AutomaticAspectRatio:
+ connect(this, &QWindow::widthChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio);
+ connect(this, &QWindow::heightChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio);
+ break;
+ case UserAspectRatio:
+ disconnect(this, &QWindow::widthChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio);
+ disconnect(this, &QWindow::heightChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio);
+ break;
+ }
+}
+
+void Qt3DQuickWindow::updateCameraAspectRatio()
+{
+ if (m_camera) {
+ m_camera->setAspectRatio(static_cast<float>(width()) /
+ static_cast<float>(height()));
+ }
}
QT_END_NAMESPACE
diff --git a/examples/qt3d/examples-common/qt3dquickwindow.h b/examples/qt3d/examples-common/qt3dquickwindow.h
index 9991a85e5..919ab7fd4 100644
--- a/examples/qt3d/examples-common/qt3dquickwindow.h
+++ b/examples/qt3d/examples-common/qt3dquickwindow.h
@@ -38,6 +38,7 @@
#define QT3DQUICKWINDOW_H
#include <QQuickWindow>
+#include <QtCore/qpointer.h>
#include <QUrl>
QT_BEGIN_NAMESPACE
@@ -51,6 +52,7 @@ class QQmlAspectEngine;
namespace Qt3DRender {
class QRenderAspect;
+class QCamera;
}
namespace Qt3DInput {
@@ -64,6 +66,8 @@ class QLogicAspect;
class Qt3DQuickWindow : public QQuickWindow
{
Q_OBJECT
+ Q_PROPERTY(CameraAspectRatioMode cameraAspectRatioMode READ cameraAspectRatioMode WRITE setCameraAspectRatioMode NOTIFY cameraAspectRatioModeChanged)
+
public:
Qt3DQuickWindow(QWindow *parent = Q_NULLPTR);
~Qt3DQuickWindow();
@@ -74,11 +78,26 @@ public:
void setSource(const QUrl &source);
Qt3DCore::Quick::QQmlAspectEngine *engine() const;
+ enum CameraAspectRatioMode {
+ AutomaticAspectRatio,
+ UserAspectRatio
+ };
+ Q_ENUM(CameraAspectRatioMode);
+
+ void setCameraAspectRatioMode(CameraAspectRatioMode mode);
+ CameraAspectRatioMode cameraAspectRatioMode() const;
+
+Q_SIGNALS:
+ void cameraAspectRatioModeChanged(CameraAspectRatioMode mode);
+
protected:
void showEvent(QShowEvent *e) Q_DECL_OVERRIDE;
private:
void onSceneCreated(QObject *rootObject);
+ void setWindowSurface(QObject *rootObject);
+ void setCameraAspectModeHelper();
+ void updateCameraAspectRatio();
QScopedPointer<Qt3DCore::Quick::QQmlAspectEngine> m_engine;
@@ -89,6 +108,8 @@ private:
QUrl m_source;
bool m_initialized;
+ QPointer<Qt3DRender::QCamera> m_camera;
+ CameraAspectRatioMode m_cameraAspectRatioMode;
};
QT_END_NAMESPACE