FlyViewMap.qml 20.2 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
    // Add ADSB vehicles to the map
    MapItemView {
259
        model: QGroundControl.adsbVehicleManager.adsbVehicles
260 261 262
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
263
            callsign:       object.callsign
264
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
265
            alert:          object.alert
266
            map:            _root
267 268 269 270
            z:              QGroundControl.zOrderVehicles
        }
    }

271 272 273 274 275
    // Add the items associated with each vehicles flight plan to the map
    Repeater {
        model: QGroundControl.multiVehicleManager.vehicles

        PlanMapItems {
276 277
            map:                    _root
            largeMapView:           !pipMode
278 279
            planMasterController:   _planMasterController
            vehicle:                _vehicle
280 281 282 283 284 285 286 287

            property var _vehicle: object

            PlanMasterController {
                id: masterController
                Component.onCompleted: startStaticActiveVehicle(object)
            }
        }
288 289
    }

290
    MapItemView {
291
        model: pipMode ? undefined : _missionController.directionArrows
292 293 294 295 296 297 298 299 300

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

301 302
    // Allow custom builds to add map items
    CustomMapItems {
303 304
        map:            _root
        largeMapView:   !pipMode
305 306
    }

307
    GeoFenceMapVisuals {
308
        map:                    _root
309
        myGeoFenceController:   _geoFenceController
310
        interactive:            false
311
        planView:               false
Gus Grubba's avatar
Gus Grubba committed
312
        homePosition:           activeVehicle && activeVehicle.homePosition.isValid ? activeVehicle.homePosition :  QtPositioning.coordinate()
313 314
    }

315 316
    // Rally points on map
    MapItemView {
317
        model: _rallyPointController.points
318 319 320

        delegate: MapQuickItem {
            id:             itemIndicator
321 322
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
323 324 325 326 327 328 329 330 331 332
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

DonLakeFlyer's avatar
DonLakeFlyer committed
333 334
    // Camera trigger points
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
335
        model: activeVehicle ? activeVehicle.cameraTriggerPoints : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
336 337 338 339 340 341 342

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

343
    // GoTo Location visuals
Don Gagne's avatar
Don Gagne committed
344
    MapQuickItem {
345 346
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
347
        z:              QGroundControl.zOrderMapItems
348 349
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
350
        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
351 352
            checked:    true
            index:      -1
353
            label:      qsTr("Go here", "Go to location waypoint")
Don Gagne's avatar
Don Gagne committed
354
        }
355

Gus Grubba's avatar
Gus Grubba committed
356
        property bool inGotoFlightMode: activeVehicle ? activeVehicle.flightMode === activeVehicle.gotoFlightMode : false
357 358

        onInGotoFlightModeChanged: {
359
            if (!inGotoFlightMode && gotoLocationItem.visible) {
360
                // Hide goto indicator when vehicle falls out of guided mode
361
                gotoLocationItem.visible = false
362 363 364
            }
        }

Gus Grubba's avatar
Gus Grubba committed
365 366 367 368
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
369
                    gotoLocationItem.visible = false
Gus Grubba's avatar
Gus Grubba committed
370
                }
371 372 373
            }
        }

374 375 376 377
        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }
378

379 380 381 382
        function hide() {
            gotoLocationItem.visible = false
        }

383 384 385 386 387 388 389 390
        function actionConfirmed() {
            // We leave the indicator visible. The handling for onInGuidedModeChanged will hide it.
        }

        function actionCancelled() {
            hide()
        }
    }
391

392
    // Orbit editing visuals
393
    QGCMapCircleVisuals {
394 395 396 397
        id:             orbitMapCircle
        mapControl:     parent
        mapCircle:      _mapCircle
        visible:        false
398

399 400
        property alias center:              _mapCircle.center
        property alias clockwiseRotation:   _mapCircle.clockwiseRotation
401 402
        readonly property real defaultRadius: 30

Gus Grubba's avatar
Gus Grubba committed
403 404 405 406
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
407
                    orbitMapCircle.visible = false
Gus Grubba's avatar
Gus Grubba committed
408
                }
409 410 411
            }
        }

412
        function show(coord) {
DonLakeFlyer's avatar
DonLakeFlyer committed
413
            _mapCircle.radius.rawValue = defaultRadius
414 415 416 417 418 419 420 421
            orbitMapCircle.center = coord
            orbitMapCircle.visible = true
        }

        function hide() {
            orbitMapCircle.visible = false
        }

422 423 424 425 426 427 428 429 430
        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
431 432 433 434
        function radius() {
            return _mapCircle.radius.rawValue
        }

435
        Component.onCompleted: mainWindow.guidedControllerFlyView.orbitMapCircle = orbitMapCircle
