QGCMapPolygonVisuals.qml 22.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15
import QtQuick                          2.11
import QtQuick.Controls                 2.4
import QtLocation                       5.3
import QtPositioning                    5.3
import QtQuick.Dialogs                  1.2
import QtQuick.Layouts                  1.11
16

17 18 19 20 21 22
import QGroundControl                   1.0
import QGroundControl.ScreenTools       1.0
import QGroundControl.Palette           1.0
import QGroundControl.Controls          1.0
import QGroundControl.FlightMap         1.0
import QGroundControl.ShapeFileHelper   1.0
23 24 25 26 27

/// QGCMapPolygon map visuals
Item {
    id: _root

DonLakeFlyer's avatar
DonLakeFlyer committed
28 29
    property var    mapControl                                  ///< Map control to place item in
    property var    mapPolygon                                  ///< QGCMapPolygon object
30
    property bool   interactive:        mapPolygon.interactive
31 32 33 34
    property color  interiorColor:      "transparent"
    property real   interiorOpacity:    1
    property int    borderWidth:        0
    property color  borderColor:        "black"
35

Don Gagne's avatar
Don Gagne committed
36
    property bool   _circleMode:                false
37
    property real   _circleRadius
Don Gagne's avatar
Don Gagne committed
38 39
    property bool   _circleRadiusDrag:          false
    property var    _circleRadiusDragCoord:     QtPositioning.coordinate()
40
    property bool   _editCircleRadius:          false
Don Gagne's avatar
Don Gagne committed
41 42 43 44
    property bool   _traceMode:                 false
    property string _instructionText:           _polygonToolsText
    property var    _savedVertices:             [ ]
    property bool   _savedCircleMode
45

Don Gagne's avatar
Don Gagne committed
46 47 48
    property real _zorderDragHandle:    QGroundControl.zOrderMapItems + 3   // Highest to prevent splitting when items overlap
    property real _zorderSplitHandle:   QGroundControl.zOrderMapItems + 2
    property real _zorderCenterHandle:  QGroundControl.zOrderMapItems + 1   // Lowest such that drag or split takes precedence
49

Don Gagne's avatar
Don Gagne committed
50 51 52 53 54 55 56
    readonly property string _polygonToolsText: qsTr("Polygon Tools")
    readonly property string _traceText:        qsTr("Click in the map to add vertices. Click 'Done Tracing' when finished.")

    function addCommonVisuals() {
        if (_objMgrCommonVisuals.empty) {
            _objMgrCommonVisuals.createObject(polygonComponent, mapControl, true)
        }
57 58
    }

Don Gagne's avatar
Don Gagne committed
59 60
    function removeCommonVisuals() {
        _objMgrCommonVisuals.destroyObjects()
61 62
    }

Don Gagne's avatar
Don Gagne committed
63 64 65
    function addEditingVisuals() {
        if (_objMgrEditingVisuals.empty) {
            _objMgrEditingVisuals.createObjects([ dragHandlesComponent, splitHandlesComponent, centerDragHandleComponent ], mapControl, false /* addToMap */)
66
        }
67 68
    }

Don Gagne's avatar
Don Gagne committed
69 70 71 72 73
    function removeEditingVisuals() {
        _objMgrEditingVisuals.destroyObjects()
    }


Don Gagne's avatar
Don Gagne committed
74
    function addToolbarVisuals() {
Don Gagne's avatar
Don Gagne committed
75
        if (_objMgrToolVisuals.empty) {
Don Gagne's avatar
Don Gagne committed
76
            _objMgrToolVisuals.createObject(toolbarComponent, mapControl)
77
        }
Don Gagne's avatar
Don Gagne committed
78 79 80 81 82 83 84 85 86
    }

    function removeToolVisuals() {
        _objMgrToolVisuals.destroyObjects()
    }

    function addCircleVisuals() {
        if (_objMgrCircleVisuals.empty) {
            _objMgrCircleVisuals.createObject(radiusVisualsComponent, mapControl)
87 88 89
        }
    }

90 91 92
    /// Calculate the default/initial 4 sided polygon
    function defaultPolygonVertices() {
        // Initial polygon is inset to take 2/3rds space
Don Gagne's avatar
Don Gagne committed
93
        var rect = Qt.rect(mapControl.centerViewport.x, mapControl.centerViewport.y, mapControl.centerViewport.width, mapControl.centerViewport.height)
94 95 96 97 98
        rect.x += (rect.width * 0.25) / 2
        rect.y += (rect.height * 0.25) / 2
        rect.width *= 0.75
        rect.height *= 0.75

Don Gagne's avatar
Don Gagne committed
99 100 101 102 103
        var centerCoord =       mapControl.toCoordinate(Qt.point(rect.x + (rect.width / 2), rect.y + (rect.height / 2)),   false /* clipToViewPort */)
        var topLeftCoord =      mapControl.toCoordinate(Qt.point(rect.x, rect.y),                                          false /* clipToViewPort */)
        var topRightCoord =     mapControl.toCoordinate(Qt.point(rect.x + rect.width, rect.y),                             false /* clipToViewPort */)
        var bottomLeftCoord =   mapControl.toCoordinate(Qt.point(rect.x, rect.y + rect.height),                            false /* clipToViewPort */)
        var bottomRightCoord =  mapControl.toCoordinate(Qt.point(rect.x + rect.width, rect.y + rect.height),               false /* clipToViewPort */)
104 105 106 107 108 109 110 111 112 113 114 115 116

        // Initial polygon has max width and height of 3000 meters
        var halfWidthMeters =   Math.min(topLeftCoord.distanceTo(topRightCoord), 3000) / 2
        var halfHeightMeters =  Math.min(topLeftCoord.distanceTo(bottomLeftCoord), 3000) / 2
        topLeftCoord =      centerCoord.atDistanceAndAzimuth(halfWidthMeters, -90).atDistanceAndAzimuth(halfHeightMeters, 0)
        topRightCoord =     centerCoord.atDistanceAndAzimuth(halfWidthMeters, 90).atDistanceAndAzimuth(halfHeightMeters, 0)
        bottomLeftCoord =   centerCoord.atDistanceAndAzimuth(halfWidthMeters, -90).atDistanceAndAzimuth(halfHeightMeters, 180)
        bottomRightCoord =  centerCoord.atDistanceAndAzimuth(halfWidthMeters, 90).atDistanceAndAzimuth(halfHeightMeters, 180)

        return [ topLeftCoord, topRightCoord, bottomRightCoord, bottomLeftCoord, centerCoord  ]
    }

    /// Reset polygon back to initial default
Don Gagne's avatar
Don Gagne committed
117 118
    function _resetPolygon() {
        mapPolygon.beginReset()
119
        mapPolygon.clear()
Don Gagne's avatar
Don Gagne committed
120
        var initialVertices = defaultPolygonVertices()
121 122 123
        for (var i=0; i<4; i++) {
            mapPolygon.appendVertex(initialVertices[i])
        }
Don Gagne's avatar
Don Gagne committed
124 125
        mapPolygon.endReset()
        _circleMode = false
126 127
    }

Don Gagne's avatar
Don Gagne committed
128
    function _createCircularPolygon(center, radius) {
129 130 131 132
        var unboundCenter = center.atDistanceAndAzimuth(0, 0)
        var segments = 16
        var angleIncrement = 360 / segments
        var angle = 0
Don Gagne's avatar
Don Gagne committed
133
        mapPolygon.beginReset()
134
        mapPolygon.clear()
Don Gagne's avatar
Don Gagne committed
135
        _circleRadius = radius
136
        for (var i=0; i<segments; i++) {
Don Gagne's avatar
Don Gagne committed
137
            var vertex = unboundCenter.atDistanceAndAzimuth(radius, angle)
138 139
            mapPolygon.appendVertex(vertex)
            angle += angleIncrement
140
        }
Don Gagne's avatar
Don Gagne committed
141 142
        mapPolygon.endReset()
        _circleMode = true
143 144 145
    }

    /// Reset polygon to a circle which fits within initial polygon
Don Gagne's avatar
Don Gagne committed
146
    function _resetCircle() {
147 148 149 150 151
        var initialVertices = defaultPolygonVertices()
        var width = initialVertices[0].distanceTo(initialVertices[1])
        var height = initialVertices[1].distanceTo(initialVertices[2])
        var radius = Math.min(width, height) / 2
        var center = initialVertices[4]
Don Gagne's avatar
Don Gagne committed
152
        _createCircularPolygon(center, radius)
153 154
    }

Don Gagne's avatar
Don Gagne committed
155
    function _handleInteractiveChanged() {
156
        if (interactive) {
Don Gagne's avatar
Don Gagne committed
157
            addEditingVisuals()
Don Gagne's avatar
Don Gagne committed
158
            addToolbarVisuals()
159
        } else {
Don Gagne's avatar
Don Gagne committed
160 161 162
            _traceMode = false
            removeEditingVisuals()
            removeToolVisuals()
163 164 165
        }
    }

Don Gagne's avatar
Don Gagne committed
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 193 194 195 196 197 198 199 200
    function _saveCurrentVertices() {
        _savedVertices = [ ]
        _savedCircleMode = _circleMode
        for (var i=0; i<mapPolygon.count; i++) {
            _savedVertices.push(mapPolygon.vertexCoordinate(i))
        }
    }

    function _restorePreviousVertices() {
        mapPolygon.beginReset()
        mapPolygon.clear()
        for (var i=0; i<_savedVertices.length; i++) {
            mapPolygon.appendVertex(_savedVertices[i])
        }
        mapPolygon.endReset()
        _circleMode = _savedCircleMode
    }

    onInteractiveChanged: _handleInteractiveChanged()

    on_TraceModeChanged: {
        if (_traceMode) {
            _instructionText = _traceText
            _objMgrTraceVisuals.createObject(traceMouseAreaComponent, mapControl, false)
        } else {
            _instructionText = _polygonToolsText
            _objMgrTraceVisuals.destroyObjects()
        }
    }

    on_CircleModeChanged: {
        if (_circleMode) {
            addCircleVisuals()
        } else {
            _objMgrCircleVisuals.destroyObjects()
201 202 203
        }
    }

Don Gagne's avatar
Don Gagne committed
204 205 206
    Component.onCompleted: {
        addCommonVisuals()
        _handleInteractiveChanged()
207 208
    }

Don Gagne's avatar
Don Gagne committed
209 210 211 212 213 214
    QGCDynamicObjectManager { id: _objMgrCommonVisuals }
    QGCDynamicObjectManager { id: _objMgrToolVisuals }
    QGCDynamicObjectManager { id: _objMgrEditingVisuals }
    QGCDynamicObjectManager { id: _objMgrTraceVisuals }
    QGCDynamicObjectManager { id: _objMgrCircleVisuals }

215 216
    QGCPalette { id: qgcPal }

Don Gagne's avatar
Don Gagne committed
217
    KMLOrSHPFileDialog {
218 219
        id:             kmlOrSHPLoadDialog
        title:          qsTr("Select Polygon File")
DonLakeFlyer's avatar
DonLakeFlyer committed
220 221 222
        selectExisting: true

        onAcceptedForLoad: {
223
            mapPolygon.loadKMLOrSHPFile(file)
224
            mapFitFunctions.fitMapViewportToMissionItems()
DonLakeFlyer's avatar
DonLakeFlyer committed
225 226 227 228
            close()
        }
    }

229
    QGCMenu {
230 231
        id: menu

232
        property int _editingVertexIndex: -1
233

234 235 236 237 238 239 240
        function popupVertex(curIndex) {
            menu._editingVertexIndex = curIndex
            removeVertexItem.visible = (mapPolygon.count > 3 && menu._editingVertexIndex >= 0)
            menu.popup()
        }

        function popupCenter() {
241 242 243
            menu.popup()
        }

244
        QGCMenuItem {
245
            id:             removeVertexItem
Don Gagne's avatar
Don Gagne committed
246
            visible:        !_circleMode
247 248
            text:           qsTr("Remove vertex")
            onTriggered: {
249 250
                if (menu._editingVertexIndex >= 0) {
                    mapPolygon.removeVertex(menu._editingVertexIndex)
251 252 253 254
                }
            }
        }

255
        QGCMenuSeparator {
256 257 258
            visible:        removeVertexItem.visible
        }

259
        QGCMenuItem {
260
            text:           qsTr("Set radius..." )
Don Gagne's avatar
Don Gagne committed
261
            visible:        _circleMode
262
            onTriggered:    _editCircleRadius = true
263 264
        }

265
        QGCMenuItem {
266
            text:           qsTr("Edit position..." )
Don Gagne's avatar
Don Gagne committed
267
            visible:        _circleMode
268
            onTriggered:    mainWindow.showComponentDialog(editCenterPositionDialog, qsTr("Edit Center Position"), mainWindow.showDialogDefaultWidth, StandardButton.Close)
269 270
        }

271
        QGCMenuItem {
272
            text:           qsTr("Edit position..." )
Don Gagne's avatar
Don Gagne committed
273
            visible:        !_circleMode && menu._editingVertexIndex >= 0
274
            onTriggered:    mainWindow.showComponentDialog(editVertexPositionDialog, qsTr("Edit Vertex Position"), mainWindow.showDialogDefaultWidth, StandardButton.Close)
275 276 277
        }
    }

278 279 280 281
    Component {
        id: polygonComponent

        MapPolygon {
282 283 284 285 286
            color:          interiorColor
            opacity:        interiorOpacity
            border.color:   borderColor
            border.width:   borderWidth
            path:           mapPolygon.path
287 288 289 290 291 292 293 294
        }
    }

    Component {
        id: splitHandleComponent

        MapQuickItem {
            id:             mapQuickItem
295 296
            anchorPoint.x:  sourceItem.width / 2
            anchorPoint.y:  sourceItem.height / 2
Don Gagne's avatar
Don Gagne committed
297
            visible:        !_circleMode
298 299 300

            property int vertexIndex

301 302
            sourceItem: SplitIndicator {
                z: _zorderSplitHandle
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            }
        }
    }

    Component {
        id: splitHandlesComponent

        Repeater {
            model: mapPolygon.path

            delegate: Item {
                property var _splitHandle
                property var _vertices:     mapPolygon.path

                function _setHandlePosition() {
                    var nextIndex = index + 1
                    if (nextIndex > _vertices.length - 1) {
                        nextIndex = 0
                    }
                    var distance = _vertices[index].distanceTo(_vertices[nextIndex])
                    var azimuth = _vertices[index].azimuthTo(_vertices[nextIndex])
                    _splitHandle.coordinate = _vertices[index].atDistanceAndAzimuth(distance / 2, azimuth)
                }

                Component.onCompleted: {
                    _splitHandle = splitHandleComponent.createObject(mapControl)
                    _splitHandle.vertexIndex = index
                    _setHandlePosition()
                    mapControl.addMapItem(_splitHandle)
                }

                Component.onDestruction: {
                    if (_splitHandle) {
                        _splitHandle.destroy()
                    }
                }
            }
        }
    }

    // Control which is used to drag polygon vertices
    Component {
        id: dragAreaComponent

        MissionItemIndicatorDrag {
348
            id:         dragArea
349
            mapControl: _root.mapControl
350
            z:          _zorderDragHandle
Don Gagne's avatar
Don Gagne committed
351
            visible:    !_circleMode
352
            onDragStop: mapPolygon.verifyClockwiseWinding()
353 354 355 356 357 358 359 360 361

            property int polygonVertex

            property bool _creationComplete: false

            Component.onCompleted: _creationComplete = true

            onItemCoordinateChanged: {
                if (_creationComplete) {
362
                    // During component creation some bad coordinate values got through which screws up draw
363 364 365 366
                    mapPolygon.adjustVertex(polygonVertex, itemCoordinate)
                }
            }

367
            onClicked: menu.popupVertex(polygonVertex)
368 369 370
        }
    }

371 372 373 374
    Component {
        id: centerDragHandle
        MapQuickItem {
            id:             mapQuickItem
375 376
            anchorPoint.x:  dragHandle.width  * 0.5
            anchorPoint.y:  dragHandle.height * 0.5
377 378
            z:              _zorderDragHandle
            sourceItem: Rectangle {
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                id:             dragHandle
                width:          ScreenTools.defaultFontPixelHeight * 1.5
                height:         width
                radius:         width * 0.5
                color:          Qt.rgba(1,1,1,0.8)
                border.color:   Qt.rgba(0,0,0,0.25)
                border.width:   1
                QGCColoredImage {
                    width:      parent.width
                    height:     width
                    color:      Qt.rgba(0,0,0,1)
                    mipmap:     true
                    fillMode:   Image.PreserveAspectFit
                    source:     "/qmlimages/MapCenter.svg"
                    sourceSize.height:  height
                    anchors.centerIn:   parent
395 396 397 398 399
                }
            }
        }
    }

400 401 402 403 404
    Component {
        id: dragHandleComponent

        MapQuickItem {
            id:             mapQuickItem
405
            anchorPoint.x:  dragHandle.width  / 2
406
            anchorPoint.y:  dragHandle.height / 2
407
            z:              _zorderDragHandle
Don Gagne's avatar
Don Gagne committed
408
            visible:        !_circleMode
409 410

            property int polygonVertex
411 412

            sourceItem: Rectangle {
413 414 415 416 417 418 419
                id:             dragHandle
                width:          ScreenTools.defaultFontPixelHeight * 1.5
                height:         width
                radius:         width * 0.5
                color:          Qt.rgba(1,1,1,0.8)
                border.color:   Qt.rgba(0,0,0,0.25)
                border.width:   1
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
            }
        }
    }

    // Add all polygon vertex drag handles to the map
    Component {
        id: dragHandlesComponent

        Repeater {
            model: mapPolygon.pathModel

            delegate: Item {
                property var _visuals: [ ]

                Component.onCompleted: {
                    var dragHandle = dragHandleComponent.createObject(mapControl)
                    dragHandle.coordinate = Qt.binding(function() { return object.coordinate })
437
                    dragHandle.polygonVertex = Qt.binding(function() { return index })
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
                    mapControl.addMapItem(dragHandle)
                    var dragArea = dragAreaComponent.createObject(mapControl, { "itemIndicator": dragHandle, "itemCoordinate": object.coordinate })
                    dragArea.polygonVertex = Qt.binding(function() { return index })
                    _visuals.push(dragHandle)
                    _visuals.push(dragArea)
                }

                Component.onDestruction: {
                    for (var i=0; i<_visuals.length; i++) {
                        _visuals[i].destroy()
                    }
                    _visuals = [ ]
                }
            }
        }
    }

455
    Component {
456
        id: editCenterPositionDialog
457 458 459

        EditPositionDialog {
            coordinate: mapPolygon.center
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
            onCoordinateChanged: {
                // Prevent spamming signals on vertex changes by setting centerDrag = true when changing center position.
                // This also fixes a bug where Qt gets confused by all the signalling and draws a bad visual.
                mapPolygon.centerDrag = true
                mapPolygon.center = coordinate
                mapPolygon.centerDrag = false
            }
        }
    }

    Component {
        id: editVertexPositionDialog

        EditPositionDialog {
            coordinate:             mapPolygon.vertexCoordinate(menu._editingVertexIndex)
475 476 477 478
            onCoordinateChanged: {
                mapPolygon.adjustVertex(menu._editingVertexIndex, coordinate)
                mapPolygon.verifyClockwiseWinding()
            }
479 480 481
        }
    }

482 483 484 485
    Component {
        id: centerDragAreaComponent

        MissionItemIndicatorDrag {
486
            mapControl:                 _root.mapControl
Don Gagne's avatar
Don Gagne committed
487
            z:                          _zorderCenterHandle
DonLakeFlyer's avatar
DonLakeFlyer committed
488 489 490
            onItemCoordinateChanged:    mapPolygon.center = itemCoordinate
            onDragStart:                mapPolygon.centerDrag = true
            onDragStop:                 mapPolygon.centerDrag = false
Don Gagne's avatar
Don Gagne committed
491 492
        }
    }
493

Don Gagne's avatar
Don Gagne committed
494 495
    Component {
        id: centerDragHandleComponent
496

Don Gagne's avatar
Don Gagne committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510
        Item {
            property var dragHandle
            property var dragArea

            Component.onCompleted: {
                dragHandle = centerDragHandle.createObject(mapControl)
                dragHandle.coordinate = Qt.binding(function() { return mapPolygon.center })
                mapControl.addMapItem(dragHandle)
                dragArea = centerDragAreaComponent.createObject(mapControl, { "itemIndicator": dragHandle, "itemCoordinate": mapPolygon.center })
            }

            Component.onDestruction: {
                dragHandle.destroy()
                dragArea.destroy()
511
            }
Don Gagne's avatar
Don Gagne committed
512 513 514 515
        }
    }

    Component {
Don Gagne's avatar
Don Gagne committed
516
        id: toolbarComponent
Don Gagne's avatar
Don Gagne committed
517

Don Gagne's avatar
Don Gagne committed
518
        PlanEditToolbar {
519 520 521 522 523
            anchors.horizontalCenter:       mapControl.left
            anchors.horizontalCenterOffset: mapControl.centerViewport.left + (mapControl.centerViewport.width / 2)
            y:                              mapControl.centerViewport.top
            z:                              QGroundControl.zOrderMapItems + 2
            availableWidth:                 mapControl.centerViewport.width
Don Gagne's avatar
Don Gagne committed
524

Don Gagne's avatar
Don Gagne committed
525 526
            QGCButton {
                _horizontalPadding: 0
DonLakeFlyer's avatar
DonLakeFlyer committed
527
                text:               qsTr("Basic")
Don Gagne's avatar
Don Gagne committed
528 529
                visible:            !_traceMode
                onClicked:          _resetPolygon()
Don Gagne's avatar
Don Gagne committed
530 531
            }

Don Gagne's avatar
Don Gagne committed
532 533
            QGCButton {
                _horizontalPadding: 0
DonLakeFlyer's avatar
DonLakeFlyer committed
534
                text:               qsTr("Circular")
Don Gagne's avatar
Don Gagne committed
535 536 537
                visible:            !_traceMode
                onClicked:          _resetCircle()
            }
Don Gagne's avatar
Don Gagne committed
538

Don Gagne's avatar
Don Gagne committed
539 540
            QGCButton {
                _horizontalPadding: 0
541
                text:               _traceMode ? qsTr("Done Tracing") : qsTr("Trace")
Don Gagne's avatar
Don Gagne committed
542 543 544 545
                onClicked: {
                    if (_traceMode) {
                        if (mapPolygon.count < 3) {
                            _restorePreviousVertices()
Don Gagne's avatar
Don Gagne committed
546
                        }
Don Gagne's avatar
Don Gagne committed
547 548 549 550 551 552
                        _traceMode = false
                    } else {
                        _saveCurrentVertices()
                        _circleMode = false
                        _traceMode = true
                        mapPolygon.clear();
553 554
                    }
                }
Don Gagne's avatar
Don Gagne committed
555
            }
556

Don Gagne's avatar
Don Gagne committed
557 558 559 560 561
            QGCButton {
                _horizontalPadding: 0
                text:               qsTr("Load KML/SHP...")
                onClicked:          kmlOrSHPLoadDialog.openForLoad()
                visible:            !_traceMode
Don Gagne's avatar
Don Gagne committed
562 563 564 565 566 567 568 569 570
            }
        }
    }

    // Mouse area to capture clicks for tracing a polygon
    Component {
        id:  traceMouseAreaComponent

        MouseArea {
Don Gagne's avatar
Don Gagne committed
571
            anchors.fill:       mapControl
Don Gagne's avatar
Don Gagne committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
            preventStealing:    true
            z:                  QGroundControl.zOrderMapItems + 1   // Over item indicators

            onClicked: {
                if (mouse.button === Qt.LeftButton) {
                    mapPolygon.appendVertex(mapControl.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */))
                }
            }
        }
    }

    Component {
        id: radiusDragHandleComponent

        MapQuickItem {
            id:             mapQuickItem
            anchorPoint.x:  dragHandle.width / 2
            anchorPoint.y:  dragHandle.height / 2
            z:              QGroundControl.zOrderMapItems + 2

            sourceItem: Rectangle {
                id:         dragHandle
                width:      ScreenTools.defaultFontPixelHeight * 1.5
                height:     width
                radius:     width / 2
                color:      "white"
                opacity:    .90
            }
        }
    }
