FWLandingPatternMapVisual.qml 9.42 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

/// Fixed Wing Landing Pattern map visuals
Item {
23 24
    id: _root

DonLakeFlyer's avatar
DonLakeFlyer committed
25 26
    property var map        ///< Map control to place item in
    property var qgcView    ///< QGCView to use for popping dialogs
27

28 29
    signal clicked(int sequenceNumber)

30
    property var _missionItem:  object
Don Gagne's avatar
Don Gagne committed
31
    property var _itemVisuals: [ ]
32
    property var _mouseArea
Don Gagne's avatar
Don Gagne committed
33
    property var _dragAreas: [ ]
34
    property var _flightPath
Don Gagne's avatar
Don Gagne committed
35 36 37 38

    readonly property int _flightPathIndex:     0
    readonly property int _loiterPointIndex:    1
    readonly property int _loiterRadiusIndex:   2
DonLakeFlyer's avatar
DonLakeFlyer committed
39 40
    readonly property int _landingAreaIndex:    3
    readonly property int _landPointIndex:      4
41

42
    function hideItemVisuals() {
Don Gagne's avatar
Don Gagne committed
43 44
        for (var i=0; i<_itemVisuals.length; i++) {
            _itemVisuals[i].destroy()
45
        }
Don Gagne's avatar
Don Gagne committed
46
        _itemVisuals = [ ]
47 48 49
    }

    function showItemVisuals() {
Don Gagne's avatar
Don Gagne committed
50 51 52 53 54 55 56 57 58 59
        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
DonLakeFlyer's avatar
DonLakeFlyer committed
60 61 62
            itemVisual = landingAreaComponent.createObject(map)
            map.addMapItem(itemVisual)
            _itemVisuals[_landingAreaIndex] = itemVisual
Don Gagne's avatar
Don Gagne committed
63 64 65
            itemVisual = landPointComponent.createObject(map)
            map.addMapItem(itemVisual)
            _itemVisuals[_landPointIndex] = itemVisual
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        }
    }

    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
84 85
        for (var i=0; i<_dragAreas.length; i++) {
            _dragAreas[i].destroy()
86
        }
Don Gagne's avatar
Don Gagne committed
87
        _dragAreas = [ ]
88 89 90
    }

    function showDragAreas() {
Don Gagne's avatar
Don Gagne committed
91
        if (_dragAreas.length === 0) {
92 93
            _dragAreas.push(loiterDragAreaComponent.createObject(map))
            _dragAreas.push(landDragAreaComponent.createObject(map))
94 95 96
        }
    }

97 98
    function _setFlightPath() {
        _flightPath = [ _missionItem.loiterTangentCoordinate, _missionItem.landingCoordinate ]
99 100
    }

101
    Component.onCompleted: {
102 103
        if (_missionItem.landingCoordSet) {
            showItemVisuals()
104
            if (!_missionItem.flyView && _missionItem.isCurrentItem) {
105 106
                showDragAreas()
            }
107
            _setFlightPath()
108
        } else if (!_missionItem.flyView && _missionItem.isCurrentItem) {
109 110
            showMouseArea()
        }
111 112 113
    }

    Component.onDestruction: {
114 115 116 117 118 119 120 121 122
        hideDragAreas()
        hideMouseArea()
        hideItemVisuals()
    }

    Connections {
        target: _missionItem

        onIsCurrentItemChanged: {
123 124 125
            if (_missionItem.flyView) {
                return
            }
126 127 128 129 130 131 132 133 134 135 136 137 138
            if (_missionItem.isCurrentItem) {
                if (_missionItem.landingCoordSet) {
                    showDragAreas()
                } else {
                    showMouseArea()
                }
            } else {
                hideMouseArea()
                hideDragAreas()
            }
        }

        onLandingCoordSetChanged: {
139 140 141
            if (_missionItem.flyView) {
                return
            }
142 143 144 145
            if (_missionItem.landingCoordSet) {
                hideMouseArea()
                showItemVisuals()
                showDragAreas()
146
                _setFlightPath()
147 148 149 150 151
            } else if (_missionItem.isCurrentItem) {
                hideDragAreas()
                showMouseArea()
            }
        }
152

153 154
        onLandingCoordinateChanged:         _setFlightPath()
        onLoiterTangentCoordinateChanged:   _setFlightPath()
155 156 157 158 159 160 161 162 163 164
    }

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

        MouseArea {
            anchors.fill: map

            onClicked: {
DonLakeFlyer's avatar
DonLakeFlyer committed
165
                var coordinate = map.toCoordinate(Qt.point(mouse.x, mouse.y), false /* clipToViewPort */)
166 167 168 169 170 171 172 173
                coordinate.latitude = coordinate.latitude.toFixed(_decimalPlaces)
                coordinate.longitude = coordinate.longitude.toFixed(_decimalPlaces)
                coordinate.altitude = coordinate.altitude.toFixed(_decimalPlaces)
                _missionItem.landingCoordinate = coordinate
            }
        }
    }

