From f0ccb395f2688ed619ed769d23cfdc4b191ad609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Korpip=C3=A4=C3=A4?= Date: Fri, 23 May 2014 07:17:02 +0300 Subject: Fixed axis dragging example for touch devices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-number: QTRD-3139 Change-Id: If81d707618d5426d6463de17334d8a391054143d Reviewed-by: Tomi Korpipää --- .../qmlaxisdrag/doc/src/qmlaxisdrag.qdoc | 2 +- .../qmlaxisdrag/qml/qmlaxisdrag/main.qml | 80 +++++++++++++++------- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/examples/datavisualization/qmlaxisdrag/doc/src/qmlaxisdrag.qdoc b/examples/datavisualization/qmlaxisdrag/doc/src/qmlaxisdrag.qdoc index 0371dcaf..8f8fff7f 100644 --- a/examples/datavisualization/qmlaxisdrag/doc/src/qmlaxisdrag.qdoc +++ b/examples/datavisualization/qmlaxisdrag/doc/src/qmlaxisdrag.qdoc @@ -48,7 +48,7 @@ \snippet qmlaxisdrag/qml/qmlaxisdrag/main.qml 2 - Mouse position (\c mouseX and \c mouseY used in \c{onPressed}) is caught in + Current mouse position, that will be needed for move distance calculation, is caught in \c{onPositionChanged}: \snippet qmlaxisdrag/qml/qmlaxisdrag/main.qml 3 diff --git a/examples/datavisualization/qmlaxisdrag/qml/qmlaxisdrag/main.qml b/examples/datavisualization/qmlaxisdrag/qml/qmlaxisdrag/main.qml index 08d6e088..91685297 100644 --- a/examples/datavisualization/qmlaxisdrag/qml/qmlaxisdrag/main.qml +++ b/examples/datavisualization/qmlaxisdrag/qml/qmlaxisdrag/main.qml @@ -28,6 +28,10 @@ Item { property int selectedAxisLabel: -1 property real dragSpeedModifier: 100.0 + property int currentMouseX: -1 + property int currentMouseY: -1 + property int previousMouseX: -1 + property int previousMouseY: -1 ListModel { id: graphModel @@ -164,67 +168,92 @@ Item { //! [1] MouseArea { - id: inputArea anchors.fill: parent hoverEnabled: true acceptedButtons: Qt.LeftButton //! [1] - property int mouseX: -1 - property int mouseY: -1 - property int previousMouseX: -1 - property int previousMouseY: -1 //! [3] onPositionChanged: { - mouseX = mouse.x; - mouseY = mouse.y; + currentMouseX = mouse.x; + currentMouseY = mouse.y; //! [3] //! [6] if (pressed && selectedAxisLabel != -1) - dragAxis(mouseX, mouseY, previousMouseX, previousMouseY); + dragAxis(); //! [6] //! [4] - previousMouseX = mouseX; - previousMouseY = mouseY; + previousMouseX = currentMouseX; + previousMouseY = currentMouseY; } //! [4] //! [2] onPressed: { - scatterGraph.scene.selectionQueryPosition = Qt.point(inputArea.mouseX, - inputArea.mouseY); + scatterGraph.scene.selectionQueryPosition = Qt.point(mouse.x, mouse.y); } //! [2] + + onReleased: { + // We need to clear mouse positions and selected axis, because touch devices cannot + // track position all the time + selectedAxisLabel = -1 + currentMouseX = -1 + currentMouseY = -1 + previousMouseX = -1 + previousMouseY = -1 + } } } //! [7] - function dragAxis(mouseX, mouseY, previousMouseX, previousMouseY) { - // Directional drag multipliers based on rotation - // Camera is locked to 45 degrees, so we can use one precalculated value instead of - // calculating xx, xy, zx and zy individually + function dragAxis() { + // Do nothing if previous mouse position is uninitialized + if (previousMouseX === -1) + return + + // Directional drag multipliers based on rotation. Camera is locked to 45 degrees, so we + // can use one precalculated value instead of calculating xx, xy, zx and zy individually var cameraMultiplier = 0.70710678 - // Get the drag amount - var moveX = mouseX - previousMouseX - var moveY = mouseY - previousMouseY + // Calculate the mouse move amount + var moveX = currentMouseX - previousMouseX + var moveY = currentMouseY - previousMouseY // Adjust axes switch (selectedAxisLabel) { case AbstractGraph3D.ElementAxisXLabel: var distance = ((moveX - moveY) * cameraMultiplier) / dragSpeedModifier - scatterGraph.axisX.min -= distance - scatterGraph.axisX.max -= distance + // Check if we need to change min or max first to avoid invalid ranges + if (distance > 0) { + scatterGraph.axisX.min -= distance + scatterGraph.axisX.max -= distance + } else { + scatterGraph.axisX.max -= distance + scatterGraph.axisX.min -= distance + } break case AbstractGraph3D.ElementAxisYLabel: distance = moveY / dragSpeedModifier - scatterGraph.axisY.min += distance - scatterGraph.axisY.max += distance + // Check if we need to change min or max first to avoid invalid ranges + if (distance > 0) { + scatterGraph.axisY.max += distance + scatterGraph.axisY.min += distance + } else { + scatterGraph.axisY.min += distance + scatterGraph.axisY.max += distance + } break case AbstractGraph3D.ElementAxisZLabel: distance = ((moveX + moveY) * cameraMultiplier) / dragSpeedModifier - scatterGraph.axisZ.min += distance - scatterGraph.axisZ.max += distance + // Check if we need to change min or max first to avoid invalid ranges + if (distance > 0) { + scatterGraph.axisZ.max += distance + scatterGraph.axisZ.min += distance + } else { + scatterGraph.axisZ.min += distance + scatterGraph.axisZ.max += distance + } break } } @@ -268,6 +297,7 @@ Item { if (scatterGraph.orthoProjection) { text = "Display Orthographic"; scatterGraph.orthoProjection = false + // Orthographic projection disables shadows, so we need to switch them back on scatterGraph.shadowQuality = AbstractGraph3D.ShadowQualityLow } else { text = "Display Perspective"; -- cgit v1.2.3