FlightDisplayViewMap.qml 19 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
47
    property var    activeVehicle:              QGroundControl.multiVehicleManager.activeVehicle
Gus Grubba's avatar
Gus Grubba committed
48
    property var    _activeVehicleCoordinate:   activeVehicle ? activeVehicle.coordinate : QtPositioning.coordinate()
49
    property real   _toolButtonTopMargin:       parent.height - mainWindow.height + (ScreenTools.defaultFontPixelHeight / 2)
Gus Grubba's avatar
Gus Grubba committed
50
    property bool   _airspaceEnabled:           QGroundControl.airmapSupported ? (QGroundControl.settingsManager.airMapSettings.enableAirMap.rawValue && QGroundControl.airspaceManager.connected): false
51

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

55
    function updateAirspace(reset) {
56
        if(_airspaceEnabled) {
57 58 59
            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) {
60
                QGroundControl.airspaceManager.setROI(coordinateNW, coordinateSE, false /*planView*/, reset)
61
            }
62
        }
63 64 65 66 67 68
    }

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

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

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

Gus Grubba's avatar
Gus Grubba committed
85
    on_AirspaceEnabledChanged: {
86
        updateAirspace(true)
Gus Grubba's avatar
Gus Grubba committed
87 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
    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() {
119
        var vehiclePoint = flightMap.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
120 121
        var toolStripRightEdge = mapFromItem(toolStrip, toolStrip.x, 0).x + toolStrip.width
        var instrumentsWidth = 0
122
        if (QGroundControl.corePlugin.options.instrumentWidget && QGroundControl.corePlugin.options.instrumentWidget.widgetPosition === CustomInstrumentWidget.POS_TOP_RIGHT) {
123 124 125 126
            // Assume standard instruments
            instrumentsWidth = flightDisplayViewWidgets.getPreferredInstrumentWidth()
        }
        var centerViewport = Qt.rect(toolStripRightEdge, 0, width - toolStripRightEdge - instrumentsWidth, height)
127
        return !pointInRect(vehiclePoint, centerViewport)
128 129 130
    }

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

    Timer {
144 145 146
        id:         panRecenterTimer
        interval:   10000
        running:    false
147
        onTriggered: {
148
            _disableVehicleTracking = false
149
            updateMapToVehiclePosition()
150 151 152
        }
    }

153 154 155 156 157 158 159
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

160
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
161

162
    Connections {
163 164
        target:                 missionController
        ignoreUnknownSignals:   true
165
        onNewItemsFromVehicle: {
Gus Grubba's avatar
Gus Grubba committed
166
            var visualItems = missionController.visualItems
167
            if (visualItems && visualItems.count !== 1) {
168
                mapFitFunctions.fitMapViewportToMissionItems()
169
                firstVehiclePositionReceived = true
170 171
            }
        }
172 173
    }

Don Gagne's avatar
Don Gagne committed
174
    MapFitFunctions {
175
        id:                         mapFitFunctions // The name for this id cannot be changed without breaking references outside of this code. Beware!
176
        map:                        mainWindow.flightDisplayMap
Don Gagne's avatar
Don Gagne committed
177
        usePlannedHomePosition:     false
Gus Grubba's avatar
Gus Grubba committed
178 179
        planMasterController:       missionController
        property real leftToolWidth: toolStrip.x + toolStrip.width
Don Gagne's avatar
Don Gagne committed
180
    }
181

182 183 184 185 186 187
    // Add trajectory lines to the map
    MapPolyline {
        id:         trajectoryPolyline
        line.width: 3
        line.color: "red"
        z:          QGroundControl.zOrderTrajectoryLines
188
        visible:    mainIsMap
189 190 191 192 193 194 195 196 197 198 199

        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
200
        }
201 202 203 204
    }

    // Add the vehicles to the map
    MapItemView {
205
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
206
        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
207 208
            vehicle:        object
            coordinate:     object.coordinate
209
            map:            flightMap
210
            size:           mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
211
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
212
        }
213 214
    }

