QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GeoMapMarkerMouseArea.qml
Go to the documentation of this file.
1import QtQuick
2
3/// Click and drag handling shared by the GeoMap point markers: a press that stays under the drag threshold is a click,
4/// past it a draggable marker reports where the cursor drags it to.
5MouseArea {
6 id: root
7
8 required property var marker ///< GeoMapItem this area belongs to
9 property bool draggable: false ///< The marker can be dragged; the map is not panned meanwhile
10
11 // Cursor-to-anchor offset at press, so the marker doesn't jump to the cursor
12 property point _grabOffset
13 property point _pressPosition
14 property bool _moved: false
15
16 signal markerClicked()
17 signal dragged(point target) ///< Where the marker's anchor is dragged to, in the marker's parent coordinates
18 signal dragFinished()
19
20 function _parentPosition(mouse) {
21 return root.marker.mapToItem(root.marker.parent, mouse.x, mouse.y)
22 }
23
24 // The map pan DragHandler would otherwise take over past the drag threshold
25 preventStealing: root.draggable
26
27 onPressed: (mouse) => {
28 // Modified drags are the map's orbit and look gestures
29 if (mouse.modifiers !== Qt.NoModifier) {
30 mouse.accepted = false
31 return
32 }
33 const pos = _parentPosition(mouse)
34 _pressPosition = pos
35 _grabOffset = Qt.point(root.marker.x + root.marker.anchorPoint.x - pos.x,
36 root.marker.y + root.marker.anchorPoint.y - pos.y)
37 _moved = false
38 }
39
40 onPositionChanged: (mouse) => {
41 if (!root.draggable || !pressed) {
42 return
43 }
44 const pos = _parentPosition(mouse)
45 if (!_moved && Math.hypot(pos.x - _pressPosition.x, pos.y - _pressPosition.y) < Qt.styleHints.startDragDistance) {
46 return
47 }
48 _moved = true
49 root.dragged(Qt.point(pos.x + _grabOffset.x, pos.y + _grabOffset.y))
50 }
51
52 onReleased: {
53 if (_moved) {
54 root.dragFinished()
55 }
56 }
57
58 onClicked: {
59 if (!_moved) {
60 root.markerClicked()
61 }
62 }
63}