FWLandingPatternMapVisual.qml 18.8 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11 12 13
import QtQuick          2.3
import QtQuick.Controls 1.2
import QtLocation       5.3
import QtPositioning    5.3
14
import QtQuick.Layouts  1.11
15

16
import QGroundControl               1.0
17 18 19
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
import QGroundControl.Controls      1.0
20
import QGroundControl.FlightMap     1.0
21 22 23

/// Fixed Wing Landing Pattern map visuals
Item {
24 25
    id: _root

DonLakeFlyer's avatar
DonLakeFlyer committed
26
    property var map        ///< Map control to place item in
27
    property bool interactive: true
28

29 30
    signal clicked(int sequenceNumber)

Don Gagne's avatar
Don Gagne committed
31 32 33
    readonly property real _landingWidthMeters:     15
    readonly property real _landingLengthMeters:    100

34
    property var    _missionItem:               object
Don Gagne's avatar
Don Gagne committed
35
    property var    _mouseArea
36
    property var    _dragAreas:                 [ ]
Don Gagne's avatar
Don Gagne committed
37
    property var    _flightPath
38
    property real   _landingAreaBearing:        _missionItem.landingCoordinate.azimuthTo(_missionItem.loiterTangentCoordinate)
Don Gagne's avatar
Don Gagne committed
39 40
    property var    _loiterPointObject
    property var    _landingPointObject
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    property real   _transitionAltitudeMeters
    property real   _midSlopeAltitudeMeters
    property real   _landingAltitudeMeters:     _missionItem.landingAltitude.rawValue
    property real   _loiterAltitudeMeters:      _missionItem.loiterAltitude.rawValue

    function _calcGlideSlopeHeights() {
        var adjacent = _missionItem.landingCoordinate.distanceTo(_missionItem.loiterTangentCoordinate)
        var opposite = _loiterAltitudeMeters - _landingAltitudeMeters
        var angleRadians = Math.atan(opposite / adjacent)
        var transitionDistance = _landingLengthMeters / 2
        var glideSlopeDistance = adjacent - transitionDistance

        _transitionAltitudeMeters = Math.tan(angleRadians) * (transitionDistance)
        _midSlopeAltitudeMeters = Math.tan(angleRadians) * (transitionDistance + (glideSlopeDistance / 2))
    }
56

57
    function hideItemVisuals() {
58
        objMgr.destroyObjects()
59 60 61
    }

    function showItemVisuals() {
62 63 64
        if (objMgr.rgDynamicObjects.length === 0) {
            _loiterPointObject = objMgr.createObject(loiterPointComponent, map, true /* parentObjectIsMap */)
            _landingPointObject = objMgr.createObject(landingPointComponent, map, true /* parentObjectIsMap */)
Don Gagne's avatar
Don Gagne committed
65

66 67 68
            var rgComponents = [ flightPathComponent, loiterRadiusComponent, landingAreaComponent, landingAreaLabelComponent,
                                glideSlopeComponent, glideSlopeLabelComponent, transitionHeightComponent, midGlideSlopeHeightComponent,
                                approachHeightComponent ]
69
            objMgr.createObjects(rgComponents, map, true /* parentObjectIsMap */)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        }
    }

    function hideMouseArea() {
        if (_mouseArea) {
            _mouseArea.destroy()
            _mouseArea = undefined
        }
    }

    function showMouseArea() {
        if (!_mouseArea) {
            _mouseArea = mouseAreaComponent.createObject(map)
            map.addMapItem(_mouseArea)
        }
    }

    function hideDragAreas() {
Don Gagne's avatar
Don Gagne committed
88 89
        for (var i=0; i<_dragAreas.length; i++) {
            _dragAreas[i].destroy()
90
        }
Don Gagne's avatar
Don Gagne committed
91
        _dragAreas = [ ]
92 93 94
    }

    function showDragAreas() {
Don Gagne's avatar
Don Gagne committed
95
        if (_dragAreas.length === 0) {
96 97
            _dragAreas.push(loiterDragAreaComponent.createObject(map))
            _dragAreas.push(landDragAreaComponent.createObject(map))
98 99 100
        }
    }

101 102
    function _setFlightPath() {
        _flightPath = [ _missionItem.loiterTangentCoordinate, _missionItem.landingCoordinate ]
103 104
    }

105 106 107 108
    QGCDynamicObjectManager {
        id: objMgr
    }

109
    Component.onCompleted: {
110 111
        if (_missionItem.landingCoordSet) {
            showItemVisuals()
112
            if (!_missionItem.flyView && _missionItem.isCurrentItem) {
113 114
                showDragAreas()
            }
115
            _setFlightPath()
116
        } else if (!_missionItem.flyView && _missionItem.isCurrentItem) {
117 118
            showMouseArea()
        }
119 120 121
    }

    Component.onDestruction: {
122 123 124 125 126
        hideDragAreas()
        hideMouseArea()
        hideItemVisuals()
    }

127 128 129
    on_LandingAltitudeMetersChanged:    _calcGlideSlopeHeights()
    on_LoiterAltitudeMetersChanged:     _calcGlideSlopeHeights()

130 131 132 133
    Connections {
        target: _missionItem

        onIsCurrentItemChanged: {
134 135 136
            if (_missionItem.flyView) {
                return
            }
137 138 139 140 141 142 143 144 145 146 147 148 149
            if (_missionItem.isCurrentItem) {
                if (_missionItem.landingCoordSet) {
                    showDragAreas()
                } else {
                    showMouseArea()
                }
            } else {
                hideMouseArea()
                hideDragAreas()
            }
        }

        onLandingCoordSetChanged: {
150 151 152
            if (_missionItem.flyView) {
                return
            }
153 154 155 156
            if (_missionItem.landingCoordSet) {
                hideMouseArea()
                showItemVisuals()
                showDragAreas()
157
                _setFlightPath()
158 159 160 161 162
            } else if (_missionItem.isCurrentItem) {
                hideDragAreas()
                showMouseArea()
            }
        }
163

164 165 166 167 168 169 170 171 172
        onLandingCoordinateChanged: {
            _calcGlideSlopeHeights()
            _setFlightPath()
        }

        onLoiterTangentCoordinateChanged: {
            _calcGlideSlopeHeights()
            _setFlightPath()
        }
173 174 175 176 177 178 179
    }

    // Mouse area to capture landing point coordindate
    Component {
        id:  mouseAreaComponent

        MouseArea {
180 181
            anchors.fill:   map
            z:              QGroundControl.zOrderMapItems + 1   // Over item indicators
182
            visible:        _root.interactive
183

184 185
            readonly property int   _decimalPlaces:             8

186
            onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
187
                var coordinate = map.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
188 189 190 191
                coordinate.latitude = coordinate.latitude.toFixed(_decimalPlaces)
                coordinate.longitude = coordinate.longitude.toFixed(_decimalPlaces)
                coordinate.altitude = coordinate.altitude.toFixed(_decimalPlaces)
                _missionItem.landingCoordinate = coordinate
192
                _missionItem.setLandingHeadingToTakeoffHeading()
193 194 195 196
            }
        }
    }