215 216
    // Add ADSB vehicles to the map
    MapItemView {
217
        model: QGroundControl.adsbVehicleManager.adsbVehicles
218 219 220
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
221
            callsign:       object.callsign
222
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
223
            alert:          object.alert
224 225 226 227 228
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

229 230 231 232 233 234
    // Add the items associated with each vehicles flight plan to the map
    Repeater {
        model: QGroundControl.multiVehicleManager.vehicles

        PlanMapItems {
            map:                flightMap
235
            largeMapView:       mainIsMap
236
            masterController:   masterController
237
            vehicle:            _vehicle
238 239 240 241 242 243 244 245

            property var _vehicle: object

            PlanMasterController {
                id: masterController
                Component.onCompleted: startStaticActiveVehicle(object)
            }
        }
246 247
    }

248 249 250 251 252 253 254 255 256 257 258
    MapItemView {
        model: mainIsMap ? _missionController.directionArrows : undefined

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

259 260 261
    // Allow custom builds to add map items
    CustomMapItems {
        map:            flightMap
262
        largeMapView:   mainIsMap
263 264
    }

265 266
    GeoFenceMapVisuals {
        map:                    flightMap
267
        myGeoFenceController:   _geoFenceController
268
        interactive:            false
269
        planView:               false
Gus Grubba's avatar
Gus Grubba committed
270
        homePosition:           activeVehicle && activeVehicle.homePosition.isValid ? activeVehicle.homePosition :  QtPositioning.coordinate()
271 272
    }

273 274
    // Rally points on map
    MapItemView {
275
        model: _rallyPointController.points
276 277 278

        delegate: MapQuickItem {
            id:             itemIndicator
279 280
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
281 282 283 284 285 286 287 288 289 290
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

DonLakeFlyer's avatar
DonLakeFlyer committed
291 292
    // Camera trigger points
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
293
        model: activeVehicle ? activeVehicle.cameraTriggerPoints : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
294 295 296 297 298 299 300

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

301
    // GoTo Location visuals
Don Gagne's avatar
Don Gagne committed
302
    MapQuickItem {
303 304
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
305
        z:              QGroundControl.zOrderMapItems
306 307
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
308
        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
309 310
            checked:    true
            index:      -1
311
            label:      qsTr("Go here", "Go to location waypoint")
Don Gagne's avatar
Don Gagne committed
312
        }
313

Gus Grubba's avatar
Gus Grubba committed
314
        property bool inGotoFlightMode: activeVehicle ? activeVehicle.flightMode === activeVehicle.gotoFlightMode : false
315 316

        onInGotoFlightModeChanged: {
317
            if (!inGotoFlightMode && gotoLocationItem.visible) {
318
                // Hide goto indicator when vehicle falls out of guided mode
319
                gotoLocationItem.visible = false
320 321 322
            }
        }

Gus Grubba's avatar
Gus Grubba committed
323 324 325 326
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
327
                    gotoLocationItem.visible = false
Gus Grubba's avatar
Gus Grubba committed
328
                }
329 330 331
            }
        }

332 333 334 335
        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }
336

337 338 339 340
        function hide() {
            gotoLocationItem.visible = false
        }

341 342 343 344 345 346 347 348
        function actionConfirmed() {
            // We leave the indicator visible. The handling for onInGuidedModeChanged will hide it.
        }

        function actionCancelled() {
            hide()
        }
    }
349

350
    // Orbit editing visuals
351
    QGCMapCircleVisuals {
352 353 354 355
        id:             orbitMapCircle
        mapControl:     parent
        mapCircle:      _mapCircle
        visible:        false
356

357 358
        property alias center:              _mapCircle.center
        property alias clockwiseRotation:   _mapCircle.clockwiseRotation
359 360
        readonly property real defaultRadius: 30

Gus Grubba's avatar
Gus Grubba committed
361 362 363 364
        Connections {
            target: mainWindow
            onActiveVehicleChanged: {
                if (!activeVehicle) {
365
                    orbitMapCircle.visible = false
Gus Grubba's avatar
Gus Grubba committed
366
                }
367 368 369
            }
        }

370
        function show(coord) {
DonLakeFlyer's avatar
DonLakeFlyer committed
371
            _mapCircle.radius.rawValue = defaultRadius
372 373 374 375 376 377 378 379
            orbitMapCircle.center = coord
            orbitMapCircle.visible = true
        }

        function hide() {
            orbitMapCircle.visible = false
        }

380 381 382 383 384 385 386 387 388
        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
389 390 391 392
        function radius() {
            return _mapCircle.radius.rawValue
        }

393 394 395 396 397
        Component.onCompleted: guidedActionsController.orbitMapCircle = orbitMapCircle

        QGCMapCircle {
            id:                 _mapCircle
            interactive:        true
DonLakeFlyer's avatar
DonLakeFlyer committed
398
            radius.rawValue:    30
399 400
            showRotation:       true
            clockwiseRotation:  true
401 402 403
        }
    }

