FlightDisplayViewMap.qml 19.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.3
import QtQuick.Controls 1.2
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 32
    id:                         flightMap
    anchors.fill:               parent
    mapName:                    _mapName
    allowGCSLocationCenter:     !userPanned
    allowVehicleLocationCenter: !_keepVehicleCentered
33
    planView:                   false
34

35

36 37
    property alias  scaleState: mapScale.state

38
    // The following properties must be set by the consumer
39
    property var    planMasterController
40
    property var    wimaController
41
    property var    guidedActionsController
Don Gagne's avatar
Don Gagne committed
42
    property var    flightWidgets
43
    property var    rightPanelWidth
44
    property var    qgcView                             ///< QGCView control which contains this map
45
    property var    multiVehicleView                    ///< true: multi-vehicle view, false: single vehicle view
Don Gagne's avatar
Don Gagne committed
46

47 48
    property rect   centerViewport:             Qt.rect(0, 0, width, height)

49 50 51 52
    property var    _planMasterController:      planMasterController
    property var    _missionController:         _planMasterController.missionController
    property var    _geoFenceController:        _planMasterController.geoFenceController
    property var    _rallyPointController:      _planMasterController.rallyPointController
53 54 55
    property var    _activeVehicle:             QGroundControl.multiVehicleManager.activeVehicle
    property var    _activeVehicleCoordinate:   _activeVehicle ? _activeVehicle.coordinate : QtPositioning.coordinate()
    property real   _toolButtonTopMargin:       parent.height - ScreenTools.availableHeight + (ScreenTools.defaultFontPixelHeight / 2)
Gus Grubba's avatar
Gus Grubba committed
56
    property bool   _airspaceEnabled:           QGroundControl.airmapSupported ? (QGroundControl.settingsManager.airMapSettings.enableAirMap.rawValue && QGroundControl.airspaceManager.connected): false
57

58 59
    property bool   _disableVehicleTracking:    false
    property bool   _keepVehicleCentered:       _mainIsMap ? false : true
60

61
    function updateAirspace(reset) {
62
        if(_airspaceEnabled) {
63 64 65
            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) {
66
                QGroundControl.airspaceManager.setROI(coordinateNW, coordinateSE, false /*planView*/, reset)
67
            }
68
        }
69 70 71 72 73 74
    }

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

    onZoomLevelChanged: {
        QGroundControl.flightMapZoom = zoomLevel
75
        updateAirspace(false)
76 77
    }
    onCenterChanged: {
78
        QGroundControl.flightMapPosition = center
79
        updateAirspace(false)
80
    }
81

82
    // When the user pans the map we stop responding to vehicle coordinate updates until the panRecenterTimer fires
83
    onUserPannedChanged: {
DonLakeFlyer's avatar
DonLakeFlyer committed
84 85 86 87 88 89
        if (userPanned) {
            console.log("user panned")
            userPanned = false
            _disableVehicleTracking = true
            panRecenterTimer.restart()
        }
90 91
    }

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

    function updateMapToVehiclePosition() {
138 139
        // We let FlightMap handle first vehicle position
        if (firstVehiclePositionReceived && _activeVehicleCoordinate.isValid && !_disableVehicleTracking) {
140
            if (_keepVehicleCentered) {
141
                flightMap.center = _activeVehicleCoordinate
142
            } else {
DonLakeFlyer's avatar
DonLakeFlyer committed
143
                if (firstVehiclePositionReceived && recenterNeeded()) {
144
                    animatedMapRecenter(flightMap.center, _activeVehicleCoordinate)
145 146
                }
            }
147 148 149 150
        }
    }

    Timer {
151 152 153
        id:         panRecenterTimer
        interval:   10000
        running:    false
154 155

        onTriggered: {
156
            _disableVehicleTracking = false
157
            updateMapToVehiclePosition()
158 159 160
        }
    }

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

