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 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 62
    property bool   _wimaEnabled:               wimaController.enableWimaController.value

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

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

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

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

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

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

    Timer {
153 154 155
        id:         panRecenterTimer
        interval:   10000
        running:    false
156 157

        onTriggered: {
158
            _disableVehicleTracking = false
159
            updateMapToVehiclePosition()
160 161 162
        }
    }

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

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

173
    Connections {
174
        target: _missionController
175 176

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

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

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

200
    ExclusiveGroup {
Don Gagne's avatar
Don Gagne committed
201
        id: _mapTypeButtonsExclusiveGroup
202 203
    }

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

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

        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)
                }
            }
            console.log(coordList)
            fitMapViewportToAllCoordinates(coordList)
        }
Don Gagne's avatar
Don Gagne committed
229
    }
230

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        function actionCancelled() {
            hide()
        }
    }
406

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

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

        readonly property real defaultRadius: 30

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

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

        function hide() {
            orbitMapCircle.visible = false
        }

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

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

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

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

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

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

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

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

489 490
            property var coord

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

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

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

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

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

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

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

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

585
}