FlightDisplayViewMap.qml 20.4 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * 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 15
import QtQuick                      2.11
import QtQuick.Controls             2.4
import QtLocation                   5.3
import QtPositioning                5.3
import QtQuick.Dialogs              1.2
16 17

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

FlightMap {
28 29 30 31
    id:                         flightMap
    mapName:                    _mapName
    allowGCSLocationCenter:     !userPanned
    allowVehicleLocationCenter: !_keepVehicleCentered
32
    planView:                   false
33

34 35
    property alias  scaleState: mapScale.state

36
    // The following properties must be set by the consumer
37
    property var    guidedActionsController
Don Gagne's avatar
Don Gagne committed
38
    property var    flightWidgets
39
    property var    rightPanelWidth
40
    property var    multiVehicleView                    ///< true: multi-vehicle view, false: single vehicle view
41
    property var    missionController:          null
Don Gagne's avatar
Don Gagne committed
42

43 44
    property rect   centerViewport:             Qt.rect(0, 0, width, height)

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

53
    property bool   _disableVehicleTracking:    false
54
    property bool   _keepVehicleCentered:       mainIsMap ? false : true
55

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

    // Track last known map position and zoom from Fly view in settings

    onZoomLevelChanged: {
        QGroundControl.flightMapZoom = zoomLevel
70
        updateAirspace(false)
71 72
    }
    onCenterChanged: {
73
        QGroundControl.flightMapPosition = center
74
        updateAirspace(false)
75
    }
76

77
    // When the user pans the map we stop responding to vehicle coordinate updates until the panRecenterTimer fires
78
    onUserPannedChanged: {
DonLakeFlyer's avatar
DonLakeFlyer committed
79 80 81 82 83
        if (userPanned) {
            userPanned = false
            _disableVehicleTracking = true
            panRecenterTimer.restart()
        }
84 85
    }

Gus Grubba's avatar
Gus Grubba committed
86
    on_AirspaceEnabledChanged: {
87
        updateAirspace(true)
Gus Grubba's avatar
Gus Grubba committed
88 89
    }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    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

    onAnimatedLatitudeChanged: flightMap.center = QtPositioning.coordinate(animatedLatitude, animatedLongitude)
    onAnimatedLongitudeChanged: flightMap.center = QtPositioning.coordinate(animatedLatitude, animatedLongitude)

    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()
    }

    function recenterNeeded() {
120
        var vehiclePoint = flightMap.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
121 122
        var toolStripRightEdge = mapFromItem(toolStrip, toolStrip.x, 0).x + toolStrip.width
        var instrumentsWidth = 0
123
        if (QGroundControl.corePlugin.options.instrumentWidget && QGroundControl.corePlugin.options.instrumentWidget.widgetPosition === CustomInstrumentWidget.POS_TOP_RIGHT) {
124 125 126 127
            // Assume standard instruments
            instrumentsWidth = flightDisplayViewWidgets.getPreferredInstrumentWidth()
        }
        var centerViewport = Qt.rect(toolStripRightEdge, 0, width - toolStripRightEdge - instrumentsWidth, height)
128
        return !pointInRect(vehiclePoint, centerViewport)
129 130 131
    }

    function updateMapToVehiclePosition() {
132
        // We let FlightMap handle first vehicle position
133
        if (!_keepMapCenteredOnVehicle && firstVehiclePositionReceived && _activeVehicleCoordinate.isValid && !_disableVehicleTracking) {
134
            if (_keepVehicleCentered) {
135
                flightMap.center = _activeVehicleCoordinate
136
            } else {
DonLakeFlyer's avatar
DonLakeFlyer committed
137
                if (firstVehiclePositionReceived && recenterNeeded()) {
138
                    animatedMapRecenter(flightMap.center, _activeVehicleCoordinate)
139 140
                }
            }
141 142 143
        }
    }

144 145 146 147 148 149
    on_ActiveVehicleCoordinateChanged: {
        if (_keepMapCenteredOnVehicle && _activeVehicleCoordinate.isValid && !_disableVehicleTracking) {
            flightMap.center = _activeVehicleCoordinate
        }
    }

