QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GeoMapView.qml
Go to the documentation of this file.
1import QtQuick
2import QtPositioning
3
4import QGroundControl
5import QGroundControl.Controls
6import QGroundControl.FlightMap
7import QGroundControl.GeoMap
8
9/// GeoMap that speaks the interface of the QtLocation FlightMap, so the views and controls written against
10/// FlightMap (center, zoomLevel, toCoordinate/fromCoordinate, setVisibleRegion, isSatelliteMap, ...) work unchanged.
11/// It also keeps the map view shared with the other views (QGroundControl.flightMapPosition/flightMapZoom) and can
12/// show the camera controls and map scale itself. Views add their own content on top.
13GeoMap {
14 id: root
15
16 // Writable like FlightMap's: assignments move the camera, camera moves are echoed back
17 property var center
18 property real zoomLevel: 0
19
20 /// false: the zoom is neither restored from nor saved to the shared view, and survives resizes untouched.
21 /// For a map that has its own zoom, such as a small picture-in-picture one.
22 property bool sharedZoom: true
23
24 /// Camera controls (2D/3D toggle and compass), optionally followed by the map scale, at the top left
25 property bool showControls: false
26 property bool showScale: false
27 property real controlsLeftMargin: ScreenTools.defaultFontPixelWidth * 0.75
28 property real controlsTopMargin: ScreenTools.defaultFontPixelWidth * 0.75
29
30 readonly property bool isSatelliteMap: _mapTypeSetting.indexOf("Satellite") > -1 || _mapTypeSetting.indexOf("Hybrid") > -1
31
32 readonly property string _mapTypeSetting: QGroundControl.settingsManager.flightMapSettings.mapType.rawValue
33
34 // Breaks the center/zoomLevel <-> camera cycle: camera moves echo into them and must not be written back
35 property bool _echoingCamera: false
36
37 // Screen point -> coordinate of the rendered terrain surface under it, invalid on a sky pick.
38 // clipToViewPort is accepted for FlightMap parity and ignored: every caller passes false.
39 function toCoordinate(point, clipToViewPort) {
40 return root.surfaceCoordinateAt(point)
41 }
42
43 // Coordinate -> screen point; an invalid or behind-the-camera coordinate projects off-screen.
44 // clipToViewPort is ignored as in toCoordinate.
45 function fromCoordinate(coordinate, clipToViewPort) {
46 const screenPos = root.scene.screenPositionFor(coordinate)
47 return (screenPos === undefined) ? Qt.point(-1, -1) : screenPos
48 }
49
50 // MapFitFunctions contract
51 function setVisibleRegion(region) {
52 root.fitToRegion(region)
53 }
54
55 // Zoom level to distance depends on the viewport size, so this also runs on every resize
56 function _applyZoomLevel() {
57 if (!_viewportValid()) {
58 return
59 }
60 root.camera.distance = root.camera.distanceForZoomLevel(root.zoomLevel)
61 }
62
63 function _viewportValid() {
64 const size = root.camera.viewportSize
65 return size.width > 0 && size.height > 0
66 }
67
68 function _echoCameraCenter() {
69 _echoingCamera = true
70 center = root.camera.center
71 _echoingCamera = false
72 }
73
74 function _echoCameraZoom() {
75 if (!_viewportValid()) {
76 return
77 }
78 _echoingCamera = true
79 zoomLevel = root.camera.zoomLevelForDistance(root.camera.distance)
80 _echoingCamera = false
81 }
82
83 function _restoreSharedView() {
84 root.camera.center = QGroundControl.flightMapPosition
85 if (sharedZoom) {
86 zoomLevel = QGroundControl.flightMapZoom
87 }
88 }
89
90 onCenterChanged: {
91 if (_echoingCamera || !center || !center.isValid) {
92 return
93 }
94 root.camera.center = center
95 }
96
97 onZoomLevelChanged: {
98 if (!_echoingCamera) {
99 _applyZoomLevel()
100 }
101 }
102
103 onVisibleChanged: {
104 if (visible) {
105 _restoreSharedView()
106 }
107 }
108
109 // Back from a map with its own zoom: the shared zoom applies again
110 onSharedZoomChanged: {
111 if (sharedZoom) {
112 zoomLevel = QGroundControl.flightMapZoom
113 }
114 }
115
116 Connections {
117 target: root.camera
118
119 function onCenterChanged() {
120 root._echoCameraCenter()
121 // Hidden: don't overwrite the position another view is sharing
122 if (root.visible) {
123 QGroundControl.flightMapPosition = root.camera.center
124 }
125 }
126
127 function onDistanceChanged() {
128 root._echoCameraZoom()
129 if (root.visible && root.sharedZoom && root._viewportValid()) {
130 QGroundControl.flightMapZoom = root.zoomLevel
131 }
132 }
133
134 function onVerticalFieldOfViewChanged() {
135 root._echoCameraZoom()
136 }
137
138 function onViewportSizeChanged() {
139 if (root.sharedZoom) {
140 root._applyZoomLevel()
141 } else {
142 root._echoCameraZoom()
143 }
144 }
145 }
146
147 Loader {
148 x: root.controlsLeftMargin
149 y: root.controlsTopMargin
150 z: 1 // Above the content that views add, so a marker under a control does not take its clicks
151 active: root.showControls || root.showScale
152 visible: !ScreenTools.isTinyScreen
153
154 sourceComponent: Row {
155 spacing: ScreenTools.defaultFontPixelWidth * 0.75
156
157 GeoMapControls {
158 geoMap: root
159 visible: root.showControls
160 }
161
162 MapScale {
163 anchors.verticalCenter: parent.verticalCenter
164 mapControl: root
165 autoHide: true
166 // The scale is ill-defined under the 3D tilt; isTopDown keeps it hidden until the 3D->2D tilt animation lands
167 visible: root.showScale && root.camera.mode === GeoMapCamera.Mode2D && root.camera.isTopDown
168 }
169 }
170 }
171
172 Component.onCompleted: {
173 _restoreSharedView()
174 _echoCameraCenter()
175 _echoCameraZoom()
176 }
177}