FlightDisplayViewMap.qml 11.6 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 53
    property var    _activeVehicle:             QGroundControl.multiVehicleManager.activeVehicle
    property var    _activeVehicleCoordinate:   _activeVehicle ? _activeVehicle.coordinate : QtPositioning.coordinate()
    property var    _gotoHereCoordinate:        QtPositioning.coordinate()
    property real   _toolButtonTopMargin:       parent.height - ScreenTools.availableHeight + (ScreenTools.defaultFontPixelHeight / 2)
54

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

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

62
    // When the user pans the map we stop responding to vehicle coordinate updates until the panRecenterTimer fires
63
    onUserPannedChanged: {
DonLakeFlyer's avatar
DonLakeFlyer committed
64 65 66 67 68 69
        if (userPanned) {
            console.log("user panned")
            userPanned = false
            _disableVehicleTracking = true
            panRecenterTimer.restart()
        }
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 101
    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() {
102
        var vehiclePoint = flightMap.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
103 104
        var centerViewport = Qt.rect(0, 0, width, height)
        return !pointInRect(vehiclePoint, centerViewport)
105 106 107
    }

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

    Timer {
121 122 123
        id:         panRecenterTimer
        interval:   10000
        running:    false
124 125

        onTriggered: {
126
            _disableVehicleTracking = false
127
            updateMapToVehiclePosition()
128 129 130
        }
    }

131 132 133 134 135 136 137
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

Don Gagne's avatar
Don Gagne committed
138
    QGCPalette { id: qgcPal; colorGroupEnabled: true }
139
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
140

141
    Connections {
142
        target: _missionController
143 144

        onNewItemsFromVehicle: {
145
            var visualItems = _missionController.visualItems
146 147
            if (visualItems && visualItems.count != 1) {
                mapFitFunctions.fitMapViewportToMissionItems()
148
                firstVehiclePositionReceived = true
149 150
            }
        }
151 152
    }

153
    ExclusiveGroup {
Don Gagne's avatar
Don Gagne committed
154
        id: _mapTypeButtonsExclusiveGroup
155 156
    }

Don Gagne's avatar
Don Gagne committed
157
    MapFitFunctions {
158
        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
159 160
        map:                        _flightMap
        usePlannedHomePosition:     false
161
        planMasterController:       _planMasterController
162

Don Gagne's avatar
Don Gagne committed
163 164
        property real leftToolWidth:    toolStrip.x + toolStrip.width
    }
165

166 167
    // Add trajectory points to the map
    MapItemView {
168
        model: _mainIsMap ? _activeVehicle ? _activeVehicle.trajectoryPoints : 0 : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
169 170

        delegate: MapPolyline {
Don Gagne's avatar
Don Gagne committed
171 172
            line.width: 3
            line.color: "red"
DonLakeFlyer's avatar
DonLakeFlyer committed
173
            z:          QGroundControl.zOrderTrajectoryLines
Don Gagne's avatar
Don Gagne committed
174
            path: [
175 176
                object.coordinate1,
                object.coordinate2,
Don Gagne's avatar
Don Gagne committed
177 178
            ]
        }
179 180 181 182
    }

    // Add the vehicles to the map
    MapItemView {
183
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
184 185

        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
186 187
            vehicle:        object
            coordinate:     object.coordinate
188
            map:            flightMap
Gus Grubba's avatar
Gus Grubba committed
189
            size:           _mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
190
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
191
        }
192 193
    }

194 195 196 197 198 199 200 201 202
    // 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
203
            callsign:       object.callsign
204 205 206 207 208 209
            heading:        object.heading
            map:            flightMap
            z:              QGroundControl.zOrderVehicles
        }
    }

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    // 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)
            }
        }
227 228
    }

229 230 231 232 233 234
    // Allow custom builds to add map items
    CustomMapItems {
        map:            flightMap
        largeMapView:   _mainIsMap
    }

235 236
    GeoFenceMapVisuals {
        map:                    flightMap
237
        myGeoFenceController:   _geoFenceController
238
        interactive:            false
239
        planView:               false
240
        homePosition:           _activeVehicle && _activeVehicle.homePosition.isValid ? _activeVehicle.homePosition : undefined
241 242
    }

243 244
    // Rally points on map
    MapItemView {
245
        model: _rallyPointController.points
246 247 248

        delegate: MapQuickItem {
            id:             itemIndicator
249 250
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
251 252 253 254 255 256 257 258 259 260
            coordinate:     object.coordinate
            z:              QGroundControl.zOrderMapItems

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

Don Gagne's avatar
Don Gagne committed
261 262 263
    // GoTo here waypoint
    MapQuickItem {
        coordinate:     _gotoHereCoordinate
264
        visible:        _activeVehicle && _activeVehicle.guidedModeSupported && _gotoHereCoordinate.isValid
Don Gagne's avatar
Don Gagne committed
265
        z:              QGroundControl.zOrderMapItems
266 267
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
268 269

        sourceItem: MissionItemIndexLabel {
DonLakeFlyer's avatar
DonLakeFlyer committed
270 271 272
            checked:    true
            index:      -1
            label:      qsTr("Goto here", "Goto here waypoint")
Don Gagne's avatar
Don Gagne committed
273
        }
274 275
    }    

276
    // Camera trigger points
277
    MapItemView {
278
        model: _activeVehicle ? _activeVehicle.cameraTriggerPoints : 0
279 280 281 282 283 284 285

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

Don Gagne's avatar
Don Gagne committed
286
    // Handle guided mode clicks
287 288
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
289 290

        onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
291
            if (guidedActionsController.showGotoLocation && !guidedActionsController.guidedUIVisible) {
292 293
                _gotoHereCoordinate = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, _gotoHereCoordinate)
Don Gagne's avatar
Don Gagne committed
294 295
            }
        }
296
    }
297 298

    MapScale {
299
        id:                     mapScale
300
        anchors.right:          parent.right
301 302 303
        anchors.margins:        ScreenTools.defaultFontPixelHeight * (0.33)
        anchors.topMargin:      ScreenTools.defaultFontPixelHeight * (0.33) + state === "bottomMode" ? 0 : ScreenTools.toolbarHeight
        anchors.bottomMargin:   ScreenTools.defaultFontPixelHeight * (0.33)
304 305
        mapControl:             flightMap
        visible:                !ScreenTools.isTinyScreen
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        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
                }
            }
        ]
325
    }
326
}