FlightDisplayViewMap.qml 14.4 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10


11 12 13 14 15
import QtQuick          2.3
import QtQuick.Controls 1.2
import QtLocation       5.3
import QtPositioning    5.3
import QtQuick.Dialogs  1.2
16 17 18 19 20 21 22 23 24 25 26

import QGroundControl               1.0
import QGroundControl.FlightDisplay 1.0
import QGroundControl.FlightMap     1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Controls      1.0
import QGroundControl.Palette       1.0
import QGroundControl.Vehicle       1.0
import QGroundControl.Controllers   1.0

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

34 35
    property alias  scaleState: mapScale.state

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

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

46 47 48 49
    property var    _planMasterController:      planMasterController
    property var    _missionController:         _planMasterController.missionController
    property var    _geoFenceController:        _planMasterController.geoFenceController
    property var    _rallyPointController:      _planMasterController.rallyPointController
50 51 52
    property var    _activeVehicle:             QGroundControl.multiVehicleManager.activeVehicle
    property var    _activeVehicleCoordinate:   _activeVehicle ? _activeVehicle.coordinate : QtPositioning.coordinate()
    property real   _toolButtonTopMargin:       parent.height - ScreenTools.availableHeight + (ScreenTools.defaultFontPixelHeight / 2)
53

54 55
    property bool   _disableVehicleTracking:    false
    property bool   _keepVehicleCentered:       _mainIsMap ? false : true
56

57
    // Track last known map position and zoom from Fly view in settings
58
    onZoomLevelChanged: QGroundControl.flightMapZoom = zoomLevel
59
    onCenterChanged:    QGroundControl.flightMapPosition = center
60

61
    // When the user pans the map we stop responding to vehicle coordinate updates until the panRecenterTimer fires
62
    onUserPannedChanged: {
DonLakeFlyer's avatar
DonLakeFlyer committed
63 64 65 66 67 68
        if (userPanned) {
            console.log("user panned")
            userPanned = false
            _disableVehicleTracking = true
            panRecenterTimer.restart()
        }
69 70
    }

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    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() {
101
        var vehiclePoint = flightMap.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
102 103 104 105 106 107 108
        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)
109
        return !pointInRect(vehiclePoint, centerViewport)
110 111 112
    }

    function updateMapToVehiclePosition() {
113 114
        // We let FlightMap handle first vehicle position
        if (firstVehiclePositionReceived && _activeVehicleCoordinate.isValid && !_disableVehicleTracking) {
115
            if (_keepVehicleCentered) {
116
                flightMap.center = _activeVehicleCoordinate
117
            } else {
DonLakeFlyer's avatar
DonLakeFlyer committed
118
                if (firstVehiclePositionReceived && recenterNeeded()) {
119
                    animatedMapRecenter(flightMap.center, _activeVehicleCoordinate)
120 121
                }
            }
122 123 124 125
        }
    }

    Timer {
126 127 128
        id:         panRecenterTimer
        interval:   10000
        running:    false
129 130

        onTriggered: {
131
            _disableVehicleTracking = false
132
            updateMapToVehiclePosition()
133 134 135
        }
    }

136 137 138 139 140 141 142
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

Don Gagne's avatar
Don Gagne committed
143
    QGCPalette { id: qgcPal; colorGroupEnabled: true }
144
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
145

146
    Connections {
147
        target: _missionController
148 149

        onNewItemsFromVehicle: {
150
            var visualItems = _missionController.visualItems
151
            if (visualItems && visualItems.count !== 1) {
152
                mapFitFunctions.fitMapViewportToMissionItems()
153
                firstVehiclePositionReceived = true
154 155
            }
        }
156 157
    }

158
    ExclusiveGroup {
Don Gagne's avatar
Don Gagne committed
159
        id: _mapTypeButtonsExclusiveGroup
160 161
    }

Don Gagne's avatar
Don Gagne committed
162
    MapFitFunctions {
163
        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
164 165
        map:                        _flightMap
        usePlannedHomePosition:     false
166
        planMasterController:       _planMasterController
167

Don Gagne's avatar
Don Gagne committed
168 169
        property real leftToolWidth:    toolStrip.x + toolStrip.width
    }
170

171 172
    // Add trajectory points to the map
    MapItemView {
173
        model: _mainIsMap ? _activeVehicle ? _activeVehicle.trajectoryPoints : 0 : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
174 175

        delegate: MapPolyline {
Don Gagne's avatar
Don Gagne committed
176 177
            line.width: 3
            line.color: "red"
DonLakeFlyer's avatar
DonLakeFlyer committed
178
            z:          QGroundControl.zOrderTrajectoryLines
Don Gagne's avatar
Don Gagne committed
179
            path: [
180 181
                object.coordinate1,
                object.coordinate2,
Don Gagne's avatar
Don Gagne committed
182 183
            ]
        }
184 185 186 187
    }

    // Add the vehicles to the map
    MapItemView {
188
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
189 190

        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
191 192
            vehicle:        object
            coordinate:     object.coordinate
193
            map:            flightMap
Gus Grubba's avatar
Gus Grubba committed
194
            size:           _mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
195
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
196
        }
