1/****************************************************************************
3 * (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
5 * QGroundControl is licensed according to the terms in the file
6 * COPYING.md in the root of the source code directory.
8 ****************************************************************************/
13import QGroundControl.GeoMap
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.
30 property var missionController
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
37 /// A marker was clicked; item is its VisualMissionItem (mirrors
38 /// MissionItemMapVisual.clicked as consumed by PlanMapItems)
39 signal itemClicked(var item)
41 /// The current item's marker was dragged; coordinate is the horizontal position it was dragged to
42 signal itemDragged(var item, var coordinate)
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)
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()
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
57 // Re-evaluates when a marker moves or the current item changes
58 readonly property var _groupingState: {
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])
70 function _waypointMarkers() {
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)
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
91 return first.item.sequenceNumber - second.item.sequenceNumber
93 if (markers.length === 0) {
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)
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])
107 groupsByRepresentative[representative].push(markers[i])
112 function _regroup() {
113 const markers = root._waypointMarkers()
114 for (const marker of markers) {
115 marker.collapsed = false
116 marker.grouped = false
118 if (!root._collapse) {
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
131 function _scheduleRegroup() {
132 Qt.callLater(root._regroup)
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)
142 root.itemClicked(marker.item)
146 on_CollapseChanged: {
150 on_GroupingStateChanged: _scheduleRegroup()
155 function onDistanceChanged() { root.groupsInvalidated() }
156 function onViewportSizeChanged() { root.groupsInvalidated() }
160 target: root._collapse ? root._camera : null
162 function onDistanceChanged() { root._scheduleRegroup() }
163 function onCenterElevationChanged() { root._scheduleRegroup() }
164 function onViewportSizeChanged() { root._scheduleRegroup() }
170 model: root.missionController ? root.missionController.visualItems : 0
174 active: object.geoMapVisualQML !== ""
176 Component.onCompleted: {
178 setSource(object.geoMapVisualQML, {
181 surfaceModel: root.surfaceModel
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)
193 if (visual.pickable !== undefined) {
194 visual.pickable = Qt.binding(() => root.pickable)
196 if (visual.node3DOpacity !== undefined) {
197 // The 3D node does not follow this container's opacity
198 visual.node3DOpacity = Qt.binding(() => root.opacity)
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))