FlightMap.qml 16.9 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.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed
9 10 11 12 13 14 15 16


/**
 * @file
 *   @brief QGC Map Background
 *   @author Gus Grubba <mavlink@grubba.com>
 */

17
import QtQuick          2.4
18
import QtQuick.Controls 1.3
19 20
import QtLocation       5.3
import QtPositioning    5.3
dogmaphobic's avatar
dogmaphobic committed
21

22
import QGroundControl                       1.0
dogmaphobic's avatar
dogmaphobic committed
23
import QGroundControl.FactSystem            1.0
24
import QGroundControl.Controls              1.0
25
import QGroundControl.FlightMap             1.0
26 27 28
import QGroundControl.ScreenTools           1.0
import QGroundControl.MultiVehicleManager   1.0
import QGroundControl.Vehicle               1.0
29
import QGroundControl.Mavlink               1.0
dogmaphobic's avatar
dogmaphobic committed
30

31 32
Map {
    id: _map
dogmaphobic's avatar
dogmaphobic committed
33

34
    property string mapName:            'defaultMap'
35
    property bool   isSatelliteMap:     activeMapType.name.indexOf("Satellite") > -1 || activeMapType.name.indexOf("Hybrid") > -1
36

dogmaphobic's avatar
dogmaphobic committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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
    readonly property real  maxZoomLevel: 20
    property variant        scaleLengths: [5, 10, 25, 50, 100, 150, 250, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000]

    function formatDistance(meters)
    {
        var dist = Math.round(meters)
        if (dist > 1000 ){
            if (dist > 100000){
                dist = Math.round(dist / 1000)
            }
            else{
                dist = Math.round(dist / 100)
                dist = dist / 10
            }
            dist = dist + " km"
        }
        else{
            dist = dist + " m"
        }
        return dist
    }

    function calculateScale() {
        var coord1, coord2, dist, text, f
        f = 0
        coord1 = _map.toCoordinate(Qt.point(0, scale.y))
        coord2 = _map.toCoordinate(Qt.point(0 + scaleImage.sourceSize.width, scale.y))
        dist = Math.round(coord1.distanceTo(coord2))
        if (dist === 0) {
            // not visible
        } else {
            for (var i = 0; i < scaleLengths.length - 1; i++) {
                if (dist < (scaleLengths[i] + scaleLengths[i+1]) / 2 ) {
                    f = scaleLengths[i] / dist
                    dist = scaleLengths[i]
                    break;
                }
            }
            if (f === 0) {
                f = dist / scaleLengths[i]
                dist = scaleLengths[i]
            }
        }
        text = formatDistance(dist)
        scaleImage.width = (scaleImage.sourceSize.width * f) - 2 * scaleImageLeft.sourceSize.width
        scaleText.text = text
    }
84

85 86 87 88 89 90 91 92
    function setVisibleRegion(region) {
        // 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
    }

93
    zoomLevel:                  18
94
    center:                     QGroundControl.lastKnownHomePosition
95
    gesture.flickDeceleration:  3000
96

97
    plugin: Plugin { name: "QGroundControl" }
98

Don Gagne's avatar
Don Gagne committed
99
    ExclusiveGroup { id: mapTypeGroup }
100

101
    property bool _initialMapPositionSet: false
dogmaphobic's avatar
dogmaphobic committed
102

103 104 105 106 107
    Connections {
        target: mainWindow
        onGcsPositionChanged: {
            if (!_initialMapPositionSet) {
                _initialMapPositionSet = true
Don Gagne's avatar
Don Gagne committed
108
                center = mainWindow.gcsPosition
109 110 111 112
            }
        }
    }

113 114
    function updateActiveMapType() {
        var fullMapName = QGroundControl.flightMapSettings.mapProvider + " " + QGroundControl.flightMapSettings.mapType
Don Gagne's avatar
Don Gagne committed
115
        for (var i = 0; i < _map.supportedMapTypes.length; i++) {
116
            if (fullMapName === _map.supportedMapTypes[i].name) {
Don Gagne's avatar
Don Gagne committed
117
                _map.activeMapType = _map.supportedMapTypes[i]
118
                return
Don Gagne's avatar
Don Gagne committed
119 120 121
            }
        }
    }
122

123 124 125 126 127 128 129
    Component.onCompleted: updateActiveMapType()

    Connections {
        target:             QGroundControl.flightMapSettings
        onMapTypeChanged:   updateActiveMapType()
    }

130
    /// Ground Station location
131 132 133 134 135
    MapQuickItem {
        anchorPoint.x:  sourceItem.width  / 2
        anchorPoint.y:  sourceItem.height / 2
        visible:        mainWindow.gcsPosition.isValid
        coordinate:     mainWindow.gcsPosition
dogmaphobic's avatar
dogmaphobic committed
136
        sourceItem:     MissionItemIndexLabel {
137 138 139
            label: "Q"
        }
    }
140 141 142 143 144 145 146 147 148

    //---- Polygon drawing code

    //
    // Usage:
    //
    // Connections {
    //     target: map.polygonDraw
    //
149
    //    onPolygonCaptureStarted: {
150 151 152
    //      // Polygon creation has started
    //    }
    //
153
    //    onPolygonCaptureFinished: {
154 155 156 157 158 159 160 161 162 163
    //      // Polygon capture complete, coordinates signal variable contains the polygon points
    //    }
    // }
    //
    // map.polygonDraqw.startPolgyon() - begin capturing a new polygon
    // map.polygonDraqw.endPolygon() - end capture (right-click will also end capture)

    // Not sure why this is needed, but trying to reference polygonDrawer directly from other code doesn't work
    property alias polygonDraw: polygonDrawer

164 165
    QGCMapLabel {
        id:                     polygonHelp        
166 167 168 169 170
        anchors.topMargin:      parent.height - ScreenTools.availableHeight
        anchors.top:            parent.top
        anchors.left:           parent.left
        anchors.right:          parent.right
        horizontalAlignment:    Text.AlignHCenter
171
        map:                    _map
172 173
        text:                   qsTr("Click to add point %1").arg(ScreenTools.isMobile || !polygonDrawer.polygonReady ? "" : qsTr("- Right Click to end polygon"))
        visible:                polygonDrawer.drawingPolygon
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

        Connections {
            target: polygonDrawer

            onDrawingPolygonChanged: {
                if (polygonDrawer.drawingPolygon) {
                    polygonHelp.text = qsTr("Click to add point")
                }
                polygonHelp.visible = polygonDrawer.drawingPolygon
            }

            onPolygonReadyChanged: {
                if (polygonDrawer.polygonReady && !ScreenTools.isMobile) {
                    polygonHelp.text = qsTr("Click to add point - Right Click to end polygon")
                }
            }

            onAdjustingPolygonChanged: {
                if (polygonDrawer.adjustingPolygon) {
                    polygonHelp.text = qsTr("Adjust polygon by dragging corners")
                }
                polygonHelp.visible = polygonDrawer.adjustingPolygon
            }
        }
198 199 200 201 202 203 204 205 206
    }

    MouseArea {
        id:                 polygonDrawer
        anchors.fill:       parent
        acceptedButtons:    Qt.LeftButton | Qt.RightButton
        visible:            drawingPolygon
        z:                  1000 // Hack to fix MouseArea layering for now

207 208
        property alias  drawingPolygon:     polygonDrawer.hoverEnabled
        property bool   adjustingPolygon:   false
209 210
        property bool   polygonReady:       polygonDrawerPolygonSet.path.length > 2 ///< true: enough points have been captured to create a closed polygon
        property bool   justClicked: false
211

212
        property var _callbackObject
213 214

        property var _vertexDragList: []
215 216

        /// Begin capturing a new polygon
217
        ///     polygonCaptureStarted will be signalled
218 219
        function startCapturePolygon(callback) {
            polygonDrawer._callbackObject = callback
220 221
            polygonDrawer.drawingPolygon = true
            polygonDrawer._clearPolygon()
222
            polygonDrawer._callbackObject.polygonCaptureStarted()
223 224 225
        }

        /// Finish capturing the polygon
226
        ///     polygonCaptureFinished will be signalled
227
        /// @return true: polygon completed, false: not enough points to complete polygon
228
        function finishCapturePolygon() {
229 230 231 232
            if (!polygonDrawer.polygonReady) {
                return false
            }

233
            var polygonPath = polygonDrawerPolygonSet.path
234
            _cancelCapturePolygon()
235
            polygonDrawer._callbackObject.polygonCaptureFinished(polygonPath)
236 237 238
            return true
        }

239 240
        function startAdjustPolygon(callback, vertexCoordinates) {
            polygonDraw._callbackObject = callback
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
            polygonDrawer.adjustingPolygon = true
            for (var i=0; i<vertexCoordinates.length; i++) {
                var mapItem = Qt.createQmlObject(
                            "import QtQuick                     2.5; " +
                            "import QtLocation                  5.3; " +
                            "import QGroundControl.ScreenTools  1.0; " +
                            "Rectangle {" +
                            "   id:     vertexDrag; " +
                            "   width:  _sideLength; " +
                            "   height: _sideLength; " +
                            "   color:  'red'; " +
                            "" +
                            "   property var coordinate; " +
                            "   property int index; " +
                            "" +
                            "   readonly property real _sideLength:     ScreenTools.defaultFontPixelWidth * 2; " +
                            "   readonly property real _halfSideLength: _sideLength / 2; " +
                            "" +
                            "   Drag.active:    dragMouseArea.drag.active; " +
                            "   Drag.hotSpot.x: _halfSideLength; " +
                            "   Drag.hotSpot.y: _halfSideLength; " +
                            "" +
                            "   onXChanged: updateCoordinate(); " +
                            "   onYChanged: updateCoordinate(); " +
                            "" +
                            "   function updateCoordinate() { " +
                            "       vertexDrag.coordinate = _map.toCoordinate(Qt.point(vertexDrag.x + _halfSideLength, vertexDrag.y + _halfSideLength), false); " +
268
                            "       polygonDrawer._callbackObject.polygonAdjustVertex(vertexDrag.index, vertexDrag.coordinate); " +
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                            "   } " +
                            "" +
                            "   function updatePosition() { " +
                            "       var vertexPoint = _map.fromCoordinate(coordinate, false); " +
                            "       vertexDrag.x = vertexPoint.x - _halfSideLength; " +
                            "       vertexDrag.y = vertexPoint.y - _halfSideLength; " +
                            "   } " +
                            "" +
                            "   Connections { " +
                            "       target: _map; " +
                            "       onCenterChanged: updatePosition(); " +
                            "       onZoomLevelChanged: updatePosition(); " +
                            "   } " +
                            "" +
                            "   MouseArea { " +
                            "       id:             dragMouseArea; " +
                            "       anchors.fill:   parent; " +
                            "       drag.target:    parent; " +
                            "       drag.minimumX:  0; " +
                            "       drag.minimumY:  0; " +
                            "       drag.maximumX:  _map.width - parent.width; " +
                            "       drag.maximumY:  _map.height - parent.height; " +
                            "   } " +
                            "} ",
                            _map)
                mapItem.z = QGroundControl.zOrderMapItems + 1
                mapItem.coordinate = vertexCoordinates[i]
                mapItem.index = i
                mapItem.updatePosition()
                polygonDrawer._vertexDragList.push(mapItem)
299
                polygonDrawer._callbackObject.polygonAdjustStarted()
300 301 302 303
            }
        }

        function finishAdjustPolygon() {
304 305 306 307 308 309 310 311 312 313 314
            _cancelAdjustPolygon()
            polygonDrawer._callbackObject.polygonAdjustFinished()
        }

        /// Cancels an in progress draw or adjust
        function cancelPolygonEdit() {
            _cancelAdjustPolygon()
            _cancelCapturePolygon()
        }

        function _cancelAdjustPolygon() {
315 316 317 318 319
            polygonDrawer.adjustingPolygon = false
            for (var i=0; i<polygonDrawer._vertexDragList.length; i++) {
                polygonDrawer._vertexDragList[i].destroy()
            }
            polygonDrawer._vertexDragList = []
320 321 322 323 324
        }

        function _cancelCapturePolygon() {
            polygonDrawer._clearPolygon()
            polygonDrawer.drawingPolygon = false
325 326
        }

327 328 329 330 331 332 333
        function _clearPolygon() {
            // Simpler methods to clear the path simply don't work due to bugs. This craziness does.
            var bogusCoord = _map.toCoordinate(Qt.point(height/2, width/2))
            polygonDrawerPolygon.path = [ bogusCoord, bogusCoord ]
            polygonDrawerNextPoint.path = [ bogusCoord, bogusCoord ]
            polygonDrawerPolygon.path = [ ]
            polygonDrawerNextPoint.path = [ ]
334 335
            polygonDrawerPolygonSet.path = [ bogusCoord, bogusCoord ]
            polygonDrawerPolygonSet.path = [ ]
336 337 338 339
        }

        onClicked: {
            if (mouse.button == Qt.LeftButton) {
340
                polygonDrawer.justClicked = true
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                if (polygonDrawerPolygon.path.length > 2) {
                    // Make sure the new line doesn't intersect the existing polygon
                    var lastSegment = polygonDrawerPolygon.path.length - 2
                    var newLineA = _map.fromCoordinate(polygonDrawerPolygon.path[lastSegment], false /* clipToViewPort */)
                    var newLineB = _map.fromCoordinate(polygonDrawerPolygon.path[lastSegment+1], false /* clipToViewPort */)
                    for (var i=0; i<lastSegment; i++) {
                        var oldLineA = _map.fromCoordinate(polygonDrawerPolygon.path[i], false /* clipToViewPort */)
                        var oldLineB = _map.fromCoordinate(polygonDrawerPolygon.path[i+1], false /* clipToViewPort */)
                        if (QGroundControl.linesIntersect(newLineA, newLineB, oldLineA, oldLineB)) {
                            return;
                        }
                    }
                }

                var clickCoordinate = _map.toCoordinate(Qt.point(mouse.x, mouse.y))
                var polygonPath = polygonDrawerPolygon.path
nickexists's avatar
nickexists committed
357
                if (polygonPath.length == 0) {
358 359 360
                    // Add first coordinate
                    polygonPath.push(clickCoordinate)
                } else {
Don Gagne's avatar
Don Gagne committed
361 362 363 364 365 366 367 368 369 370 371
                    // Add subsequent coordinate
                    if (ScreenTools.isMobile) {
                        // Since mobile has no mouse, the onPositionChangedHandler will not fire. We have to add the coordinate
                        // here instead.
                        polygonDrawer.justClicked = false
                        polygonPath.push(clickCoordinate)
                    } else {
                        // The onPositionChanged handler for mouse movement will have already added the coordinate to the array.
                        // Just update it to the final position
                        polygonPath[polygonDrawerPolygon.path.length - 1] = clickCoordinate
                    }
372
                }
373
                polygonDrawerPolygonSet.path = polygonPath
374 375
                polygonDrawerPolygon.path = polygonPath
            } else if (polygonDrawer.polygonReady) {
376
                finishCapturePolygon()
377 378 379 380
            }
        }

        onPositionChanged: {
Don Gagne's avatar
Don Gagne committed
381 382 383 384
            if (ScreenTools.isMobile) {
                // We don't track mouse drag on mobile
                return
            }
385 386
            if (polygonDrawerPolygon.path.length) {
                var dragCoordinate = _map.toCoordinate(Qt.point(mouse.x, mouse.y))
387 388
                var polygonPath = polygonDrawerPolygon.path
                if (polygonDrawer.justClicked){
Don Gagne's avatar
Don Gagne committed
389
                    // Add new drag coordinate
390 391 392
                    polygonPath.push(dragCoordinate)
                    polygonDrawer.justClicked = false
                }
393 394 395 396 397

                // Update drag line
                polygonDrawerNextPoint.path = [ polygonDrawerPolygon.path[polygonDrawerPolygon.path.length - 2], dragCoordinate ]

                polygonPath[polygonDrawerPolygon.path.length - 1] = dragCoordinate
398 399
                polygonDrawerPolygon.path = polygonPath

400 401 402 403
            }
        }
    }

404
    /// Polygon being drawn
405 406 407 408
    MapPolygon {
        id:         polygonDrawerPolygon
        color:      "blue"
        opacity:    0.5
409 410 411 412 413 414 415
        visible:    polygonDrawerPolygon.path.length > 2
    }
    MapPolygon {
        id:         polygonDrawerPolygonSet
        color:      'green'
        opacity:    0.5
        visible:    polygonDrawer.polygonReady
416 417
    }

418
    /// Next line for polygon
419 420 421
    MapPolyline {
        id:         polygonDrawerNextPoint
        line.color: "green"
422
        line.width: 3
423 424 425 426
        visible:    polygonDrawer.drawingPolygon
    }

    //---- End Polygon Drawing code
427
} // Map