Don Gagne's avatar
Don Gagne committed
168
    QGCPalette { id: qgcPal; colorGroupEnabled: true }
169
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
170

171
    Connections {
172
        target: _missionController
173 174

        onNewItemsFromVehicle: {
175
            var visualItems = _missionController.visualItems
176
            if (visualItems && visualItems.count !== 1) {
177
                mapFitFunctions.fitMapViewportToMissionItems()
178
                firstVehiclePositionReceived = true
179 180
            }
        }
181 182
    }

183
    ExclusiveGroup {
Don Gagne's avatar
Don Gagne committed
184
        id: _mapTypeButtonsExclusiveGroup
185 186
    }

Don Gagne's avatar
Don Gagne committed
187
    MapFitFunctions {
188
        id:                         mapFitFunctions // The name for this id cannot be changed without breaking references outside of this code. Beware!
Don Gagne's avatar
Don Gagne committed
189 190
        map:                        _flightMap
        usePlannedHomePosition:     false
191
        planMasterController:       _planMasterController
192

Don Gagne's avatar
Don Gagne committed
193 194
        property real leftToolWidth:    toolStrip.x + toolStrip.width
    }
195

196
    // Add wima Areas to the Map
197 198 199 200 201 202
    MapItemView {
        model: wimaController.visualItems

        delegate: MapPolygon{
            path:           object.path;
            border.color:   "black"
203 204 205 206
            color:          object.type === "WimaJoinedAreaData" ? "gray"
                                : object.type === "WimaServiceAreaData" ? "yellow"
                                    : object.type === "WimaMeasurementAreaData" ? "green"
                                        : "transparent"
207 208 209
            opacity:        0.25
            z:          QGroundControl.zOrderTrajectoryLines-1
        }
210 211
    }

212 213 214 215 216 217 218 219 220 221
    // Add mission items generated by wima planer to the map
    PlanMapItems {
        map:                flightMap
        largeMapView:       _mainIsMap
        masterController:   masterController
        isActiveVehicle:    _vehicle.active

        property var _vehicle: object
    }

222 223
    // Add trajectory points to the map
    MapItemView {
224
        model: _mainIsMap ? _activeVehicle ? _activeVehicle.trajectoryPoints : 0 : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
225 226

        delegate: MapPolyline {
Don Gagne's avatar
Don Gagne committed
227 228
            line.width: 3
            line.color: "red"
DonLakeFlyer's avatar
DonLakeFlyer committed
229
            z:          QGroundControl.zOrderTrajectoryLines
Don Gagne's avatar
Don Gagne committed
230
            path: [
231 232
                object.coordinate1,
                object.coordinate2,
Don Gagne's avatar
Don Gagne committed
233 234
            ]
        }
235 236 237 238
    }

    // Add the vehicles to the map
    MapItemView {
239
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
240 241

        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
242 243
            vehicle:        object
            coordinate:     object.coordinate
244
            map:            flightMap
Gus Grubba's avatar
Gus Grubba committed
245
            size:           _mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
246
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
247
        }
248 249
    }

250 251
    // Add ADSB vehicles to the map
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
252
        model: _activeVehicle ? _activeVehicle.adsbVehicles : []
253 254 255 256
        property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
257
            callsign:       object.callsign
258
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
259
            alert:          object.alert
260 261 262 263 264
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    // Add the items associated with each vehicles flight plan to the map
    Repeater {
        model: QGroundControl.multiVehicleManager.vehicles

        PlanMapItems {
            map:                flightMap
            largeMapView:       _mainIsMap
            masterController:   masterController
            isActiveVehicle:    _vehicle.active

            property var _vehicle: object

            PlanMasterController {
                id: masterController
                Component.onCompleted: startStaticActiveVehicle(object)
            }
        }
282 283
    }

284 285 286 287 288 289
    // Allow custom builds to add map items
    CustomMapItems {
        map:            flightMap
        largeMapView:   _mainIsMap
    }

