FWLandingPatternMapVisual.qml 20.4 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 38 39
    property var    _flightPath
    property var    _loiterPointObject
    property var    _landingPointObject
40 41
    property real   _transitionAltitudeMeters
    property real   _midSlopeAltitudeMeters
42 43 44 45
    property real   _landingAltitudeMeters:         _missionItem.landingAltitude.rawValue
    property real   _finalApproachAltitudeMeters:   _missionItem.finalApproachAltitude.rawValue
    property bool   _useLoiterToAlt:                _missionItem.useLoiterToAlt.rawValue
    property real   _landingAreaBearing:            _missionItem.landingCoordinate.azimuthTo(_useLoiterToAlt ? _missionItem.loiterTangentCoordinate : _missionItem.finalApproachCoordinate)
46 47

    function _calcGlideSlopeHeights() {
48 49 50 51 52 53 54
        var adjacent
        if (_useLoiterToAlt) {
            adjacent = _missionItem.landingCoordinate.distanceTo(_missionItem.loiterTangentCoordinate)
        } else {
            adjacent = _missionItem.landingCoordinate.distanceTo(_missionItem.finalApproachCoordinate)
        }
        var opposite = _finalApproachAltitudeMeters - _landingAltitudeMeters
55 56 57 58 59 60 61
        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))
    }
62

63
    function hideItemVisuals() {
64
        objMgr.destroyObjects()
65 66 67
    }

    function showItemVisuals() {
68
        if (objMgr.rgDynamicObjects.length === 0) {
69
            _loiterPointObject = objMgr.createObject(finalApproachComponent, map, true /* parentObjectIsMap */)
70
            _landingPointObject = objMgr.createObject(landingPointComponent, map, true /* parentObjectIsMap */)
Don Gagne's avatar
Don Gagne committed
71

72 73 74
            var rgComponents = [ flightPathComponent, loiterRadiusComponent, landingAreaComponent, landingAreaLabelComponent,
                                glideSlopeComponent, glideSlopeLabelComponent, transitionHeightComponent, midGlideSlopeHeightComponent,
                                approachHeightComponent ]
75
            objMgr.createObjects(rgComponents, map, true /* parentObjectIsMap */)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        }
    }

    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
94 95
        for (var i=0; i<_dragAreas.length; i++) {
            _dragAreas[i].destroy()
96
        }
Don Gagne's avatar
Don Gagne committed
97
        _dragAreas = [ ]
98 99 100
    }

    function showDragAreas() {
Don Gagne's avatar
Don Gagne committed
101
        if (_dragAreas.length === 0) {
102
            _dragAreas.push(finalApproachDragAreaComponent.createObject(map))
103
            _dragAreas.push(landDragAreaComponent.createObject(map))
104 105 106
        }
    }

107
    function _setFlightPath() {
108 109 110 111 112
        if (_useLoiterToAlt) {
            _flightPath = [ _missionItem.loiterTangentCoordinate, _missionItem.landingCoordinate ]
        } else {
            _flightPath = [ _missionItem.finalApproachCoordinate, _missionItem.landingCoordinate ]
        }
113 114
    }

115 116 117 118
    QGCDynamicObjectManager {
        id: objMgr
    }

119
    Component.onCompleted: {
120 121
        if (_missionItem.landingCoordSet) {
            showItemVisuals()
122
            if (!_missionItem.flyView && _missionItem.isCurrentItem) {
123 124
                showDragAreas()
            }
125
            _setFlightPath()
126
        } else if (!_missionItem.flyView && _missionItem.isCurrentItem) {
127 128
            showMouseArea()
        }
129 130 131
    }

    Component.onDestruction: {
132 133 134 135 136
        hideDragAreas()
        hideMouseArea()
        hideItemVisuals()
    }

137 138 139
    on_LandingAltitudeMetersChanged:        _calcGlideSlopeHeights()
    on_FinalApproachAltitudeMetersChanged:  _calcGlideSlopeHeights()
    on_UseLoiterToAltChanged:               { _calcGlideSlopeHeights(); _setFlightPath() }
