GeoFenceMapVisuals.qml 10 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 84 85 86 87 88 89 90 91 92 93 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/

import QtQuick          2.3
import QtQuick.Controls 1.2
import QtLocation       5.3
import QtPositioning    5.3

import QGroundControl               1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
import QGroundControl.Controls      1.0
import QGroundControl.FlightMap     1.0

/// Survey Complex Mission Item visuals
Item {
    z: QGroundControl.zOrderMapItems

    property var    map
    property var    myGeoFenceController
    property bool   interactive:            false   ///< true: user can interact with items
    property bool   planView:               false   ///< true: visuals showing in plan view
    property var    homePosition

    property var _polygonComponent
    property var _dragHandles
    property var _splitHandles
    property var _breachReturnPointComponent
    property var _mouseAreaComponent
    property var _circleComponent
    property var _mapPolygon:       myGeoFenceController.mapPolygon

    function _addVisualElements() {
        _polygonComponent = polygonComponent.createObject(map)
        map.addMapItem(_polygonComponent)
        _circleComponent = circleComponent.createObject(map)
        map.addMapItem(_circleComponent)
        _breachReturnPointComponent = breachReturnPointComponent.createObject(map)
        map.addMapItem(_breachReturnPointComponent)
        _mouseAreaComponent = mouseAreaComponent.createObject(map)
    }

    function _destroyVisualElements() {
        _polygonComponent.destroy()
        _circleComponent.destroy()
        _breachReturnPointComponent.destroy()
        _mouseAreaComponent.destroy()
    }

    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() {
        // Initial polygon is inset to take 2/3rds space
        var rect = map.centerViewport
        rect.x += (rect.width * 0.25) / 2
        rect.y += (rect.height * 0.25) / 2
        rect.width *= 0.75
        rect.height *= 0.75
        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)
        _mapPolygon.clear()
        _mapPolygon.appendVertex(map.toCoordinate(topLeft, false /* clipToViewPort */))
        _mapPolygon.appendVertex(map.toCoordinate(topRight, false /* clipToViewPort */))
        _mapPolygon.appendVertex(map.toCoordinate(bottomRight, false /* clipToViewPort */))
        _mapPolygon.appendVertex(map.toCoordinate(bottomLeft, false /* clipToViewPort */))
    }

    Component.onCompleted: {
        _addVisualElements()
        _addDragHandles()
    }

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

    Connections {
        target: myGeoFenceController

        onAddFencePolygon: {
            _addInitialPolygon()
        }
    }


    // Mouse area to capture breach return point coordinate
    Component {
        id: mouseAreaComponent

        MouseArea {
            anchors.fill:   map
            visible:        interactive
            onClicked:      myGeoFenceController.breachReturnPoint = map.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
        }
    }

    // GeoFence polygon
    Component {
        id: polygonComponent

        MapPolygon {
            z:              QGroundControl.zOrderMapItems
            border.width:   2
            border.color:   "orange"
            color:          "transparent"
            opacity:        0.5
            path:           _mapPolygon.path
            visible:        planView || geoFenceController.polygonEnabled
        }
    }

    // GeoFence circle
    Component {
        id: circleComponent

        MapCircle {
            z:              QGroundControl.zOrderMapItems
            border.width:   2
            border.color:   "orange"
            color:          "transparent"
149
            center:         homePosition ? homePosition : QtPositioning.coordinate()
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 201 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
            radius:         myGeoFenceController.circleRadius
            visible:        planView || geoFenceController.circleEnabled
        }
    }

    Component {
        id: splitHandleComponent

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

            property int vertexIndex

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

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

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

    Component {
        id: splitHandlesComponent

        Repeater {
            model: _mapPolygon.path.length > 2 ? _mapPolygon.path : 0

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

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

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

        MissionItemIndicatorDrag {
            id:         dragArea
            visible:    interactive

            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
                    _mapPolygon.adjustVertex(polygonVertex, itemCoordinate)
                }
            }

            onClicked: _mapPolygon.removePolygonVertex(polygonVertex)
        }
    }

    Component {
        id: dragHandleComponent

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

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

    // 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(map)
                    dragHandle.coordinate = Qt.binding(function() { return object.coordinate })
                    map.addMapItem(dragHandle)
                    var dragArea = dragAreaComponent.createObject(map, { "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 = [ ]
                }
            }
        }
    }

    // Breach return point
    Component {
        id: breachReturnPointComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
            z:              QGroundControl.zOrderMapItems
            coordinate:     myGeoFenceController.breachReturnPoint

            sourceItem: MissionItemIndexLabel {
                label: "B"
            }
        }
    }
}