290 291
    GeoFenceMapVisuals {
        map:                    flightMap
292
        myGeoFenceController:   _geoFenceController
293
        interactive:            false
294
        planView:               false
295
        homePosition:           _activeVehicle && _activeVehicle.homePosition.isValid ? _activeVehicle.homePosition :  QtPositioning.coordinate()
296 297
    }

298 299
    // Rally points on map
    MapItemView {
300
        model: _rallyPointController.points
301 302 303

        delegate: MapQuickItem {
            id:             itemIndicator
304 305
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
306 307 308 309 310 311 312 313 314 315
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

DonLakeFlyer's avatar
DonLakeFlyer committed
316 317 318 319 320 321 322 323 324 325
    // Camera trigger points
    MapItemView {
        model: _activeVehicle ? _activeVehicle.cameraTriggerPoints : 0

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

326
    // GoTo Location visuals
Don Gagne's avatar
Don Gagne committed
327
    MapQuickItem {
328 329
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
330
        z:              QGroundControl.zOrderMapItems
331 332
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
333 334

        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
335 336 337
            checked:    true
            index:      -1
            label:      qsTr("Goto here", "Goto here waypoint")
Don Gagne's avatar
Don Gagne committed
338
        }
339

340
        property bool inGotoFlightMode: _activeVehicle ? _activeVehicle.flightMode === _activeVehicle.gotoFlightMode : false
341
        property var activeVehicle: _activeVehicle
342 343 344 345 346 347 348 349

        onInGotoFlightModeChanged: {
            if (!inGotoFlightMode && visible) {
                // Hide goto indicator when vehicle falls out of guided mode
                visible = false
            }
        }

350 351 352 353 354 355
        onActiveVehicleChanged: {
            if (!_activeVehicle) {
                visible = false
            }
        }

356 357 358 359
        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }
360

361 362 363 364
        function hide() {
            gotoLocationItem.visible = false
        }

365 366 367 368 369 370 371 372
        function actionConfirmed() {
            // We leave the indicator visible. The handling for onInGuidedModeChanged will hide it.
        }

        function actionCancelled() {
            hide()
        }
    }
373

374
    // Orbit editing visuals
375
    QGCMapCircleVisuals {
376 377 378 379
        id:             orbitMapCircle
        mapControl:     parent
        mapCircle:      _mapCircle
        visible:        false
380

381 382
        property alias center:              _mapCircle.center
        property alias clockwiseRotation:   _mapCircle.clockwiseRotation
383
        property var   activeVehicle:       _activeVehicle
384 385 386

        readonly property real defaultRadius: 30

387 388 389 390 391 392
        onActiveVehicleChanged: {
            if (!_activeVehicle) {
                visible = false
            }
        }

393
        function show(coord) {
DonLakeFlyer's avatar
DonLakeFlyer committed
394
            _mapCircle.radius.rawValue = defaultRadius
395 396 397 398 399 400 401 402
            orbitMapCircle.center = coord
            orbitMapCircle.visible = true
        }

        function hide() {
            orbitMapCircle.visible = false
        }

403 404 405 406 407 408 409 410 411
        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
412 413 414 415
        function radius() {
            return _mapCircle.radius.rawValue
        }

416 417 418 419 420
        Component.onCompleted: guidedActionsController.orbitMapCircle = orbitMapCircle

        QGCMapCircle {
            id:                 _mapCircle
            interactive:        true
DonLakeFlyer's avatar
DonLakeFlyer committed
421
            radius.rawValue:    30
422 423
            showRotation:       true
            clockwiseRotation:  true
424 425 426
        }
    }

427
    // Orbit telemetry visuals
428
    QGCMapCircleVisuals {
429
        id:             orbitTelemetryCircle
430
        mapControl:     parent
Gus Grubba's avatar
Gus Grubba committed
431
        mapCircle:      _activeVehicle ? _activeVehicle.orbitMapCircle : null
432 433 434
        visible:        _activeVehicle ? _activeVehicle.orbitActive : false
    }

435 436 437 438
    MapQuickItem {
        id:             orbitCenterIndicator
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
439
        coordinate:     _activeVehicle ? _activeVehicle.orbitMapCircle.center : QtPositioning.coordinate()
440 441 442 443 444 445 446 447 448
        visible:        orbitTelemetryCircle.visible

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

Don Gagne's avatar
Don Gagne committed
449
    // Handle guided mode clicks
450 451
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
452

DonLakeFlyer's avatar
DonLakeFlyer committed
453 454 455
        Menu {
            id: clickMenu

456 457
            property var coord

DonLakeFlyer's avatar
DonLakeFlyer committed
458 459 460 461 462
            MenuItem {
                text:           qsTr("Go to location")
                visible:        guidedActionsController.showGotoLocation

                onTriggered: {
463 464
                    gotoLocationItem.show(clickMenu.coord)
                    orbitMapCircle.hide()
465
                    guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickMenu.coord, gotoLocationItem)
DonLakeFlyer's avatar
DonLakeFlyer committed
466 467 468 469 470 471
                }
            }

            MenuItem {
                text:           qsTr("Orbit at location")
                visible:        guidedActionsController.showOrbit
472

DonLakeFlyer's avatar
DonLakeFlyer committed
473
                onTriggered: {
474 475
                    orbitMapCircle.show(clickMenu.coord)
                    gotoLocationItem.hide()
476
                    guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickMenu.coord, orbitMapCircle)
DonLakeFlyer's avatar
DonLakeFlyer committed
477 478 479 480
                }
            }
        }

