ProximityRadarMapView.qml 6.11 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/****************************************************************************
 *
 * (c) 2009-2020 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.12
import QtLocation               5.3
import QtPositioning            5.3
import QtGraphicalEffects       1.0

import QGroundControl               1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Vehicle       1.0
import QGroundControl.Controls      1.0



/// Marker for displaying a vehicle location on the map
MapQuickItem {
    id:             _root
    property var    vehicle                                                         /// Vehicle object, undefined for ADSB vehicle
    property var    map
    property double heading:        vehicle ? vehicle.heading.value : Number.NaN    ///< Vehicle heading, NAN for none

    anchorPoint.x:  vehicleItem.width  / 2
    anchorPoint.y:  vehicleItem.height / 2
    visible:        coordinate.isValid

    property bool   _adsbVehicle:   vehicle ? false : true
    property real   _uavSize:       ScreenTools.defaultFontPixelHeight * 5
    property real   _adsbSize:      ScreenTools.defaultFontPixelHeight * 2.5
    property var    _map:           map
    property bool   _multiVehicle:  QGroundControl.multiVehicleManager.vehicles.count > 1
    property real   _ratio:         1

    property var    _distanceSensor:        vehicle?vehicle.distanceSensors:null
    property var    _maxSensor:             _distanceSensor?_distanceSensor.maxDistance.value:1 //need to change in cc
    property var    _rotationNone:          _distanceSensor?_distanceSensor.rotationNone.value:0
    property var    _rotationYaw45:         _distanceSensor?_distanceSensor.rotationYaw45.value:0
    property var    _rotationYaw90:         _distanceSensor?_distanceSensor.rotationYaw90.value:0
    property var    _rotationYaw135:        _distanceSensor?_distanceSensor.rotationYaw135.value:0
    property var    _rotationYaw180:        _distanceSensor?_distanceSensor.rotationYaw180.value:0
    property var    _rotationYaw225:        _distanceSensor?_distanceSensor.rotationYaw225.value:0
    property var    _rotationYaw270:        _distanceSensor?_distanceSensor.rotationYaw270.value:0
    property var    _rotationYaw315:        _distanceSensor?_distanceSensor.rotationYaw315.value:0
    property var    _rottab:                [_rotationNone,_rotationYaw45,_rotationYaw90,_rotationYaw135,_rotationYaw180,_rotationYaw225,_rotationYaw270,_rotationYaw315]

    function calcSize(){
        if(_map) {
            var scaleLinePixelLength = 100
            var leftCoord  = _map.toCoordinate(Qt.point(0, 0), false /* clipToViewPort */)
            var rightCoord = _map.toCoordinate(Qt.point(scaleLinePixelLength, 0), false /* clipToViewPort */)
            var scaleLineMeters = Math.round(leftCoord.distanceTo(rightCoord))
        }
        _ratio=scaleLinePixelLength/scaleLineMeters;
    }

    on_RotationNoneChanged:     vehicleSensors.requestPaint()
    on_RotationYaw45Changed:    vehicleSensors.requestPaint()
    on_RotationYaw90Changed:    vehicleSensors.requestPaint()
    on_RotationYaw135Changed:   vehicleSensors.requestPaint()
    on_RotationYaw180Changed:   vehicleSensors.requestPaint()
    on_RotationYaw225Changed:   vehicleSensors.requestPaint()
    on_RotationYaw270Changed:   vehicleSensors.requestPaint()
    on_RotationYaw315Changed:   vehicleSensors.requestPaint()



    Connections {
        target:             map
        onWidthChanged:     scaleTimer.restart()
        onHeightChanged:    scaleTimer.restart()
        onZoomLevelChanged: scaleTimer.restart()
    }

    Timer {
        id:                 scaleTimer
        interval:           100
        running:            false
        repeat:             false
        onTriggered:        calcSize()
    }

    sourceItem: Item {
        id:         vehicleItem
        width:      detectionLimitCircle.width
        height:     detectionLimitCircle.height
        opacity:    0.5

        Canvas{
            id:                 vehicleSensors
            anchors.fill:       detectionLimitCircle
            transform: Rotation {
                origin.x:       detectionLimitCircle.width  / 2
                origin.y:       detectionLimitCircle.height / 2
                angle:          isNaN(heading) ? 0 : heading
            }
            function deg2rad(degrees)
            {
              var pi = Math.PI;
              return degrees * (pi/180);
            }
            onPaint: {
                    if(_distanceSensor)
                        {
                            var ctx = getContext("2d");
                            ctx.reset();
                            ctx.translate(width/2,height/2)
                            ctx.rotate(-Math.PI/2);
                            ctx.lineWidth = 5;
                            ctx.strokeStyle = Qt.rgba(1, 0, 0, 1);
                            for(var i=0; i<_rottab.length;i++){
                                var a=deg2rad(360-22.5)+Math.PI/4*i;
                                ctx.beginPath();
                                ctx.arc(0,0,_rottab[i]*_ratio,   a,a+Math.PI/4,false);
                                ctx.stroke();
                            }

                        }
                }
        }

        Rectangle {
            id:                 detectionLimitCircle
            width:              _maxSensor*2*_ratio
            height:             _maxSensor*2*_ratio
            anchors.fill:       detectionLimitCircle
            color:              Qt.rgba(1,1,1,0)
            border.color:       Qt.rgba(1,1,1,1)
            border.width:       5
            radius:             width*0.5
            transform: Rotation {
                origin.x:       detectionLimitCircle.width  / 2
                origin.y:       detectionLimitCircle.height / 2
                angle:          isNaN(heading) ? 0 : heading
            }
        }

        Component.onCompleted: {
                calcSize();
            }
    }
}