140

141 142 143 144
    Connections {
        target: _missionItem

        onIsCurrentItemChanged: {
145 146 147
            if (_missionItem.flyView) {
                return
            }
148 149 150 151 152 153 154 155 156 157 158 159 160
            if (_missionItem.isCurrentItem) {
                if (_missionItem.landingCoordSet) {
                    showDragAreas()
                } else {
                    showMouseArea()
                }
            } else {
                hideMouseArea()
                hideDragAreas()
            }
        }

        onLandingCoordSetChanged: {
161 162 163
            if (_missionItem.flyView) {
                return
            }
164 165 166 167
            if (_missionItem.landingCoordSet) {
                hideMouseArea()
                showItemVisuals()
                showDragAreas()
168
                _setFlightPath()
169 170 171 172 173
            } else if (_missionItem.isCurrentItem) {
                hideDragAreas()
                showMouseArea()
            }
        }
174

175 176 177 178 179 180 181 182 183
        onLandingCoordinateChanged: {
            _calcGlideSlopeHeights()
            _setFlightPath()
        }

        onLoiterTangentCoordinateChanged: {
            _calcGlideSlopeHeights()
            _setFlightPath()
        }
184 185 186 187 188

        onFinalApproachCoordinateChanged: {
            _calcGlideSlopeHeights()
            _setFlightPath()
        }
189 190 191 192 193 194 195
    }

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

        MouseArea {
196 197
            anchors.fill:   map
            z:              QGroundControl.zOrderMapItems + 1   // Over item indicators
198
            visible:        _root.interactive
199

200 201
            readonly property int   _decimalPlaces:             8

202
            onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
203
                var coordinate = map.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
204 205 206 207
                coordinate.latitude = coordinate.latitude.toFixed(_decimalPlaces)
                coordinate.longitude = coordinate.longitude.toFixed(_decimalPlaces)
                coordinate.altitude = coordinate.altitude.toFixed(_decimalPlaces)
                _missionItem.landingCoordinate = coordinate
208
                _missionItem.setLandingHeadingToTakeoffHeading()
209 210 211 212
            }
        }
    }

213
    // Control which is used to drag the final approach point
214
    Component {
215
        id: finalApproachDragAreaComponent
216

217
        MissionItemIndicatorDrag {
218
            mapControl:     _root.map
Don Gagne's avatar
Don Gagne committed
219
            itemIndicator:  _loiterPointObject
220
            itemCoordinate: _missionItem.finalApproachCoordinate
221
            visible:        _root.interactive
222

223 224 225 226
            property bool _preventReentrancy: false

            onItemCoordinateChanged: {
                if (!_preventReentrancy) {
227
                    if (Drag.active) {
228 229
                        _preventReentrancy = true
                        var angle = _missionItem.landingCoordinate.azimuthTo(itemCoordinate)
230 231
                        var distance = _missionItem.landingCoordinate.distanceTo(_missionItem.finalApproachCoordinate)
                        _missionItem.finalApproachCoordinate = _missionItem.landingCoordinate.atDistanceAndAzimuth(distance, angle)
232 233 234 235
                        _preventReentrancy = false
                    }
                }
            }
236 237 238
        }
    }

239
    // Control which is used to drag the landing point
240 241 242 243
    Component {
        id: landDragAreaComponent

        MissionItemIndicatorDrag {
244
            mapControl:     _root.map
Don Gagne's avatar
Don Gagne committed
245
            itemIndicator:  _landingPointObject
246
            itemCoordinate: _missionItem.landingCoordinate
247
            visible:        _root.interactive
248

249
            onItemCoordinateChanged: _missionItem.moveLandingPosition(itemCoordinate)
250
        }
251 252 253 254 255 256 257
    }

    // Flight path
    Component {
        id: flightPathComponent

        MapPolyline {
258
            z:          QGroundControl.zOrderMapItems - 1   // Under item indicators
Don Gagne's avatar
Don Gagne committed
259
            line.color: "#be781c"
260
            line.width: 2
261
            path:       _flightPath
262 263 264
        }
    }

