FlightDisplayViewMap.qml 20.5 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
import QGroundControl.Vehicle       1.0

27 28
import Wima 1.0

29
FlightMap {
30 31 32 33 34
    id:                         flightMap
    anchors.fill:               parent
    mapName:                    _mapName
    allowGCSLocationCenter:     !userPanned
    allowVehicleLocationCenter: !_keepVehicleCentered
35
    planView:                   false
36

37

38 39
    property alias  scaleState: mapScale.state

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

49 50
    property rect   centerViewport:             Qt.rect(0, 0, width, height)

51 52 53 54
    property var    _planMasterController:      planMasterController
    property var    _missionController:         _planMasterController.missionController
    property var    _geoFenceController:        _planMasterController.geoFenceController
    property var    _rallyPointController:      _planMasterController.rallyPointController
55 56 57
    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
58
    property bool   _airspaceEnabled:           QGroundControl.airmapSupported ? (QGroundControl.settingsManager.airMapSettings.enableAirMap.rawValue && QGroundControl.airspaceManager.connected): false
59

60 61
    property bool   _disableVehicleTracking:    false
    property bool   _keepVehicleCentered:       _mainIsMap ? false : true
62

63 64
    property bool   _wimaEnabled:               wimaController.enableWimaController.value

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

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

    onZoomLevelChanged: {
        QGroundControl.flightMapZoom = zoomLevel
79
        updateAirspace(false)
80 81
    }
    onCenterChanged: {
82
        QGroundControl.flightMapPosition = center
83
        updateAirspace(false)
84
    }
85

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

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

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

    Timer {
155 156 157
        id:         panRecenterTimer
        interval:   10000
        running:    false
158 159

        onTriggered: {
160
            _disableVehicleTracking = false
161
            updateMapToVehiclePosition()
162 163 164
        }
    }

165 166 167 168 169 170 171
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

Don Gagne's avatar
Don Gagne committed
172
    QGCPalette { id: qgcPal; colorGroupEnabled: true }
173
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
174

175
    Connections {
176
        target: _missionController
177 178

        onNewItemsFromVehicle: {
179
            var visualItems = _missionController.visualItems
180
            if (visualItems && visualItems.count !== 1) {
181 182 183
                if (recenterNeeded()){
                    mapFitFunctions.fitMapViewportToMissionItems()
                }
184
                firstVehiclePositionReceived = true
185 186
            }
        }
187 188
    }

189 190 191 192 193 194 195 196 197 198 199 200 201
    Connections {
        target: _wimaController

        onVisualItemsChanged: {
            var visualItems = _wimaController.visualItems
            if (visualItems && visualItems.count > 0) {
                if (recenterNeeded()){
                    mapFitFunctions.fitMapViewportToAreas()
                }
            }
        }
    }

202
    ExclusiveGroup {
Don Gagne's avatar
Don Gagne committed
203
        id: _mapTypeButtonsExclusiveGroup
204 205
    }

Don Gagne's avatar
Don Gagne committed
206
    MapFitFunctions {
207
        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
208 209
        map:                        _flightMap
        usePlannedHomePosition:     false
210
        planMasterController:       _planMasterController
211

Don Gagne's avatar
Don Gagne committed
212
        property real leftToolWidth:    toolStrip.x + toolStrip.width
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

        function fitMapViewportToAreas() {
            if (!_wimaController.visualItems) {
                // Being called prior to controller.start
                return
            }

            var coordList = [ ]
            for (var i = 0; i < _wimaController.visualItems.count; i++){
                var area = _wimaController.visualItems.get(i)
                for (var j = 0; j < area.path.length; ++j){
                    var vertex = area.path[j]
                    coordList.push(vertex)
                }
            }
            fitMapViewportToAllCoordinates(coordList)
        }
Don Gagne's avatar
Don Gagne committed
230
    }
231

232
    // Add wima Areas to the Map
233
    Repeater {
234 235
        property bool _enableWima: wimaController.enableWimaController.value
        model: _enableWima ? wimaController.visualItems : 0
236 237 238 239
        delegate: WimaMapVisual{
            map:               flightMap
            qgcView:           flightMap.qgcView
            visible: true
240
        }
241 242
    }

243
    // Add missionItems generated by wima planer to the map
Valentin Platzgummer's avatar
Valentin Platzgummer committed
244
    // all Items
245 246 247 248 249
    WimaPlanMapItems {
        map:            flightMap
        largeMapView:   _mainIsMap
        missionItems:   wimaController.missionItems
        path:           wimaController.waypointPath
250
        showItems:      _wimaEnabled
251 252
        zOrderWP:       QGroundControl.zOrderWimaAllWaypointIndicators
        zOrderLines:    QGroundControl.zOrderWimaAllWaypointLines
253
        color:          "green"
254 255
    }

256 257
    // Add trajectory points to the map
    MapItemView {
258
        model: _mainIsMap ? _activeVehicle ? _activeVehicle.trajectoryPoints : 0 : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
259 260

        delegate: MapPolyline {
Don Gagne's avatar
Don Gagne committed
261 262
            line.width: 3
            line.color: "red"
DonLakeFlyer's avatar
DonLakeFlyer committed
263
            z:          QGroundControl.zOrderTrajectoryLines
Don Gagne's avatar
Don Gagne committed
264
            path: [
265 266
                object.coordinate1,
                object.coordinate2,
Don Gagne's avatar
Don Gagne committed
267 268
            ]
        }
269 270 271 272
    }

    // Add the vehicles to the map
    MapItemView {
273
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
274 275

        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
276 277
            vehicle:        object
            coordinate:     object.coordinate
278
            map:            flightMap
Gus Grubba's avatar
Gus Grubba committed
279
            size:           _mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
280
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
281
        }
282 283
    }

