FlightDisplayViewMap.qml 11.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.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

33 34
    property alias  scaleState: mapScale.state

35 36
    property var    missionController
    property var    guidedActionsController
Don Gagne's avatar
Don Gagne committed
37
    property var    flightWidgets
38
    property var    rightPanelWidth
39
    property var    qgcView                             ///< QGCView control which contains this map
Don Gagne's avatar
Don Gagne committed
40

41 42 43 44
    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)
45

46 47
    property bool   _disableVehicleTracking:    false
    property bool   _keepVehicleCentered:       _mainIsMap ? false : true
48

49
    // Track last known map position and zoom from Fly view in settings
50
    onZoomLevelChanged: QGroundControl.flightMapZoom = zoomLevel
51
    onCenterChanged:    QGroundControl.flightMapPosition = center
52

53
    // When the user pans the map we stop responding to vehicle coordinate updates until the panRecenterTimer fires
54 55 56
    onUserPannedChanged: {
        _disableVehicleTracking = true
        panRecenterTimer.start()
57 58
    }

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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() {
89
        var vehiclePoint = flightMap.fromCoordinate(_activeVehicleCoordinate, false /* clipToViewport */)
90 91
        var centerViewport = Qt.rect(0, 0, width, height)
        return !pointInRect(vehiclePoint, centerViewport)
92 93 94
    }

    function updateMapToVehiclePosition() {
95 96
        // We let FlightMap handle first vehicle position
        if (firstVehiclePositionReceived && _activeVehicleCoordinate.isValid && !_disableVehicleTracking) {
97
            if (_keepVehicleCentered) {
98
                flightMap.center = _activeVehicleCoordinate
99
            } else {
DonLakeFlyer's avatar
DonLakeFlyer committed
100
                if (firstVehiclePositionReceived && recenterNeeded()) {
101
                    animatedMapRecenter(flightMap.center, _activeVehicleCoordinate)
102 103
                }
            }
104 105 106 107
        }
    }

    Timer {
108 109 110
        id:         panRecenterTimer
        interval:   10000
        running:    false
111 112

        onTriggered: {
113
            _disableVehicleTracking = false
114
            updateMapToVehiclePosition()
115 116 117
        }
    }

118 119 120 121 122 123 124
    Timer {
        interval:       500
        running:        true
        repeat:         true
        onTriggered:    updateMapToVehiclePosition()
    }

Don Gagne's avatar
Don Gagne committed
125
    QGCPalette { id: qgcPal; colorGroupEnabled: true }
126
    QGCMapPalette { id: mapPal; lightColors: isSatelliteMap }
Don Gagne's avatar
Don Gagne committed
127

128 129
    Connections {
        target: missionController
130 131

        onNewItemsFromVehicle: {
132
            var visualItems = missionController.visualItems
133 134
            if (visualItems && visualItems.count != 1) {
                mapFitFunctions.fitMapViewportToMissionItems()
135
                firstVehiclePositionReceived = true
136 137
            }
        }
138 139
    }

140
    GeoFenceController {
141
        id: geoFenceController
142 143 144
        Component.onCompleted: start(false /* editMode */)
    }

145 146 147 148 149
    RallyPointController {
        id: rallyPointController
        Component.onCompleted: start(false /* editMode */)
    }

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    // The following code is used to track vehicle states such that we prompt to remove mission from vehicle when mission completes

    property bool vehicleArmed:                 _activeVehicle ? _activeVehicle.armed : false
    property bool vehicleWasArmed:              false
    property bool vehicleInMissionFlightMode:   _activeVehicle ? (_activeVehicle.flightMode === _activeVehicle.missionFlightMode) : false
    property bool promptForMissionRemove:       false

    onVehicleArmedChanged: {
        if (vehicleArmed) {
            if (!promptForMissionRemove) {
                promptForMissionRemove = vehicleInMissionFlightMode
                vehicleWasArmed = true
            }
        } else {
            if (promptForMissionRemove && (missionController.containsItems || geoFenceController.containsItems || rallyPointController.containsItems)) {
                qgcView.showDialog(removeMissionDialogComponent, qsTr("Flight complete"), showDialogDefaultWidth, StandardButton.No | StandardButton.Yes)
            }
            promptForMissionRemove = false
        }
    }

    onVehicleInMissionFlightModeChanged: {
        if (!promptForMissionRemove && vehicleArmed) {
            promptForMissionRemove = true
        }
    }

    Component {
        id: removeMissionDialogComponent

        QGCViewMessage {
            message: qsTr("Do you want to remove the mission from the vehicle?")

            function accept() {
                missionController.removeAllFromVehicle()
                geoFenceController.removeAllFromVehicle()
                rallyPointController.removeAllFromVehicle()
                hideDialog()

            }
        }
    }

