QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
FenceTabPage.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Layouts
3import QtPositioning
4
5import QGroundControl
6import QGroundControl.Controls
7
8import "PlanPanelLogic.js" as Logic
9
10/// GeoFence tab: add buttons, the list of fences with the breach return point, and the inspector of the selected entry.
11/// The selected fence is the one being edited on the map.
12ColumnLayout {
13 id: root
14
15 required property var planMasterController
16 required property var editorMap
17
18 property var selection: ({ "kind": "", "index": -1 }) ///< kind is "polygon", "circle", "breach" or empty
19
20 readonly property string selectedKind: selection.kind
21 readonly property int selectedIndex: selection.index
22 readonly property var controller: planMasterController.geoFenceController
23 readonly property bool supported: controller.supported
24 readonly property var entries: _buildEntries(controller.polygons.count, controller.circles.count)
25 readonly property string selectedKey: selectedKind === "breach" ? "breach" : (selectedKind === "" ? "" : selectedKind + "_" + selectedIndex)
26 readonly property var selectedObject: objectFor(selection.kind, selection.index)
27 readonly property bool hasSelection: supported && (selection.kind === "breach" || objectFor(selection.kind, selection.index) !== null)
28 readonly property int selectedEntryIndex: entries.findIndex(entry => entry.key === selectedKey)
29
30 function objectFor(kind, index) {
31 if (kind === "polygon" && index >= 0 && index < controller.polygons.count) {
32 return controller.polygons.get(index)
33 }
34 if (kind === "circle" && index >= 0 && index < controller.circles.count) {
35 return controller.circles.get(index)
36 }
37 return null
38 }
39
40 function select(kind, index) {
41 if (kind === selectedKind && index === selectedIndex) {
42 return
43 }
44 selection = { "kind": kind, "index": index }
45 controller.clearAllInteractive()
46 const object = objectFor(kind, index)
47 if (object) {
48 object.interactive = true
49 }
50 }
51
52 function clearSelection() {
53 selection = { "kind": "", "index": -1 }
54 }
55
56 function _clearSelectionIfGone() {
57 if ((selection.kind === "polygon" || selection.kind === "circle") && objectFor(selection.kind, selection.index) === null) {
58 clearSelection()
59 }
60 }
61
62 function selectEntry(entry) {
63 if (!mainWindow.allowViewSwitch()) {
64 return
65 }
66 select(entry.kind, entry.index)
67 }
68
69 function step(delta) {
70 const index = Logic.steppedIndex(selectedEntryIndex, delta, entries.length)
71 if (index >= 0) {
72 selectEntry(entries[index])
73 }
74 }
75
76 function deleteSelected() {
77 if (selectedKind === "polygon") {
78 controller.deletePolygon(selectedIndex)
79 } else if (selectedKind === "circle") {
80 controller.deleteCircle(selectedIndex)
81 }
82 clearSelection()
83 }
84
85 function openInspector() {
86 listInspector.openInspector()
87 }
88
89 function _buildEntries(polygonCount, circleCount) {
90 const list = []
91 for (let i = 0; i < polygonCount; i++) {
92 list.push({ key: "polygon_" + i, kind: "polygon", index: i })
93 }
94 for (let i = 0; i < circleCount; i++) {
95 list.push({ key: "circle_" + i, kind: "circle", index: i })
96 }
97 list.push({ key: "breach", kind: "breach", index: 0 })
98 return list
99 }
100
101 // A GeoMap viewport corner can point into the sky in a 3D tilt: moves it toward the center until it meets the
102 // ground, null if no point does
103 function _groundCoordinateInViewport(corner, center) {
104 for (let step = 0; step < 10; step++) {
105 const fraction = step / 10
106 const coordinate = editorMap.toCoordinate(
107 Qt.point(corner.x + (center.x - corner.x) * fraction, corner.y + (center.y - corner.y) * fraction),
108 false /* clipToViewPort */)
109 if (coordinate.isValid) {
110 return coordinate
111 }
112 }
113 return null
114 }
115
116 function _normalizedLongitude(longitude) {
117 let normalized = longitude
118 while (normalized > 180) {
119 normalized -= 360
120 }
121 while (normalized < -180) {
122 normalized += 360
123 }
124 return normalized
125 }
126
127 // Whether point is inside the convex quadrilateral quad, edges included; a point sits on an edge within a tolerance
128 // of about a centimeter (the points are {x: longitude offset, y: latitude} in degrees)
129 function _insideQuadrilateral(point, quad) {
130 const tolerance = 1e-7
131 let side = 0
132 for (let i = 0; i < quad.length; i++) {
133 const from = quad[i]
134 const to = quad[(i + 1) % quad.length]
135 const edgeX = to.x - from.x
136 const edgeY = to.y - from.y
137 const edgeLength = Math.hypot(edgeX, edgeY)
138 if (edgeLength === 0) {
139 continue
140 }
141 const distance = (edgeX * (point.y - from.y) - edgeY * (point.x - from.x)) / edgeLength
142 if (distance > tolerance) {
143 if (side < 0) {
144 return false
145 }
146 side = 1
147 } else if (distance < -tolerance) {
148 if (side > 0) {
149 return false
150 }
151 side = -1
152 }
153 }
154 return true
155 }
156
157 // Geographic bounds {north, south, west, east} of a rectangle, centered in the viewport, that lies within the
158 // ground the viewport shows. The controller adds fences inside the bounds it is given, which the bounds of the
159 // viewport corners are not once the map is rotated or tilted: they enclose the ground shown instead of lying
160 // within it. The rectangle is shrunk from them until its corners are inside the quadrilateral of the corners'
161 // ground. This only compares ground picks with each other, so it does not depend on the map projecting back to
162 // the screen. Longitudes are taken relative to the viewport center so a viewport over the dateline works. Null if
163 // the viewport shows no ground.
164 function _viewportGroundBounds() {
165 const viewport = editorMap.centerViewport
166 const centerPoint = Qt.point(viewport.x + viewport.width / 2, viewport.y + viewport.height / 2)
167 const corners = [
168 Qt.point(viewport.x, viewport.y),
169 Qt.point(viewport.x + viewport.width, viewport.y),
170 Qt.point(viewport.x + viewport.width, viewport.y + viewport.height),
171 Qt.point(viewport.x, viewport.y + viewport.height)
172 ]
173 const cornerCoordinates = []
174 for (const corner of corners) {
175 const coordinate = _groundCoordinateInViewport(corner, centerPoint)
176 if (!coordinate) {
177 return null
178 }
179 cornerCoordinates.push(coordinate)
180 }
181 // Relative to the center, a viewport of any width up to the whole world keeps its corners within a half
182 // turn of it on either side
183 const centerCoordinate = editorMap.toCoordinate(centerPoint, false /* clipToViewPort */)
184 const referenceLongitude = centerCoordinate.isValid ? centerCoordinate.longitude : cornerCoordinates[0].longitude
185 const quad = cornerCoordinates.map(
186 coordinate => Qt.point(_normalizedLongitude(coordinate.longitude - referenceLongitude), coordinate.latitude))
187 const minX = Math.min(...quad.map(point => point.x))
188 const maxX = Math.max(...quad.map(point => point.x))
189 const minY = Math.min(...quad.map(point => point.y))
190 const maxY = Math.max(...quad.map(point => point.y))
191 if (maxX - minX < 1e-9 || maxY - minY < 1e-9) {
192 return null
193 }
194
195 const center = centerCoordinate.isValid ? Qt.point(0, centerCoordinate.latitude)
196 : Qt.point((minX + maxX) / 2, (minY + maxY) / 2)
197 const halfX = Math.max(maxX - center.x, center.x - minX)
198 const halfY = Math.max(maxY - center.y, center.y - minY)
199
200 for (let scale = 1; scale > 0.01; scale *= 0.9) {
201 const west = center.x - halfX * scale
202 const east = center.x + halfX * scale
203 const south = center.y - halfY * scale
204 const north = center.y + halfY * scale
205 if ([Qt.point(west, south), Qt.point(east, south), Qt.point(east, north), Qt.point(west, north)]
206 .every(corner => _insideQuadrilateral(corner, quad))) {
207 return {
208 north: Math.min(north, 90),
209 south: Math.max(south, -90),
210 west: _normalizedLongitude(referenceLongitude + west),
211 east: _normalizedLongitude(referenceLongitude + east)
212 }
213 }
214 }
215 return null
216 }
217
218 function _addFence(addFunction) {
219 if (!mainWindow.allowViewSwitch()) {
220 return
221 }
222 const bounds = _viewportGroundBounds()
223 if (!bounds) {
224 return
225 }
226 addFunction(QtPositioning.coordinate(bounds.north, bounds.west), QtPositioning.coordinate(bounds.south, bounds.east))
227 }
228
229 objectName: "planPanel_page_fence"
230 spacing: ScreenTools.defaultFontPixelHeight * 0.3
231
232 // A cleared or loaded plan empties the lists, so an index kept across it would select an unrelated fence
233 Connections {
234 target: root.controller.polygons
235
236 function onCountChanged() {
237 root._clearSelectionIfGone()
238 }
239 }
240
241 Connections {
242 target: root.controller.circles
243
244 function onCountChanged() {
245 root._clearSelectionIfGone()
246 }
247 }
248
249 // The map can make a fence the interactive one as well
250 Instantiator {
251 model: root.controller.polygons
252
253 delegate: QtObject {
254 id: polygonTracker
255
256 required property var object
257 required property int index
258
259 property Connections connection: Connections {
260 target: polygonTracker.object
261
262 function onInteractiveChanged() {
263 if (polygonTracker.object.interactive) {
264 root.select("polygon", polygonTracker.index)
265 }
266 }
267 }
268 }
269
270 onObjectAdded: (index, item) => {
271 if (item.object.interactive) {
272 root.select("polygon", index)
273 }
274 }
275 }
276
277 Instantiator {
278 model: root.controller.circles
279
280 delegate: QtObject {
281 id: circleTracker
282
283 required property var object
284 required property int index
285
286 property Connections connection: Connections {
287 target: circleTracker.object
288
289 function onInteractiveChanged() {
290 if (circleTracker.object.interactive) {
291 root.select("circle", circleTracker.index)
292 }
293 }
294 }
295 }
296
297 onObjectAdded: (index, item) => {
298 if (item.object.interactive) {
299 root.select("circle", index)
300 }
301 }
302 }
303
304 QGCLabel {
305 objectName: "planPanel_fenceUnsupported"
306 Layout.fillWidth: true
307 Layout.margins: ScreenTools.defaultFontPixelWidth
308 visible: !root.supported
309 wrapMode: Text.WordWrap
310 text: qsTr("This vehicle does not support GeoFence.")
311 }
312
313 RowLayout {
314 Layout.fillWidth: true
315 Layout.leftMargin: ScreenTools.defaultFontPixelWidth
316 Layout.rightMargin: ScreenTools.defaultFontPixelWidth
317 Layout.topMargin: ScreenTools.defaultFontPixelHeight * 0.3
318 spacing: ScreenTools.defaultFontPixelWidth
319 visible: root.supported
320
321 QGCButton {
322 objectName: "planPanel_fenceAddPolygon"
323 Layout.fillWidth: true
324 small: true
325 text: qsTr("+ Polygon")
326 onClicked: root._addFence((topLeft, bottomRight) => root.controller.addInclusionPolygon(topLeft, bottomRight))
327 }
328
329 QGCButton {
330 objectName: "planPanel_fenceAddCircle"
331 Layout.fillWidth: true
332 small: true
333 text: qsTr("+ Circle")
334 onClicked: root._addFence((topLeft, bottomRight) => root.controller.addInclusionCircle(topLeft, bottomRight))
335 }
336 }
337
338 PlanListInspector {
339 id: listInspector
340
341 objectName: "planPanel_fenceListInspector"
342 Layout.fillWidth: true
343 Layout.fillHeight: true
344 visible: root.supported
345 hasSelection: root.hasSelection
346 listComponent: fenceListComponent
347 inspectorComponent: fenceInspectorComponent
348 }
349
350 Component {
351 id: fenceListComponent
352
353 FenceItemList {
354 page: root
355 }
356 }
357
358 Component {
359 id: fenceInspectorComponent
360
361 FenceInspector {
362 page: root
363 drillIn: listInspector.layoutMode === "drillIn"
364 }
365 }
366}