436 437 438 439

        QGCMapCircle {
            id:                 _mapCircle
            interactive:        true
DonLakeFlyer's avatar
DonLakeFlyer committed
440
            radius.rawValue:    30
441 442
            showRotation:       true
            clockwiseRotation:  true
443 444 445
        }
    }

Gus Grubba's avatar
Gus Grubba committed
446 447 448
    // ROI Location visuals
    MapQuickItem {
        id:             roiLocationItem
449
        visible:        activeVehicle && activeVehicle.isROIEnabled
Gus Grubba's avatar
Gus Grubba committed
450 451 452 453 454 455 456 457 458
        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")
        }

459
        //-- Visibilty controlled by actual state
Gus Grubba's avatar
Gus Grubba committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473
        function show(coord) {
            roiLocationItem.coordinate = coord
        }

        function hide() {
        }

        function actionConfirmed() {
        }

        function actionCancelled() {
        }
    }

474
    // Orbit telemetry visuals
475
    QGCMapCircleVisuals {
476
        id:             orbitTelemetryCircle
477
        mapControl:     parent
Gus Grubba's avatar
Gus Grubba committed
478 479
        mapCircle:      activeVehicle ? activeVehicle.orbitMapCircle : null
        visible:        activeVehicle ? activeVehicle.orbitActive : false
480 481
    }

482 483 484 485
    MapQuickItem {
        id:             orbitCenterIndicator
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Gus Grubba's avatar
Gus Grubba committed
486
        coordinate:     activeVehicle ? activeVehicle.orbitMapCircle.center : QtPositioning.coordinate()
487 488 489 490 491 492 493 494 495
        visible:        orbitTelemetryCircle.visible

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

Don Gagne's avatar
Don Gagne committed
496
    // Handle guided mode clicks
497 498
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
499

500
        QGCMenu {
DonLakeFlyer's avatar
DonLakeFlyer committed
501
            id: clickMenu
502
            property var coord
503
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
504
                text:           qsTr("Go to location")
505
                visible:        mainWindow.guidedControllerFlyView.showGotoLocation
DonLakeFlyer's avatar
DonLakeFlyer committed
506 507

                onTriggered: {
508
                    gotoLocationItem.show(clickMenu.coord)
509
                    mainWindow.guidedControllerFlyView.confirmAction(mainWindow.guidedControllerFlyView.actionGoto, clickMenu.coord, gotoLocationItem)
DonLakeFlyer's avatar
DonLakeFlyer committed
510 511
                }
            }
512
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
513
                text:           qsTr("Orbit at location")
514
                visible:        mainWindow.guidedControllerFlyView.showOrbit
515

DonLakeFlyer's avatar
DonLakeFlyer committed
516
                onTriggered: {
517
                    orbitMapCircle.show(clickMenu.coord)
518
                    mainWindow.guidedControllerFlyView.confirmAction(mainWindow.guidedControllerFlyView.actionOrbit, clickMenu.coord, orbitMapCircle)
DonLakeFlyer's avatar
DonLakeFlyer committed
519 520
                }
            }
Gus Grubba's avatar
Gus Grubba committed
521 522
            QGCMenuItem {
                text:           qsTr("ROI at location")
523
                visible:        mainWindow.guidedControllerFlyView.showROI
Gus Grubba's avatar
Gus Grubba committed
524 525 526

                onTriggered: {
                    roiLocationItem.show(clickMenu.coord)
527
                    mainWindow.guidedControllerFlyView.confirmAction(mainWindow.guidedControllerFlyView.actionROI, clickMenu.coord, roiLocationItem)
Gus Grubba's avatar
Gus Grubba committed
528 529
                }
            }
DonLakeFlyer's avatar
DonLakeFlyer committed
530 531
        }

Don Gagne's avatar
Don Gagne committed
532
        onClicked: {
533
            if (!mainWindow.guidedControllerFlyView.guidedUIVisible && (mainWindow.guidedControllerFlyView.showGotoLocation || mainWindow.guidedControllerFlyView.showOrbit || mainWindow.guidedControllerFlyView.showROI)) {
534 535 536
                orbitMapCircle.hide()
                gotoLocationItem.hide()
                var clickCoord = _root.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
537
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
538
                clickMenu.popup()
Don Gagne's avatar
Don Gagne committed
539 540
            }
        }
541
    }
542

543
    // Airspace overlap support
544
    MapItemView {
545
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.circles : []
546 547 548
        delegate: MapCircle {
            center:         object.center
            radius:         object.radius
549
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
550 551
            border.color:   object.lineColor
            border.width:   object.lineWidth
552 553 554 555
        }
    }

    MapItemView {
556
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.polygons : []
557 558
        delegate: MapPolygon {
            path:           object.polygon
559
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
560 561
            border.color:   object.lineColor
            border.width:   object.lineWidth
562 563
        }
    }
564

565
}