summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiikka Heikkinen <miikka.heikkinen@digia.com>2014-09-26 13:41:31 +0300
committerMiikka Heikkinen <miikka.heikkinen@digia.com>2014-09-26 14:15:51 +0300
commit5b8b37ebe42f9fb3cdc0bb269146c18ebbd56ed3 (patch)
treeee7e1ae9add63d37904de843df5741ce943052ae
parent23717be4663a087d4d906da3e7c2c751dc2e07d5 (diff)
Allow setting bounds for camera zoom level
Task-number: QTRD-3337 Change-Id: I503dc4402907a2fdfa74ca86698b0e98a23b3b08 Reviewed-by: Tomi Korpipää <tomi.korpipaa@digia.com>
-rw-r--r--examples/datavisualization/volumetric/volumetric.cpp12
-rw-r--r--examples/datavisualization/volumetric/volumetric.h1
-rw-r--r--src/datavisualization/engine/q3dcamera.cpp118
-rw-r--r--src/datavisualization/engine/q3dcamera.h8
-rw-r--r--src/datavisualization/engine/q3dcamera_p.h16
-rw-r--r--src/datavisualization/input/q3dinputhandler.cpp32
-rw-r--r--src/datavisualization/input/qtouch3dinputhandler.cpp27
7 files changed, 154 insertions, 60 deletions
diff --git a/examples/datavisualization/volumetric/volumetric.cpp b/examples/datavisualization/volumetric/volumetric.cpp
index 65599c31..ade74fb2 100644
--- a/examples/datavisualization/volumetric/volumetric.cpp
+++ b/examples/datavisualization/volumetric/volumetric.cpp
@@ -87,8 +87,9 @@ VolumetricModifier::VolumetricModifier(Q3DScatter *scatter)
//! [6]
m_graph->activeTheme()->setBackgroundEnabled(false);
- // Only allow zooming at the center to avoid clipping issues
+ // Only allow zooming at the center and limit the zoom to 200% to avoid clipping issues
static_cast<Q3DInputHandler *>(m_graph->activeInputHandler())->setZoomAtTargetEnabled(false);
+ m_graph->scene()->activeCamera()->setMaxZoomLevel(200.0f);
toggleAreaAll(true);
@@ -204,8 +205,6 @@ VolumetricModifier::VolumetricModifier(Q3DScatter *scatter)
m_graph->addCustomItem(warningLabel);
#endif
- QObject::connect(m_graph->scene()->activeCamera(), &Q3DCamera::zoomLevelChanged, this,
- &VolumetricModifier::handleZoomLevelChange);
QObject::connect(m_graph, &QAbstract3DGraph::currentFpsChanged, this,
&VolumetricModifier::handleFpsChange);
QObject::connect(&m_timer, &QTimer::timeout, this,
@@ -310,13 +309,6 @@ void VolumetricModifier::adjustSliceZ(int value)
}
}
-void VolumetricModifier::handleZoomLevelChange()
-{
- // Zooming inside volumetric object causes ugly clipping issues, so restrict zoom level a bit
- if (m_graph->scene()->activeCamera()->zoomLevel() > 200)
- m_graph->scene()->activeCamera()->setZoomLevel(200);
-}
-
void VolumetricModifier::handleFpsChange(qreal fps)
{
const QString fpsFormat = QStringLiteral("FPS: %1");
diff --git a/examples/datavisualization/volumetric/volumetric.h b/examples/datavisualization/volumetric/volumetric.h
index 7a4e7179..8d28b524 100644
--- a/examples/datavisualization/volumetric/volumetric.h
+++ b/examples/datavisualization/volumetric/volumetric.h
@@ -50,7 +50,6 @@ public slots:
void adjustSliceX(int value);
void adjustSliceY(int value);
void adjustSliceZ(int value);
- void handleZoomLevelChange();
void handleFpsChange(qreal fps);
void handleTimeout();
void toggleLowDetail(bool enabled);
diff --git a/src/datavisualization/engine/q3dcamera.cpp b/src/datavisualization/engine/q3dcamera.cpp
index 0c2a8d82..9c3e4c08 100644
--- a/src/datavisualization/engine/q3dcamera.cpp
+++ b/src/datavisualization/engine/q3dcamera.cpp
@@ -110,8 +110,36 @@ QT_BEGIN_NAMESPACE_DATAVISUALIZATION
/*!
* \qmlproperty float Camera3D::zoomLevel
*
- * This property contains the the camera zoom level in percentage. 100.0 means there is no zoom
- * in or out set in the camera.
+ * This property contains the the camera zoom level in percentage. The default value of \c{100.0}
+ * means there is no zoom in or out set in the camera.
+ * The value is limited within the bounds defined by minZoomLevel and maxZoomLevel properties.
+ *
+ * \sa minZoomLevel, maxZoomLevel
+ */
+
+/*!
+ * \qmlproperty float Camera3D::minZoomLevel
+ *
+ * This property contains the the minimum allowed camera zoom level.
+ * If the new minimum level is more than the existing maximum level, the maximum level is
+ * adjusted to the new minimum as well.
+ * If current zoomLevel is outside the new bounds, it is adjusted as well.
+ * The minZoomLevel cannot be set below \c{1.0}.
+ * Defaults to \c{10.0}.
+ *
+ * \sa zoomLevel, maxZoomLevel
+ */
+
+/*!
+ * \qmlproperty float Camera3D::maxZoomLevel
+ *
+ * This property contains the the maximum allowed camera zoom level.
+ * If the new maximum level is less than the existing minimum level, the minimum level is
+ * adjusted to the new maximum as well.
+ * If current zoomLevel is outside the new bounds, it is adjusted as well.
+ * Defaults to \c{500.0f}.
+ *
+ * \sa zoomLevel, minZoomLevel
*/
/*!
@@ -191,6 +219,8 @@ void Q3DCamera::copyValuesFrom(const Q3DObject &source)
d_ptr->m_wrapYRotation = sourceCamera.d_ptr->m_wrapYRotation;
d_ptr->m_zoomLevel = sourceCamera.d_ptr->m_zoomLevel;
+ d_ptr->m_minZoomLevel = sourceCamera.d_ptr->m_minZoomLevel;
+ d_ptr->m_maxZoomLevel = sourceCamera.d_ptr->m_maxZoomLevel;
d_ptr->m_activePreset = sourceCamera.d_ptr->m_activePreset;
}
@@ -401,8 +431,11 @@ void Q3DCamera::setCameraPreset(CameraPreset preset)
/*!
* \property Q3DCamera::zoomLevel
*
- * This property contains the the camera zoom level in percentage. \c 100.0f means there is no zoom
- * in or out set in the camera.
+ * This property contains the the camera zoom level in percentage. The default value of \c{100.0f}
+ * means there is no zoom in or out set in the camera.
+ * The value is limited within the bounds defined by minZoomLevel and maxZoomLevel properties.
+ *
+ * \sa minZoomLevel, maxZoomLevel
*/
float Q3DCamera::zoomLevel() const
{
@@ -411,10 +444,73 @@ float Q3DCamera::zoomLevel() const
void Q3DCamera::setZoomLevel(float zoomLevel)
{
- if (d_ptr->m_zoomLevel != zoomLevel) {
- d_ptr->m_zoomLevel = zoomLevel;
+ float newZoomLevel = qBound(d_ptr->m_minZoomLevel, zoomLevel, d_ptr->m_maxZoomLevel);
+
+ if (d_ptr->m_zoomLevel != newZoomLevel) {
+ d_ptr->m_zoomLevel = newZoomLevel;
+ setDirty(true);
+ emit zoomLevelChanged(newZoomLevel);
+ }
+}
+
+/*!
+ * \property Q3DCamera::minZoomLevel
+ *
+ * This property contains the the minimum allowed camera zoom level.
+ * If the new minimum level is more than the existing maximum level, the maximum level is
+ * adjusted to the new minimum as well.
+ * If current zoomLevel is outside the new bounds, it is adjusted as well.
+ * The minZoomLevel cannot be set below \c{1.0f}.
+ * Defaults to \c{10.0f}.
+ *
+ * \sa zoomLevel, maxZoomLevel
+ */
+float Q3DCamera::minZoomLevel() const
+{
+ return d_ptr->m_minZoomLevel;
+}
+
+void Q3DCamera::setMinZoomLevel(float zoomLevel)
+{
+ // Don't allow minimum to be below one, as that can cause zoom to break.
+ float newMinLevel = qMax(zoomLevel, 1.0f);
+ if (d_ptr->m_minZoomLevel != newMinLevel) {
+ d_ptr->m_minZoomLevel = newMinLevel;
+ if (d_ptr->m_maxZoomLevel < newMinLevel)
+ setMaxZoomLevel(newMinLevel);
+ setZoomLevel(d_ptr->m_zoomLevel);
+ setDirty(true);
+ emit minZoomLevelChanged(newMinLevel);
+ }
+}
+
+/*!
+ * \property Q3DCamera::maxZoomLevel
+ *
+ * This property contains the the maximum allowed camera zoom level.
+ * If the new maximum level is less than the existing minimum level, the minimum level is
+ * adjusted to the new maximum as well.
+ * If current zoomLevel is outside the new bounds, it is adjusted as well.
+ * Defaults to \c{500.0f}.
+ *
+ * \sa zoomLevel, minZoomLevel
+ */
+float Q3DCamera::maxZoomLevel() const
+{
+ return d_ptr->m_maxZoomLevel;
+}
+
+void Q3DCamera::setMaxZoomLevel(float zoomLevel)
+{
+ // Don't allow maximum to be below one, as that can cause zoom to break.
+ float newMaxLevel = qMax(zoomLevel, 1.0f);
+ if (d_ptr->m_maxZoomLevel != newMaxLevel) {
+ d_ptr->m_maxZoomLevel = newMaxLevel;
+ if (d_ptr->m_minZoomLevel > newMaxLevel)
+ setMinZoomLevel(newMaxLevel);
+ setZoomLevel(d_ptr->m_zoomLevel);
setDirty(true);
- emit zoomLevelChanged(zoomLevel);
+ emit maxZoomLevelChanged(newMaxLevel);
}
}
@@ -461,12 +557,12 @@ void Q3DCamera::setWrapYRotation(bool isEnabled)
/*!
* Utility function that sets the camera rotations and distance.\a horizontal and \a vertical
* define the camera rotations to be used.
- * Optional \a zoom parameter can be given to set the zoom percentage of the camera in range of
- * \c{10.0f - 500.0f}.
+ * Optional \a zoom parameter can be given to set the zoom percentage of the camera within
+ * the bounds defined by minZoomLevel and maxZoomLevel properties.
*/
void Q3DCamera::setCameraPosition(float horizontal, float vertical, float zoom)
{
- setZoomLevel(qBound(10.0f, zoom, 500.0f));
+ setZoomLevel(zoom);
setXRotation(horizontal);
setYRotation(vertical);
}
@@ -524,6 +620,8 @@ Q3DCameraPrivate::Q3DCameraPrivate(Q3DCamera *q) :
m_maxXRotation(180.0f),
m_maxYRotation(90.0f),
m_zoomLevel(100.0f),
+ m_minZoomLevel(10.0f),
+ m_maxZoomLevel(500.0f),
m_wrapXRotation(true),
m_wrapYRotation(false),
m_activePreset(Q3DCamera::CameraPresetNone)
diff --git a/src/datavisualization/engine/q3dcamera.h b/src/datavisualization/engine/q3dcamera.h
index a7da9031..5b539ccf 100644
--- a/src/datavisualization/engine/q3dcamera.h
+++ b/src/datavisualization/engine/q3dcamera.h
@@ -36,6 +36,8 @@ class QT_DATAVISUALIZATION_EXPORT Q3DCamera : public Q3DObject
Q_PROPERTY(bool wrapXRotation READ wrapXRotation WRITE setWrapXRotation NOTIFY wrapXRotationChanged)
Q_PROPERTY(bool wrapYRotation READ wrapYRotation WRITE setWrapYRotation NOTIFY wrapYRotationChanged)
Q_PROPERTY(QVector3D target READ target WRITE setTarget NOTIFY targetChanged REVISION 1)
+ Q_PROPERTY(float minZoomLevel READ minZoomLevel WRITE setMinZoomLevel NOTIFY minZoomLevelChanged REVISION 1)
+ Q_PROPERTY(float maxZoomLevel READ maxZoomLevel WRITE setMaxZoomLevel NOTIFY maxZoomLevelChanged REVISION 1)
public:
enum CameraPreset {
@@ -87,6 +89,10 @@ public:
float zoomLevel() const;
void setZoomLevel(float zoomLevel);
+ float minZoomLevel() const;
+ void setMinZoomLevel(float zoomLevel);
+ float maxZoomLevel() const;
+ void setMaxZoomLevel(float zoomLevel);
void setCameraPosition(float horizontal, float vertical, float zoom = 100.0f);
@@ -101,6 +107,8 @@ signals:
void wrapXRotationChanged(bool isEnabled);
void wrapYRotationChanged(bool isEnabled);
Q_REVISION(1) void targetChanged(const QVector3D &target);
+ Q_REVISION(1) void minZoomLevelChanged(float zoomLevel);
+ Q_REVISION(1) void maxZoomLevelChanged(float zoomLevel);
private:
QScopedPointer<Q3DCameraPrivate> d_ptr;
diff --git a/src/datavisualization/engine/q3dcamera_p.h b/src/datavisualization/engine/q3dcamera_p.h
index 6e9fd190..b7826a5b 100644
--- a/src/datavisualization/engine/q3dcamera_p.h
+++ b/src/datavisualization/engine/q3dcamera_p.h
@@ -90,13 +90,15 @@ public:
QMatrix4x4 m_viewMatrix;
bool m_isViewMatrixUpdateActive;
- GLfloat m_xRotation;
- GLfloat m_yRotation;
- GLfloat m_minXRotation;
- GLfloat m_minYRotation;
- GLfloat m_maxXRotation;
- GLfloat m_maxYRotation;
- GLfloat m_zoomLevel;
+ float m_xRotation;
+ float m_yRotation;
+ float m_minXRotation;
+ float m_minYRotation;
+ float m_maxXRotation;
+ float m_maxYRotation;
+ float m_zoomLevel;
+ float m_minZoomLevel;
+ float m_maxZoomLevel;
bool m_wrapXRotation;
bool m_wrapYRotation;
Q3DCamera::CameraPreset m_activePreset;
diff --git a/src/datavisualization/input/q3dinputhandler.cpp b/src/datavisualization/input/q3dinputhandler.cpp
index 0bed3cb9..43cee84e 100644
--- a/src/datavisualization/input/q3dinputhandler.cpp
+++ b/src/datavisualization/input/q3dinputhandler.cpp
@@ -22,18 +22,16 @@
QT_BEGIN_NAMESPACE_DATAVISUALIZATION
-const int minZoomLevel = 10;
-const int halfSizeZoomLevel = 50;
-const int oneToOneZoomLevel = 100;
-const int maxZoomLevel = 500;
-const int driftTowardCenterLevel = 175;
-const float wheelZoomDrift = 0.1f;
+static const int halfSizeZoomLevel = 50;
+static const int oneToOneZoomLevel = 100;
+static const int driftTowardCenterLevel = 175;
+static const float wheelZoomDrift = 0.1f;
-const int nearZoomRangeDivider = 12;
-const int midZoomRangeDivider = 60;
-const int farZoomRangeDivider = 120;
+static const int nearZoomRangeDivider = 12;
+static const int midZoomRangeDivider = 60;
+static const int farZoomRangeDivider = 120;
-const float rotationSpeed = 100.0f;
+static const float rotationSpeed = 100.0f;
/*!
* \class Q3DInputHandler
@@ -58,7 +56,7 @@ const float rotationSpeed = 100.0f;
* \l {QAbstract3DGraph::selectionMode}{selection mode}.
* \row
* \li Mouse wheel
- * \li Zoom in/out within default range (10...500%).
+ * \li Zoom in/out within the allowable zoom range set for Q3DCamera.
* \row
* \li Left click on the primary view when the secondary view is visible
* \li Closes the secondary view.
@@ -239,17 +237,17 @@ void Q3DInputHandler::wheelEvent(QWheelEvent *event)
return;
// Adjust zoom level based on what zoom range we're in.
- int zoomLevel = scene()->activeCamera()->zoomLevel();
+ Q3DCamera *camera = scene()->activeCamera();
+ int zoomLevel = int(camera->zoomLevel());
+ const int minZoomLevel = int(camera->minZoomLevel());
+ const int maxZoomLevel = int(camera->maxZoomLevel());
if (zoomLevel > oneToOneZoomLevel)
zoomLevel += event->angleDelta().y() / nearZoomRangeDivider;
else if (zoomLevel > halfSizeZoomLevel)
zoomLevel += event->angleDelta().y() / midZoomRangeDivider;
else
zoomLevel += event->angleDelta().y() / farZoomRangeDivider;
- if (zoomLevel > maxZoomLevel)
- zoomLevel = maxZoomLevel;
- else if (zoomLevel < minZoomLevel)
- zoomLevel = minZoomLevel;
+ zoomLevel = qBound(minZoomLevel, zoomLevel, maxZoomLevel);
if (isZoomAtTargetEnabled()) {
scene()->setGraphPositionQuery(event->pos());
@@ -259,7 +257,7 @@ void Q3DInputHandler::wheelEvent(QWheelEvent *event)
d_ptr->m_requestedZoomLevel = zoomLevel;
d_ptr->m_driftMultiplier = wheelZoomDrift;
} else {
- scene()->activeCamera()->setZoomLevel(zoomLevel);
+ camera->setZoomLevel(zoomLevel);
}
}
}
diff --git a/src/datavisualization/input/qtouch3dinputhandler.cpp b/src/datavisualization/input/qtouch3dinputhandler.cpp
index b811548b..20eebb58 100644
--- a/src/datavisualization/input/qtouch3dinputhandler.cpp
+++ b/src/datavisualization/input/qtouch3dinputhandler.cpp
@@ -22,18 +22,16 @@
QT_BEGIN_NAMESPACE_DATAVISUALIZATION
-const float maxTapAndHoldJitter = 20.0f;
-const int maxPinchJitter = 10;
+static const float maxTapAndHoldJitter = 20.0f;
+static const int maxPinchJitter = 10;
#if defined (Q_OS_ANDROID) || defined(Q_OS_IOS)
-const int maxSelectionJitter = 10;
+static const int maxSelectionJitter = 10;
#else
-const int maxSelectionJitter = 5;
+static const int maxSelectionJitter = 5;
#endif
-const int tapAndHoldTime = 250;
-const float rotationSpeed = 200.0f;
-const int minZoomLevel = 10;
-const int maxZoomLevel = 500;
-const float touchZoomDrift = 0.02f;
+static const int tapAndHoldTime = 250;
+static const float rotationSpeed = 200.0f;
+static const float touchZoomDrift = 0.02f;
/*!
* \class QTouch3DInputHandler
@@ -61,7 +59,7 @@ const float touchZoomDrift = 0.02f;
* \li Same as tap.
* \row
* \li Pinch
- * \li Zoom in/out within default range (10...500%).
+ * \li Zoom in/out within the allowable zoom range set for Q3DCamera.
* \row
* \li Tap on the primary view when the secondary view is visible
* \li Closes the secondary view.
@@ -193,16 +191,15 @@ void QTouch3DInputHandlerPrivate::handlePinchZoom(float distance, const QPoint &
return;
m_inputState = QAbstract3DInputHandlerPrivate::InputStatePinching;
Q3DCamera *camera = q_ptr->scene()->activeCamera();
- int zoomLevel = camera->zoomLevel();
+ int zoomLevel = int(camera->zoomLevel());
+ const int minZoomLevel = int(camera->minZoomLevel());
+ const int maxZoomLevel = int(camera->maxZoomLevel());
float zoomRate = qSqrt(qSqrt(zoomLevel));
if (newDistance > prevDist)
zoomLevel += zoomRate;
else
zoomLevel -= zoomRate;
- if (zoomLevel > maxZoomLevel)
- zoomLevel = maxZoomLevel;
- else if (zoomLevel < minZoomLevel)
- zoomLevel = minZoomLevel;
+ zoomLevel = qBound(minZoomLevel, zoomLevel, maxZoomLevel);
if (q_ptr->isZoomAtTargetEnabled()) {
q_ptr->scene()->setGraphPositionQuery(pos);