FWLandingPatternMapVisual.qml 9.59 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 23 24

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

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

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

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

    function showItemVisuals() {
Don Gagne's avatar
Don Gagne committed
45 46 47 48 49 50 51 52 53 54 55 56 57
        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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        }
    }

    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
76 77
        for (var i=0; i<_dragAreas.length; i++) {
            _dragAreas[i].destroy()
78
        }
Don Gagne's avatar
Don Gagne committed
79
        _dragAreas = [ ]
80 81 82
    }

    function showDragAreas() {
Don Gagne's avatar
Don Gagne committed
83
        if (_dragAreas.length === 0) {
84 85
            _dragAreas.push(loiterDragAreaComponent.createObject(map))
            _dragAreas.push(landDragAreaComponent.createObject(map))
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
    function radiansToDegrees(radians) {
        return radians * (180.0 / Math.PI)
    }

    function calcPointTangentToCircleWithCenter() {
        if (_missionItem.landingCoordSet) {
            console.log("recalc")
            var radius = _missionItem.loiterRadius.value
            var loiterPointPixels = map.fromCoordinate(_missionItem.loiterCoordinate, false /* clipToViewport */)
            var landPointPixels = map.fromCoordinate(_missionItem.landingCoordinate, false /* clipToViewport */)

            var dxHypotenuse = loiterPointPixels.x - landPointPixels.x
            var dyHypotenuse = loiterPointPixels.y - landPointPixels.y
            var oppositeLength = radius
            var hypotenuseLength = _missionItem.landingCoordinate.distanceTo(_missionItem.loiterCoordinate)
            var adjacentLength = Math.sqrt(Math.pow(hypotenuseLength, 2) - Math.pow(oppositeLength, 2))
            var angleToCenterRadians = -Math.atan2(dyHypotenuse, dxHypotenuse)
            var angleCenterToTangentRadians = Math.asin(oppositeLength / hypotenuseLength)
            var angleToTangentRadians
            if (_missionItem.loiterClockwise) {
                angleToTangentRadians = angleToCenterRadians - angleCenterToTangentRadians
            } else {
                angleToTangentRadians = angleToCenterRadians + angleCenterToTangentRadians
            }
            var angleToTangentDegrees = (radiansToDegrees(angleToTangentRadians) - 90) * -1
            /*
              Keep in for debugging for now
            console.log("dxHypotenuse", dxHypotenuse)
            console.log("dyHypotenuse", dyHypotenuse)
            console.log("oppositeLength", oppositeLength)
            console.log("hypotenuseLength", hypotenuseLength)
            console.log("adjacentLength", adjacentLength)
            console.log("angleCenterToTangentRadians", angleCenterToTangentRadians, radiansToDegrees(angleCenterToTangentRadians))
            console.log("angleToCenterRadians", angleToCenterRadians, radiansToDegrees(angleToCenterRadians))
            console.log("angleToTangentDegrees", angleToTangentDegrees)
            */
            _loiterTangentCoordinate = _missionItem.landingCoordinate.atDistanceAndAzimuth(adjacentLength, angleToTangentDegrees)
            _flightPath = [ _loiterTangentCoordinate, _missionItem.landingCoordinate ]
        } else {
            _flightPath = undefined
        }
    }

132
    Component.onCompleted: {
133 134 135 136 137 138 139 140
        if (_missionItem.landingCoordSet) {
            showItemVisuals()
            if (_missionItem.isCurrentItem) {
                showDragAreas()
            }
        } else if (_missionItem.isCurrentItem) {
            showMouseArea()
        }
141
        calcPointTangentToCircleWithCenter()
142 143 144
    }

    Component.onDestruction: {
145 146 147 148 149 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
        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()
            }
175
            calcPointTangentToCircleWithCenter()
176
        }
177 178 179 180

        onLandingCoordinateChanged: calcPointTangentToCircleWithCenter()
        onLoiterCoordinateChanged:  calcPointTangentToCircleWithCenter()
        onLoiterClockwiseChanged:   calcPointTangentToCircleWithCenter()
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    }

    // 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
            }
        }
    }

200
    // Control which is used to drag the loiter point
201
    Component {
202
        id: loiterDragAreaComponent
203

204
        MissionItemIndicatorDrag {
205 206
            itemIndicator:  _itemVisuals[_loiterPointIndex]
            itemCoordinate: _missionItem.loiterCoordinate
207

208
            onItemCoordinateChanged: _missionItem.loiterCoordinate = itemCoordinate
209 210 211 212 213 214 215 216
        }
    }

    // Control which is used to drag the loiter point
    Component {
        id: landDragAreaComponent

        MissionItemIndicatorDrag {
217 218
            itemIndicator:  _itemVisuals[_landPointIndex]
            itemCoordinate: _missionItem.landingCoordinate
219

220
            onItemCoordinateChanged: _missionItem.landingCoordinate = itemCoordinate
221
        }
222 223 224 225 226 227 228
    }

    // Flight path
    Component {
        id: flightPathComponent

        MapPolyline {
229
            z:          QGroundControl.zOrderMapItems - 1   // Under item indicators
Don Gagne's avatar
Don Gagne committed
230
            line.color: "#be781c"
231
            line.width: 2
232
            path:       _flightPath
233 234 235 236 237
        }
    }

    // Loiter point
    Component {
238
        id: loiterPointComponent
239 240

        MapQuickItem {
241 242
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
243 244
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.loiterCoordinate
245 246 247

            sourceItem:
                MissionItemIndexLabel {
248 249 250 251
                label:      "Loiter"
                checked:    _missionItem.isCurrentItem

                onClicked: setCurrentItem(_missionItem.sequenceNumber)
252 253 254 255
            }
        }
    }

Don Gagne's avatar
Don Gagne committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269
    // 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"
        }
    }

270 271 272 273 274
    // Land point
    Component {
        id: landPointComponent

        MapQuickItem {
275 276
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
277 278 279 280 281
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.landingCoordinate

            sourceItem:
                MissionItemIndexLabel {
282
                label:      "Land"
Don Gagne's avatar
Don Gagne committed
283
                checked:    _missionItem.isCurrentItem
284 285

                onClicked: setCurrentItem(_missionItem.sequenceNumber)
286
            }
287 288 289
        }
    }
}