MeasurementItemMapVisual.qml 10.3 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
/****************************************************************************
 *
 *   (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 {
    id: _root

Valentin Platzgummer's avatar
Valentin Platzgummer committed
25
    property var map: undefined        ///< Map control to place item in
26 27

    property var _missionItem:      object
28 29
    property bool _editing:         _missionItem.editing
    property var _generator:        _missionItem.generator
30

31 32 33 34 35 36
    property var _routeObject: 	undefined
    property var _entryCoordinateObject: 	 	undefined
    property var _exitCoordinateObject: 		undefined
    property var _generatorObject: 	undefined
    property var _entryArrowObject: 	undefined
    property var _exitArrowObject: 	undefined
37

38
    property bool _isCurrentItem: _missionItem.isCurrentItem
39

40
    signal clicked(int sequenceNumber)
41

42 43 44 45 46 47 48 49 50 51 52 53 54
//    on_EditingChanged: {
//        if (_editing){
//            _destroyEntryCoordinate()
//            _destroyExitCoordinate()
//            _destroyTransectsComponent()
//            _destroyGeneratorVisuals()
//        } else {
//            _addEntryCoordinate()
//            _addExitCoordinate()
//            _addTransectsComponent()
//            _addGeneratorVisuals()
//        }
//    }
55

56
    Component.onCompleted: {
57
        console.assert(map != undefined, "please set the map property")
58

59
        if (!_missionItem.initialized()){
60
            var bbox = boundingBox()
61
            _missionItem.initialize(bbox[0], bbox[1])
62 63
        }
        //        _addEntryCoordinate()
64 65 66 67 68
//        _addExitCoordinate()
        _addTransects()
        _addGeneratorVisuals()
        _addEntryArrow()
        _addExitArrow()
69 70 71
    }

    Component.onDestruction: {
72 73 74
//        _destroyEntryCoordinate()
//        _destroyExitCoordinate()
        _destroyTransects()
75
        _destroyGeneratorVisuals()
76 77
        _destroyEntryArrow()
        _destroyExitArrow()
78 79 80 81 82
    }

    on_GeneratorChanged: {
        _destroyGeneratorVisuals()
        _addGeneratorVisuals()
83 84
    }

85 86 87 88 89
    // Transect lines
    Component {
        id: visualTransectsComponent

        MapPolyline {
90
            visible: !_missionItem.editing
91 92
            line.color: "white"
            line.width: 2
93
            path:       _missionItem.route
Valentin Platzgummer's avatar
Valentin Platzgummer committed
94
            z:         QGroundControl.zOrderMapItems-1
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
        }
    }

    // Entry point
    Component {
        id: entryPointComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.coordinate
            visible:        _missionItem.exitCoordinate.isValid

            sourceItem: MissionItemIndexLabel {
                index:      _missionItem.sequenceNumber
                label:      "Entry"
                checked:    _missionItem.isCurrentItem
                onClicked:  _root.clicked(_missionItem.sequenceNumber)
            }
        }
    }

    // Exit point
    Component {
        id: exitPointComponent

        MapQuickItem {
            anchorPoint.x:  sourceItem.anchorPointX
            anchorPoint.y:  sourceItem.anchorPointY
            z:              QGroundControl.zOrderMapItems
            coordinate:     _missionItem.exitCoordinate
            visible:        _missionItem.exitCoordinate.isValid

            sourceItem: MissionItemIndexLabel {
                index:      _missionItem.lastSequenceNumber
                label:      "Exit"
                checked:    _missionItem.isCurrentItem
                onClicked:  _root.clicked(_missionItem.sequenceNumber)
            }
        }
    }
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    // Entry arrow
    Component {
        id: entryArrowComponent

        MapLineArrow {
            property int length: _missionItem.route.length
            property var route: _missionItem.route

            fromCoord:      length > 1 ? route[0] : QtPositioning.coordinate()
            toCoord:        length > 1 ? route[1] : QtPositioning.coordinate()
            arrowPosition:  2
            visible: length > 1 && !_missionItem.editing
            opacity: _root.opacity
        }
    }

    // Exit arrow
    Component {
        id: exitArrowComponent

        MapLineArrow {
            property int length: _missionItem.route.length
            property var route: _missionItem.route

            fromCoord:      length > 3 ? route[length-2] : QtPositioning.coordinate()
            toCoord:        length > 3 ? route[length-1] : QtPositioning.coordinate()
            arrowPosition:  2
            visible: length > 3 && !_missionItem.editing
            opacity:        _root.opacity
        }
    }
169

Valentin Platzgummer's avatar
Valentin Platzgummer committed
170
    // GeoAreas
171
    Repeater {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
172
        model: _missionItem.areaData.areaList
173
        delegate: GeoAreaVisualLoader {
174
            map:                _root.map
Valentin Platzgummer's avatar
Valentin Platzgummer committed
175
            geoArea: 			object
176 177 178
        }
    }

179 180
    // Generator visuals
    function _addGeneratorVisuals(){
181
        if (_generator && _generator.mapVisualQml && !_generatorObject) {
182 183 184 185 186
            var component = Qt.createComponent(_generator.mapVisualQml)
            if (component.status === Component.Error) {
                console.log("Error loading Qml: ",
                            _generator.mapVisualQml, component.errorString())
            } else {
187 188 189 190 191 192
                _generatorObject = component.createObject(_root, {
                                                               "map": _root.map,
                                                               "generator": _root._generator,
                                                               "checked": Qt.binding(function(){return _root._isCurrentItem})
                                                           })
                _generatorObject.clicked.connect(
193 194 195 196 197 198
                            function(){_root.clicked(_missionItem.sequenceNumber)})
            }
        }
    }

    function _destroyGeneratorVisuals(){
199 200 201
        if(_generatorObject){
            _generatorObject.destroy()
            _generatorObject = undefined
202 203 204
        }
    }

205 206 207 208
    function _addTransects(){
        if (!_routeObject){
            _routeObject= visualTransectsComponent.createObject(_root)
            map.addMapItem(_routeObject)
209 210 211 212
        }
    }

    function _addExitCoordinate(){
213 214 215
        if (!_exitCoordinateObject){
            _exitCoordinateObject = exitPointComponent.createObject(_root)
            map.addMapItem(_exitCoordinateObject)
216 217 218 219
        }
    }

    function _addEntryCoordinate(){
220 221 222 223 224 225 226 227 228 229
        if (!_entryCoordinateObject){
            _entryCoordinateObject = entryPointComponent.createObject(_root)
            map.addMapItem(_entryCoordinateObject)
        }
    }

    function _addEntryArrow(){
        if (!_entryArrowObject){
            _entryArrowObject = entryArrowComponent.createObject(_root)
            map.addMapItem(_entryArrowObject)
230 231 232
        }
    }

233 234 235 236 237 238
    function _addExitArrow(){
        if (!_exitArrowObject){
            _exitArrowObject = exitArrowComponent.createObject(_root)
            map.addMapItem(_exitArrowObject)
        }
    }
239
    function _destroyEntryCoordinate(){
240 241 242 243
        if (_entryCoordinateObject){
            map.removeMapItem(_entryCoordinateObject)
            _entryCoordinateObject.destroy()
            _entryCoordinateObject = undefined
244 245 246 247
        }
    }

    function _destroyExitCoordinate(){
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        if (_exitCoordinateObject){
            map.removeMapItem(_exitCoordinateObject)
            _exitCoordinateObject.destroy()
            _exitCoordinateObject = undefined
        }
    }

    function _destroyTransects(){
        if (_routeObject){
            map.removeMapItem(_routeObject)
            _routeObject.destroy()
            _routeObject= undefined
        }
    }

    function _destroyEntryArrow(){
        if (_entryArrowObject){
            map.removeMapItem(_entryArrowObject)
            _entryArrowObject.destroy()
            _entryArrowObject = undefined
268 269 270
        }
    }

271 272 273 274 275
    function _destroyExitArrow(){
        if (_exitArrowObject){
            map.removeMapItem(_exitArrowObject)
            _exitArrowObject.destroy()
            _exitArrowObject = undefined
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

    function boundingBox() {
        // Initial polygon is inset to take 2/3rds space
        var rect = Qt.rect(map.centerViewport.x,
                           map.centerViewport.y,
                           map.centerViewport.width,
                           map.centerViewport.height)
        rect.x += (rect.width * 0.25) / 2
        rect.y += (rect.height * 0.25) / 2
        rect.width *= 0.75
        rect.height *= 0.75

        var centerCoord =       map.toCoordinate(Qt.point(rect.x + (rect.width / 2),
                                                          rect.y + (rect.height / 2)),
                                                 false /* clipToViewPort */)
        var topRightCoord =     map.toCoordinate(Qt.point(rect.x + rect.width, rect.y),                             false /* clipToViewPort */)
        var bottomLeftCoord =   map.toCoordinate(Qt.point(rect.x, rect.y + rect.height),                            false /* clipToViewPort */)
        var topLeftCoord =      map.toCoordinate(Qt.point(rect.x, rect.y),                                          false /* clipToViewPort */)
        var bottomRightCoord =  map.toCoordinate(Qt.point(rect.x + rect.width, rect.y + rect.height),               false /* clipToViewPort */)

        // Initial polygon has max width and height of 3000 meters
        var halfWidthMeters =   Math.min(topLeftCoord.distanceTo(topRightCoord), 3000) / 2
        var halfHeightMeters =  Math.min(topLeftCoord.distanceTo(bottomLeftCoord), 3000) / 2
        topRightCoord =     centerCoord.atDistanceAndAzimuth(halfWidthMeters, 90).atDistanceAndAzimuth(halfHeightMeters, 0)
        bottomLeftCoord =   centerCoord.atDistanceAndAzimuth(halfWidthMeters, -90).atDistanceAndAzimuth(halfHeightMeters, 180)

        return [  bottomLeftCoord, topRightCoord ]
    }
306
}