FWLandingPatternMapVisual.qml 8.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/****************************************************************************
 *
 *   (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.2
import QtQuick.Controls 1.2
import QtLocation       5.3
import QtPositioning    5.2

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

/// Fixed Wing Landing Pattern map visuals
Item {
    property var map    ///< Map control to place item in

24
    property var _missionItem:  object
Don Gagne's avatar
Don Gagne committed
25
    property var _itemVisuals: [ ]
26
    property var _mouseArea
Don Gagne's avatar
Don Gagne committed
27 28 29 30 31 32
    property var _dragAreas: [ ]

    readonly property int _flightPathIndex:     0
    readonly property int _loiterPointIndex:    1
    readonly property int _loiterRadiusIndex:   2
    readonly property int _landPointIndex:      3
33

34
    function hideItemVisuals() {
Don Gagne's avatar
Don Gagne committed
35 36
        for (var i=0; i<_itemVisuals.length; i++) {
            _itemVisuals[i].destroy()
37
        }
Don Gagne's avatar
Don Gagne committed
38
        _itemVisuals = [ ]
39 40 41
    }

    function showItemVisuals() {
Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47 48 49 50 51 52 53 54
        if (_itemVisuals.length === 0) {
            var itemVisual = flightPathComponent.createObject(map)
            map.addMapItem(itemVisual)
            _itemVisuals[_flightPathIndex] =itemVisual
            itemVisual = loiterPointComponent.createObject(map)
            map.addMapItem(itemVisual)
            _itemVisuals[_loiterPointIndex] = itemVisual
            itemVisual = loiterRadiusComponent.createObject(map)
            map.addMapItem(itemVisual)
            _itemVisuals[_loiterRadiusIndex] = itemVisual
            itemVisual = landPointComponent.createObject(map)
            map.addMapItem(itemVisual)
            _itemVisuals[_landPointIndex] = itemVisual
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        }
    }

    function hideMouseArea() {
        if (_mouseArea) {
            _mouseArea.destroy()
            _mouseArea = undefined
        }
    }

    function showMouseArea() {
        if (!_mouseArea) {
            _mouseArea = mouseAreaComponent.createObject(map)
            map.addMapItem(_mouseArea)
        }
    }

    function hideDragAreas() {
Don Gagne's avatar
Don Gagne committed
73 74
        for (var i=0; i<_dragAreas.length; i++) {
            _dragAreas[i].destroy()
75
        }
Don Gagne's avatar
Don Gagne committed
76
        _dragAreas = [ ]
77 78 79
    }

    function showDragAreas() {
Don Gagne's avatar
Don Gagne committed
80 81 82
        if (_dragAreas.length === 0) {
            _dragAreas.push(dragAreaComponent.createObject(map, { "dragLoiter": true }))
            _dragAreas.push(dragAreaComponent.createObject(map, { "dragLoiter": false }))
83 84 85
        }
    }

86
    Component.onCompleted: {
87 88 89 90 91 92 93 94
        if (_missionItem.landingCoordSet) {
            showItemVisuals()
            if (_missionItem.isCurrentItem) {
                showDragAreas()
            }
        } else if (_missionItem.isCurrentItem) {
            showMouseArea()
        }
95 96 97
    }

    Component.onDestruction: {
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 149 150 151 152 153
        hideDragAreas()
        hideMouseArea()
        hideItemVisuals()
    }

    Connections {
        target: _missionItem

        onIsCurrentItemChanged: {
            if (_missionItem.isCurrentItem) {
                if (_missionItem.landingCoordSet) {
                    showDragAreas()
                } else {
                    showMouseArea()
                }
            } else {
                hideMouseArea()
                hideDragAreas()
            }
        }

        onLandingCoordSetChanged: {
            if (_missionItem.landingCoordSet) {
                hideMouseArea()
                showItemVisuals()
                showDragAreas()
            } else if (_missionItem.isCurrentItem) {
                hideDragAreas()
                showMouseArea()
            }
        }
    }

    // Mouse area to capture landing point coordindate
    Component {
        id:  mouseAreaComponent

        MouseArea {
            anchors.fill: map

            onClicked: {
                var coordinate = map.toCoordinate(Qt.point(mouse.x, mouse.y))
                coordinate.latitude = coordinate.latitude.toFixed(_decimalPlaces)
                coordinate.longitude = coordinate.longitude.toFixed(_decimalPlaces)
                coordinate.altitude = coordinate.altitude.toFixed(_decimalPlaces)
                _missionItem.landingCoordinate = coordinate
            }
        }
    }

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

        Rectangle {
            id:             itemDragger
154 155 156 157
            x:              mapQuickItem.x
            y:              mapQuickItem.y
            width:          mapQuickItem.width
            height:         mapQuickItem.height
158 159 160 161
            color:          "transparent"
            z:              QGroundControl.zOrderMapItems + 1    // Above item icons

            property bool   dragLoiter
Don Gagne's avatar
Don Gagne committed
162
            property var    mapQuickItem:                   dragLoiter ? _itemVisuals[_loiterPointIndex] : _itemVisuals[_landPointIndex]
163 164 165 166 167 168 169
            property bool   _preventCoordinateBindingLoop:  false

            onXChanged: liveDrag()
            onYChanged: liveDrag()

            function liveDrag() {
                if (!itemDragger._preventCoordinateBindingLoop && Drag.active) {
170
                    var point = Qt.point(itemDragger.x + mapQuickItem.anchorPoint.x, itemDragger.y + mapQuickItem.anchorPoint.y)
171 172 173 174 175 176 177 178 179 180 181 182 183
                    var coordinate = map.toCoordinate(point)
                    itemDragger._preventCoordinateBindingLoop = true
                    if (dragLoiter) {
                        coordinate.altitude = _missionItem.loiterCoordinate.altitude
                        _missionItem.loiterCoordinate = coordinate
                    } else {
                        coordinate.altitude = _missionItem.landingCoordinate.altitude
                        _missionItem.landingCoordinate = coordinate
                    }
                    itemDragger._preventCoordinateBindingLoop = false
                }
            }

184
            Drag.active: itemDrag.drag.active
185 186 187 188 189 190 191 192 193 194 195

            MouseArea {
                id:             itemDrag
                anchors.fill:   parent
                drag.target:    parent
                drag.minimumX:  0
                drag.minimumY:  0
                drag.maximumX:  itemDragger.parent.width - parent.width
                drag.maximumY:  itemDragger.parent.height - parent.height
            }
        }
196 197 198 199 200 201 202
    }

    // Flight path
    Component {
        id: flightPathComponent

        MapPolyline {
203
            z:          QGroundControl.zOrderMapItems - 1   // Under item indicators
Don Gagne's avatar
Don Gagne committed
204
            line.color: "#be781c"
205
            line.width: 2
206
            path:       _missionItem.landingCoordSet ? [ _missionItem.loiterCoordinate, _missionItem.landingCoordinate ] : undefined
207 208 209 210 211
        }
    }

    // Loiter point
    Component {
212
        id: loiterPointComponent
213 214

        MapQuickItem {
215 216
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
217 218
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.loiterCoordinate
219 220 221

            sourceItem:
                MissionItemIndexLabel {
222 223 224 225
                label:      "Loiter"
                checked:    _missionItem.isCurrentItem

                onClicked: setCurrentItem(_missionItem.sequenceNumber)
226 227 228 229
            }
        }
    }

Don Gagne's avatar
Don Gagne committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243
    // Loiter radius visual
    Component {
        id: loiterRadiusComponent

        MapCircle {
            z:              QGroundControl.zOrderMapItems
            center:         _missionItem.loiterCoordinate
            radius:         _missionItem.loiterRadius.value
            border.width:   2
            border.color:   "green"
            color:          "transparent"
        }
    }

244 245 246 247 248
    // Land point
    Component {
        id: landPointComponent

        MapQuickItem {
249 250
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
251 252 253 254 255
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.landingCoordinate

            sourceItem:
                MissionItemIndexLabel {
256
                label:      "Land"
Don Gagne's avatar
Don Gagne committed
257
                checked:    _missionItem.isCurrentItem
258 259

                onClicked: setCurrentItem(_missionItem.sequenceNumber)
260
            }
261 262 263
        }
    }
}