FlyViewMap.qml 20.5 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
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

10 11 12 13 14
import QtQuick                      2.11
import QtQuick.Controls             2.4
import QtLocation                   5.3
import QtPositioning                5.3
import QtQuick.Dialogs              1.2
15 16

import QGroundControl               1.0
17 18 19
import QGroundControl.Airspace      1.0
import QGroundControl.Controllers   1.0
import QGroundControl.Controls      1.0
20 21 22
import QGroundControl.FlightDisplay 1.0
import QGroundControl.FlightMap     1.0
import QGroundControl.Palette       1.0
23
import QGroundControl.ScreenTools   1.0
24 25 26
import QGroundControl.Vehicle       1.0

FlightMap {
27 28
    id:                         _root
    allowGCSLocationCenter:     true
29
    allowVehicleLocationCenter: !_keepVehicleCentered
30
    planView:                   false
31 32
    zoomLevel:                  QGroundControl.flightMapZoom
    center:                     QGroundControl.flightMapPosition
33

34 35 36 37 38 39 40
    property Item pipState: _pipState
    QGCPipState {
        id:         _pipState
        pipOverlay: _pipOverlay
        isDark:     _isFullWindowItemDark
    }

41
    property var    rightPanelWidth
42
    property var    planMasterController
43 44
    property bool   pipMode:                    false   // true: map is shown in a small pip mode
    property var    toolInsets                          // Insets for the center viewport area
45

46 47 48
    property var    _planMasterController:      planMasterController
    property var    _geoFenceController:        planMasterController.geoFenceController
    property var    _rallyPointController:      planMasterController.rallyPointController
Gus Grubba's avatar
Gus Grubba committed
49
    property var    _activeVehicleCoordinate:   activeVehicle ? activeVehicle.coordinate : QtPositioning.coordinate()
50
    property real   _toolButtonTopMargin:       parent.height - mainWindow.height + (ScreenTools.defaultFontPixelHeight / 2)
Gus Grubba's avatar
Gus Grubba committed
51
    property bool   _airspaceEnabled:           QGroundControl.airmapSupported ? (QGroundControl.settingsManager.airMapSettings.enableAirMap.rawValue && QGroundControl.airspaceManager.connected): false
52 53
    property var    _flyViewSettings:           QGroundControl.settingsManager.flyViewSettings
    property bool   _keepMapCenteredOnVehicle:  _flyViewSettings.keepMapCenteredOnVehicle.rawValue
54

55
    property bool   _disableVehicleTracking:    false
56 57
    property bool   _keepVehicleCentered:       pipMode ? true : false
    property bool   _saveZoomLevelSetting:      true
58

59
    function updateAirspace(reset) {
60
        if(_airspaceEnabled) {
61 62
            var coordinateNW = _root.toCoordinate(Qt.point(0,0), false /* clipToViewPort */)
            var coordinateSE = _root.toCoordinate(Qt.point(width,height), false /* clipToViewPort */)
63
            if(coordinateNW.isValid && coordinateSE.isValid) {
64
                QGroundControl.airspaceManager.setROI(coordinateNW, coordinateSE, false /*planView*/, reset)
65
            }
66
        }
67 68
    }

69 70 71 72 73 74 75 76
    function _adjustMapZoomForPipMode() {
        _saveZoomLevelSetting = false
        if (pipMode) {
            if (QGroundControl.flightMapZoom > 3) {
                zoomLevel = QGroundControl.flightMapZoom - 3
            }
        } else {
            zoomLevel = QGroundControl.flightMapZoom
77
        }
78
        _saveZoomLevelSetting = true
79 80
    }

81
    onPipModeChanged: _adjustMapZoomForPipMode()
82

83
    onVisibleChanged: {
84 85 86
        if (visible) {
            // Synchronize center position with Plan View
            center = QGroundControl.flightMapPosition
87 88 89
        }
    }

90
    onZoomLevelChanged: {
91
        if (_saveZoomLevelSetting) {
92 93 94
            QGroundControl.flightMapZoom = zoomLevel
            updateAirspace(false)
        }
95 96
    }
    onCenterChanged: {
97
        QGroundControl.flightMapPosition = center
98
        updateAirspace(false)
99
    }
100

Gus Grubba's avatar
Gus Grubba committed
101
    on_AirspaceEnabledChanged: {
102
        updateAirspace(true)
Gus Grubba's avatar
Gus Grubba committed
103 104
    }

105 106 107 108 109 110 111 112 113 114
    // We track whether the user has panned or not to correctly handle automatic map positioning
    Connections {
        target: gesture

        onPanStarted:       _disableVehicleTracking = true
        onFlickStarted:     _disableVehicleTracking = true
        onPanFinished:      panRecenterTimer.restart()
        onFlickFinished:    panRecenterTimer.restart()
    }

115 116 117 118 119 120 121 122 123 124 125 126 127 128
    function pointInRect(point, rect) {
        return point.x > rect.x &&
                point.x < rect.x + rect.width &&
                point.y > rect.y &&
                point.y < rect.y + rect.height;
    }

    property real _animatedLatitudeStart
    property real _animatedLatitudeStop
    property real _animatedLongitudeStart
    property real _animatedLongitudeStop
    property real animatedLatitude
    property real animatedLongitude

129 130
    onAnimatedLatitudeChanged: _root.center = QtPositioning.coordinate(animatedLatitude, animatedLongitude)
    onAnimatedLongitudeChanged: _root.center = QtPositioning.coordinate(animatedLatitude, animatedLongitude)
131 132 133 134 135 136 137 138 139 140 141 142 143

    NumberAnimation on animatedLatitude { id: animateLat; from: _animatedLatitudeStart; to: _animatedLatitudeStop; duration: 1000 }
    NumberAnimation on animatedLongitude { id: animateLong; from: _animatedLongitudeStart; to: _animatedLongitudeStop; duration: 1000 }

    function animatedMapRecenter(fromCoord, toCoord) {
        _animatedLatitudeStart = fromCoord.latitude
        _animatedLongitudeStart = fromCoord.longitude
        _animatedLatitudeStop = toCoord.latitude
        _animatedLongitudeStop = toCoord.longitude
        animateLat.start()
        animateLong.start()
    }

144 145 146 147 148 149 150
    function _insetRect() {
        return Qt.rect(toolInsets.leftEdgeCenterInset,
                       toolInsets.topEdgeCenterInset,
                       _root.width - toolInsets.leftEdgeCenterInset - toolInsets.rightEdgeCenterInset,
                       _root.height - toolInsets.topEdgeCenterInset - toolInsets.bottomEdgeCenterInset)
    }

151
    function recenterNeeded() {
152 153 154
        var vehiclePoint = _root.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
        var insetRect = _insetRect()
        return !pointInRect(vehiclePoint, insetRect)
155 156 157
    }

    function updateMapToVehiclePosition() {
158 159 160
        if (animateLat.running || animateLong.running) {
            return
        }
161
        // We let FlightMap handle first vehicle position
162
        if (!_keepMapCenteredOnVehicle && firstVehiclePositionReceived && _activeVehicleCoordinate.isValid && !_disableVehicleTracking) {
163
            if (_keepVehicleCentered) {
164
                _root.center = _activeVehicleCoordinate
165
            } else {
DonLakeFlyer's avatar
DonLakeFlyer committed
166
                if (firstVehiclePositionReceived && recenterNeeded()) {
167 168 169 170 171 172 173 174
                    // Move the map such that the vehicle is centered within the inset area
                    var vehiclePoint = _root.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
                    var insetRect = _insetRect()
                    var centerInsetPoint = Qt.point(insetRect.x + insetRect.width / 2, insetRect.y + insetRect.height / 2)
                    var centerOffset = Qt.point((_root.width / 2) - centerInsetPoint.x, (_root.height / 2) - centerInsetPoint.y)
                    var vehicleOffsetPoint = Qt.point(vehiclePoint.x + centerOffset.x, vehiclePoint.y + centerOffset.y)
                    var vehicleOffsetCoord = _root.toCoordinate(vehicleOffsetPoint, false /* clipToViewport */)
                    animatedMapRecenter(_root.center, vehicleOffsetCoord)
175 176
                }
            }
177 178 179
        }
    }

180 181
    on_ActiveVehicleCoordinateChanged: {
        if (_keepMapCenteredOnVehicle && _activeVehicleCoordinate.isValid && !_disableVehicleTracking) {
182
            _root.center = _activeVehicleCoordinate
183 184 185
        }
    }

186
    Timer {
187 188 189
        id:         panRecenterTimer
        interval:   10000
        running:    false
190
        onTriggered: {
191
            _disableVehicleTracking = false
192
            updateMapToVehiclePosition()
193 194 195
        }
    }

196 197 198 199 200 201 202
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

203
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
204

205
    Connections {
206
        target:                 _missionController
207
        ignoreUnknownSignals:   true
208
        onNewItemsFromVehicle: {
209
            var visualItems = _missionController.visualItems
210
            if (visualItems && visualItems.count !== 1) {
211
                mapFitFunctions.fitMapViewportToMissionItems()
212
                firstVehiclePositionReceived = true
213 214
            }
        }
215 216
    }

Don Gagne's avatar
Don Gagne committed
217
    MapFitFunctions {
218
        id:                         mapFitFunctions // The name for this id cannot be changed without breaking references outside of this code. Beware!
219
        map:                        _root
Don Gagne's avatar
Don Gagne committed
220
        usePlannedHomePosition:     false
221
        planMasterController:       _planMasterController
Don Gagne's avatar
Don Gagne committed
222
    }
223

224 225 226 227 228 229
    // Add trajectory lines to the map
    MapPolyline {
        id:         trajectoryPolyline
        line.width: 3
        line.color: "red"
        z:          QGroundControl.zOrderTrajectoryLines
230
        visible:    !pipMode
231 232 233 234 235 236 237 238 239 240 241

        Connections {
            target:                 QGroundControl.multiVehicleManager
            onActiveVehicleChanged: trajectoryPolyline.path = activeVehicle ? activeVehicle.trajectoryPoints.list() : []
        }

        Connections {
            target:                 activeVehicle ? activeVehicle.trajectoryPoints : null
            onPointAdded:           trajectoryPolyline.addCoordinate(coordinate)
            onUpdateLastPoint:      trajectoryPolyline.replaceCoordinate(trajectoryPolyline.pathLength() - 1, coordinate)
            onPointsCleared:        trajectoryPolyline.path = []
Don Gagne's avatar
Don Gagne committed
242
        }
243 244 245 246
    }

    // Add the vehicles to the map
    MapItemView {
247
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
248
        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
249 250
            vehicle:        object
            coordinate:     object.coordinate
251 252
            map:            _root
            size:           pipMode ? ScreenTools.defaultFontPixelHeight : ScreenTools.defaultFontPixelHeight * 3
DonLakeFlyer's avatar
DonLakeFlyer committed
253
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
254
        }
255
    }