284 285
    // Add ADSB vehicles to the map
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
286
        model: _activeVehicle ? _activeVehicle.adsbVehicles : []
287 288 289 290
        property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
291
            callsign:       object.callsign
292
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
293
            alert:          object.alert
294 295 296 297 298
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    // 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)
            }
        }
316 317
    }

318 319 320 321 322 323
    // Allow custom builds to add map items
    CustomMapItems {
        map:            flightMap
        largeMapView:   _mainIsMap
    }

324 325
    GeoFenceMapVisuals {
        map:                    flightMap
326
        myGeoFenceController:   _geoFenceController
327
        interactive:            false
328
        planView:               false
329
        homePosition:           _activeVehicle && _activeVehicle.homePosition.isValid ? _activeVehicle.homePosition :  QtPositioning.coordinate()
330 331
    }

332 333
    // Rally points on map
    MapItemView {
334
        model: _rallyPointController.points
335 336 337

        delegate: MapQuickItem {
            id:             itemIndicator
338 339
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
340 341 342 343 344 345 346 347 348 349
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

DonLakeFlyer's avatar
DonLakeFlyer committed
350 351 352 353 354 355 356 357 358 359
    // Camera trigger points
    MapItemView {
        model: _activeVehicle ? _activeVehicle.cameraTriggerPoints : 0

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

360
    // GoTo Location visuals
Don Gagne's avatar
Don Gagne committed
361
    MapQuickItem {
362 363
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
364
        z:              QGroundControl.zOrderMapItems
365 366
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
367 368

        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
369 370 371
            checked:    true
            index:      -1
            label:      qsTr("Goto here", "Goto here waypoint")
Don Gagne's avatar
Don Gagne committed
372
        }
373

374
        property bool inGotoFlightMode: _activeVehicle ? _activeVehicle.flightMode === _activeVehicle.gotoFlightMode : false
375
        property var activeVehicle: _activeVehicle
376 377 378 379 380 381 382 383

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

384 385 386 387 388 389
        onActiveVehicleChanged: {
            if (!_activeVehicle) {
                visible = false
            }
        }

390 391 392 393
        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }
394

395 396 397 398
        function hide() {
            gotoLocationItem.visible = false
        }

399 400 401 402 403 404 405 406
        function actionConfirmed() {
            // We leave the indicator visible. The handling for onInGuidedModeChanged will hide it.
        }

        function actionCancelled() {
            hide()
        }
    }
407

408
    // Orbit editing visuals
409
    QGCMapCircleVisuals {
410 411 412 413
        id:             orbitMapCircle
        mapControl:     parent
        mapCircle:      _mapCircle
        visible:        false
414

415 416
        property alias center:              _mapCircle.center
        property alias clockwiseRotation:   _mapCircle.clockwiseRotation
417
        property var   activeVehicle:       _activeVehicle
418 419 420

        readonly property real defaultRadius: 30

421 422 423 424 425 426
        onActiveVehicleChanged: {
            if (!_activeVehicle) {
                visible = false
            }
        }

427
        function show(coord) {
DonLakeFlyer's avatar
DonLakeFlyer committed
428
            _mapCircle.radius.rawValue = defaultRadius
429 430 431 432 433 434 435 436
            orbitMapCircle.center = coord
            orbitMapCircle.visible = true
        }

        function hide() {
            orbitMapCircle.visible = false
        }

437 438 439 440 441 442 443 444 445
        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
446 447 448 449
        function radius() {
            return _mapCircle.radius.rawValue
        }

450 451 452 453 454
        Component.onCompleted: guidedActionsController.orbitMapCircle = orbitMapCircle

        QGCMapCircle {
            id:                 _mapCircle
            interactive:        true
DonLakeFlyer's avatar
DonLakeFlyer committed
455
            radius.rawValue:    30
456 457
            showRotation:       true
            clockwiseRotation:  true
458 459 460
        }
    }

461
    // Orbit telemetry visuals
462
    QGCMapCircleVisuals {
463
        id:             orbitTelemetryCircle
464
        mapControl:     parent
Gus Grubba's avatar
Gus Grubba committed
465
        mapCircle:      _activeVehicle ? _activeVehicle.orbitMapCircle : null
466 467 468
        visible:        _activeVehicle ? _activeVehicle.orbitActive : false
    }

469 470 471 472
    MapQuickItem {
        id:             orbitCenterIndicator
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
473
        coordinate:     _activeVehicle ? _activeVehicle.orbitMapCircle.center : QtPositioning.coordinate()
474 475 476 477 478 479 480 481 482
        visible:        orbitTelemetryCircle.visible

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

Don Gagne's avatar
Don Gagne committed
483
    // Handle guided mode clicks
484 485
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
486

DonLakeFlyer's avatar
DonLakeFlyer committed
487 488 489
        Menu {
            id: clickMenu

490 491
            property var coord

DonLakeFlyer's avatar
DonLakeFlyer committed
492 493 494 495 496
            MenuItem {
                text:           qsTr("Go to location")
                visible:        guidedActionsController.showGotoLocation

                onTriggered: {
497 498
                    gotoLocationItem.show(clickMenu.coord)
                    orbitMapCircle.hide()
499
                    guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickMenu.coord, gotoLocationItem)
DonLakeFlyer's avatar
DonLakeFlyer committed
500 501 502 503 504 505
                }
            }

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

DonLakeFlyer's avatar
DonLakeFlyer committed
507
                onTriggered: {
508 509
                    orbitMapCircle.show(clickMenu.coord)
                    gotoLocationItem.hide()
510
                    guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickMenu.coord, orbitMapCircle)
DonLakeFlyer's avatar
DonLakeFlyer committed
511 512 513 514
                }
            }
        }