150
    Timer {
151 152 153
        id:         panRecenterTimer
        interval:   10000
        running:    false
154
        onTriggered: {
155
            _disableVehicleTracking = false
156
            updateMapToVehiclePosition()
157 158 159
        }
    }

160 161 162 163 164 165 166
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

167
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
168

169
    Connections {
170 171
        target:                 missionController
        ignoreUnknownSignals:   true
172
        onNewItemsFromVehicle: {
Gus Grubba's avatar
Gus Grubba committed
173
            var visualItems = missionController.visualItems
174
            if (visualItems && visualItems.count !== 1) {
175
                mapFitFunctions.fitMapViewportToMissionItems()
176
                firstVehiclePositionReceived = true
177 178
            }
        }
179 180
    }

Don Gagne's avatar
Don Gagne committed
181
    MapFitFunctions {
182
        id:                         mapFitFunctions // The name for this id cannot be changed without breaking references outside of this code. Beware!
183
        map:                        mainWindow.flightDisplayMap
Don Gagne's avatar
Don Gagne committed
184
        usePlannedHomePosition:     false
Gus Grubba's avatar
Gus Grubba committed
185 186
        planMasterController:       missionController
        property real leftToolWidth: toolStrip.x + toolStrip.width
Don Gagne's avatar
Don Gagne committed
187
    }
188

189 190 191 192 193 194
    // Add trajectory lines to the map
    MapPolyline {
        id:         trajectoryPolyline
        line.width: 3
        line.color: "red"
        z:          QGroundControl.zOrderTrajectoryLines
195
        visible:    mainIsMap
196 197 198 199 200 201 202 203 204 205 206

        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
207
        }
208 209 210 211
    }

    // Add the vehicles to the map
    MapItemView {
212
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
213
        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
214 215
            vehicle:        object
            coordinate:     object.coordinate
216
            map:            flightMap
217
            size:           mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
218
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
219
        }
220 221
    }

222 223
    // Add ADSB vehicles to the map
    MapItemView {
224
        model: QGroundControl.adsbVehicleManager.adsbVehicles
225 226 227
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
228
            callsign:       object.callsign
229
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
230
            alert:          object.alert
231 232 233 234 235
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

236 237 238 239 240 241
    // Add the items associated with each vehicles flight plan to the map
    Repeater {
        model: QGroundControl.multiVehicleManager.vehicles

        PlanMapItems {
            map:                flightMap
242
            largeMapView:       mainIsMap
243
            masterController:   masterController
244
            vehicle:            _vehicle
245 246 247 248 249 250 251 252

            property var _vehicle: object

            PlanMasterController {
                id: masterController
                Component.onCompleted: startStaticActiveVehicle(object)
            }
        }
253 254
    }

255 256 257 258 259 260 261 262 263 264 265
    MapItemView {
        model: mainIsMap ? _missionController.directionArrows : undefined

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

266 267 268
    // Allow custom builds to add map items
    CustomMapItems {
        map:            flightMap
269
        largeMapView:   mainIsMap
270 271
    }

272 273
    GeoFenceMapVisuals {
        map:                    flightMap
274
        myGeoFenceController:   _geoFenceController
275
        interactive:            false
276
        planView:               false
Gus Grubba's avatar
Gus Grubba committed
277
        homePosition:           activeVehicle && activeVehicle.homePosition.isValid ? activeVehicle.homePosition :  QtPositioning.coordinate()
278 279
    }

280 281
    // Rally points on map
    MapItemView {
282
        model: _rallyPointController.points
283 284 285

        delegate: MapQuickItem {
            id:             itemIndicator
286 287
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
288 289 290 291 292 293 294 295 296 297
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

DonLakeFlyer's avatar
DonLakeFlyer committed
298 299
    // Camera trigger points
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
300
        model: activeVehicle ? activeVehicle.cameraTriggerPoints : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
301 302 303 304 305 306 307

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

308
    // GoTo Location visuals
Don Gagne's avatar
Don Gagne committed
309
    MapQuickItem {
310 311
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
312
        z:              QGroundControl.zOrderMapItems
313 314
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
315
        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
316 317
            checked:    true
            index:      -1
318
            label:      qsTr("Go here", "Go to location waypoint")
Don Gagne's avatar
Don Gagne committed
319
        }
320

Gus Grubba's avatar
Gus Grubba committed
321
        property bool inGotoFlightMode: activeVehicle ? activeVehicle.flightMode === activeVehicle.gotoFlightMode : false
322 323

        onInGotoFlightModeChanged: {
324
            if (!inGotoFlightMode && gotoLocationItem.visible) {
325
                // Hide goto indicator when vehicle falls out of guided mode
326
                gotoLocationItem.visible = false
327 328 329
            }
        }

Gus Grubba's avatar
Gus Grubba committed
330 331 332 333
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
334
                    gotoLocationItem.visible = false
Gus Grubba's avatar
Gus Grubba committed
335
                }
336 337 338
            }
        }

339 340 341 342
        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }
