6import QGroundControl.Controls
8import "PlanPanelLogic.js" as Logic
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.
15 required property var planMasterController
16 required property var editorMap
18 property var selection: ({ "kind": "", "index": -1 }) ///< kind is "polygon", "circle", "breach" or empty
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)
30 function objectFor(kind, index) {
31 if (kind === "polygon" && index >= 0 && index < controller.polygons.count) {
32 return controller.polygons.get(index)
34 if (kind === "circle" && index >= 0 && index < controller.circles.count) {
35 return controller.circles.get(index)
40 function select(kind, index) {
41 if (kind === selectedKind && index === selectedIndex) {
44 selection = { "kind": kind, "index": index }
45 controller.clearAllInteractive()
46 const object = objectFor(kind, index)
48 object.interactive = true
52 function clearSelection() {
53 selection = { "kind": "", "index": -1 }
56 function _clearSelectionIfGone() {
57 if ((selection.kind === "polygon" || selection.kind === "circle") && objectFor(selection.kind, selection.index) === null) {
62 function selectEntry(entry) {
63 if (!mainWindow.allowViewSwitch()) {
66 select(entry.kind, entry.index)
69 function step(delta) {
70 const index = Logic.steppedIndex(selectedEntryIndex, delta, entries.length)
72 selectEntry(entries[index])
76 function deleteSelected() {
77 if (selectedKind === "polygon") {
78 controller.deletePolygon(selectedIndex)
79 } else if (selectedKind === "circle") {
80 controller.deleteCircle(selectedIndex)
85 function openInspector() {
86 listInspector.openInspector()
89 function _buildEntries(polygonCount, circleCount) {
91 for (let i = 0; i < polygonCount; i++) {
92 list.push({ key: "polygon_" + i, kind: "polygon", index: i })
94 for (let i = 0; i < circleCount; i++) {
95 list.push({ key: "circle_" + i, kind: "circle", index: i })
97 list.push({ key: "breach", kind: "breach", index: 0 })
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) {
116 function _normalizedLongitude(longitude) {
117 let normalized = longitude
118 while (normalized > 180) {
121 while (normalized < -180) {
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
132 for (let i = 0; i < quad.length; 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) {
141 const distance = (edgeX * (point.y - from.y) - edgeY * (point.x - from.x)) / edgeLength
142 if (distance > tolerance) {
147 } else if (distance < -tolerance) {
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)
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)
173 const cornerCoordinates = []
174 for (const corner of corners) {
175 const coordinate = _groundCoordinateInViewport(corner, centerPoint)
179 cornerCoordinates.push(coordinate)
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) {
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)
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))) {
208 north: Math.min(north, 90),
209 south: Math.max(south, -90),
210 west: _normalizedLongitude(referenceLongitude + west),
211 east: _normalizedLongitude(referenceLongitude + east)
218 function _addFence(addFunction) {
219 if (!mainWindow.allowViewSwitch()) {
222 const bounds = _viewportGroundBounds()
226 addFunction(QtPositioning.coordinate(bounds.north, bounds.west), QtPositioning.coordinate(bounds.south, bounds.east))
229 objectName: "planPanel_page_fence"
230 spacing: ScreenTools.defaultFontPixelHeight * 0.3
232 // A cleared or loaded plan empties the lists, so an index kept across it would select an unrelated fence
234 target: root.controller.polygons
236 function onCountChanged() {
237 root._clearSelectionIfGone()
242 target: root.controller.circles
244 function onCountChanged() {
245 root._clearSelectionIfGone()
249 // The map can make a fence the interactive one as well
251 model: root.controller.polygons
256 required property var object
257 required property int index
259 property Connections connection: Connections {
260 target: polygonTracker.object
262 function onInteractiveChanged() {
263 if (polygonTracker.object.interactive) {
264 root.select("polygon", polygonTracker.index)
270 onObjectAdded: (index, item) => {
271 if (item.object.interactive) {
272 root.select("polygon", index)
278 model: root.controller.circles
283 required property var object
284 required property int index
286 property Connections connection: Connections {
287 target: circleTracker.object
289 function onInteractiveChanged() {
290 if (circleTracker.object.interactive) {
291 root.select("circle", circleTracker.index)
297 onObjectAdded: (index, item) => {
298 if (item.object.interactive) {
299 root.select("circle", index)
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.")
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
322 objectName: "planPanel_fenceAddPolygon"
323 Layout.fillWidth: true
325 text: qsTr("+ Polygon")
326 onClicked: root._addFence((topLeft, bottomRight) => root.controller.addInclusionPolygon(topLeft, bottomRight))
330 objectName: "planPanel_fenceAddCircle"
331 Layout.fillWidth: true
333 text: qsTr("+ Circle")
334 onClicked: root._addFence((topLeft, bottomRight) => root.controller.addInclusionCircle(topLeft, bottomRight))
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
351 id: fenceListComponent
359 id: fenceInspectorComponent
363 drillIn: listInspector.layoutMode === "drillIn"