| 1 |
/* |
| 2 |
* Copyright 2013 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import "mathUtils.js" as MathLocal |
| 19 |
|
| 20 |
MouseArea { |
| 21 |
id: draggingArea |
| 22 |
|
| 23 |
property int orientation: Qt.Vertical |
| 24 |
property bool dragging |
| 25 |
property real dragVelocity: 0 |
| 26 |
property real dragValue: (orientation == Qt.Vertical ? (mouseY - __pressedPosition.y) |
| 27 |
: (mouseX - __pressedPosition.x)) |
| 28 |
property real lateralPosition: orientation == Qt.Horizontal ? MathLocal.clamp(mouseY, 0, height) : MathLocal.clamp(mouseX, 0, width) |
| 29 |
property point __pressedPosition: Qt.point(0, 0) |
| 30 |
property var __dragEvents: [] |
| 31 |
property bool clickValidated: true |
| 32 |
property bool zeroVelocityCounts: false |
| 33 |
|
| 34 |
|
| 35 |
signal dragStart |
| 36 |
signal dragEnd |
| 37 |
|
| 38 |
onDragValueChanged: { |
| 39 |
if (dragValue != 0 && pressed) { |
| 40 |
dragging = true |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
onDraggingChanged: { |
| 45 |
if (dragging) { |
| 46 |
dragStart() |
| 47 |
} |
| 48 |
else { |
| 49 |
dragEnd() |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
function updateSpeed() { |
| 54 |
var totalSpeed = 0 |
| 55 |
for (var i=0; i<__dragEvents.length; i++) { |
| 56 |
totalSpeed += __dragEvents[i][3] |
| 57 |
} |
| 58 |
|
| 59 |
if (zeroVelocityCounts || Math.abs(totalSpeed) > 0.001) { |
| 60 |
dragVelocity = totalSpeed / __dragEvents.length * 1000 |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
function cullOldDragEvents(currentTime) { |
| 65 |
// cull events older than 50 ms but always keep the latest 2 events |
| 66 |
for (var numberOfCulledEvents=0; numberOfCulledEvents<__dragEvents.length-2; numberOfCulledEvents++) { |
| 67 |
// __dragEvents[numberOfCulledEvents][0] is the dragTime |
| 68 |
if (currentTime - __dragEvents[numberOfCulledEvents][0] <= 50) break |
| 69 |
} |
| 70 |
|
| 71 |
__dragEvents.splice(0, numberOfCulledEvents) |
| 72 |
} |
| 73 |
|
| 74 |
function getEventSpeed(currentTime, event) { |
| 75 |
if (__dragEvents.length != 0) { |
| 76 |
var lastDrag = __dragEvents[__dragEvents.length-1] |
| 77 |
var duration = Math.max(1, currentTime - lastDrag[0]) |
| 78 |
if (orientation == Qt.Vertical) { |
| 79 |
return (event.y - lastDrag[2]) / duration |
| 80 |
} else { |
| 81 |
return (event.x - lastDrag[1]) / duration |
| 82 |
} |
| 83 |
} else { |
| 84 |
return 0 |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
function pushDragEvent(event) { |
| 89 |
var currentTime = new Date().getTime() |
| 90 |
__dragEvents.push([currentTime, event.x, event.y, getEventSpeed(currentTime, event)]) |
| 91 |
cullOldDragEvents(currentTime) |
| 92 |
updateSpeed() |
| 93 |
} |
| 94 |
|
| 95 |
onPositionChanged: { |
| 96 |
if (dragging) { |
| 97 |
pushDragEvent(mouse) |
| 98 |
} |
| 99 |
if (!draggingArea.containsMouse) |
| 100 |
clickValidated = false |
| 101 |
} |
| 102 |
|
| 103 |
onPressed: { |
| 104 |
__pressedPosition = Qt.point(mouse.x, mouse.y) |
| 105 |
__dragEvents = [] |
| 106 |
pushDragEvent(mouse) |
| 107 |
clickValidated = true |
| 108 |
} |
| 109 |
|
| 110 |
onReleased: { |
| 111 |
dragging = false |
| 112 |
__pressedPosition = Qt.point(mouse.x, mouse.y) |
| 113 |
} |
| 114 |
} |