265
    // Final approach point
266
    Component {
267
        id: finalApproachComponent
268 269

        MapQuickItem {
270 271
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
272
            z:              QGroundControl.zOrderMapItems
273
            coordinate:     _missionItem.finalApproachCoordinate
274 275 276

            sourceItem:
                MissionItemIndexLabel {
277
                index:      _missionItem.sequenceNumber
278
                label:      _useLoiterToAlt ? qsTr("Loiter") : qsTr("Approach")
279 280
                checked:    _missionItem.isCurrentItem

281
                onClicked: _root.clicked(_missionItem.sequenceNumber)
282 283 284 285
            }
        }
    }

Don Gagne's avatar
Don Gagne committed
286 287 288 289 290 291 292 293 294 295 296 297
    // Landing point
    Component {
        id: landingPointComponent

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

            sourceItem:
                MissionItemIndexLabel {
298
                index:      _missionItem.lastSequenceNumber
Don Gagne's avatar
Don Gagne committed
299 300 301 302 303 304 305
                checked:    _missionItem.isCurrentItem

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

Don Gagne's avatar
Don Gagne committed
306 307 308 309 310
    Component {
        id: loiterRadiusComponent

        MapCircle {
            z:              QGroundControl.zOrderMapItems
311
            center:         _missionItem.finalApproachCoordinate
312
            radius:         _missionItem.loiterRadius.rawValue
Don Gagne's avatar
Don Gagne committed
313 314 315
            border.width:   2
            border.color:   "green"
            color:          "transparent"
316
            visible:        _useLoiterToAlt
Don Gagne's avatar
Don Gagne committed
317 318 319
        }
    }

320
    Component {
Don Gagne's avatar
Don Gagne committed
321
        id: landingAreaLabelComponent
322 323

        MapQuickItem {
Don Gagne's avatar
Don Gagne committed
324 325
            anchorPoint.x:  sourceItem.contentWidth / 2
            anchorPoint.y:  sourceItem.contentHeight / 2
326 327
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.landingCoordinate
Don Gagne's avatar
Don Gagne committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            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
                    }
                }
348

Don Gagne's avatar
Don Gagne committed
349 350 351 352 353 354 355 356
                transform: Rotation {
                    origin.x:   landingAreaLabel.width / 2
                    origin.y:   landingAreaLabel.height / 2
                    angle:      landingAreaLabel._adjustedBearing
                }
            }
        }
    }
357

Don Gagne's avatar
Don Gagne committed
358 359 360 361
    Component {
        id: glideSlopeLabelComponent

        MapQuickItem {
362
            anchorPoint.x:  sourceItem._rawBearing > 180 ? sourceItem.contentWidth : 0
Don Gagne's avatar
Don Gagne committed
363 364 365 366
            anchorPoint.y:  sourceItem.contentHeight / 2
            z:              QGroundControl.zOrderMapItems
            visible:        _missionItem.isCurrentItem

367

Don Gagne's avatar
Don Gagne committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
            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 {
388
                    origin.x:   sourceItem._rawBearing > 180 ? sourceItem.contentWidth : 0
Don Gagne's avatar
Don Gagne committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
                    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()
404
                onFinalApproachCoordinateChanged:   recalc()
405
            }
406 407
        }
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
408 409 410 411

    Component {
        id: landingAreaComponent

DonLakeFlyer's avatar
DonLakeFlyer committed
412
        MapPolygon {
DonLakeFlyer's avatar
DonLakeFlyer committed
413 414 415 416 417 418
            z:              QGroundControl.zOrderMapItems
            border.width:   1
            border.color:   "black"
            color:          "green"
            opacity:        0.5

Don Gagne's avatar
Don Gagne committed
419
            readonly property real angleRadians:    Math.atan((_landingWidthMeters / 2) / (_landingLengthMeters / 2))
DonLakeFlyer's avatar
DonLakeFlyer committed
420
            readonly property real angleDegrees:    (angleRadians * (180 / Math.PI))
Don Gagne's avatar
Don Gagne committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
            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()
437
                onFinalApproachCoordinateChanged:   recalc()
Don Gagne's avatar
Don Gagne committed
438 439 440 441 442 443 444 445 446 447 448
            }
        }
    }

    Component {
        id: glideSlopeComponent

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

Don Gagne's avatar
Don Gagne committed
452 453 454
            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
455

Don Gagne's avatar
Don Gagne committed
456
            function recalc() {
DonLakeFlyer's avatar
DonLakeFlyer committed
457
                path = [ ]
Don Gagne's avatar
Don Gagne committed
458 459
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing - angleDegrees))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, _landingAreaBearing + angleDegrees))
460
                addCoordinate(_useLoiterToAlt ? _missionItem.loiterTangentCoordinate : _missionItem.finalApproachCoordinate)
