MapScale.qml 8.4 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
import QtQuick          2.3
import QtQuick.Controls 1.2
12

13 14 15 16
import QGroundControl                   1.0
import QGroundControl.Controls          1.0
import QGroundControl.ScreenTools       1.0
import QGroundControl.SettingsManager   1.0
17 18 19 20

/// Map scale control
Item {
    id:     scale
21
    width:  buttonsOnLeft || !_zoomButtonsVisible ? rightEnd.x + rightEnd.width : zoomDownButton.x + zoomDownButton.width
22 23
    height: rightEnd.y + rightEnd.height

24 25 26 27 28 29 30
    property var    mapControl                      ///< Map control for which this scale control is being used
    property bool   terrainButtonVisible:   false
    property alias  terrainButtonChecked:   terrainButton.checked
    property bool   zoomButtonsVisible:     true
    property bool   buttonsOnLeft:          true    ///< Buttons to left/right of scale bar

    signal terrainButtonClicked
31

Don Gagne's avatar
Don Gagne committed
32 33
    property variant _scaleLengthsMeters: [5, 10, 25, 50, 100, 150, 250, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000]
    property variant _scaleLengthsFeet: [10, 25, 50, 100, 250, 500, 1000, 2000, 3000, 4000, 5280, 5280*2, 5280*5, 5280*10, 5280*25, 5280*50, 5280*100, 5280*250, 5280*500, 5280*1000]
34

35 36
    property bool _zoomButtonsVisible:      zoomButtonsVisible && !ScreenTools.isMobile
    property bool _terrainButtonVisible:    terrainButtonVisible && !ScreenTools.isMobile
37

38
    property var _color: mapControl.isSatelliteMap ? "white" : "black"
39

Don Gagne's avatar
Don Gagne committed
40
    function formatDistanceMeters(meters) {
41 42 43 44
        var dist = Math.round(meters)
        if (dist > 1000 ){
            if (dist > 100000){
                dist = Math.round(dist / 1000)
Don Gagne's avatar
Don Gagne committed
45
            } else {
46 47 48
                dist = Math.round(dist / 100)
                dist = dist / 10
            }
Don Gagne's avatar
Don Gagne committed
49 50 51
            dist = dist + qsTr(" km")
        } else {
            dist = dist + qsTr(" m")
52
        }
Don Gagne's avatar
Don Gagne committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        return dist
    }

    function formatDistanceFeet(feet) {
        var dist = Math.round(feet)
        if (dist >= 5280) {
            dist = Math.round(dist / 5280)
            dist = dist
            if (dist == 1) {
                dist += qsTr(" mile")
            } else {
                dist += qsTr(" miles")
            }
        } else {
            dist = dist + qsTr(" ft")
68 69 70 71
        }
        return dist
    }

Don Gagne's avatar
Don Gagne committed
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
    function calculateMetersRatio(scaleLineMeters, scaleLinePixelLength) {
        var scaleLineRatio = 0

        if (scaleLineMeters === 0) {
            // not visible
        } else {
            for (var i = 0; i < _scaleLengthsMeters.length - 1; i++) {
                if (scaleLineMeters < (_scaleLengthsMeters[i] + _scaleLengthsMeters[i+1]) / 2 ) {
                    scaleLineRatio = _scaleLengthsMeters[i] / scaleLineMeters
                    scaleLineMeters = _scaleLengthsMeters[i]
                    break;
                }
            }
            if (scaleLineRatio === 0) {
                scaleLineRatio = scaleLineMeters / _scaleLengthsMeters[i]
                scaleLineMeters = _scaleLengthsMeters[i]
            }
        }

        var text = formatDistanceMeters(scaleLineMeters)
        centerLine.width = (scaleLinePixelLength * scaleLineRatio) - (2 * leftEnd.width)
        scaleText.text = text
    }

    function calculateFeetRatio(scaleLineMeters, scaleLinePixelLength) {
        var scaleLineRatio = 0
        var scaleLineFeet = scaleLineMeters * 3.2808399

        if (scaleLineFeet === 0) {
101 102
            // not visible
        } else {
Don Gagne's avatar
Don Gagne committed
103 104 105 106
            for (var i = 0; i < _scaleLengthsFeet.length - 1; i++) {
                if (scaleLineFeet < (_scaleLengthsFeet[i] + _scaleLengthsFeet[i+1]) / 2 ) {
                    scaleLineRatio = _scaleLengthsFeet[i] / scaleLineFeet
                    scaleLineFeet = _scaleLengthsFeet[i]
107 108 109
                    break;
                }
            }
Don Gagne's avatar
Don Gagne committed
110 111 112
            if (scaleLineRatio === 0) {
                scaleLineRatio = scaleLineFeet / _scaleLengthsFeet[i]
                scaleLineFeet = _scaleLengthsFeet[i]
113 114
            }
        }
Don Gagne's avatar
Don Gagne committed
115 116 117

        var text = formatDistanceFeet(scaleLineFeet)
        centerLine.width = (scaleLinePixelLength * scaleLineRatio) - (2 * leftEnd.width)
118 119 120
        scaleText.text = text
    }

Don Gagne's avatar
Don Gagne committed
121
    function calculateScale() {
122 123 124 125 126 127 128 129 130 131
        if(mapControl) {
            var scaleLinePixelLength = 100
            var leftCoord  = mapControl.toCoordinate(Qt.point(0, scale.y), false /* clipToViewPort */)
            var rightCoord = mapControl.toCoordinate(Qt.point(scaleLinePixelLength, scale.y), false /* clipToViewPort */)
            var scaleLineMeters = Math.round(leftCoord.distanceTo(rightCoord))
            if (QGroundControl.settingsManager.unitsSettings.distanceUnits.value === UnitsSettings.DistanceUnitsFeet) {
                calculateFeetRatio(scaleLineMeters, scaleLinePixelLength)
            } else {
                calculateMetersRatio(scaleLineMeters, scaleLinePixelLength)
            }
Don Gagne's avatar
Don Gagne committed
132 133 134
        }
    }

135
    Connections {
136 137 138
        target:             mapControl
        onWidthChanged:     scaleTimer.restart()
        onHeightChanged:    scaleTimer.restart()
139 140 141 142
        onZoomLevelChanged: scaleTimer.restart()
    }

    Timer {
143 144 145 146 147
        id:                 scaleTimer
        interval:           100
        running:            false
        repeat:             false
        onTriggered:        calculateScale()
148 149
    }

150
    QGCMapLabel {
151 152 153 154
        id:                 scaleText
        map:                mapControl
        font.family:        ScreenTools.demiboldFontFamily
        anchors.left:       parent.left
155
        anchors.right:      rightEnd.right
156 157
        horizontalAlignment:Text.AlignRight
        text:               "0 m"
158 159 160
    }

    Rectangle {
161 162
        id:                 leftEnd
        anchors.top:        scaleText.bottom
163 164
        anchors.leftMargin: buttonsOnLeft && _zoomButtonsVisible ? ScreenTools.defaultFontPixelWidth / 2 : 0
        anchors.left:       buttonsOnLeft && _zoomButtonsVisible ? zoomDownButton.right : parent.left
165 166 167
        width:              2
        height:             ScreenTools.defaultFontPixelHeight
        color:              _color
168 169 170
    }

    Rectangle {
171
        id:                 centerLine
172
        anchors.bottomMargin:   2
173 174 175 176
        anchors.bottom:     leftEnd.bottom
        anchors.left:       leftEnd.right
        height:             2
        color:              _color
177 178 179
    }

    Rectangle {
180 181 182 183 184 185
        id:                 rightEnd
        anchors.top:        leftEnd.top
        anchors.left:       centerLine.right
        width:              2
        height:             ScreenTools.defaultFontPixelHeight
        color:              _color
186 187
    }

188 189 190 191 192 193 194 195 196 197 198 199 200
    QGCButton {
        id:                 terrainButton
        anchors.top:        scaleText.top
        anchors.bottom:     rightEnd.bottom
        anchors.leftMargin: buttonsOnLeft ? 0 : ScreenTools.defaultFontPixelWidth / 2
        anchors.left:       buttonsOnLeft ? parent.left : rightEnd.right
        text:               qsTr("T")
        width:              height
        opacity:            0.75
        visible:            _terrainButtonVisible
        onClicked:          terrainButtonClicked()
    }

201 202 203 204
    QGCButton {
        id:                 zoomUpButton
        anchors.top:        scaleText.top
        anchors.bottom:     rightEnd.bottom
205 206
        anchors.leftMargin: terrainButton.visible ? ScreenTools.defaultFontPixelWidth / 2 : 0
        anchors.left:       terrainButton.visible ? terrainButton.right : terrainButton.left
207 208 209 210 211 212 213 214 215
        text:               qsTr("+")
        width:              height
        opacity:            0.75
        visible:            _zoomButtonsVisible
        onClicked:          mapControl.zoomLevel += 0.5
    }

    QGCButton {
        id:                 zoomDownButton
216 217
        anchors.top:        scaleText.top
        anchors.bottom:     rightEnd.bottom
218 219 220
        anchors.leftMargin: ScreenTools.defaultFontPixelWidth / 2
        anchors.left:       zoomUpButton.right
        text:               qsTr("-")
221
        width:              height
222 223 224 225 226
        opacity:            0.75
        visible:            _zoomButtonsVisible
        onClicked:          mapControl.zoomLevel -= 0.5
    }

227 228 229 230 231 232
    Component.onCompleted: {
        if (scale.visible) {
            calculateScale();
        }
    }
}