SurveyMapVisual.qml 10.2 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.
 *
 ****************************************************************************/

10 11 12 13
import QtQuick          2.3
import QtQuick.Controls 1.2
import QtLocation       5.3
import QtPositioning    5.3
14

15
import QGroundControl               1.0
16 17 18
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
import QGroundControl.Controls      1.0
19
import QGroundControl.FlightMap     1.0
20 21 22

/// Survey Complex Mission Item visuals
Item {
23 24
    id: _root

25 26
    property var map    ///< Map control to place item in

27
    property var _missionItem:      object
28 29
    property var _polygon
    property var _grid
30 31
    property var _entryCoordinate
    property var _exitCoordinate
32 33
    property var _dragHandles
    property var _splitHandles
34

35 36
    signal clicked(int sequenceNumber)

37
    function _addVisualElements() {
38 39
        _polygon = polygonComponent.createObject(map)
        _grid = gridComponent.createObject(map)
40 41
        _entryCoordinate = entryPointComponent.createObject(map)
        _exitCoordinate = exitPointComponent.createObject(map)
42 43
        map.addMapItem(_polygon)
        map.addMapItem(_grid)
44 45
        map.addMapItem(_entryCoordinate)
        map.addMapItem(_exitCoordinate)
46 47
    }

48
    function _destroyVisualElements() {
49 50
        _polygon.destroy()
        _grid.destroy()
51 52
        _entryCoordinate.destroy()
        _exitCoordinate.destroy()
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
    function _addDragHandles() {
        if (!_dragHandles) {
            _dragHandles = dragHandlesComponent.createObject(map)
        }
        if (!_splitHandles) {
            _splitHandles = splitHandlesComponent.createObject(map)
        }
    }

    function _destroyDragHandles() {
        if (_dragHandles) {
            _dragHandles.destroy()
            _dragHandles = undefined
        }
        if (_splitHandles) {
            _splitHandles.destroy()
            _splitHandles = undefined
        }
    }

    /// Add an initial 4 sided polygon if there is none
    function _addInitialPolygon() {
        if (_missionItem.polygonPath.length < 3) {
            // Initial polygon is inset to take 2/3rds space
79 80
            var rect = Qt.rect(map.centerViewport.x, map.centerViewport.y, map.centerViewport.width, map.centerViewport.height)
            console.log(map.centerViewport)
81 82 83 84
            rect.x += (rect.width * 0.25) / 2
            rect.y += (rect.height * 0.25) / 2
            rect.width *= 0.75
            rect.height *= 0.75
85
            console.log(map.centerViewport)
86 87 88 89
            var topLeft = Qt.point(rect.x, rect.y)
            var topRight = Qt.point(rect.x + rect.width, rect.y)
            var bottomLeft = Qt.point(rect.x, rect.y + rect.height)
            var bottomRight = Qt.point(rect.x + rect.width, rect.y + rect.height)
DonLakeFlyer's avatar
DonLakeFlyer committed
90 91 92 93
            _missionItem.addPolygonCoordinate(map.toCoordinate(topLeft, false /* clipToViewPort */))
            _missionItem.addPolygonCoordinate(map.toCoordinate(topRight, false /* clipToViewPort */))
            _missionItem.addPolygonCoordinate(map.toCoordinate(bottomRight, false /* clipToViewPort */))
            _missionItem.addPolygonCoordinate(map.toCoordinate(bottomLeft, false /* clipToViewPort */))
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        }
    }

    Component.onCompleted: {
        _addInitialPolygon()
        _addVisualElements()
        if (_missionItem.isCurrentItem) {
            _addDragHandles()
        }
    }

    Component.onDestruction: {
        _destroyVisualElements()
        _destroyDragHandles()
    }

    Connections {
        target: _missionItem

        onIsCurrentItemChanged: {
            if (_missionItem.isCurrentItem) {
                _addDragHandles()
            } else {
                _destroyDragHandles()
            }
        }
    }

122 123 124 125 126 127 128
    // Survey area polygon
    Component {
        id: polygonComponent

        MapPolygon {
            color: "green"
            opacity:    0.5
129
            path:       _missionItem.polygonPath
130 131 132 133 134 135 136 137 138 139
        }
    }

    // Survey grid lines
    Component {
        id: gridComponent

        MapPolyline {
            line.color: "white"
            line.width: 2
140 141 142 143 144 145 146 147 148
            path:       _missionItem.gridPoints
        }
    }

    // Entry point
    Component {
        id: entryPointComponent

        MapQuickItem {
149 150
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
151 152
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.coordinate
153
            visible:        _missionItem.exitCoordinate.isValid
154

155
            sourceItem: MissionItemIndexLabel {
156
                index:      _missionItem.sequenceNumber
157 158
                label:      "Entry"
                checked:    _missionItem.isCurrentItem
159
                onClicked:  _root.clicked(_missionItem.sequenceNumber)
160 161 162 163 164 165 166 167 168
            }
        }
    }

    // Exit point
    Component {
        id: exitPointComponent

        MapQuickItem {
169 170
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
171 172
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.exitCoordinate
173
            visible:        _missionItem.exitCoordinate.isValid
174

175
            sourceItem: MissionItemIndexLabel {
176
                index:      _missionItem.lastSequenceNumber
177 178
                label:      "Exit"
                checked:    _missionItem.isCurrentItem
179
                onClicked:  _root.clicked(_missionItem.sequenceNumber)
180
            }
181 182
        }
    }
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

    Component {
        id: splitHandleComponent

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

            property int vertexIndex

            sourceItem: Rectangle {
                id:         dragHandle
                width:      ScreenTools.defaultFontPixelHeight * 1.5
                height:     width
                radius:     width / 2
                color:      "white"
201
                opacity:    .50
202 203 204 205 206 207 208 209 210 211 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 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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

                QGCLabel {
                    anchors.horizontalCenter:   parent.horizontalCenter
                    anchors.verticalCenter:     parent.verticalCenter
                    text:                       "+"
                }

                QGCMouseArea {
                    fillItem:   parent
                    onClicked:  _missionItem.splitPolygonSegment(mapQuickItem.vertexIndex)
                }
            }
        }
    }

    Component {
        id: splitHandlesComponent

        Repeater {
            model: _missionItem.polygonPath

            delegate: Item {
                property var _splitHandle
                property var _vertices:     _missionItem.polygonPath

                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(map)
                    _splitHandle.vertexIndex = index
                    _setHandlePosition()
                    map.addMapItem(_splitHandle)
                }

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

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

        MissionItemIndicatorDrag {
            id: dragArea

            property int polygonVertex

            property bool _creationComplete: false

            Component.onCompleted: _creationComplete = true

            onItemCoordinateChanged: {
                if (_creationComplete) {
                    // During component creation some bad coordinate values got through which screws up polygon draw
                    _missionItem.adjustPolygonCoordinate(polygonVertex, itemCoordinate)
                }
            }

            onClicked: _missionItem.removePolygonVertex(polygonVertex)
        }
    }

    Component {
        id: dragHandleComponent

        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"
292
                opacity:    .90
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
            }
        }
    }

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

        Repeater {
            model: _missionItem.polygonModel

            delegate: Item {
                property var _visuals: [ ]

                Component.onCompleted: {
                    var dragHandle = dragHandleComponent.createObject(map)
                    dragHandle.coordinate = Qt.binding(function() { return object.coordinate })
                    map.addMapItem(dragHandle)
311 312
                    var dragArea = dragAreaComponent.createObject(map, { "itemIndicator": dragHandle, "itemCoordinate": object.coordinate })
                    dragArea.polygonVertex = Qt.binding(function() { return index })
313 314 315 316 317 318 319 320 321 322 323 324 325
                    _visuals.push(dragHandle)
                    _visuals.push(dragArea)
                }

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