| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Data Visualization module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 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 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include "axesinputhandler.h" |
| 31 | #include <QtCore/qmath.h> |
| 32 | |
| 33 | AxesInputHandler::AxesInputHandler(QAbstract3DGraph *graph, QObject *parent) : |
| 34 | Q3DInputHandler(parent), |
| 35 | m_mousePressed(false), |
| 36 | m_state(StateNormal), |
| 37 | m_axisX(0), |
| 38 | m_axisZ(0), |
| 39 | m_axisY(0), |
| 40 | m_speedModifier(15.0f) |
| 41 | { |
| 42 | //! [3] |
| 43 | // Connect to the item selection signal from graph |
| 44 | connect(sender: graph, signal: &QAbstract3DGraph::selectedElementChanged, receiver: this, |
| 45 | slot: &AxesInputHandler::handleElementSelected); |
| 46 | //! [3] |
| 47 | } |
| 48 | |
| 49 | //! [0] |
| 50 | void AxesInputHandler::mousePressEvent(QMouseEvent *event, const QPoint &mousePos) |
| 51 | { |
| 52 | Q3DInputHandler::mousePressEvent(event, mousePos); |
| 53 | if (Qt::LeftButton == event->button()) |
| 54 | m_mousePressed = true; |
| 55 | } |
| 56 | //! [0] |
| 57 | |
| 58 | //! [2] |
| 59 | void AxesInputHandler::mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos) |
| 60 | { |
| 61 | //! [5] |
| 62 | // Check if we're trying to drag axis label |
| 63 | if (m_mousePressed && m_state != StateNormal) { |
| 64 | //! [5] |
| 65 | setPreviousInputPos(inputPosition()); |
| 66 | setInputPosition(mousePos); |
| 67 | handleAxisDragging(); |
| 68 | } else { |
| 69 | Q3DInputHandler::mouseMoveEvent(event, mousePos); |
| 70 | } |
| 71 | } |
| 72 | //! [2] |
| 73 | |
| 74 | //! [1] |
| 75 | void AxesInputHandler::mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos) |
| 76 | { |
| 77 | Q3DInputHandler::mouseReleaseEvent(event, mousePos); |
| 78 | m_mousePressed = false; |
| 79 | m_state = StateNormal; |
| 80 | } |
| 81 | //! [1] |
| 82 | |
| 83 | void AxesInputHandler::handleElementSelected(QAbstract3DGraph::ElementType type) |
| 84 | { |
| 85 | //! [4] |
| 86 | switch (type) { |
| 87 | case QAbstract3DGraph::ElementAxisXLabel: |
| 88 | m_state = StateDraggingX; |
| 89 | break; |
| 90 | case QAbstract3DGraph::ElementAxisYLabel: |
| 91 | m_state = StateDraggingY; |
| 92 | break; |
| 93 | case QAbstract3DGraph::ElementAxisZLabel: |
| 94 | m_state = StateDraggingZ; |
| 95 | break; |
| 96 | default: |
| 97 | m_state = StateNormal; |
| 98 | break; |
| 99 | } |
| 100 | //! [4] |
| 101 | } |
| 102 | |
| 103 | void AxesInputHandler::handleAxisDragging() |
| 104 | { |
| 105 | float distance = 0.0f; |
| 106 | |
| 107 | //! [6] |
| 108 | // Get scene orientation from active camera |
| 109 | float xRotation = scene()->activeCamera()->xRotation(); |
| 110 | float yRotation = scene()->activeCamera()->yRotation(); |
| 111 | //! [6] |
| 112 | |
| 113 | //! [7] |
| 114 | // Calculate directional drag multipliers based on rotation |
| 115 | float xMulX = qCos(v: qDegreesToRadians(degrees: xRotation)); |
| 116 | float xMulY = qSin(v: qDegreesToRadians(degrees: xRotation)); |
| 117 | float zMulX = qSin(v: qDegreesToRadians(degrees: xRotation)); |
| 118 | float zMulY = qCos(v: qDegreesToRadians(degrees: xRotation)); |
| 119 | //! [7] |
| 120 | |
| 121 | //! [8] |
| 122 | // Get the drag amount |
| 123 | QPoint move = inputPosition() - previousInputPos(); |
| 124 | |
| 125 | // Flip the effect of y movement if we're viewing from below |
| 126 | float yMove = (yRotation < 0) ? -move.y() : move.y(); |
| 127 | //! [8] |
| 128 | |
| 129 | //! [9] |
| 130 | // Adjust axes |
| 131 | switch (m_state) { |
| 132 | case StateDraggingX: |
| 133 | distance = (move.x() * xMulX - yMove * xMulY) / m_speedModifier; |
| 134 | m_axisX->setRange(min: m_axisX->min() - distance, max: m_axisX->max() - distance); |
| 135 | break; |
| 136 | case StateDraggingZ: |
| 137 | distance = (move.x() * zMulX + yMove * zMulY) / m_speedModifier; |
| 138 | m_axisZ->setRange(min: m_axisZ->min() + distance, max: m_axisZ->max() + distance); |
| 139 | break; |
| 140 | case StateDraggingY: |
| 141 | distance = move.y() / m_speedModifier; // No need to use adjusted y move here |
| 142 | m_axisY->setRange(min: m_axisY->min() + distance, max: m_axisY->max() + distance); |
| 143 | break; |
| 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | //! [9] |
| 148 | } |
| 149 | |