MeasurementItemMapVisual.qml 10.1 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
        var bbox = boundingBox()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
59
        _missionItem.areaData.initialize(bbox[0], bbox[1])
60 61 62 63 64 65
//        _addEntryCoordinate()
//        _addExitCoordinate()
        _addTransects()
        _addGeneratorVisuals()
        _addEntryArrow()
        _addExitArrow()
66 67 68
    }

    Component.onDestruction: {
69 70 71
//        _destroyEntryCoordinate()
//        _destroyExitCoordinate()
        _destroyTransects()
72
        _destroyGeneratorVisuals()
73 74
        _destroyEntryArrow()
        _destroyExitArrow()
75 76 77 78 79
    }

    on_GeneratorChanged: {
        _destroyGeneratorVisuals()
        _addGeneratorVisuals()
80 81
    }

82 83 84 85 86
    // Transect lines
    Component {
        id: visualTransectsComponent

        MapPolyline {
87
            visible: !_missionItem.editing
88 89
            line.color: "white"
            line.width: 2
90
            path:       _missionItem.route
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
        }
    }

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

134 135 136 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
    // 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
        }
    }
165

Valentin Platzgummer's avatar
Valentin Platzgummer committed
166
    // GeoAreas
167
    Repeater {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
168
        model: _missionItem.areaData.areaList
169
        delegate: GeoAreaVisualLoader {
170
            map:                _root.map
Valentin Platzgummer's avatar
Valentin Platzgummer committed
171
            geoArea: 			object
172 173 174
        }
    }

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

    function _destroyGeneratorVisuals(){
195 196 197
        if(_generatorObject){
            _generatorObject.destroy()
            _generatorObject = undefined
198 199 200
        }
    }

201 202 203 204
    function _addTransects(){
        if (!_routeObject){
            _routeObject= visualTransectsComponent.createObject(_root)
            map.addMapItem(_routeObject)
205 206 207 208
        }
    }

    function _addExitCoordinate(){
209 210 211
        if (!_exitCoordinateObject){
            _exitCoordinateObject = exitPointComponent.createObject(_root)
            map.addMapItem(_exitCoordinateObject)
212 213 214 215
        }
    }

    function _addEntryCoordinate(){
216 217 218 219 220 221 222 223 224 225
        if (!_entryCoordinateObject){
            _entryCoordinateObject = entryPointComponent.createObject(_root)
            map.addMapItem(_entryCoordinateObject)
        }
    }

    function _addEntryArrow(){
        if (!_entryArrowObject){
            _entryArrowObject = entryArrowComponent.createObject(_root)
            map.addMapItem(_entryArrowObject)
226 227 228
        }
    }

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

    function _destroyExitCoordinate(){
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        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
264 265 266
        }
    }

267 268 269 270 271
    function _destroyExitArrow(){
        if (_exitArrowObject){
            map.removeMapItem(_exitArrowObject)
            _exitArrowObject.destroy()
            _exitArrowObject = undefined
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

    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 ]
    }
302
}