404
    // Orbit telemetry visuals
405
    QGCMapCircleVisuals {
406
        id:             orbitTelemetryCircle
407
        mapControl:     parent
Gus Grubba's avatar
Gus Grubba committed
408 409
        mapCircle:      activeVehicle ? activeVehicle.orbitMapCircle : null
        visible:        activeVehicle ? activeVehicle.orbitActive : false
410 411
    }

412 413 414 415
    MapQuickItem {
        id:             orbitCenterIndicator
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Gus Grubba's avatar
Gus Grubba committed
416
        coordinate:     activeVehicle ? activeVehicle.orbitMapCircle.center : QtPositioning.coordinate()
417 418 419 420 421 422 423 424 425
        visible:        orbitTelemetryCircle.visible

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

Don Gagne's avatar
Don Gagne committed
426
    // Handle guided mode clicks
427 428
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
429

430
        QGCMenu {
DonLakeFlyer's avatar
DonLakeFlyer committed
431 432
            id: clickMenu

433 434
            property var coord

435
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
436 437 438 439
                text:           qsTr("Go to location")
                visible:        guidedActionsController.showGotoLocation

                onTriggered: {
440 441
                    gotoLocationItem.show(clickMenu.coord)
                    orbitMapCircle.hide()
442
                    guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickMenu.coord, gotoLocationItem)
DonLakeFlyer's avatar
DonLakeFlyer committed
443 444 445
                }
            }

446
            QGCMenuItem {
DonLakeFlyer's avatar
DonLakeFlyer committed
447 448
                text:           qsTr("Orbit at location")
                visible:        guidedActionsController.showOrbit
449

DonLakeFlyer's avatar
DonLakeFlyer committed
450
                onTriggered: {
451 452
                    orbitMapCircle.show(clickMenu.coord)
                    gotoLocationItem.hide()
453
                    guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickMenu.coord, orbitMapCircle)
DonLakeFlyer's avatar
DonLakeFlyer committed
454 455 456 457
                }
            }
        }

Don Gagne's avatar
Don Gagne committed
458
        onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
459 460
            if (guidedActionsController.guidedUIVisible || (!guidedActionsController.showGotoLocation && !guidedActionsController.showOrbit)) {
                return
Gus Grubba's avatar
Gus Grubba committed
461
            }
462 463 464
            orbitMapCircle.hide()
            gotoLocationItem.hide()
            var clickCoord = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
DonLakeFlyer's avatar
DonLakeFlyer committed
465
            if (guidedActionsController.showGotoLocation && guidedActionsController.showOrbit) {
466
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
467 468
                clickMenu.popup()
            } else if (guidedActionsController.showGotoLocation) {
DonLakeFlyer's avatar
DonLakeFlyer committed
469
                gotoLocationItem.show(clickCoord)
470
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickCoord)
DonLakeFlyer's avatar
DonLakeFlyer committed
471
            } else if (guidedActionsController.showOrbit) {
472 473
                orbitMapCircle.show(clickCoord)
                guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickCoord)
Don Gagne's avatar
Don Gagne committed
474 475
            }
        }
476
    }
477 478

    MapScale {
479
        id:                     mapScale
480
        anchors.right:          parent.right
481 482
        anchors.margins:        _toolsMargin
        anchors.topMargin:      _toolsMargin + state === "bottomMode" ? 0 : ScreenTools.toolbarHeight
483
        mapControl:             flightMap
484
        buttonsOnLeft:          false
485
        visible:                !ScreenTools.isTinyScreen && QGroundControl.corePlugin.options.enableMapScale && mainIsMap
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
        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
                }
            }
        ]
505
    }
506

507
    // Airspace overlap support
508
    MapItemView {
509
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.circles : []
510 511 512
        delegate: MapCircle {
            center:         object.center
            radius:         object.radius
513
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
514 515
            border.color:   object.lineColor
            border.width:   object.lineWidth
516 517 518 519
        }
    }

    MapItemView {
520
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.polygons : []
521 522
        delegate: MapPolygon {
            path:           object.polygon
523
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
524 525
            border.color:   object.lineColor
            border.width:   object.lineWidth
526 527
        }
    }
528

529
}