FlightDisplayViewMap.qml 16 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
    property alias  scaleState: mapScale.state

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

45 46
    property rect   centerViewport:             Qt.rect(0, 0, width, height)

47 48 49 50
    property var    _planMasterController:      planMasterController
    property var    _missionController:         _planMasterController.missionController
    property var    _geoFenceController:        _planMasterController.geoFenceController
    property var    _rallyPointController:      _planMasterController.rallyPointController
51 52 53
    property var    _activeVehicle:             QGroundControl.multiVehicleManager.activeVehicle
    property var    _activeVehicleCoordinate:   _activeVehicle ? _activeVehicle.coordinate : QtPositioning.coordinate()
    property real   _toolButtonTopMargin:       parent.height - ScreenTools.availableHeight + (ScreenTools.defaultFontPixelHeight / 2)
54
    property bool   _airspaceEnabled:           QGroundControl.airmapSupported ? QGroundControl.settingsManager.airMapSettings.enableAirMap.rawValue : false
55

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

59
    function updateAirspace() {
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*/)
65
            }
66
        }
67 68 69 70 71 72 73 74 75
    }

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

    onZoomLevelChanged: {
        QGroundControl.flightMapZoom = zoomLevel
        updateAirspace()
    }
    onCenterChanged: {
76
        QGroundControl.flightMapPosition = center
77
        updateAirspace()
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 87
        if (userPanned) {
            console.log("user panned")
            userPanned = false
            _disableVehicleTracking = true
            panRecenterTimer.restart()
        }
88 89
    }

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

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

    Timer {
145 146 147
        id:         panRecenterTimer
        interval:   10000
        running:    false
148 149

        onTriggered: {
150
            _disableVehicleTracking = false
151
            updateMapToVehiclePosition()
152 153 154
        }
    }

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

Don Gagne's avatar
Don Gagne committed
162
    QGCPalette { id: qgcPal; colorGroupEnabled: true }
163
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
164

165
    Connections {
166
        target: _missionController
167 168

        onNewItemsFromVehicle: {
169
            var visualItems = _missionController.visualItems
170
            if (visualItems && visualItems.count !== 1) {
171
                mapFitFunctions.fitMapViewportToMissionItems()
172
                firstVehiclePositionReceived = true
173 174
            }
        }
175 176
    }

177
    ExclusiveGroup {
Don Gagne's avatar
Don Gagne committed
178
        id: _mapTypeButtonsExclusiveGroup
179 180
    }

Don Gagne's avatar
Don Gagne committed
181
    MapFitFunctions {
182
        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
183 184
        map:                        _flightMap
        usePlannedHomePosition:     false
185
        planMasterController:       _planMasterController
186

Don Gagne's avatar
Don Gagne committed
187 188
        property real leftToolWidth:    toolStrip.x + toolStrip.width
    }
189

190 191
    // Add trajectory points to the map
    MapItemView {
192
        model: _mainIsMap ? _activeVehicle ? _activeVehicle.trajectoryPoints : 0 : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
193 194

        delegate: MapPolyline {
Don Gagne's avatar
Don Gagne committed
195 196
            line.width: 3
            line.color: "red"
DonLakeFlyer's avatar
DonLakeFlyer committed
197
            z:          QGroundControl.zOrderTrajectoryLines
Don Gagne's avatar
Don Gagne committed
198
            path: [
199 200
                object.coordinate1,
                object.coordinate2,
Don Gagne's avatar
Don Gagne committed
201 202
            ]
        }
203 204 205 206
    }

    // Add the vehicles to the map
    MapItemView {
207
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
208 209

        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
210 211
            vehicle:        object
            coordinate:     object.coordinate
212
            map:            flightMap
Gus Grubba's avatar
Gus Grubba committed
213
            size:           _mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
214
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
215
        }
216 217
    }

218 219
    // Add ADSB vehicles to the map
    MapItemView {
Gus Grubba's avatar
Gus Grubba committed
220
        model: _activeVehicle ? _activeVehicle.adsbVehicles : []
221 222 223 224
        property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
225
            callsign:       object.callsign
226
            heading:        object.heading
Gus Grubba's avatar
Gus Grubba committed
227
            alert:          object.alert
228 229 230 231 232
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    // 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)
            }
        }
250 251
    }

252 253 254 255 256 257
    // Allow custom builds to add map items
    CustomMapItems {
        map:            flightMap
        largeMapView:   _mainIsMap
    }

258 259
    GeoFenceMapVisuals {
        map:                    flightMap
260
        myGeoFenceController:   _geoFenceController
261
        interactive:            false
262
        planView:               false
263
        homePosition:           _activeVehicle && _activeVehicle.homePosition.isValid ? _activeVehicle.homePosition :  QtPositioning.coordinate()
264 265
    }