Don Gagne's avatar
Don Gagne committed
481
        onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
482 483
            if (guidedActionsController.guidedUIVisible || (!guidedActionsController.showGotoLocation && !guidedActionsController.showOrbit)) {
                return
Gus Grubba's avatar
Gus Grubba committed
484
            }
485 486 487
            orbitMapCircle.hide()
            gotoLocationItem.hide()
            var clickCoord = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
DonLakeFlyer's avatar
DonLakeFlyer committed
488
            if (guidedActionsController.showGotoLocation && guidedActionsController.showOrbit) {
489
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
490 491
                clickMenu.popup()
            } else if (guidedActionsController.showGotoLocation) {
DonLakeFlyer's avatar
DonLakeFlyer committed
492
                gotoLocationItem.show(clickCoord)
493
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickCoord)
DonLakeFlyer's avatar
DonLakeFlyer committed
494
            } else if (guidedActionsController.showOrbit) {
495 496
                orbitMapCircle.show(clickCoord)
                guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickCoord)
Don Gagne's avatar
Don Gagne committed
497 498
            }
        }
499
    }
500 501

    MapScale {
502
        id:                     mapScale
503
        anchors.right:          parent.right
504 505 506
        anchors.margins:        ScreenTools.defaultFontPixelHeight * (0.33)
        anchors.topMargin:      ScreenTools.defaultFontPixelHeight * (0.33) + state === "bottomMode" ? 0 : ScreenTools.toolbarHeight
        anchors.bottomMargin:   ScreenTools.defaultFontPixelHeight * (0.33)
507 508
        mapControl:             flightMap
        visible:                !ScreenTools.isTinyScreen
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
        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
                }
            }
        ]
528
    }
529

530
    // Airspace overlap support
531
    MapItemView {
532
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.circles : []
533 534 535
        delegate: MapCircle {
            center:         object.center
            radius:         object.radius
536
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
537 538
            border.color:   object.lineColor
            border.width:   object.lineWidth
539 540 541 542
        }
    }

    MapItemView {
543
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.polygons : []
544 545
        delegate: MapPolygon {
            path:           object.polygon
546
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
547 548
            border.color:   object.lineColor
            border.width:   object.lineWidth
549 550
        }
    }
551

552
}