Don Gagne's avatar
Don Gagne committed
515
        onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
516 517
            if (guidedActionsController.guidedUIVisible || (!guidedActionsController.showGotoLocation && !guidedActionsController.showOrbit)) {
                return
Gus Grubba's avatar
Gus Grubba committed
518
            }
519 520 521
            orbitMapCircle.hide()
            gotoLocationItem.hide()
            var clickCoord = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
DonLakeFlyer's avatar
DonLakeFlyer committed
522
            if (guidedActionsController.showGotoLocation && guidedActionsController.showOrbit) {
523
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
524 525
                clickMenu.popup()
            } else if (guidedActionsController.showGotoLocation) {
DonLakeFlyer's avatar
DonLakeFlyer committed
526
                gotoLocationItem.show(clickCoord)
527
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickCoord)
DonLakeFlyer's avatar
DonLakeFlyer committed
528
            } else if (guidedActionsController.showOrbit) {
529 530
                orbitMapCircle.show(clickCoord)
                guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickCoord)
Don Gagne's avatar
Don Gagne committed
531 532
            }
        }
533
    }
534 535

    MapScale {
536
        id:                     mapScale
537
        anchors.right:          parent.right
538 539 540
        anchors.margins:        ScreenTools.defaultFontPixelHeight * (0.33)
        anchors.topMargin:      ScreenTools.defaultFontPixelHeight * (0.33) + state === "bottomMode" ? 0 : ScreenTools.toolbarHeight
        anchors.bottomMargin:   ScreenTools.defaultFontPixelHeight * (0.33)
541 542
        mapControl:             flightMap
        visible:                !ScreenTools.isTinyScreen
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        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
                }
            }
        ]
562
    }
563

564
    // Airspace overlap support
565
    MapItemView {
566
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.circles : []
567 568 569
        delegate: MapCircle {
            center:         object.center
            radius:         object.radius
570
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
571 572
            border.color:   object.lineColor
            border.width:   object.lineWidth
573 574 575 576
        }
    }

    MapItemView {
577
        model:              _airspaceEnabled && QGroundControl.settingsManager.airMapSettings.enableAirspace && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.polygons : []
578 579
        delegate: MapPolygon {
            path:           object.polygon
580
            color:          object.color
Gus Grubba's avatar
Gus Grubba committed
581 582
            border.color:   object.lineColor
            border.width:   object.lineWidth
583 584
        }
    }
585

586
}