InstrumentValue.qml 7.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/****************************************************************************
 *
 * (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

14
import QGroundControl               1.0
15 16 17 18 19 20 21 22
import QGroundControl.Controls      1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0

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

23
    property var    instrumentValueData:            null
24 25 26 27 28 29 30
    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 ]
31 32 33
    property real   _tightHeight:                   _rgFontSizeTightHeights[instrumentValueData.instrumentValueArea.fontSize]
    property real   _fontSize:                      _rgFontSizes[instrumentValueData.instrumentValueArea.fontSize]
    property real   _horizontalLabelSpacing:        ScreenTools.defaultFontPixelWidth
34
    property real   _blankEntryHeight:              ScreenTools.defaultFontPixelHeight * 2
35
    property bool   _showIcon:                      instrumentValueData.icon || instrumentValueData.rangeType === InstrumentValueData.IconSelectRange
36 37 38 39 40 41

    // 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
        }
42 43 44 45 46
        var smallVerticalSpacing = 2
        var halfWidth = width / 2
        var halfHorizontalSpacing = _horizontalLabelSpacing / 2
        if (_showIcon) {
            if (instrumentValueData.instrumentValueArea.orientation === InstrumentValueArea.VerticalOrientation) {
47 48 49
                valueIcon.x = (width - valueIcon.width) / 2
                valueIcon.y = 0
                value.x = (width - value.width) / 2
50
                value.y = valueIcon.height + smallVerticalSpacing
51
            } else {
52
                value.y = 0 // value assumed to be taller
53
                valueIcon.y = (value.height - valueIcon.height) / 2
54 55
                value.x = halfWidth + halfHorizontalSpacing
                valueIcon.x = halfWidth - halfHorizontalSpacing - valueIcon.width
56 57 58
            }
            label.x = label.y = 0
        } else {
59 60 61 62 63 64 65 66 67 68 69 70
            if (instrumentValueData.text) {
                if (instrumentValueData.instrumentValueArea.orientation === InstrumentValueArea.VerticalOrientation) {
                    label.x = (width - label.width) / 2
                    label.y = 0
                    value.x = (width - value.width) / 2
                    value.y = label.height + smallVerticalSpacing
                } else {
                    value.y = 0 // value assumed to be taller
                    label.y = (value.height - label.height) / 2
                    value.x = halfWidth + halfHorizontalSpacing
                    label.x = halfWidth - halfHorizontalSpacing - label.width
                }
71
            } else {
72 73
                value.x = (width - value.width) / 2
                value.y = (height - value.height) / 2
74 75 76 77 78 79
            }
            valueIcon.x = valueIcon.y = 0
        }
    }

    onRecalcOkChanged:    recalcPositions()
80
    onWidthChanged:       recalcPositions()
81 82

    Connections {
83 84
        target:         instrumentValueData
        onIconChanged:  recalcPositions()
85 86 87 88
    }

    QGCColoredImage {
        id:                         valueIcon
89
        height:                     _tightHeight
90 91 92 93 94 95
        width:                      height
        source:                     icon
        sourceSize.height:          height
        fillMode:                   Image.PreserveAspectFit
        mipmap:                     true
        smooth:                     true
96 97 98
        color:                      instrumentValueData.isValidColor(instrumentValueData.currentColor) ? instrumentValueData.currentColor : qgcPal.text
        opacity:                    instrumentValueData.currentOpacity
        visible:                    _showIcon
99 100 101 102 103 104 105
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()

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

        function updateIcon() {
106 107 108 109
            if (instrumentValueData.rangeType === InstrumentValueData.IconSelectRange) {
                icon = iconPrefix + instrumentValueData.currentIcon
            } else if (instrumentValueData.icon) {
                icon = iconPrefix + instrumentValueData.icon
110 111 112 113 114 115
            } else {
                icon = ""
            }
        }

        Connections {
116
            target:                 instrumentValueData
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
            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
132
        visible:                    !instrumentValueData.fact
133 134 135 136 137 138
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()
    }

    QGCLabel {
        id:                         label
139 140
        height:                     _tightHeight
        text:                       instrumentValueData.text
141
        verticalAlignment:          Text.AlignVCenter
142
        visible:                    !_showIcon
143 144 145 146 147 148
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()
    }

    QGCLabel {
        id:                         value
149 150
        font.pointSize:             _fontSize
        text:                       visible ? (instrumentValueData.fact.enumOrValueString + (instrumentValueData.showUnits ? instrumentValueData.fact.units : "")) : ""
151
        verticalAlignment:          Text.AlignVCenter
152
        visible:                    instrumentValueData.fact
153 154 155 156
        onWidthChanged:             root.recalcPositions()
        onHeightChanged:            root.recalcPositions()
    }
}