197
    // Control which is used to drag the loiter point
198
    Component {
199
        id: loiterDragAreaComponent
200

201
        MissionItemIndicatorDrag {
202
            mapControl:     _root.map
Don Gagne's avatar
Don Gagne committed
203
            itemIndicator:  _loiterPointObject
204
            itemCoordinate: _missionItem.loiterCoordinate
205
            visible:        _root.interactive
206

207 208 209 210
            property bool _preventReentrancy: false

            onItemCoordinateChanged: {
                if (!_preventReentrancy) {
211
                    if (Drag.active) {
212 213 214 215 216 217 218 219
                        _preventReentrancy = true
                        var angle = _missionItem.landingCoordinate.azimuthTo(itemCoordinate)
                        var distance = _missionItem.landingCoordinate.distanceTo(_missionItem.loiterCoordinate)
                        _missionItem.loiterCoordinate = _missionItem.landingCoordinate.atDistanceAndAzimuth(distance, angle)
                        _preventReentrancy = false
                    }
                }
            }
220 221 222
        }
    }

223
    // Control which is used to drag the landing point
224 225 226 227
    Component {
        id: landDragAreaComponent

        MissionItemIndicatorDrag {
228
            mapControl:     _root.map
Don Gagne's avatar
Don Gagne committed
229
            itemIndicator:  _landingPointObject
230
            itemCoordinate: _missionItem.landingCoordinate
231
            visible:        _root.interactive
232

233
            onItemCoordinateChanged: _missionItem.moveLandingPosition(itemCoordinate)
234
        }
235 236 237 238 239 240 241
    }

    // Flight path
    Component {
        id: flightPathComponent

        MapPolyline {
242
            z:          QGroundControl.zOrderMapItems - 1   // Under item indicators
Don Gagne's avatar
Don Gagne committed
243
            line.color: "#be781c"
244
            line.width: 2
245
            path:       _flightPath
246 247 248 249 250
        }
    }

    // Loiter point
    Component {
251
        id: loiterPointComponent
252 253

        MapQuickItem {
254 255
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
256 257
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.loiterCoordinate
258 259 260

            sourceItem:
                MissionItemIndexLabel {
261
                index:      _missionItem.sequenceNumber
DonLakeFlyer's avatar
DonLakeFlyer committed
262
                label:      qsTr("Loiter")
263 264
                checked:    _missionItem.isCurrentItem

265
                onClicked: _root.clicked(_missionItem.sequenceNumber)
266 267 268 269
            }
        }
    }

