QGCMapPolylineVisuals.qml 12.9 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 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11 12 13 14
import QtQuick                      2.11
import QtQuick.Controls             2.4
import QtLocation                   5.3
import QtPositioning                5.3
import QtQuick.Dialogs              1.2
15

16 17 18 19 20 21
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
22

Don Gagne's avatar
Don Gagne committed
23
/// QGCMapPolyline map visuals
24 25 26 27 28 29 30 31 32 33 34
Item {
    id: _root

    property var    mapControl                  ///< Map control to place item in
    property var    mapPolyline                 ///< QGCMapPolyline object
    property bool   interactive:    mapPolyline.interactive
    property int    lineWidth:      3
    property color  lineColor:      "#be781c"

    property var    _dragHandlesComponent
    property var    _splitHandlesComponent
Don Gagne's avatar
Don Gagne committed
35 36 37 38 39
    property string _instructionText:       _corridorToolsText
    property real   _zorderDragHandle:      QGroundControl.zOrderMapItems + 3   // Highest to prevent splitting when items overlap
    property real   _zorderSplitHandle:     QGroundControl.zOrderMapItems + 2
    property var    _savedVertices:         [ ]

DonLakeFlyer's avatar
DonLakeFlyer committed
40
    readonly property string _corridorToolsText:    qsTr("Polyline Tools")
Don Gagne's avatar
Don Gagne committed
41 42 43 44 45
    readonly property string _traceText:            qsTr("Click in the map to add vertices. Click 'Done Tracing' when finished.")

    function _addCommonVisuals() {
        if (_objMgrCommonVisuals.empty) {
            _objMgrCommonVisuals.createObject(polylineComponent, mapControl, true)
46 47 48
        }
    }

Don Gagne's avatar
Don Gagne committed
49 50 51
    function _addInteractiveVisuals() {
        if (_objMgrInteractiveVisuals.empty) {
            _objMgrInteractiveVisuals.createObjects([ dragHandlesComponent, splitHandlesComponent, toolbarComponent ], mapControl)
52 53 54 55
        }
    }

    /// Calculate the default/initial polyline
Don Gagne's avatar
Don Gagne committed
56 57 58 59 60
    function _defaultPolylineVertices() {
        var x = mapControl.centerViewport.x + (mapControl.centerViewport.width / 2)
        var yInset = mapControl.centerViewport.height / 4
        var topPointCoord =     mapControl.toCoordinate(Qt.point(x, mapControl.centerViewport.y + yInset),                                false /* clipToViewPort */)
        var bottomPointCoord =  mapControl.toCoordinate(Qt.point(x, mapControl.centerViewport.y + mapControl.centerViewport.height - yInset),    false /* clipToViewPort */)
61 62 63
        return [ topPointCoord, bottomPointCoord ]
    }

Don Gagne's avatar
Don Gagne committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    /// Reset polyline back to initial default
    function _resetPolyline() {
        mapPolyline.beginReset()
        mapPolyline.clear()
        var initialVertices = _defaultPolylineVertices()
        mapPolyline.appendVertex(initialVertices[0])
        mapPolyline.appendVertex(initialVertices[1])
        mapPolyline.endReset()
    }

    function _saveCurrentVertices() {
        _savedVertices = [ ]
        for (var i=0; i<mapPolyline.count; i++) {
            _savedVertices.push(mapPolyline.vertexCoordinate(i))
78 79 80
        }
    }

Don Gagne's avatar
Don Gagne committed
81 82
    function _restorePreviousVertices() {
        mapPolyline.beginReset()
83
        mapPolyline.clear()
Don Gagne's avatar
Don Gagne committed
84 85 86 87
        for (var i=0; i<_savedVertices.length; i++) {
            mapPolyline.appendVertex(_savedVertices[i])
        }
        mapPolyline.endReset()
88 89 90 91
    }

    onInteractiveChanged: {
        if (interactive) {
Don Gagne's avatar
Don Gagne committed
92 93 94 95 96 97
            _addInteractiveVisuals()
        } else {
            _objMgrInteractiveVisuals.destroyObjects()
        }
    }

98 99 100 101 102 103 104 105 106 107
    Connections {
        target: mapPolyline
        onTraceModeChanged: {
            if (mapPolyline.traceMode) {
                _instructionText = _traceText
                _objMgrTraceVisuals.createObject(traceMouseAreaComponent, mapControl, false)
            } else {
                _instructionText = _corridorToolsText
                _objMgrTraceVisuals.destroyObjects()
            }
108 109 110 111
        }
    }

    Component.onCompleted: {
Don Gagne's avatar
Don Gagne committed
112
        _addCommonVisuals()
113
        if (interactive) {
Don Gagne's avatar
Don Gagne committed
114
            _addInteractiveVisuals()
115 116
        }
    }
117
    Component.onDestruction: mapPolyline.traceMode = false
118

Don Gagne's avatar
Don Gagne committed
119 120 121
    QGCDynamicObjectManager { id: _objMgrCommonVisuals }
    QGCDynamicObjectManager { id: _objMgrInteractiveVisuals }
    QGCDynamicObjectManager { id: _objMgrTraceVisuals }
122 123 124 125 126 127 128 129

    QGCPalette { id: qgcPal }

    QGCFileDialog {
        id:             kmlLoadDialog
        folder:         QGroundControl.settingsManager.appSettings.missionSavePath
        title:          qsTr("Select KML File")
        selectExisting: true
130 131
        nameFilters:    ShapeFileHelper.fileDialogKMLFilters
        fileExtension:  QGroundControl.settingsManager.appSettings.kmlFileExtension
132 133 134 135 136 137 138

        onAcceptedForLoad: {
            mapPolyline.loadKMLFile(file)
            close()
        }
    }

139
    QGCMenu {
DonLakeFlyer's avatar
DonLakeFlyer committed
140
        id: menu
141 142 143 144 145 146 147
        property int _removeVertexIndex

        function popUpWithIndex(curIndex) {
            _removeVertexIndex = curIndex
            removeVertexItem.visible = mapPolyline.count > 2
            menu.popup()
        }
DonLakeFlyer's avatar
DonLakeFlyer committed
148

149
        QGCMenuItem {
150
            id:             removeVertexItem
DonLakeFlyer's avatar
DonLakeFlyer committed
151
            text:           qsTr("Remove vertex" )
152 153 154
            onTriggered:    mapPolyline.removeVertex(menu._removeVertexIndex)
        }

155
        QGCMenuItem {
yl5006's avatar
yl5006 committed
156
            text:           qsTr("Edit position..." )
157
            onTriggered:    mainWindow.showComponentDialog(editPositionDialog, qsTr("Edit Position"), mainWindow.showDialogDefaultWidth, StandardButton.Cancel)
yl5006's avatar
yl5006 committed
158
        }
DonLakeFlyer's avatar
DonLakeFlyer committed
159 160
    }

yl5006's avatar
yl5006 committed
161 162 163 164 165 166 167 168 169
    Component {
        id: editPositionDialog

        EditPositionDialog {
            Component.onCompleted: coordinate = mapPolyline.path[menu._removeVertexIndex]
            onCoordinateChanged:  mapPolyline.adjustVertex(menu._removeVertexIndex,coordinate)
        }
    }

170 171 172 173 174 175 176
    Component {
        id: polylineComponent

        MapPolyline {
            line.width: lineWidth
            line.color: lineColor
            path:       mapPolyline.path
177
            visible:    _root.visible
178
            opacity:    _root.opacity
179 180 181 182 183 184 185 186
        }
    }

    Component {
        id: splitHandleComponent

        MapQuickItem {
            id:             mapQuickItem
187 188 189
            anchorPoint.x:  sourceItem.width / 2
            anchorPoint.y:  sourceItem.height / 2
            z:              _zorderSplitHandle
190
            opacity:        _root.opacity
191 192 193

            property int vertexIndex

194 195
            sourceItem: SplitIndicator {
                onClicked:  mapPolyline.splitSegment(mapQuickItem.vertexIndex)
196 197 198 199 200 201 202 203 204 205 206 207 208 209
            }
        }
    }

    Component {
        id: splitHandlesComponent

        Repeater {
            model: mapPolyline.path

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

210 211
                opacity:    _root.opacity

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                function _setHandlePosition() {
                    var nextIndex = index + 1
                    var distance = _vertices[index].distanceTo(_vertices[nextIndex])
                    var azimuth = _vertices[index].azimuthTo(_vertices[nextIndex])
                    _splitHandle.coordinate = _vertices[index].atDistanceAndAzimuth(distance / 2, azimuth)
                }

                Component.onCompleted: {
                    if (index + 1 <= _vertices.length - 1) {
                        _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 {
242
            mapControl: _root.mapControl
243 244
            id:         dragArea
            z:          _zorderDragHandle
245
            opacity:    _root.opacity
246 247 248 249 250 251 252 253 254 255 256 257 258 259

            property int polylineVertex

            property bool _creationComplete: false

            Component.onCompleted: _creationComplete = true

            onItemCoordinateChanged: {
                if (_creationComplete) {
                    // During component creation some bad coordinate values got through which screws up draw
                    mapPolyline.adjustVertex(polylineVertex, itemCoordinate)
                }
            }

DonLakeFlyer's avatar
DonLakeFlyer committed
260
            onClicked: {
261
                menu.popUpWithIndex(polylineVertex)
DonLakeFlyer's avatar
DonLakeFlyer committed
262 263
            }

264 265 266 267 268 269 270 271 272 273 274
        }
    }

    Component {
        id: dragHandleComponent

        MapQuickItem {
            id:             mapQuickItem
            anchorPoint.x:  dragHandle.width / 2
            anchorPoint.y:  dragHandle.height / 2
            z:              _zorderDragHandle
275
            opacity:        _root.opacity
276 277 278 279

            property int polylineVertex

            sourceItem: Rectangle {
280 281 282 283 284 285 286
                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
287 288 289 290 291 292 293 294 295 296 297 298 299 300
            }
        }
    }

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

        Repeater {
            model: mapPolyline.pathModel

            delegate: Item {
                property var _visuals: [ ]

301 302
                opacity:    _root.opacity

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
                Component.onCompleted: {
                    var dragHandle = dragHandleComponent.createObject(mapControl)
                    dragHandle.coordinate = Qt.binding(function() { return object.coordinate })
                    dragHandle.polylineVertex = Qt.binding(function() { return index })
                    mapControl.addMapItem(dragHandle)
                    var dragArea = dragAreaComponent.createObject(mapControl, { "itemIndicator": dragHandle, "itemCoordinate": object.coordinate })
                    dragArea.polylineVertex = 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 = [ ]
                }
            }
        }
    }
Don Gagne's avatar
Don Gagne committed
323 324 325 326 327

    Component {
        id: toolbarComponent

        PlanEditToolbar {
328 329 330 331 332
            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
333 334 335

            QGCButton {
                _horizontalPadding: 0
DonLakeFlyer's avatar
DonLakeFlyer committed
336
                text:               qsTr("Basic")
337
                visible:            !mapPolyline.traceMode
Don Gagne's avatar
Don Gagne committed
338 339 340 341 342
                onClicked:          _resetPolyline()
            }

            QGCButton {
                _horizontalPadding: 0
343
                text:               mapPolyline.traceMode ? qsTr("Done Tracing") : qsTr("Trace")
Don Gagne's avatar
Don Gagne committed
344
                onClicked: {
345
                    if (mapPolyline.traceMode) {
Don Gagne's avatar
Don Gagne committed
346 347 348
                        if (mapPolyline.count < 2) {
                            _restorePreviousVertices()
                        }
349
                        mapPolyline.traceMode = false
Don Gagne's avatar
Don Gagne committed
350 351
                    } else {
                        _saveCurrentVertices()
352
                        mapPolyline.traceMode = true
Don Gagne's avatar
Don Gagne committed
353 354 355 356 357 358 359 360 361
                        mapPolyline.clear();
                    }
                }
            }

            QGCButton {
                _horizontalPadding: 0
                text:               qsTr("Load KML...")
                onClicked:          kmlLoadDialog.openForLoad()
362
                visible:            !mapPolyline.traceMode
Don Gagne's avatar
Don Gagne committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376
            }
        }
    }

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

        MouseArea {
            anchors.fill:       mapControl
            preventStealing:    true
            z:                  QGroundControl.zOrderMapItems + 1   // Over item indicators

            onClicked: {
377
                if (mouse.button === Qt.LeftButton && _root.interactive) {
Don Gagne's avatar
Don Gagne committed
378 379 380 381 382
                    mapPolyline.appendVertex(mapControl.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */))
                }
            }
        }
    }
383 384
}