diff options
author | Keränen Pasi <pasi.keranen@digia.com> | 2013-08-22 13:22:21 +0300 |
---|---|---|
committer | Pasi Keränen <pasi.keranen@digia.com> | 2013-08-23 09:33:19 +0300 |
commit | 9db025fbc86e54c72abebf4ef3a146b372ac9e5e (patch) | |
tree | 44045036e84effe3b6cf78dbca0792955d0e53c5 /src/datavis3d/input | |
parent | 44f83ac64537ca6ac13a97a2c267492bdd288fe7 (diff) |
First implementation of new input handling. Not integrated yet.
Change-Id: I8be494bf072926f4bd9db32572678bf7a4ea674a
Reviewed-by: Tomi Korpipää <tomi.korpipaa@digia.com>
Diffstat (limited to 'src/datavis3d/input')
-rw-r--r-- | src/datavis3d/input/input.pri | 10 | ||||
-rw-r--r-- | src/datavis3d/input/q3dinputhandler.cpp | 102 | ||||
-rw-r--r-- | src/datavis3d/input/q3dinputhandler.h | 49 | ||||
-rw-r--r-- | src/datavis3d/input/qabstract3dinputhandler.cpp | 163 | ||||
-rw-r--r-- | src/datavis3d/input/qabstract3dinputhandler.h | 86 | ||||
-rw-r--r-- | src/datavis3d/input/qabstract3dinputhandler_p.h | 64 | ||||
-rw-r--r-- | src/datavis3d/input/qtouch3dinputhandler.cpp | 97 | ||||
-rw-r--r-- | src/datavis3d/input/qtouch3dinputhandler.h | 41 |
8 files changed, 612 insertions, 0 deletions
diff --git a/src/datavis3d/input/input.pri b/src/datavis3d/input/input.pri new file mode 100644 index 00000000..89b032da --- /dev/null +++ b/src/datavis3d/input/input.pri @@ -0,0 +1,10 @@ +HEADERS += \ + $$PWD/qabstract3dinputhandler.h \ + $$PWD/q3dinputhandler.h \ + input/qtouch3dinputhandler.h \ + input/qabstract3dinputhandler_p.h + +SOURCES += \ + $$PWD/qabstract3dinputhandler.cpp \ + $$PWD/q3dinputhandler.cpp \ + input/qtouch3dinputhandler.cpp diff --git a/src/datavis3d/input/q3dinputhandler.cpp b/src/datavis3d/input/q3dinputhandler.cpp new file mode 100644 index 00000000..4623a69b --- /dev/null +++ b/src/datavis3d/input/q3dinputhandler.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVis3D module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ +#include "q3dinputhandler.h" + +QT_DATAVIS3D_BEGIN_NAMESPACE + +Q3DInputHandler::Q3DInputHandler() : + QAbstract3DInputHandler() +{ +} + +// Input event listeners +void Q3DInputHandler::mousePressEvent(QMouseEvent *event, const QPoint &mousePos) +{ + QRect mainViewPort = mainViewPortRect(); + if (Qt::LeftButton == event->button()) { + if (slicingActivated()) { + if (mousePos.x() <= mainViewPort.width() + && mousePos.y() <= mainViewPort.height()) { + setInputState(QDataVis::InputOnOverview); + //qDebug() << "Mouse pressed on overview"; + } else { + setInputState(QDataVis::InputOnSlice); + //qDebug() << "Mouse pressed on zoom"; + } + } else { + setInputState(QDataVis::InputOnScene); + // update mouse positions to prevent jumping when releasing or repressing a button + setInputPosition(mousePos); + //qDebug() << "Mouse pressed on scene"; + } + } else if (Qt::MiddleButton == event->button()) { + // reset rotations + setInputPosition(QPoint(0, 0)); + } else if (!slicingActivated() && Qt::RightButton == event->button()) { + // disable rotating when in slice view + setInputState(QDataVis::InputRotating); + // update mouse positions to prevent jumping when releasing or repressing a button + setInputPosition(mousePos); + } + // TODO: Call actual camera class when it's been written. + //m_cameraHelper->updateMousePos(m_mousePos);} +} + +void Q3DInputHandler::mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos) +{ + Q_UNUSED(event); + if (QDataVis::InputRotating == inputState()) { + // update mouse positions to prevent jumping when releasing or repressing a button + setInputPosition(mousePos); + // TODO: Call actual camera class when it's been written. + //m_cameraHelper->updateMousePos(mousePos); + } + setInputState(QDataVis::InputNone); + +} + +void Q3DInputHandler::mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos) +{ + Q_UNUSED(event); + if (QDataVis::InputRotating == inputState()) + setInputPosition(mousePos); +} + +void Q3DInputHandler::wheelEvent(QWheelEvent *event) +{ + // disable zooming if in slice view + if (slicingActivated()) + return; + + // Adjust zoom level based on what zoom range we're in. + int zoomLevel = QAbstract3DInputHandler::zoomLevel(); + 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; + + setZoomLevel(zoomLevel); +} + +QT_DATAVIS3D_END_NAMESPACE diff --git a/src/datavis3d/input/q3dinputhandler.h b/src/datavis3d/input/q3dinputhandler.h new file mode 100644 index 00000000..d995fe3f --- /dev/null +++ b/src/datavis3d/input/q3dinputhandler.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVis3D module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#ifndef QDEFAULT3DINPUTHANDLER_H +#define QDEFAULT3DINPUTHANDLER_H + +const int minZoomLevel = 10; +const int halfSizeZoomLevel = 50; +const int oneToOneZoomLevel = 100; +const int maxZoomLevel = 500; + +const int nearZoomRangeDivider = 12; +const int midZoomRangeDivider = 60; +const int farZoomRangeDivider = 120; + +#include "qabstract3dinputhandler.h" + +QT_DATAVIS3D_BEGIN_NAMESPACE + +class Q3DInputHandler : public QAbstract3DInputHandler +{ +public: + explicit Q3DInputHandler(); + + // Input event listeners + virtual void mousePressEvent(QMouseEvent *event, const QPoint &mousePos); + virtual void mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos); + virtual void mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos); + virtual void wheelEvent(QWheelEvent *event); +}; + +QT_DATAVIS3D_END_NAMESPACE + +#endif // QDEFAULT3DINPUTHANDLER_H diff --git a/src/datavis3d/input/qabstract3dinputhandler.cpp b/src/datavis3d/input/qabstract3dinputhandler.cpp new file mode 100644 index 00000000..5155e2e2 --- /dev/null +++ b/src/datavis3d/input/qabstract3dinputhandler.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVis3D module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ +#include "qabstract3dinputhandler.h" + +QT_DATAVIS3D_BEGIN_NAMESPACE + +QAbstract3DInputHandler::QAbstract3DInputHandler() : + QObject(), + d_ptr(new QAbstract3DInputHandlerPrivate(this)) +{ +} + +// Input event listeners +void QAbstract3DInputHandler::mouseDoubleClickEvent(QMouseEvent *event) +{ + Q_UNUSED(event); +} + +void QAbstract3DInputHandler::touchEvent(QTouchEvent *event) +{ + Q_UNUSED(event); +} + +void QAbstract3DInputHandler::mousePressEvent(QMouseEvent *event, const QPoint &mousePos) +{ + Q_UNUSED(event); + Q_UNUSED(mousePos); +} + +void QAbstract3DInputHandler::mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos) +{ + Q_UNUSED(event); + Q_UNUSED(mousePos); +} + +void QAbstract3DInputHandler::mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos) +{ + Q_UNUSED(event); + Q_UNUSED(mousePos); +} + +void QAbstract3DInputHandler::wheelEvent(QWheelEvent *event) +{ + Q_UNUSED(event); +} + +// Property get/set +QDataVis::InputState QAbstract3DInputHandler::inputState() +{ + return d_ptr->m_inputState; +} + +void QAbstract3DInputHandler::setInputState(const QDataVis::InputState inputState) +{ + if (inputState != d_ptr->m_inputState) { + d_ptr->m_inputState = inputState; + emit inputStateChanged(inputState); + } +} + +QPoint QAbstract3DInputHandler::inputPosition() +{ + return d_ptr->m_inputPosition; +} + +void QAbstract3DInputHandler::setInputPosition(const QPoint position) +{ + if (position != d_ptr->m_inputPosition) { + d_ptr->m_inputPosition = position; + emit positionChanged(position); + } +} + +bool QAbstract3DInputHandler::slicingActivated() +{ + return d_ptr->m_isSlicingActivated; +} + +void QAbstract3DInputHandler::setSlicingActivated(const bool isSlicing) +{ + if (isSlicing != d_ptr->m_isSlicingActivated) { + d_ptr->m_isSlicingActivated = isSlicing; + emit slicingActiveChanged(isSlicing); + } +} + +int QAbstract3DInputHandler::zoomLevel() +{ + return d_ptr->m_zoomLevel; +} + +void QAbstract3DInputHandler::setZoomLevel(const int zoomLevel) +{ + if (zoomLevel != d_ptr->m_zoomLevel) { + d_ptr->m_zoomLevel = zoomLevel; + emit zoomLevelChanged(zoomLevel); + } +} + +void QAbstract3DInputHandler::setPrevDistance(int distance) +{ + d_ptr->m_prevDistance = distance; +} + +int QAbstract3DInputHandler::prevDistance() +{ + return d_ptr->m_prevDistance; +} + + +QRect QAbstract3DInputHandler::mainViewPortRect() +{ + return d_ptr->m_mainViewPort; +} + +void QAbstract3DInputHandler::setMainViewPortRect(const QRect viewPort) +{ + d_ptr->m_mainViewPort = viewPort; +} + +CameraHelper *QAbstract3DInputHandler::camera() +{ + return d_ptr->m_camera; +} + +void QAbstract3DInputHandler::setCamera(CameraHelper *camera) +{ + d_ptr->m_camera = camera; +} + + +QAbstract3DInputHandlerPrivate::QAbstract3DInputHandlerPrivate(QAbstract3DInputHandler *q) : + q_ptr(q), + m_prevDistance(0), + m_inputState(QDataVis::InputNone), + m_isSlicingActivated(false), + m_inputPosition(QPoint(0,0)), + m_zoomLevel(0), + m_camera(0) +{ +} + +QAbstract3DInputHandlerPrivate::~QAbstract3DInputHandlerPrivate() +{ + +} + +QT_DATAVIS3D_END_NAMESPACE diff --git a/src/datavis3d/input/qabstract3dinputhandler.h b/src/datavis3d/input/qabstract3dinputhandler.h new file mode 100644 index 00000000..5d98afa1 --- /dev/null +++ b/src/datavis3d/input/qabstract3dinputhandler.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVis3D module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#ifndef QABSTRACT3DINPUTHANDLER_H +#define QABSTRACT3DINPUTHANDLER_H + +#include <QObject> + +#include "qdatavis3denums.h" +#include "qabstract3dinputhandler_p.h" + +QT_DATAVIS3D_BEGIN_NAMESPACE + +class QAbstract3DInputHandler : public QObject +{ + Q_OBJECT + Q_PROPERTY(QtDataVis3D::QDataVis::InputState inputState READ inputState WRITE setInputState NOTIFY inputStateChanged) + Q_PROPERTY(QPoint inputPosition READ inputPosition WRITE setInputPosition NOTIFY positionChanged) + Q_PROPERTY(bool slicingActivated READ slicingActivated WRITE setSlicingActivated NOTIFY slicingActiveChanged) + Q_PROPERTY(int zoomLevel READ zoomLevel WRITE setZoomLevel NOTIFY zoomLevelChanged) + Q_PROPERTY(QRect mainViewPortRect READ mainViewPortRect WRITE setMainViewPortRect) + + Q_PROPERTY(CameraHelper *camera READ camera WRITE setCamera) + +private: + QScopedPointer<QAbstract3DInputHandlerPrivate> d_ptr; + +public: + explicit QAbstract3DInputHandler(); + + // Input event listeners + virtual void mouseDoubleClickEvent(QMouseEvent *event); + virtual void touchEvent(QTouchEvent *event); + virtual void mousePressEvent(QMouseEvent *event, const QPoint &mousePos); + virtual void mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos); + virtual void mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos); + virtual void wheelEvent(QWheelEvent *event); + +public: + // Property get/set functions + + // TODO: Check if the inputState needs to be visible outside of subclasses in the final architecture + QDataVis::InputState inputState(); + void setInputState(const QDataVis::InputState inputState); + void setInputPosition(const QPoint position); + QPoint inputPosition(); + void setSlicingActivated(const bool isSlicing); + bool slicingActivated(); + void setZoomLevel(const int zoomLevel); + int zoomLevel(); + QRect mainViewPortRect(); + void setMainViewPortRect(const QRect viewPort); + + // TODO: Modify for proper camera once that is available + CameraHelper *camera(); + void setCamera(CameraHelper *camera); + +protected: + void setPrevDistance(int distance); + int prevDistance(); + +signals: + void positionChanged(QPoint position); + void inputStateChanged(QDataVis::InputState state); + void slicingActiveChanged(bool isSlicing); + void zoomLevelChanged(int zoomLevel); +}; + +QT_DATAVIS3D_END_NAMESPACE + +#endif // QABSTRACT3DINPUTHANDLER_H diff --git a/src/datavis3d/input/qabstract3dinputhandler_p.h b/src/datavis3d/input/qabstract3dinputhandler_p.h new file mode 100644 index 00000000..7c56c2fa --- /dev/null +++ b/src/datavis3d/input/qabstract3dinputhandler_p.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVis3D module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the QtDataVis3D API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef QABSTRACT3DINPUTHANDLER_P_H +#define QABSTRACT3DINPUTHANDLER_P_H + +#include <QMouseEvent> + +QT_DATAVIS3D_BEGIN_NAMESPACE + +class QAbstract3DInputHandler; +class CameraHelper; + +class QAbstract3DInputHandlerPrivate +{ +public: + QAbstract3DInputHandlerPrivate(QAbstract3DInputHandler *q); + ~QAbstract3DInputHandlerPrivate(); + +public: + QAbstract3DInputHandler *q_ptr; + int m_prevDistance; + +private: + QDataVis::InputState m_inputState; + bool m_isSlicingActivated; + QPoint m_inputPosition; + int m_zoomLevel; + QRect m_mainViewPort; + + // TODO: Replace with proper camera once it's available + CameraHelper *m_camera; + + friend class QAbstract3DInputHandler; +}; + +QT_DATAVIS3D_END_NAMESPACE + +#endif // QABSTRACT3DINPUTHANDLER_P_H diff --git a/src/datavis3d/input/qtouch3dinputhandler.cpp b/src/datavis3d/input/qtouch3dinputhandler.cpp new file mode 100644 index 00000000..128b8c49 --- /dev/null +++ b/src/datavis3d/input/qtouch3dinputhandler.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVis3D module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ +#include "qtouch3dinputhandler.h" + +QT_DATAVIS3D_BEGIN_NAMESPACE + +QTouch3DInputHandler::QTouch3DInputHandler() : + Q3DInputHandler() +{ +} + +// Input event listeners +void QTouch3DInputHandler::mouseDoubleClickEvent(QMouseEvent *event) +{ + if (!slicingActivated()) { + setInputState( QDataVis::InputOnScene ); + // update mouse positions to prevent jumping when releasing or repressing a button + setInputPosition( event->pos() ); + } +} + +void QTouch3DInputHandler::touchEvent(QTouchEvent *event) +{ + QList<QTouchEvent::TouchPoint> points; + points = event->touchPoints(); + + if (!slicingActivated() && points.count() == 2) { + setInputState( QDataVis::InputOnPinch ); + + QPointF distance = points.at(0).pos() - points.at(1).pos(); + int newDistance = distance.manhattanLength(); + int zoomRate = 1; + int zoomLevel = QAbstract3DInputHandler::zoomLevel(); + if (zoomLevel > 100) + zoomRate = 5; + if (newDistance > prevDistance()) + zoomLevel += zoomRate; + else + zoomLevel -= zoomRate; + if (zoomLevel > 500) + zoomLevel = 500; + else if (zoomLevel < 10) + zoomLevel = 10; + setZoomLevel(zoomLevel); + setPrevDistance(newDistance); + } +} + +void QTouch3DInputHandler::mousePressEvent(QMouseEvent *event, const QPoint &mousePos) +{ + // TODO: This code needs revisiting with new Qt releases and possibly move to using touch events for these as well. + QRect mainViewPort = mainViewPortRect(); + if (Qt::LeftButton == event->button()) { + if (slicingActivated()) { + if (mousePos.x() <= mainViewPort.width() + && mousePos.y() <= mainViewPort.height()) { + setInputState(QDataVis::InputOnOverview); + //qDebug() << "Mouse pressed on overview"; + } else { + setInputState(QDataVis::InputOnSlice); + //qDebug() << "Mouse pressed on zoom"; + } + } else { + setInputState(QDataVis::InputRotating); + // update mouse positions to prevent jumping when releasing or repressing a button + setInputPosition(mousePos); + //qDebug() << "Mouse pressed on scene"; + } + } else if (Qt::MiddleButton == event->button()) { + // reset rotations + setInputPosition(QPoint(0, 0)); + } else if (!slicingActivated() && Qt::RightButton == event->button()) { + // disable rotating when in slice view + setInputState(QDataVis::InputOnScene); + // update mouse positions to prevent jumping when releasing or repressing a button + setInputPosition(mousePos); + } + // TODO: Call actual camera class when it's been written. + //m_cameraHelper->updateMousePos(m_mousePos); +} + +QT_DATAVIS3D_END_NAMESPACE diff --git a/src/datavis3d/input/qtouch3dinputhandler.h b/src/datavis3d/input/qtouch3dinputhandler.h new file mode 100644 index 00000000..a56f82ce --- /dev/null +++ b/src/datavis3d/input/qtouch3dinputhandler.h @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVis3D module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#ifndef QTOUCH3DINPUTHANDLER_H +#define QTOUCH3DINPUTHANDLER_H + +#include "q3dinputhandler.h" + +QT_DATAVIS3D_BEGIN_NAMESPACE + +class QTouch3DInputHandler : public Q3DInputHandler +{ + Q_OBJECT + +public: + explicit QTouch3DInputHandler(); + + // Input event listeners + virtual void mouseDoubleClickEvent(QMouseEvent *event); + virtual void touchEvent(QTouchEvent *event); + virtual void mousePressEvent(QMouseEvent *event, const QPoint &mousePos); +}; + +QT_DATAVIS3D_END_NAMESPACE + +#endif // QTOUCH3DINPUTHANDLER_H |