Don Gagne's avatar
Don Gagne committed
270 271 272 273 274 275 276 277 278 279 280 281
    // Landing point
    Component {
        id: landingPointComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.landingCoordinate

            sourceItem:
                MissionItemIndexLabel {
282
                index:      _missionItem.lastSequenceNumber
Don Gagne's avatar
Don Gagne committed
283 284 285 286 287 288 289
                checked:    _missionItem.isCurrentItem

                onClicked: _root.clicked(_missionItem.sequenceNumber)
            }
        }
    }

Don Gagne's avatar
Don Gagne committed
290 291 292 293 294 295
    Component {
        id: loiterRadiusComponent

        MapCircle {
            z:              QGroundControl.zOrderMapItems
            center:         _missionItem.loiterCoordinate
296
            radius:         _missionItem.loiterRadius.rawValue
Don Gagne's avatar
Don Gagne committed
297 298 299 300 301 302
            border.width:   2
            border.color:   "green"
            color:          "transparent"
        }
    }

303
    Component {
Don Gagne's avatar
Don Gagne committed
304
        id: landingAreaLabelComponent
305 306

        MapQuickItem {
Don Gagne's avatar
Don Gagne committed
307 308
            anchorPoint.x:  sourceItem.contentWidth / 2
            anchorPoint.y:  sourceItem.contentHeight / 2
309 310
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.landingCoordinate
Don Gagne's avatar
Don Gagne committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
            visible:        _missionItem.isCurrentItem

            sourceItem: QGCLabel {
                id:     landingAreaLabel
                text:   qsTr("Landing Area")
                color:  "white"

                property real _rawBearing:      _landingAreaBearing
                property real _adjustedBearing

                on_RawBearingChanged: {
                    _adjustedBearing = _rawBearing
                    if (_adjustedBearing > 180) {
                        _adjustedBearing -= 180
                    }
                    _adjustedBearing -= 90
                    if (_adjustedBearing < 0) {
                        _adjustedBearing += 360
                    }
                }
331

Don Gagne's avatar
Don Gagne committed
332 333 334 335 336 337 338 339
                transform: Rotation {
                    origin.x:   landingAreaLabel.width / 2
                    origin.y:   landingAreaLabel.height / 2
                    angle:      landingAreaLabel._adjustedBearing
                }
            }
        }
    }
340