193
    ExclusiveGroup {
Don Gagne's avatar
Don Gagne committed
194
        id: _mapTypeButtonsExclusiveGroup
195 196
    }

Don Gagne's avatar
Don Gagne committed
197 198 199 200 201 202 203
    MapFitFunctions {
        id:                         mapFitFunctions
        map:                        _flightMap
        usePlannedHomePosition:     false
        mapMissionController:      missionController
        mapGeoFenceController:     geoFenceController
        mapRallyPointController:   rallyPointController
204

Don Gagne's avatar
Don Gagne committed
205 206
        property real leftToolWidth:    toolStrip.x + toolStrip.width
    }
207

208 209
    // Add trajectory points to the map
    MapItemView {
210
        model: _mainIsMap ? _activeVehicle ? _activeVehicle.trajectoryPoints : 0 : 0
DonLakeFlyer's avatar
DonLakeFlyer committed
211 212

        delegate: MapPolyline {
Don Gagne's avatar
Don Gagne committed
213 214
            line.width: 3
            line.color: "red"
DonLakeFlyer's avatar
DonLakeFlyer committed
215
            z:          QGroundControl.zOrderTrajectoryLines
Don Gagne's avatar
Don Gagne committed
216
            path: [
217 218
                object.coordinate1,
                object.coordinate2,
Don Gagne's avatar
Don Gagne committed
219 220
            ]
        }
221 222 223 224
    }

    // Add the vehicles to the map
    MapItemView {
225
        model: QGroundControl.multiVehicleManager.vehicles
DonLakeFlyer's avatar
DonLakeFlyer committed
226 227

        delegate: VehicleMapItem {
Don Gagne's avatar
Don Gagne committed
228 229 230
            vehicle:        object
            coordinate:     object.coordinate
            isSatellite:    flightMap.isSatelliteMap
Gus Grubba's avatar
Gus Grubba committed
231
            size:           _mainIsMap ? ScreenTools.defaultFontPixelHeight * 3 : ScreenTools.defaultFontPixelHeight
DonLakeFlyer's avatar
DonLakeFlyer committed
232
            z:              QGroundControl.zOrderVehicles
Don Gagne's avatar
Don Gagne committed
233
        }
234 235
    }

236 237
    // Add the mission item visuals to the map
    Repeater {
238
        model: _mainIsMap ? missionController.visualItems : 0
239 240

        delegate: MissionItemMapVisual {
241
            map:        flightMap
242
            onClicked:  guidedActionsController.confirmAction(guidedActionsController.actionSetWaypoint, object.sequenceNumber)
243
        }
244 245 246 247
    }

    // Add lines between waypoints
    MissionLineView {
DonLakeFlyer's avatar
DonLakeFlyer committed
248
        model:  _mainIsMap ? missionController.waypointLines : 0
249 250
    }

251 252 253 254
    GeoFenceMapVisuals {
        map:                    flightMap
        myGeoFenceController:   geoFenceController
        interactive:            false
255
        homePosition:           _activeVehicle && _activeVehicle.homePosition.isValid ? _activeVehicle.homePosition : undefined
256 257
    }

258 259 260 261 262 263
    // Rally points on map
    MapItemView {
        model: rallyPointController.points

        delegate: MapQuickItem {
            id:             itemIndicator
264 265
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
266 267 268 269 270 271 272 273 274 275
            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
276 277 278
    // GoTo here waypoint
    MapQuickItem {
        coordinate:     _gotoHereCoordinate
Don Gagne's avatar
Don Gagne committed
279
        visible:        _activeVehicle && _activeVehicle.guidedMode && _gotoHereCoordinate.isValid
Don Gagne's avatar
Don Gagne committed
280
        z:              QGroundControl.zOrderMapItems
281 282
        anchorPoint.x:  sourceItem.anchorPointX
        anchorPoint.y:  sourceItem.anchorPointY
Don Gagne's avatar
Don Gagne committed
283 284

        sourceItem: MissionItemIndexLabel {
285 286
            checked: true
            label:   qsTr("G", "Goto here waypoint") // second string is translator's hint.
Don Gagne's avatar
Don Gagne committed
287
        }
288 289
    }    

Don Gagne's avatar
Don Gagne committed
290
    // Handle guided mode clicks
291 292
    MouseArea {
        anchors.fill: parent
Don Gagne's avatar
Don Gagne committed
293 294

        onClicked: {
295 296 297
            if (guidedActionsController.showGotoLocation) {
                _gotoHereCoordinate = flightMap.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
                guidedActionsController.confirmAction(guidedActionsController.actionGoto, _gotoHereCoordinate)
Don Gagne's avatar
Don Gagne committed
298 299
            }
        }
300
    }
301 302

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