QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GeoMapMissionItems.qml
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 *
5 * QGroundControl is licensed according to the terms in the file
6 * COPYING.md in the root of the source code directory.
7 *
8 ****************************************************************************/
9
10import QtQuick
11
12import QGroundControl
13import QGroundControl.GeoMap
14
15/// Mission-item overlay for the GeoMap: instantiates the GeoMap-native visual
16/// declared by each item's VisualMissionItem.geoMapVisualQML (mirrors the
17/// existing mapVisualQML dispatch consumed by src/PlanView/MissionItemMapVisual.qml
18/// — every item type states its own visual, simple items included, rather
19/// than special-casing GeoMapWaypointItem outside the dispatch). Items with
20/// no GeoMap-native visual render nothing: the home item (already shown via
21/// its own GeoMapPin) and the Survey/Corridor/Structure complex items, which
22/// never appear in the fly-view controller (vehicle downloads reconstruct
23/// only simple items and landing patterns).
24/// Overlapping waypoint markers are grouped (MissionItemIndicatorGroup parity): a
25/// click reports the whole group in either mode, and settled top-down 2D also
26/// collapses each group into one badged marker.
27Item {
28 id: root
29
30 property var missionController
31 property var scene
32 property var surfaceModel
33 property real homeTerrainBias: 0 ///< See GeoMapWaypointItem.homeTerrainBias
34 property bool interactive: false ///< The current item's marker can be dragged
35 property bool pickable: true ///< The markers take clicks; false lets them through to the map
36
37 /// A marker was clicked; item is its VisualMissionItem (mirrors
38 /// MissionItemMapVisual.clicked as consumed by PlanMapItems)
39 signal itemClicked(var item)
40
41 /// The current item's marker was dragged; coordinate is the horizontal position it was dragged to
42 signal itemDragged(var item, var coordinate)
43
44 /// A marker overlapping others was clicked; items are the group's
45 /// VisualMissionItems by sequence number, marker the clicked GeoMapWaypointItem
46 signal groupClicked(var items, var marker)
47
48 /// Markers moved on screen (zoom, resize, 2D/3D), so an open group picker is stale.
49 /// Center moves don't count: vehicle follow recenters on every position update.
50 signal groupsInvalidated()
51
52 readonly property var _camera: scene ? scene.camera : null
53 // Top-down 2D only pans, rotates and zooms, so groups change only with the zoom
54 readonly property bool _collapse: _camera !== null && _camera.mode === GeoMapCamera.Mode2D && _camera.isTopDown
55 && scene.terrainScale === 0
56
57 // Re-evaluates when a marker moves or the current item changes
58 readonly property var _groupingState: {
59 const state = []
60 const items = root.missionController ? root.missionController.visualItems : null
61 for (let i = 0; items && i < items.count; ++i) {
62 const item = items.get(i)
63 if (item.isSimpleItem && item.specifiesCoordinate) {
64 state.push([item.coordinate, item.isCurrentItem])
65 }
66 }
67 return state
68 }
69
70 function _waypointMarkers() {
71 const markers = []
72 for (let i = 0; i < repeater.count; ++i) {
73 const loader = repeater.itemAt(i)
74 if (loader && loader.item && loader.item.hasOwnProperty("collapsed")) {
75 markers.push(loader.item)
76 }
77 }
78 return markers
79 }
80
81 // Groups of on-screen waypoint markers, each led by its representative: the
82 // current item, else the lowest sequence number
83 function _markerGroups() {
84 const markers = root._waypointMarkers().filter(marker => marker.hasMarker && marker.projected)
85 markers.sort((first, second) => {
86 const firstCurrent = first.item.isCurrentItem || first.item.hasCurrentChildItem
87 const secondCurrent = second.item.isCurrentItem || second.item.hasCurrentChildItem
88 if (firstCurrent !== secondCurrent) {
89 return firstCurrent ? -1 : 1
90 }
91 return first.item.sequenceNumber - second.item.sequenceNumber
92 })
93 if (markers.length === 0) {
94 return []
95 }
96 const points = markers.map(marker => marker.mapToItem(root, marker.anchorPoint.x, marker.anchorPoint.y))
97 // A small marker's radius, as MissionItemIndicatorGroup
98 const representatives = MapMarkerGrouping.representatives(points, markers[0].smallIndicatorSize / 2)
99 const groups = []
100 const groupsByRepresentative = {}
101 for (let i = 0; i < markers.length; ++i) {
102 const representative = representatives[i]
103 if (!groupsByRepresentative[representative]) {
104 groupsByRepresentative[representative] = []
105 groups.push(groupsByRepresentative[representative])
106 }
107 groupsByRepresentative[representative].push(markers[i])
108 }
109 return groups
110 }
111
112 function _regroup() {
113 const markers = root._waypointMarkers()
114 for (const marker of markers) {
115 marker.collapsed = false
116 marker.grouped = false
117 }
118 if (!root._collapse) {
119 return
120 }
121 for (const group of root._markerGroups()) {
122 if (group.length > 1) {
123 group[0].grouped = true
124 for (let i = 1; i < group.length; ++i) {
125 group[i].collapsed = true
126 }
127 }
128 }
129 }
130
131 function _scheduleRegroup() {
132 Qt.callLater(root._regroup)
133 }
134
135 function _markerClicked(marker) {
136 const group = root._markerGroups().find(members => members.includes(marker))
137 if (group && group.length > 1) {
138 const items = group.map(member => member.item)
139 items.sort((first, second) => first.sequenceNumber - second.sequenceNumber)
140 root.groupClicked(items, marker)
141 } else {
142 root.itemClicked(marker.item)
143 }
144 }
145
146 on_CollapseChanged: {
147 _scheduleRegroup()
148 groupsInvalidated()
149 }
150 on_GroupingStateChanged: _scheduleRegroup()
151
152 Connections {
153 target: root._camera
154
155 function onDistanceChanged() { root.groupsInvalidated() }
156 function onViewportSizeChanged() { root.groupsInvalidated() }
157 }
158
159 Connections {
160 target: root._collapse ? root._camera : null
161
162 function onDistanceChanged() { root._scheduleRegroup() }
163 function onCenterElevationChanged() { root._scheduleRegroup() }
164 function onViewportSizeChanged() { root._scheduleRegroup() }
165 }
166
167 Repeater {
168 id: repeater
169
170 model: root.missionController ? root.missionController.visualItems : 0
171
172 Loader {
173 asynchronous: true
174 active: object.geoMapVisualQML !== ""
175
176 Component.onCompleted: {
177 if (active) {
178 setSource(object.geoMapVisualQML, {
179 item: object,
180 scene: root.scene,
181 surfaceModel: root.surfaceModel
182 })
183 }
184 }
185
186 onLoaded: {
187 // Declared by GeoMapWaypointItem and the landing pattern
188 // visuals; wire it up without hardcoding filenames here
189 if (item.hasOwnProperty("homeTerrainBias")) {
190 item.homeTerrainBias = Qt.binding(() => root.homeTerrainBias)
191 }
192 const visual = item
193 if (visual.pickable !== undefined) {
194 visual.pickable = Qt.binding(() => root.pickable)
195 }
196 if (visual.node3DOpacity !== undefined) {
197 // The 3D node does not follow this container's opacity
198 visual.node3DOpacity = Qt.binding(() => root.opacity)
199 }
200 if (visual.hasOwnProperty("collapsed")) {
201 visual.clicked.connect(() => root._markerClicked(visual))
202 visual.draggable = Qt.binding(() => root.interactive && object.isCurrentItem)
203 visual.dragged.connect((coordinate) => root.itemDragged(object, coordinate))
204 root._scheduleRegroup()
205 } else if (visual.clicked !== undefined) {
206 visual.clicked.connect(() => root.itemClicked(object))
207 }
208 }
209 }
210 }
211}