Don Gagne's avatar
Don Gagne committed
341 342 343 344
    Component {
        id: glideSlopeLabelComponent

        MapQuickItem {
345
            anchorPoint.x:  sourceItem._rawBearing > 180 ? sourceItem.contentWidth : 0
Don Gagne's avatar
Don Gagne committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
            anchorPoint.y:  sourceItem.contentHeight / 2
            z:              QGroundControl.zOrderMapItems
            visible:        _missionItem.isCurrentItem

            sourceItem: QGCLabel {
                id:     glideSlopeLabel
                text:   qsTr("Glide Slope")
                color:  "white"

                property real _rawBearing:      _landingAreaBearing
                property real _adjustedBearing

                on_RawBearingChanged: {
                    _adjustedBearing = _rawBearing
                    if (_adjustedBearing > 180) {
                        _adjustedBearing -= 180
                    }
                    _adjustedBearing -= 90
                    if (_adjustedBearing < 0) {
                        _adjustedBearing += 360
                    }
                }

                transform: Rotation {
370
                    origin.x:   sourceItem._rawBearing > 180 ? sourceItem.contentWidth : 0
Don Gagne's avatar
Don Gagne committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
                    origin.y:   glideSlopeLabel.contentHeight / 2
                    angle:      glideSlopeLabel._adjustedBearing
                }
            }

            function recalc() {
                coordinate = _missionItem.landingCoordinate.atDistanceAndAzimuth(_landingLengthMeters / 2 + 2, _landingAreaBearing)
            }

            Component.onCompleted: recalc()

            Connections {
                target:                             _missionItem
                onLandingCoordinateChanged:         recalc()
                onLoiterTangentCoordinateChanged:   recalc()
386
            }
387 388
        }
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
389 390 391 392

    Component {
        id: landingAreaComponent

DonLakeFlyer's avatar
DonLakeFlyer committed
393
        MapPolygon {
DonLakeFlyer's avatar
DonLakeFlyer committed
394 395 396 397 398 399
            z:              QGroundControl.zOrderMapItems
            border.width:   1
            border.color:   "black"
            color:          "green"
            opacity:        0.5

Don Gagne's avatar
Don Gagne committed
400
            readonly property real angleRadians:    Math.atan((_landingWidthMeters / 2) / (_landingLengthMeters / 2))
DonLakeFlyer's avatar
DonLakeFlyer committed
401
            readonly property real angleDegrees:    (angleRadians * (180 / Math.PI))
Don Gagne's avatar
Don Gagne committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
            readonly property real hypotenuse:      (_landingWidthMeters / 2) / Math.sin(angleRadians)

            function recalc() {
                path = [ ]
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing - angleDegrees))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing + angleDegrees))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing + (180 - angleDegrees)))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing - (180 - angleDegrees)))
            }

            Component.onCompleted: recalc()

            Connections {
                target:                             _missionItem
                onLandingCoordinateChanged:         recalc()
                onLoiterTangentCoordinateChanged:   recalc()
            }
        }
    }

    Component {
        id: glideSlopeComponent

        MapPolygon {
            z:              QGroundControl.zOrderMapItems
            border.width:   1
            border.color:   "black"
429
            color:          _missionItem.terrainCollision ? "red" : "orange"
Don Gagne's avatar
Don Gagne committed
430
            opacity:        0.5
DonLakeFlyer's avatar
DonLakeFlyer committed
431

Don Gagne's avatar
Don Gagne committed
432 433 434
            readonly property real angleRadians:    Math.atan((_landingWidthMeters / 2) / (_landingLengthMeters / 2))
            readonly property real angleDegrees:    (angleRadians * (180 / Math.PI))
            readonly property real hypotenuse:      (_landingWidthMeters / 2) / Math.sin(angleRadians)
DonLakeFlyer's avatar
DonLakeFlyer committed
435

Don Gagne's avatar
Don Gagne committed
436
            function recalc() {
DonLakeFlyer's avatar
DonLakeFlyer committed
437
                path = [ ]
Don Gagne's avatar
Don Gagne committed
438 439 440
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing - angleDegrees))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing + angleDegrees))
                addCoordinate(_missionItem.loiterTangentCoordinate)