256 257 258 259 260 261 262 263 264 265
    // Add distance sensor view
    MapItemView{
        model: QGroundControl.multiVehicleManager.vehicles
        delegate: ProximityRadarMapView {
            vehicle:        object
            coordinate:     object.coordinate
            map:            _root
            z:              QGroundControl.zOrderVehicles
        }
    }
266 267
    // Add ADSB vehicles to the map
    MapItemView {
268
        model: QGroundControl.adsbVehicleManager.adsbVehicles
269 270 271
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
272
            callsign:       object.callsign
273
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
274
            alert:          object.alert
275
            map:            _root
276 277 278 279
            z:              QGroundControl.zOrderVehicles
        }
    }

280 281 282 283 284
    // Add the items associated with each vehicles flight plan to the map
    Repeater {
        model: QGroundControl.multiVehicleManager.vehicles

        PlanMapItems {
285 286
            map:                    _root
            largeMapView:           !pipMode
287 288
            planMasterController:   _planMasterController
            vehicle:                _vehicle
289 290 291 292 293 294 295 296

            property var _vehicle: object

            PlanMasterController {
                id: masterController
                Component.onCompleted: startStaticActiveVehicle(object)
            }
        }
297 298
    }

299
    MapItemView {
300
        model: pipMode ? undefined : _missionController.directionArrows
301 302 303 304 305 306 307 308 309

        delegate: MapLineArrow {
            fromCoord:      object ? object.coordinate1 : undefined
            toCoord:        object ? object.coordinate2 : undefined
            arrowPosition:  2
            z:              QGroundControl.zOrderWaypointLines
        }
    }