343

344 345 346 347
        function hide() {
            gotoLocationItem.visible = false
        }

348 349 350 351 352 353 354 355
        function actionConfirmed() {
            // We leave the indicator visible. The handling for onInGuidedModeChanged will hide it.
        }

        function actionCancelled() {
            hide()
        }
    }
356

357
    // Orbit editing visuals
358
    QGCMapCircleVisuals {
359 360 361 362
        id:             orbitMapCircle
        mapControl:     parent
        mapCircle:      _mapCircle
        visible:        false
363

364 365
        property alias center:              _mapCircle.center
        property alias clockwiseRotation:   _mapCircle.clockwiseRotation
366 367
        readonly property real defaultRadius: 30

Gus Grubba's avatar
Gus Grubba committed
368 369 370 371
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
372
                    orbitMapCircle.visible = false
Gus Grubba's avatar
Gus Grubba committed
373
                }
374 375 376
            }
        }

377
        function show(coord) {
DonLakeFlyer's avatar
DonLakeFlyer committed
378
            _mapCircle.radius.rawValue = defaultRadius
379 380 381 382 383 384 385 386
            orbitMapCircle.center = coord
            orbitMapCircle.visible = true
        }

        function hide() {
            orbitMapCircle.visible = false
        }

387 388 389 390 391 392 393 394 395
        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
396 397 398 399
        function radius() {
            return _mapCircle.radius.rawValue
        }

400 401 402 403 404
        Component.onCompleted: guidedActionsController.orbitMapCircle = orbitMapCircle

        QGCMapCircle {
            id:                 _mapCircle
            interactive:        true
DonLakeFlyer's avatar
DonLakeFlyer committed
405
            radius.rawValue:    30
406 407
            showRotation:       true
            clockwiseRotation:  true
408 409 410
        }
    }

Gus Grubba's avatar
Gus Grubba committed
411 412 413
    // ROI Location visuals
    MapQuickItem {
        id:             roiLocationItem
414
        visible:        activeVehicle && activeVehicle.isROIEnabled
Gus Grubba's avatar
Gus Grubba committed
415 416 417 418 419 420 421 422 423
        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")
        }

424
        //-- Visibilty controlled by actual state
Gus Grubba's avatar
Gus Grubba committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438
        function show(coord) {
            roiLocationItem.coordinate = coord
        }

        function hide() {
        }

        function actionConfirmed() {
        }

        function actionCancelled() {
        }
    }

439
    // Orbit telemetry visuals
440
    QGCMapCircleVisuals {
441
        id:             orbitTelemetryCircle
442
        mapControl:     parent
Gus Grubba's avatar
Gus Grubba committed
443 444
        mapCircle:      activeVehicle ? activeVehicle.orbitMapCircle : null
        visible:        activeVehicle ? activeVehicle.orbitActive : false
445 446
    }