602

Don Gagne's avatar
Don Gagne committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    Component {
        id: radiusDragAreaComponent

        MissionItemIndicatorDrag {
            mapControl: _root.mapControl

            property real _lastRadius

            onItemCoordinateChanged: {
                var radius = mapPolygon.center.distanceTo(itemCoordinate)
                // Prevent signalling re-entrancy
                if (!_circleRadiusDrag && Math.abs(radius - _lastRadius) > 0.1) {
                    _circleRadiusDrag = true
                    _createCircularPolygon(mapPolygon.center, radius)
                    _circleRadiusDragCoord = itemCoordinate
                    _circleRadiusDrag = false
                    _lastRadius = radius
                }
            }
622 623 624 625
        }
    }

    Component {
Don Gagne's avatar
Don Gagne committed
626
        id: radiusVisualsComponent
627 628

        Item {
Don Gagne's avatar
Don Gagne committed
629
            property var    circleCenterCoord:  mapPolygon.center
630

Don Gagne's avatar
Don Gagne committed
631 632
            function _calcRadiusDragCoord() {
                _circleRadiusDragCoord = circleCenterCoord.atDistanceAndAzimuth(_circleRadius, 90)
633 634
            }

Don Gagne's avatar
Don Gagne committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
            onCircleCenterCoordChanged: {
                if (!_circleRadiusDrag) {
                    _calcRadiusDragCoord()
                }
            }

            QGCDynamicObjectManager {
                id: _objMgr
            }

            Component.onCompleted: {
                _calcRadiusDragCoord()
                var radiusDragHandle = _objMgr.createObject(radiusDragHandleComponent, mapControl, true)
                radiusDragHandle.coordinate = Qt.binding(function() { return _circleRadiusDragCoord })
                var radiusDragIndicator = radiusDragAreaComponent.createObject(mapControl, { "itemIndicator": radiusDragHandle, "itemCoordinate": _circleRadiusDragCoord })
                _objMgr.addObject(radiusDragIndicator)
651 652 653 654 655
            }
        }
    }
}