310 311
    // Allow custom builds to add map items
    CustomMapItems {
312 313
        map:            _root
        largeMapView:   !pipMode
314 315
    }

316
    GeoFenceMapVisuals {
317
        map:                    _root
318
        myGeoFenceController:   _geoFenceController
319
        interactive:            false
320
        planView:               false
Gus Grubba's avatar
Gus Grubba committed
321
        homePosition:           activeVehicle && activeVehicle.homePosition.isValid ? activeVehicle.homePosition :  QtPositioning.coordinate()
322 323
    }

324 325
    // Rally points on map
    MapItemView {
326
        model: _rallyPointController.points
327 328 329

        delegate: MapQuickItem {
            id:             itemIndicator
330 331
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
332 333 334 335 336 337 338 339 340 341
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

            sourceItem: MissionItemIndexLabel {
                id:         itemIndexLabel
                label:      qsTr("R", "rally point map item label")
            }
        }
    }

DonLakeFlyer's avatar
DonLakeFlyer committed
342 343
    // Camera trigger points
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
344
        model: activeVehicle ? activeVehicle.cameraTriggerPoints : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
345 346 347 348 349 350 351

        delegate: CameraTriggerIndicator {
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderTopMost
        }
    }

352
    // GoTo Location visuals
Don Gagne's avatar
Don Gagne committed
353
    MapQuickItem {
354 355
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
356
        z:              QGroundControl.zOrderMapItems
357 358
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
359
        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
360 361
            checked:    true
            index:      -1
362
            label:      qsTr("Go here", "Go to location waypoint")
Don Gagne's avatar
Don Gagne committed
363
        }