DonLakeFlyer's avatar
DonLakeFlyer committed
461 462
            }

Don Gagne's avatar
Don Gagne committed
463
            Component.onCompleted: recalc()
DonLakeFlyer's avatar
DonLakeFlyer committed
464 465

            Connections {
Don Gagne's avatar
Don Gagne committed
466 467 468
                target:                             _missionItem
                onLandingCoordinateChanged:         recalc()
                onLoiterTangentCoordinateChanged:   recalc()
469 470 471 472 473 474
                onFinalApproachCoordinateChanged:   recalc()
            }

            Connections {
                target:             _missionItem.useLoiterToAlt
                onRawValueChanged:  recalc()
DonLakeFlyer's avatar
DonLakeFlyer committed
475
            }
DonLakeFlyer's avatar
DonLakeFlyer committed
476 477
        }
    }
478 479 480 481 482 483 484 485 486 487 488

    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
489
                map:        _root.map
Remek Zajac's avatar
Remek Zajac committed
490 491
                heightText: Math.floor(QGroundControl.unitsConversion.metersToAppSettingsHorizontalDistanceUnits(_transitionAltitudeMeters)) +
                            QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString + "<sup>*</sup>"
492 493 494 495 496 497 498 499 500 501 502 503 504 505
            }

            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()
506
                onFinalApproachCoordinateChanged:   recalc()
507 508 509 510 511 512 513 514 515 516 517 518 519 520
            }
        }
    }

    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
521
                map:        _root.map
Remek Zajac's avatar
Remek Zajac committed
522 523
                heightText: Math.floor(QGroundControl.unitsConversion.metersToAppSettingsHorizontalDistanceUnits(_midSlopeAltitudeMeters)) +
                            QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString + "<sup>*</sup>"
524 525 526 527
            }

            function recalc() {
                var transitionCoordinate = _missionItem.landingCoordinate.atDistanceAndAzimuth(_landingLengthMeters / 2, _landingAreaBearing)
528
                var halfDistance = transitionCoordinate.distanceTo(_useLoiterToAlt ? _missionItem.loiterTangentCoordinate : _missionItem.finalApproachCoordinate) / 2
529 530 531 532 533 534 535 536 537 538 539
                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()
540
                onFinalApproachCoordinateChanged:   recalc()
541 542
            }

543 544 545 546
            Connections {
                target:             _missionItem.useLoiterToAlt
                onRawValueChanged:  recalc()
            }
547 548 549 550 551 552 553 554 555 556 557
        }
    }

    Component {
        id: approachHeightComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.width / 2
            anchorPoint.y:  0
            z:              QGroundControl.zOrderMapItems
            visible:        _missionItem.isCurrentItem
558
            coordinate:     _useLoiterToAlt ? _missionItem.loiterTangentCoordinate : _missionItem.finalApproachCoordinate
559 560

            sourceItem: HeightIndicator {
DonLakeFlyer's avatar
DonLakeFlyer committed
561
                map:        _root.map
562
                heightText: _missionItem.finalApproachAltitude.value.toFixed(1) + QGroundControl.unitsConversion.appSettingsHorizontalDistanceUnitsString
563 564 565
            }
        }
    }
566
}