FlightDisplayViewMap.qml 18.7 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 36 37 38
    onVisibleChanged: {
        // I don't know what is causing this to become invisible when a connection is dropped
        if(!visible) visible = true
    }

39 40
    property alias  scaleState: mapScale.state

41
    // The following properties must be set by the consumer
42
    property var    guidedActionsController
Don Gagne's avatar
Don Gagne committed
43
    property var    flightWidgets
44
    property var    rightPanelWidth
45
    property var    multiVehicleView                    ///< true: multi-vehicle view, false: single vehicle view
46
    property var    missionController:          null
Don Gagne's avatar
Don Gagne committed
47

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

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

56
    property bool   _disableVehicleTracking:    false
57
    property bool   _keepVehicleCentered:       mainIsMap ? false : true
58

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

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

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

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

Gus Grubba's avatar
Gus Grubba committed
89
    on_AirspaceEnabledChanged: {
90
        updateAirspace(true)
Gus Grubba's avatar
Gus Grubba committed
91 92
    }

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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() {
123
        var vehiclePoint = flightMap.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
124 125
        var toolStripRightEdge = mapFromItem(toolStrip, toolStrip.x, 0).x + toolStrip.width
        var instrumentsWidth = 0
126
        if (QGroundControl.corePlugin.options.instrumentWidget && QGroundControl.corePlugin.options.instrumentWidget.widgetPosition === CustomInstrumentWidget.POS_TOP_RIGHT) {
127 128 129 130
            // Assume standard instruments
            instrumentsWidth = flightDisplayViewWidgets.getPreferredInstrumentWidth()
        }
        var centerViewport = Qt.rect(toolStripRightEdge, 0, width - toolStripRightEdge - instrumentsWidth, height)
131
        return !pointInRect(vehiclePoint, centerViewport)
132 133 134
    }

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

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

157 158 159 160 161 162 163
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

164
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
165

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

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

186 187
    // Add trajectory points to the map
    MapItemView {
188
        model: mainIsMap ? activeVehicle ? activeVehicle.trajectoryPoints : 0 : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
189
        delegate: MapPolyline {
Don Gagne's avatar
Don Gagne committed
190 191
            line.width: 3
            line.color: "red"
DonLakeFlyer's avatar
DonLakeFlyer committed
192
            z:          QGroundControl.zOrderTrajectoryLines
Don Gagne's avatar
Don Gagne committed
193
            path: [
194 195
                object.coordinate1,
                object.coordinate2,
Don Gagne's avatar
Don Gagne committed
196 197
            ]
        }
198 199 200 201
    }

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

212 213
    // Add ADSB vehicles to the map
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
214 215
        model: activeVehicle ? activeVehicle.adsbVehicles : []
        property var activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
216 217 218
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
219
            callsign:       object.callsign
220
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
221
            alert:          object.alert
222 223 224 225 226
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

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

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

            property var _vehicle: object

            PlanMasterController {
                id: masterController
                Component.onCompleted: startStaticActiveVehicle(object)
            }
        }
244 245
    }

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

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

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

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

271 272
    // Rally points on map
    MapItemView {
273
        model: _rallyPointController.points
274 275 276

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

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

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

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

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

Gus Grubba's avatar
Gus Grubba committed
312
        property bool inGotoFlightMode: activeVehicle ? activeVehicle.flightMode === activeVehicle.gotoFlightMode : false
313 314 315 316 317 318 319 320

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

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

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

335 336 337 338
        function hide() {
            gotoLocationItem.visible = false
        }

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

        function actionCancelled() {
            hide()
        }
    }
347

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

355 356
        property alias center:              _mapCircle.center
        property alias clockwiseRotation:   _mapCircle.clockwiseRotation
357 358
        readonly property real defaultRadius: 30

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

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

        function hide() {
            orbitMapCircle.visible = false
        }

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

391 392 393 394 395
        Component.onCompleted: guidedActionsController.orbitMapCircle = orbitMapCircle

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

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

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

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

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

428
        QGCMenu {
DonLakeFlyer's avatar
DonLakeFlyer committed
429 430
            id: clickMenu

431 432
            property var coord

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

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

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

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

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

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

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

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

528
}