364

Gus Grubba's avatar
Gus Grubba committed
365
        property bool inGotoFlightMode: activeVehicle ? activeVehicle.flightMode === activeVehicle.gotoFlightMode : false
366 367

        onInGotoFlightModeChanged: {
368
            if (!inGotoFlightMode && gotoLocationItem.visible) {
369
                // Hide goto indicator when vehicle falls out of guided mode
370
                gotoLocationItem.visible = false
371 372 373
            }
        }

Gus Grubba's avatar
Gus Grubba committed
374 375 376 377
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
378
                    gotoLocationItem.visible = false
Gus Grubba's avatar
Gus Grubba committed
379
                }
380 381 382
            }
        }

383 384 385 386
        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }
387

388 389 390 391
        function hide() {
            gotoLocationItem.visible = false
        }

392 393 394 395 396 397 398 399
        function actionConfirmed() {
            // We leave the indicator visible. The handling for onInGuidedModeChanged will hide it.
        }

        function actionCancelled() {
            hide()
        }
    }
400

401
    // Orbit editing visuals
402
    QGCMapCircleVisuals {
403 404 405 406
        id:             orbitMapCircle
        mapControl:     parent
        mapCircle:      _mapCircle
        visible:        false
407

408 409
        property alias center:              _mapCircle.center
        property alias clockwiseRotation:   _mapCircle.clockwiseRotation
410 411
        readonly property real defaultRadius: 30

Gus Grubba's avatar
Gus Grubba committed
412 413 414 415
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
416
                    orbitMapCircle.visible = false
Gus Grubba's avatar
Gus Grubba committed
417
                }
418 419 420
            }
        }

421
        function show(coord) {
DonLakeFlyer's avatar
DonLakeFlyer committed
422
            _mapCircle.radius.rawValue = defaultRadius
423 424 425 426 427 428 429 430
            orbitMapCircle.center = coord
            orbitMapCircle.visible = true
        }

        function hide() {
            orbitMapCircle.visible = false
        }

431 432 433 434 435 436 437 438 439
        function actionConfirmed() {
            // Live orbit status is handled by telemetry so we hide here and telemetry will show again.
            hide()
        }

        function actionCancelled() {
            hide()
        }

DonLakeFlyer's avatar
DonLakeFlyer committed
440 441 442 443
        function radius() {
            return _mapCircle.radius.rawValue
        }

444
        Component.onCompleted: mainWindow.guidedControllerFlyView.orbitMapCircle = orbitMapCircle
445 446 447 448

        QGCMapCircle {
            id:                 _mapCircle
            interactive:        true
DonLakeFlyer's avatar
DonLakeFlyer committed
449
            radius.rawValue:    30
450 451
            showRotation:       true
            clockwiseRotation:  true
452 453 454
        }
    }

Gus Grubba's avatar
Gus Grubba committed
455 456 457
    // ROI Location visuals
    MapQuickItem {
        id:             roiLocationItem
458
        visible:        activeVehicle && activeVehicle.isROIEnabled
Gus Grubba's avatar
Gus Grubba committed
459 460 461 462 463 464 465 466 467
        z:              QGroundControl.zOrderMapItems
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
        sourceItem: MissionItemIndexLabel {
            checked:    true
            index:      -1
            label:      qsTr("ROI here", "Make this a Region Of Interest")
        }

468
        //-- Visibilty controlled by actual state
Gus Grubba's avatar
Gus Grubba committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482
        function show(coord) {
            roiLocationItem.coordinate = coord
        }

        function hide() {
        }

        function actionConfirmed() {
        }

        function actionCancelled() {
        }
    }

