FlightMap.qml 6.28 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed
9

10 11 12 13
import QtQuick          2.3
import QtQuick.Controls 1.2
import QtLocation       5.3
import QtPositioning    5.3
14
import QtQuick.Dialogs  1.2
dogmaphobic's avatar
dogmaphobic committed
15

16
import QGroundControl                       1.0
dogmaphobic's avatar
dogmaphobic committed
17
import QGroundControl.FactSystem            1.0
18
import QGroundControl.Controls              1.0
19
import QGroundControl.FlightMap             1.0
20 21 22
import QGroundControl.ScreenTools           1.0
import QGroundControl.MultiVehicleManager   1.0
import QGroundControl.Vehicle               1.0
23
import QGroundControl.QGCPositionManager    1.0
dogmaphobic's avatar
dogmaphobic committed
24

25 26
Map {
    id: _map
dogmaphobic's avatar
dogmaphobic committed
27

28 29
    //-- Qt 5.9 has rotation gesture enabled by default. Here we limit the possible gestures.
    gesture.acceptedGestures:   MapGestureArea.PinchGesture | MapGestureArea.PanGesture | MapGestureArea.FlickGesture
30 31 32
    gesture.flickDeceleration:  3000
    plugin:                     Plugin { name: "QGroundControl" }

33 34
    property string mapName:                        'defaultMap'
    property bool   isSatelliteMap:                 activeMapType.name.indexOf("Satellite") > -1 || activeMapType.name.indexOf("Hybrid") > -1
35
    property var    gcsPosition:                    QGroundControl.qgcPositionManger.gcsPosition
36
    property real   gcsHeading:                     QGroundControl.qgcPositionManger.gcsHeading
37 38 39 40 41
    property bool   userPanned:                     false   ///< true: the user has manually panned the map
    property bool   allowGCSLocationCenter:         false   ///< true: map will center/zoom to gcs location one time
    property bool   allowVehicleLocationCenter:     false   ///< true: map will center/zoom to vehicle location one time
    property bool   firstGCSPositionReceived:       false   ///< true: first gcs position update was responded to
    property bool   firstVehiclePositionReceived:   false   ///< true: first vehicle position update was responded to
42
    property bool   planView:                       false   ///< true: map being using for Plan view, items should be draggable
43

dogmaphobic's avatar
dogmaphobic committed
44 45
    readonly property real  maxZoomLevel: 20

Gus Grubba's avatar
Gus Grubba committed
46
    property var    activeVehicleCoordinate:        activeVehicle ? activeVehicle.coordinate : QtPositioning.coordinate()
47

48
    function setVisibleRegion(region) {
49
        // TODO: Is this still necessary with Qt 5.11?
50 51 52 53 54 55 56
        // This works around a bug on Qt where if you set a visibleRegion and then the user moves or zooms the map
        // and then you set the same visibleRegion the map will not move/scale appropriately since it thinks there
        // is nothing to do.
        _map.visibleRegion = QtPositioning.rectangle(QtPositioning.coordinate(0, 0), QtPositioning.coordinate(0, 0))
        _map.visibleRegion = region
    }

57 58 59 60 61 62 63 64
    function _possiblyCenterToVehiclePosition() {
        if (!firstVehiclePositionReceived && allowVehicleLocationCenter && activeVehicleCoordinate.isValid) {
            firstVehiclePositionReceived = true
            center = activeVehicleCoordinate
            zoomLevel = QGroundControl.flightMapInitialZoom
        }
    }

65
    function centerToSpecifiedLocation() {
66
        mainWindow.showComponentDialog(specifyMapPositionDialog, qsTr("Specify Position"), mainWindow.showDialogDefaultWidth, StandardButton.Close)
67 68 69 70 71 72 73 74 75 76
    }

    Component {
        id: specifyMapPositionDialog
        EditPositionDialog {
            coordinate:             center
            onCoordinateChanged:    center = coordinate
        }
    }

77 78 79 80
    // Center map to gcs location
    onGcsPositionChanged: {
        if (gcsPosition.isValid && allowGCSLocationCenter && !firstGCSPositionReceived && !firstVehiclePositionReceived) {
            firstGCSPositionReceived = true
81 82 83 84
            //-- Only center on gsc if we have no vehicle (and we are supposed to do so)
            var activeVehicleCoordinate = activeVehicle ? activeVehicle.coordinate : QtPositioning.coordinate()
            if(QGroundControl.settingsManager.flyViewSettings.keepMapCenteredOnVehicle.rawValue || !activeVehicleCoordinate.isValid)
                center = gcsPosition
85 86
        }
    }
87

88 89 90 91 92 93 94 95
    // We track whether the user has panned or not to correctly handle automatic map positioning
    Connections {
        target: gesture

        onPanFinished:      userPanned = true
        onFlickFinished:    userPanned = true
    }

96
    function updateActiveMapType() {
97
        var settings =  QGroundControl.settingsManager.flightMapSettings
98
        var fullMapName = settings.mapProvider.value + " " + settings.mapType.value
99

Don Gagne's avatar
Don Gagne committed
100
        for (var i = 0; i < _map.supportedMapTypes.length; i++) {
101
            if (fullMapName === _map.supportedMapTypes[i].name) {
Don Gagne's avatar
Don Gagne committed
102
                _map.activeMapType = _map.supportedMapTypes[i]
103
                return
Don Gagne's avatar
Don Gagne committed
104 105 106
            }
        }
    }
107

108 109 110 111 112 113
    onActiveVehicleCoordinateChanged: _possiblyCenterToVehiclePosition()

    Component.onCompleted: {
        updateActiveMapType()
        _possiblyCenterToVehiclePosition()
    }
114 115

    Connections {
116 117 118 119 120 121 122
        target:             QGroundControl.settingsManager.flightMapSettings.mapType
        onRawValueChanged:  updateActiveMapType()
    }

    Connections {
        target:             QGroundControl.settingsManager.flightMapSettings.mapProvider
        onRawValueChanged:  updateActiveMapType()
123 124
    }

125
    /// Ground Station location
126
    MapQuickItem {
127 128
        anchorPoint.x:  sourceItem.width / 2
        anchorPoint.y:  sourceItem.height / 2
129 130
        visible:        gcsPosition.isValid
        coordinate:     gcsPosition
131 132

        sourceItem: Image {
133 134
            id:             mapItemImage
            source:         isNaN(gcsHeading) ? "/res/QGCLogoFull" : "/res/QGCLogoArrow"
135 136 137
            mipmap:         true
            antialiasing:   true
            fillMode:       Image.PreserveAspectFit
138
            height:         ScreenTools.defaultFontPixelHeight * (isNaN(gcsHeading) ? 1.75 : 2.5 )
139
            sourceSize.height: height
140 141 142 143 144
            transform: Rotation {
                origin.x:       mapItemImage.width  / 2
                origin.y:       mapItemImage.height / 2
                angle:          isNaN(gcsHeading) ? 0 : gcsHeading
            }
145 146
        }
    }
147
} // Map