266 267
    // Rally points on map
    MapItemView {
268
        model: _rallyPointController.points
269 270 271

        delegate: MapQuickItem {
            id:             itemIndicator
272 273
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
274 275 276 277 278 279 280 281 282 283
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

DonLakeFlyer's avatar
DonLakeFlyer committed
284 285 286 287 288 289 290 291 292 293
    // Camera trigger points
    MapItemView {
        model: _activeVehicle ? _activeVehicle.cameraTriggerPoints : 0

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

Don Gagne's avatar
Don Gagne committed
294
    MapQuickItem {
295 296
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
297
        z:              QGroundControl.zOrderMapItems
298 299
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
300 301

        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
302 303 304
            checked:    true
            index:      -1
            label:      qsTr("Goto here", "Goto here waypoint")
Don Gagne's avatar
Don Gagne committed
305
        }
306

307 308 309 310
        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }
311

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        function hide() {
            gotoLocationItem.visible = false
        }
    }

    QGCMapCircleVisuals {
        id:         orbitMapCircle
        mapControl: parent
        mapCircle:  _mapCircle
        visible:    false

        property alias center:  _mapCircle.center
        property real radius:   defaultRadius

        readonly property real defaultRadius: 30

        function show(coord) {
            orbitMapCircle.radius = defaultRadius
            orbitMapCircle.center = coord
            orbitMapCircle.visible = true
        }

        function hide() {
            orbitMapCircle.visible = false
        }

        Component.onCompleted: guidedActionsController.orbitMapCircle = orbitMapCircle

        QGCMapCircle {
            id:                 _mapCircle
            interactive:        true
            radius.rawValue:    orbitMapCircle.radius
344 345 346
        }
    }

Don Gagne's avatar
Don Gagne committed
347
    // Handle guided mode clicks
348 349
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
350

DonLakeFlyer's avatar
DonLakeFlyer committed
351 352 353
        Menu {
            id: clickMenu

354 355
            property var coord

DonLakeFlyer's avatar
DonLakeFlyer committed
356 357 358 359 360
            MenuItem {
                text:           qsTr("Go to location")
                visible:        guidedActionsController.showGotoLocation

                onTriggered: {
361 362 363
                    gotoLocationItem.show(clickMenu.coord)
                    orbitMapCircle.hide()
                    guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickMenu.coord)
DonLakeFlyer's avatar
DonLakeFlyer committed
364 365 366 367 368 369
                }
            }

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

DonLakeFlyer's avatar
DonLakeFlyer committed
371
                onTriggered: {
372 373 374
                    orbitMapCircle.show(clickMenu.coord)
                    gotoLocationItem.hide()
                    guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickMenu.coord)
DonLakeFlyer's avatar
DonLakeFlyer committed
375 376 377 378
                }
            }
        }

Don Gagne's avatar
Don Gagne committed
379
        onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
380 381
            if (guidedActionsController.guidedUIVisible || (!guidedActionsController.showGotoLocation && !guidedActionsController.showOrbit)) {
                return
382 383 384 385
            }            
            orbitMapCircle.hide()
            gotoLocationItem.hide()
            var clickCoord = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
DonLakeFlyer's avatar
DonLakeFlyer committed
386
            if (guidedActionsController.showGotoLocation && guidedActionsController.showOrbit) {
387
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
388 389
                clickMenu.popup()
            } else if (guidedActionsController.showGotoLocation) {
390 391
                _guidedLocationCoordinate = clickCoord
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickCoord)
DonLakeFlyer's avatar
DonLakeFlyer committed
392
            } else if (guidedActionsController.showOrbit) {
393 394
                orbitMapCircle.show(clickCoord)
                guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickCoord)
Don Gagne's avatar
Don Gagne committed
395 396
            }
        }
397
    }
398 399

    MapScale {
400
        id:                     mapScale
401
        anchors.right:          parent.right
402 403 404
        anchors.margins:        ScreenTools.defaultFontPixelHeight * (0.33)
        anchors.topMargin:      ScreenTools.defaultFontPixelHeight * (0.33) + state === "bottomMode" ? 0 : ScreenTools.toolbarHeight
        anchors.bottomMargin:   ScreenTools.defaultFontPixelHeight * (0.33)
405 406
        mapControl:             flightMap
        visible:                !ScreenTools.isTinyScreen
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        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
                }
            }
        ]
426
    }
427

428
    // Airspace overlap support
429
    MapItemView {
430
        model:              _airspaceEnabled && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.circles : []
431 432 433
        delegate: MapCircle {
            center:         object.center
            radius:         object.radius
434
            color:          Qt.rgba(0.94, 0.87, 0, 0.15)
Gus Grubba's avatar
Gus Grubba committed
435
            border.color:   Qt.rgba(1,1,1,0.85)
436 437 438 439
        }
    }

    MapItemView {
440
        model:              _airspaceEnabled && QGroundControl.airspaceManager.airspaceVisible ? QGroundControl.airspaceManager.airspaces.polygons : []
441 442
        delegate: MapPolygon {
            path:           object.polygon
443 444
            color:          Qt.rgba(0.94, 0.87, 0, 0.15)
            border.color:   Qt.rgba(1,1,1,0.85)
445 446
        }
    }
447
}