483
    // Orbit telemetry visuals
484
    QGCMapCircleVisuals {
485
        id:             orbitTelemetryCircle
486
        mapControl:     parent
Gus Grubba's avatar
Gus Grubba committed
487 488
        mapCircle:      activeVehicle ? activeVehicle.orbitMapCircle : null
        visible:        activeVehicle ? activeVehicle.orbitActive : false
489 490
    }

491 492 493 494
    MapQuickItem {
        id:             orbitCenterIndicator
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Gus Grubba's avatar
Gus Grubba committed
495
        coordinate:     activeVehicle ? activeVehicle.orbitMapCircle.center : QtPositioning.coordinate()
496 497 498 499 500 501 502 503 504
        visible:        orbitTelemetryCircle.visible

        sourceItem: MissionItemIndexLabel {
            checked:    true
            index:      -1
            label:      qsTr("Orbit", "Orbit waypoint")
        }
    }

Don Gagne's avatar
Don Gagne committed
505
    // Handle guided mode clicks
506 507
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
508

509
        QGCMenu {
DonLakeFlyer's avatar
DonLakeFlyer committed
510
            id: clickMenu
511
            property var coord
512
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
513
                text:           qsTr("Go to location")
514
                visible:        mainWindow.guidedControllerFlyView.showGotoLocation
DonLakeFlyer's avatar
DonLakeFlyer committed
515 516

                onTriggered: {
517
                    gotoLocationItem.show(clickMenu.coord)
518
                    mainWindow.guidedControllerFlyView.confirmAction(mainWindow.guidedControllerFlyView.actionGoto, clickMenu.coord, gotoLocationItem)
DonLakeFlyer's avatar
DonLakeFlyer committed
519 520
                }
            }
521
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
522
                text:           qsTr("Orbit at location")
523
                visible:        mainWindow.guidedControllerFlyView.showOrbit
524

DonLakeFlyer's avatar
DonLakeFlyer committed
525
                onTriggered: {
526
                    orbitMapCircle.show(clickMenu.coord)
527
                    mainWindow.guidedControllerFlyView.confirmAction(mainWindow.guidedControllerFlyView.actionOrbit, clickMenu.coord, orbitMapCircle)
DonLakeFlyer's avatar
DonLakeFlyer committed
528 529
                }
            }
Gus Grubba's avatar
Gus Grubba committed
530 531
            QGCMenuItem {
                text:           qsTr("ROI at location")
532
                visible:        mainWindow.guidedControllerFlyView.showROI
Gus Grubba's avatar
Gus Grubba committed
533 534 535

                onTriggered: {
                    roiLocationItem.show(clickMenu.coord)
536
                    mainWindow.guidedControllerFlyView.confirmAction(mainWindow.guidedControllerFlyView.actionROI, clickMenu.coord, roiLocationItem)
Gus Grubba's avatar
Gus Grubba committed
537 538
                }
            }
DonLakeFlyer's avatar
DonLakeFlyer committed
539 540
        }

Don Gagne's avatar
Don Gagne committed
541
        onClicked: {
542
            if (!mainWindow.guidedControllerFlyView.guidedUIVisible && (mainWindow.guidedControllerFlyView.showGotoLocation || mainWindow.guidedControllerFlyView.showOrbit || mainWindow.guidedControllerFlyView.showROI)) {
543 544 545
                orbitMapCircle.hide()
                gotoLocationItem.hide()
                var clickCoord = _root.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
546
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
547
                clickMenu.popup()
Don Gagne's avatar
Don Gagne committed
548 549
            }
        }
550
    }
551

552
    // Airspace overlap support
553
    MapItemView {
554
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.circles : []
555 556 557
        delegate: MapCircle {
            center:         object.center
            radius:         object.radius
558
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
559 560
            border.color:   object.lineColor
            border.width:   object.lineWidth
561 562 563 564
        }
    }

    MapItemView {
565
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.polygons : []
566 567
        delegate: MapPolygon {
            path:           object.polygon
568
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
569 570
            border.color:   object.lineColor
            border.width:   object.lineWidth
571 572
        }
    }
573

574
}