447 448 449 450
    MapQuickItem {
        id:             orbitCenterIndicator
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Gus Grubba's avatar
Gus Grubba committed
451
        coordinate:     activeVehicle ? activeVehicle.orbitMapCircle.center : QtPositioning.coordinate()
452 453 454 455 456 457 458 459 460
        visible:        orbitTelemetryCircle.visible

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

Don Gagne's avatar
Don Gagne committed
461
    // Handle guided mode clicks
462 463
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
464

465
        QGCMenu {
DonLakeFlyer's avatar
DonLakeFlyer committed
466
            id: clickMenu
467
            property var coord
468
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
469 470 471 472
                text:           qsTr("Go to location")
                visible:        guidedActionsController.showGotoLocation

                onTriggered: {
473 474
                    gotoLocationItem.show(clickMenu.coord)
                    orbitMapCircle.hide()
475
                    guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickMenu.coord, gotoLocationItem)
DonLakeFlyer's avatar
DonLakeFlyer committed
476 477
                }
            }
478
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
479 480
                text:           qsTr("Orbit at location")
                visible:        guidedActionsController.showOrbit
481

DonLakeFlyer's avatar
DonLakeFlyer committed
482
                onTriggered: {
483 484
                    orbitMapCircle.show(clickMenu.coord)
                    gotoLocationItem.hide()
485
                    guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickMenu.coord, orbitMapCircle)
DonLakeFlyer's avatar
DonLakeFlyer committed
486 487
                }
            }
Gus Grubba's avatar
Gus Grubba committed
488 489 490 491 492 493 494 495 496
            QGCMenuItem {
                text:           qsTr("ROI at location")
                visible:        guidedActionsController.showROI

                onTriggered: {
                    roiLocationItem.show(clickMenu.coord)
                    guidedActionsController.confirmAction(guidedActionsController.actionROI, clickMenu.coord, orbitMapCircle)
                }
            }
DonLakeFlyer's avatar
DonLakeFlyer committed
497 498
        }

Don Gagne's avatar
Don Gagne committed
499
        onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
500 501
            if (guidedActionsController.guidedUIVisible || (!guidedActionsController.showGotoLocation && !guidedActionsController.showOrbit)) {
                return
Gus Grubba's avatar
Gus Grubba committed
502
            }
503 504 505
            orbitMapCircle.hide()
            gotoLocationItem.hide()
            var clickCoord = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
DonLakeFlyer's avatar
DonLakeFlyer committed
506
            if (guidedActionsController.showGotoLocation && guidedActionsController.showOrbit) {
507
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
508 509
                clickMenu.popup()
            } else if (guidedActionsController.showGotoLocation) {
DonLakeFlyer's avatar
DonLakeFlyer committed
510
                gotoLocationItem.show(clickCoord)
511
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickCoord)
DonLakeFlyer's avatar
DonLakeFlyer committed
512
            } else if (guidedActionsController.showOrbit) {
513 514
                orbitMapCircle.show(clickCoord)
                guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickCoord)
Don Gagne's avatar
Don Gagne committed
515 516
            }
        }
517
    }
518 519

    MapScale {
520
        id:                     mapScale
521
        anchors.right:          parent.right
522 523
        anchors.margins:        _toolsMargin
        anchors.topMargin:      _toolsMargin + state === "bottomMode" ? 0 : ScreenTools.toolbarHeight
524
        mapControl:             flightMap
525
        buttonsOnLeft:          false
526
        visible:                !ScreenTools.isTinyScreen && QGroundControl.corePlugin.options.enableMapScale && mainIsMap
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
        state:                  "bottomMode"
        states: [
            State {
                name:   "topMode"
                AnchorChanges {
                    target:                 mapScale
                    anchors.top:            parent.top
                    anchors.bottom:         undefined
                }
            },
            State {
                name:   "bottomMode"
                AnchorChanges {
                    target:                 mapScale
                    anchors.top:            undefined
                    anchors.bottom:         parent.bottom
                }
            }
        ]
546
    }
547

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

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

570
}