197 198
    }

199 200 201 202 203 204 205 206 207
    // Add ADSB vehicles to the map
    MapItemView {
        model: _activeVehicle ? _activeVehicle.adsbVehicles : 0

        property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle

        delegate: VehicleMapItem {
            coordinate:     object.coordinate
            altitude:       object.altitude
208
            callsign:       object.callsign
209 210 211 212 213 214
            heading:        object.heading
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    // 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)
            }
        }
232 233
    }

234 235 236 237 238 239
    // Allow custom builds to add map items
    CustomMapItems {
        map:            flightMap
        largeMapView:   _mainIsMap
    }

240 241
    GeoFenceMapVisuals {
        map:                    flightMap
242
        myGeoFenceController:   _geoFenceController
243
        interactive:            false
244
        planView:               false
245
        homePosition:           _activeVehicle && _activeVehicle.homePosition.isValid ? _activeVehicle.homePosition :  QtPositioning.coordinate()
246 247
    }

248 249
    // Rally points on map
    MapItemView {
250
        model: _rallyPointController.points
251 252 253

        delegate: MapQuickItem {
            id:             itemIndicator
254 255
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
256 257 258 259 260 261 262 263 264 265
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

DonLakeFlyer's avatar
DonLakeFlyer committed
266 267 268 269 270 271 272 273 274 275
    // Camera trigger points
    MapItemView {
        model: _activeVehicle ? _activeVehicle.cameraTriggerPoints : 0

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

Don Gagne's avatar
Don Gagne committed
276
    MapQuickItem {
277 278
        id:             gotoLocationItem
        visible:        false
Don Gagne's avatar
Don Gagne committed
279
        z:              QGroundControl.zOrderMapItems
280 281
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
282 283

        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
284 285
            checked:    true
            index:      -1
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
            label:      qsTr("Goto here", "Goto here waypoint")
        }

        function show(coord) {
            gotoLocationItem.coordinate = coord
            gotoLocationItem.visible = true
        }

        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
Don Gagne's avatar
Don Gagne committed
326
        }
327
    }
328

Don Gagne's avatar
Don Gagne committed
329
    // Handle guided mode clicks
330 331
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
332

DonLakeFlyer's avatar
DonLakeFlyer committed
333 334 335
        Menu {
            id: clickMenu

336 337
            property var coord

DonLakeFlyer's avatar
DonLakeFlyer committed
338 339 340 341 342
            MenuItem {
                text:           qsTr("Go to location")
                visible:        guidedActionsController.showGotoLocation

                onTriggered: {
343 344 345
                    gotoLocationItem.show(clickMenu.coord)
                    orbitMapCircle.hide()
                    guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickMenu.coord)
DonLakeFlyer's avatar
DonLakeFlyer committed
346 347 348 349 350 351
                }
            }

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

DonLakeFlyer's avatar
DonLakeFlyer committed
353
                onTriggered: {
354 355 356
                    orbitMapCircle.show(clickMenu.coord)
                    gotoLocationItem.hide()
                    guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickMenu.coord)
DonLakeFlyer's avatar
DonLakeFlyer committed
357 358 359 360
                }
            }
        }

Don Gagne's avatar
Don Gagne committed
361
        onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
362 363
            if (guidedActionsController.guidedUIVisible || (!guidedActionsController.showGotoLocation && !guidedActionsController.showOrbit)) {
                return
364 365 366 367
            }            
            orbitMapCircle.hide()
            gotoLocationItem.hide()
            var clickCoord = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
DonLakeFlyer's avatar
DonLakeFlyer committed
368
            if (guidedActionsController.showGotoLocation && guidedActionsController.showOrbit) {
369
                clickMenu.coord = clickCoord
DonLakeFlyer's avatar
DonLakeFlyer committed
370 371
                clickMenu.popup()
            } else if (guidedActionsController.showGotoLocation) {
372 373
                _guidedLocationCoordinate = clickCoord
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, clickCoord)
DonLakeFlyer's avatar
DonLakeFlyer committed
374
            } else if (guidedActionsController.showOrbit) {
375 376
                orbitMapCircle.show(clickCoord)
                guidedActionsController.confirmAction(guidedActionsController.actionOrbit, clickCoord)
Don Gagne's avatar
Don Gagne committed
377 378
            }
        }
379
    }
380 381

    MapScale {
382
        id:                     mapScale
383
        anchors.right:          parent.right
384 385 386
        anchors.margins:        ScreenTools.defaultFontPixelHeight * (0.33)
        anchors.topMargin:      ScreenTools.defaultFontPixelHeight * (0.33) + state === "bottomMode" ? 0 : ScreenTools.toolbarHeight
        anchors.bottomMargin:   ScreenTools.defaultFontPixelHeight * (0.33)
387 388
        mapControl:             flightMap
        visible:                !ScreenTools.isTinyScreen
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
        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
                }
            }
        ]
408
    }
409
}