InstrumentValue.qml 6.45 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
/****************************************************************************
 *
 * (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 QtQuick.Layouts  1.2
import QtQuick.Controls 2.5

import QGroundControl.Controls      1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0

Item {
    id:     root
    height: value.y + value.height

    property var    instrumentValue:                null
    property bool   recalcOk:                       false

    property var    _rgFontSizes:                   [ ScreenTools.defaultFontPointSize, ScreenTools.smallFontPointSize, ScreenTools.mediumFontPointSize, ScreenTools.largeFontPointSize ]
    property var    _rgFontSizeRatios:              [ 1, ScreenTools.smallFontPointRatio, ScreenTools.mediumFontPointRatio, ScreenTools.largeFontPointRatio ]
    property real   _doubleDescent:                 ScreenTools.defaultFontDescent * 2
    property real   _tightDefaultFontHeight:        ScreenTools.defaultFontPixelHeight - _doubleDescent
    property var    _rgFontSizeTightHeights:        [ _tightDefaultFontHeight * _rgFontSizeRatios[0] + 2, _tightDefaultFontHeight * _rgFontSizeRatios[1] + 2, _tightDefaultFontHeight * _rgFontSizeRatios[2] + 2, _tightDefaultFontHeight * _rgFontSizeRatios[3] + 2 ]
    property real   _blankEntryHeight:              ScreenTools.defaultFontPixelHeight * 2

    // After fighting with using layout and/or anchors I gave up and just do a manual recalc to position items which ends up being much simpler
    function recalcPositions() {
        if (!recalcOk) {
            return
        }
        var smallSpacing = 2
        if (instrumentValue.icon) {
            if (instrumentValue.labelPosition === InstrumentValue.LabelAbove) {
                valueIcon.x = (width - valueIcon.width) / 2
                valueIcon.y = 0
                value.x = (width - value.width) / 2
                value.y = valueIcon.height + smallSpacing
            } else {
                var iconPlusValueWidth = valueIcon.width + value.width + ScreenTools.defaultFontPixelWidth
                valueIcon.x = (width - iconPlusValueWidth) / 2
                valueIcon.y = (value.height - valueIcon.height) / 2
                value.x = valueIcon.x + valueIcon.width + (ScreenTools.defaultFontPixelWidth / 2)
                value.y = 0
            }
            label.x = label.y = 0
        } else {
            // label above value
            if (instrumentValue.text) {
                label.x = (width - label.width) / 2
                label.y = 0
                value.y = label.height + smallSpacing
            } else {
                value.y = 0
            }
            value.x = (width - value.width) / 2
            valueIcon.x = valueIcon.y = 0
        }
    }

    onRecalcOkChanged:    recalcPositions()
    onWidthChanged:                         recalcPositions()

    Connections {
        target:                 instrumentValue
        onIconChanged:          recalcPositions()
        onLabelPositionChanged: recalcPositions()
    }

    QGCColoredImage {
        id:                         valueIcon
        height:                     _rgFontSizeTightHeights[instrumentValue.fontSize]
        width:                      height
        source:                     icon
        sourceSize.height:          height
        fillMode:                   Image.PreserveAspectFit
        mipmap:                     true
        smooth:                     true
        color:                      instrumentValue.isValidColor(instrumentValue.currentColor) ? instrumentValue.currentColor : qgcPal.text
        opacity:                    instrumentValue.currentOpacity
        visible:                    instrumentValue.icon
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()

        property string icon
        readonly property string iconPrefix: "/InstrumentValueIcons/"

        function updateIcon() {
            if (instrumentValue.rangeType == InstrumentValue.IconSelectRange) {
                icon = iconPrefix + instrumentValue.currentIcon
            } else if (instrumentValue.icon) {
                icon = iconPrefix + instrumentValue.icon
            } else {
                icon = ""
            }
        }

        Connections {
            target:                 instrumentValue
            onRangeTypeChanged:     valueIcon.updateIcon()
            onCurrentIconChanged:   valueIcon.updateIcon()
            onIconChanged:          valueIcon.updateIcon()
        }
        Component.onCompleted:      updateIcon();
    }

    QGCLabel {
        id:                         blank
        anchors.horizontalCenter:   parent.horizontalCenter
        height:                     _columnButtonsTotalHeight
        font.pointSize:             ScreenTools.smallFontPointSize
        text:                       _settingsUnlocked ? qsTr("BLANK") : ""
        horizontalAlignment:        Text.AlignHCenter
        verticalAlignment:          Text.AlignVCenter
        visible:                    !instrumentValue.fact
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()
    }

    QGCLabel {
        id:                         label
        height:                     _rgFontSizeTightHeights[InstrumentValue.SmallFontSize]
        font.pointSize:             ScreenTools.smallFontPointSize
        text:                       instrumentValue.text.toUpperCase()
        verticalAlignment:          Text.AlignVCenter
        visible:                    instrumentValue.fact && instrumentValue.text && !instrumentValue.icon
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()
    }

    QGCLabel {
        id:                         value
        font.pointSize:             _rgFontSizes[instrumentValue.fontSize]
        text:                       visible ? (instrumentValue.fact.enumOrValueString + (instrumentValue.showUnits ? instrumentValue.fact.units : "")) : ""
        verticalAlignment:          Text.AlignVCenter
        visible:                    instrumentValue.fact
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()
    }
}