174
    // Control which is used to drag the loiter point
175
    Component {
176
        id: loiterDragAreaComponent
177

178
        MissionItemIndicatorDrag {
179 180
            itemIndicator:  _itemVisuals[_loiterPointIndex]
            itemCoordinate: _missionItem.loiterCoordinate
181

182
            onItemCoordinateChanged: _missionItem.loiterCoordinate = itemCoordinate
183 184 185 186 187 188 189 190
        }
    }

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

        MissionItemIndicatorDrag {
191 192
            itemIndicator:  _itemVisuals[_landPointIndex]
            itemCoordinate: _missionItem.landingCoordinate
193

194
            onItemCoordinateChanged: _missionItem.landingCoordinate = itemCoordinate
195
        }
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:       _flightPath
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
                index:      _missionItem.sequenceNumber
223 224 225
                label:      "Loiter"
                checked:    _missionItem.isCurrentItem

226
                onClicked: _root.clicked(_missionItem.sequenceNumber)
227 228 229 230
            }
        }
    }

Don Gagne's avatar
Don Gagne committed
231 232 233 234 235 236
    Component {
        id: loiterRadiusComponent

        MapCircle {
            z:              QGroundControl.zOrderMapItems
            center:         _missionItem.loiterCoordinate
237
            radius:         _missionItem.loiterRadius.rawValue
Don Gagne's avatar
Don Gagne committed
238 239 240 241 242 243
            border.width:   2
            border.color:   "green"
            color:          "transparent"
        }
    }

244 245 246 247
    Component {
        id: landPointComponent

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

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

259
                onClicked: _root.clicked(_missionItem.sequenceNumber)
260
            }
261 262
        }
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
263 264 265 266

    Component {
        id: landingAreaComponent

DonLakeFlyer's avatar
DonLakeFlyer committed
267
        MapPolygon {
DonLakeFlyer's avatar
DonLakeFlyer committed
268 269 270 271 272 273
            z:              QGroundControl.zOrderMapItems
            border.width:   1
            border.color:   "black"
            color:          "green"
            opacity:        0.5

DonLakeFlyer's avatar
DonLakeFlyer committed
274
            readonly property real landingWidth:    15
DonLakeFlyer's avatar
DonLakeFlyer committed
275 276
            readonly property real landingLength:   100
            readonly property real angleRadians:    Math.atan((landingWidth / 2) / (landingLength / 2))
DonLakeFlyer's avatar
DonLakeFlyer committed
277
            readonly property real angleDegrees:    (angleRadians * (180 / Math.PI))
DonLakeFlyer's avatar
DonLakeFlyer committed
278
            readonly property real hypotenuse:      (landingWidth / 2) / Math.sin(angleRadians)
DonLakeFlyer's avatar
DonLakeFlyer committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

            property real landingAreaAngle: _missionItem.landingCoordinate.azimuthTo(_missionItem.loiterTangentCoordinate)

            function calcPoly() {
                path = [ ]
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, landingAreaAngle - angleDegrees))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, landingAreaAngle + angleDegrees))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, landingAreaAngle + (180 - angleDegrees)))
                addCoordinate(_missionItem.landingCoordinate.atDistanceAndAzimuth(hypotenuse, landingAreaAngle - (180 - angleDegrees)))
            }

            Component.onCompleted: calcPoly()

            Connections {
                target: _missionItem
                onLandingCoordinateChanged: calcPoly()
                onLoiterTangentCoordinateChanged:  calcPoly()
            }
DonLakeFlyer's avatar
DonLakeFlyer committed
297 298
        }
    }
299
}