MeasurementItemMapVisual.qml 8.33 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
    property var _transectsComponent: 	undefined
    property var _entryCoordinate: 	 	undefined
    property var _exitCoordinate: 		undefined
    property var _generatorVisuals: 	undefined
35

36
    property bool _isCurrentItem: _missionItem.isCurrentItem
37

38
    signal clicked(int sequenceNumber)
39

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

54
    Component.onCompleted: {
55
        console.assert(map != undefined, "please set the map property")
56 57 58
        _addEntryCoordinate()
        _addExitCoordinate()
        _addTransectsComponent()
59
        _addGeneratorVisuals()
60
        var bbox = boundingBox()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
61
        _missionItem.areaData.initialize(bbox[0], bbox[1])
62 63 64
    }

    Component.onDestruction: {
65 66 67
        _destroyEntryCoordinate()
        _destroyExitCoordinate()
        _destroyTransectsComponent()
68 69 70 71 72 73
        _destroyGeneratorVisuals()
    }

    on_GeneratorChanged: {
        _destroyGeneratorVisuals()
        _addGeneratorVisuals()
74 75
    }

76 77 78 79 80 81 82
    // Transect lines
    Component {
        id: visualTransectsComponent

        MapPolyline {
            line.color: "white"
            line.width: 2
83 84 85
            path:       _missionItem.route

            onPathChanged: console.log("path:" + path)
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
        }
    }

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

129

Valentin Platzgummer's avatar
Valentin Platzgummer committed
130
    // GeoAreas
131
    Repeater {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
132
        model: _missionItem.areaData.areaList
133
        delegate: GeoAreaVisualLoader {
134
            map:                _root.map
Valentin Platzgummer's avatar
Valentin Platzgummer committed
135
            geoArea: 			object
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    // Generator visuals
    function _addGeneratorVisuals(){
        if (_generator.mapVisualQml && !_generatorVisuals) {
            var component = Qt.createComponent(_generator.mapVisualQml)
            if (component.status === Component.Error) {
                console.log("Error loading Qml: ",
                            _generator.mapVisualQml, component.errorString())
            } else {
                _generatorVisuals =
                        component.createObject(_root, {
                                                   "map": _root.map,
                                                   "generator": _root._generator,
                                                   "checked": Qt.binding(
                                                                  function(){
                                                                      return _root._isCurrentItem
                                                                  })
                                               })
                _generatorVisuals.clicked.connect(
                            function(){_root.clicked(_missionItem.sequenceNumber)})
            }
        }
    }

    function _destroyGeneratorVisuals(){
        if(_generatorVisuals){
            _generatorVisuals.destroy()
            _generatorVisuals = undefined
        }
    }

    function _addTransectsComponent(){
        if (!_transectsComponent){
            _transectsComponent = visualTransectsComponent.createObject(_root)
            map.addMapItem(_transectsComponent)
        }
    }

    function _addExitCoordinate(){
        if (!_exitCoordinate){
            _exitCoordinate = exitPointComponent.createObject(_root)
            map.addMapItem(_exitCoordinate)
        }
    }

    function _addEntryCoordinate(){
        if (!_entryCoordinate){
            _entryCoordinate = entryPointComponent.createObject(_root)
            map.addMapItem(_entryCoordinate)
        }
    }

    function _destroyEntryCoordinate(){
        if (_entryCoordinate){
            map.removeMapItem(_entryCoordinate)
            _entryCoordinate.destroy()
            _entryCoordinate = undefined
        }
    }

    function _destroyExitCoordinate(){
        if (_exitCoordinate){
            map.removeMapItem(_exitCoordinate)
            _exitCoordinate.destroy()
            _exitCoordinate = undefined
        }
    }

    function _destroyTransectsComponent(){
        if (_transectsComponent){
            map.removeMapItem(_transectsComponent)
            _transectsComponent.destroy()
            _transectsComponent = undefined
        }
    }
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

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