DonLakeFlyer's avatar
DonLakeFlyer committed
441 442
            }

Don Gagne's avatar
Don Gagne committed
443
            Component.onCompleted: recalc()
DonLakeFlyer's avatar
DonLakeFlyer committed
444 445

            Connections {
Don Gagne's avatar
Don Gagne committed
446 447 448
                target:                             _missionItem
                onLandingCoordinateChanged:         recalc()
                onLoiterTangentCoordinateChanged:   recalc()
DonLakeFlyer's avatar
DonLakeFlyer committed
449
            }
DonLakeFlyer's avatar
DonLakeFlyer committed
450 451
        }
    }
452 453 454 455 456 457 458 459 460 461 462

    Component {
        id: transitionHeightComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.width / 2
            anchorPoint.y:  0
            z:              QGroundControl.zOrderMapItems
            visible:        _missionItem.isCurrentItem

            sourceItem: HeightIndicator {
DonLakeFlyer's avatar
DonLakeFlyer committed
463
                map:        _root.map
Remek Zajac's avatar
Remek Zajac committed
464 465
                heightText: Math.floor(QGroundControl.unitsConversion.metersToAppSettingsHorizontalDistanceUnits(_transitionAltitudeMeters)) +
                            QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString + "<sup>*</sup>"
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
            }

            function recalc() {
                var centeredCoordinate = _missionItem.landingCoordinate.atDistanceAndAzimuth(_landingLengthMeters / 2, _landingAreaBearing)
                var angleIncrement = _landingAreaBearing > 180 ? -90 : 90
                coordinate = centeredCoordinate.atDistanceAndAzimuth(_landingWidthMeters, _landingAreaBearing + angleIncrement)
            }

            Component.onCompleted: recalc()

            Connections {
                target:                             _missionItem
                onLandingCoordinateChanged:         recalc()
                onLoiterTangentCoordinateChanged:   recalc()
            }

        }
    }

    Component {
        id: midGlideSlopeHeightComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.width / 2
            anchorPoint.y:  0
            z:              QGroundControl.zOrderMapItems
            visible:        _missionItem.isCurrentItem

            sourceItem: HeightIndicator {
DonLakeFlyer's avatar
DonLakeFlyer committed
495
                map:        _root.map
Remek Zajac's avatar
Remek Zajac committed
496 497
                heightText: Math.floor(QGroundControl.unitsConversion.metersToAppSettingsHorizontalDistanceUnits(_midSlopeAltitudeMeters)) +
                            QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString + "<sup>*</sup>"
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
            }

            function recalc() {
                var transitionCoordinate = _missionItem.landingCoordinate.atDistanceAndAzimuth(_landingLengthMeters / 2, _landingAreaBearing)
                var halfDistance = transitionCoordinate.distanceTo(_missionItem.loiterTangentCoordinate) / 2
                var centeredCoordinate = transitionCoordinate.atDistanceAndAzimuth(halfDistance, _landingAreaBearing)
                var angleIncrement = _landingAreaBearing > 180 ? -90 : 90
                coordinate = centeredCoordinate.atDistanceAndAzimuth(_landingWidthMeters / 2, _landingAreaBearing + angleIncrement)
            }

            Component.onCompleted: recalc()

            Connections {
                target:                             _missionItem
                onLandingCoordinateChanged:         recalc()
                onLoiterTangentCoordinateChanged:   recalc()
            }

        }
    }

    Component {
        id: approachHeightComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.width / 2
            anchorPoint.y:  0
            z:              QGroundControl.zOrderMapItems
            visible:        _missionItem.isCurrentItem
            coordinate:     _missionItem.loiterTangentCoordinate

            sourceItem: HeightIndicator {
DonLakeFlyer's avatar
DonLakeFlyer committed
530
                map:        _root.map
Remek Zajac's avatar
Remek Zajac committed
531
                heightText: _missionItem.loiterAltitude.value.toFixed(1) + QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString
532 533 534
            }
        }
    }
535
}