FactValueSlider.qml 5.4 KB
Newer Older
1 2
import QtQuick          2.3
import QtQuick.Controls 1.2
3
import QtQuick.Dialogs  1.2
4 5 6 7 8 9 10 11 12 13 14

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

Rectangle {
    height: _itemHeight
    width:  _totalSlots * _itemWidth
    color:  qgcPal.textField

15
    property Fact   fact:               undefined
DonLakeFlyer's avatar
DonLakeFlyer committed
16
    property int    digitCount:         4           ///< The minimum number of digits to show for each value
17
    property int    incrementSlots:     1           ///< The number of visible slots to left/right of center value
18

DonLakeFlyer's avatar
DonLakeFlyer committed
19 20
    property int    _adjustedDigitCount:    Math.max(digitCount, _model.initialValueAtPrecision.toString().length)
    property int    _totalDigitCount:       _adjustedDigitCount + 1 + fact.units.length
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    property real   _margins:               (ScreenTools.implicitTextFieldHeight - ScreenTools.defaultFontPixelHeight) / 2
    property real   _increment:             fact.increment
    property real   _value:                 fact.value
    property int    _decimalPlaces:         fact.decimalPlaces
    property string _units:                 fact.units
    property real   _prevValue:             _value - _increment
    property real   _nextValue:             _value + _increment
    property real   _itemWidth:             (_totalDigitCount * ScreenTools.defaultFontPixelWidth) + (_margins * 2)
    property real   _itemHeight:            ScreenTools.implicitTextFieldHeight
    property var    _valueModel
    property int    _totalSlots:            (incrementSlots * 2) + 1
    property int    _currentIndex:          _totalSlots / 2
    property int    _currentRelativeIndex:  _currentIndex
    property int    _prevIncrementSlots:    incrementSlots
    property int    _nextIncrementSlots:    incrementSlots
    property int    _selectionWidth:        3
37 38
    property var    _model:                 fact.valueSliderModel()
    property var    _fact:                  fact
39 40 41 42 43 44 45 46 47 48 49 50 51 52

    QGCPalette { id: qgcPal; colorGroupEnabled: parent.enabled }
    QGCPalette { id: qgcPalDisabled; colorGroupEnabled: false }

    function firstVisibleIndex() {
        return valueListView.contentX / _itemWidth
    }

    function recalcRelativeIndex() {
        _currentRelativeIndex = _currentIndex - firstVisibleIndex()
        _prevIncrementSlots = _currentRelativeIndex
        _nextIncrementSlots = _totalSlots - _currentRelativeIndex - 1
    }

53 54 55
    function reset() {
        valueListView.positionViewAtIndex(0, ListView.Beginning)
        _currentIndex = _model.resetInitialValue()
56 57 58 59
        valueListView.positionViewAtIndex(_currentIndex, ListView.Center)
        recalcRelativeIndex()
    }

60 61 62 63 64 65 66 67 68
    Component.onCompleted: {
        valueListView.maximumFlickVelocity = valueListView.maximumFlickVelocity / 2
        reset()
    }

    Connections {
        target:         _fact
        onValueChanged: reset()
    }
69 70 71 72 73

    Component {
        id: editDialogComponent

        ParameterEditorDialog {
74 75
            fact:       _fact
            setFocus:   ScreenTools.isMobile ? false : true // Works around strange android bug where wrong virtual keyboard is displayed
76 77 78
        }
    }

79 80 81 82 83 84
    QGCListView {
        id:             valueListView
        anchors.fill:   parent
        orientation:    ListView.Horizontal
        snapMode:       ListView.SnapToItem
        clip:           true
85 86
        model:          _model

87 88 89 90 91
        delegate: QGCLabel {
            width:                  _itemWidth
            height:                 _itemHeight
            verticalAlignment:      Text.AlignVCenter
            horizontalAlignment:    Text.AlignHCenter
92
            text:                   value + " " + _units
93 94 95 96 97
            color:                  qgcPal.textFieldText

            MouseArea {
                anchors.fill:   parent
                onClicked: {
98 99 100 101 102 103 104 105 106
                    valueListView.focus = true
                    if (_currentIndex === index) {
                        qgcView.showDialog(editDialogComponent, qsTr("Value Details"), qgcView.showDialogDefaultWidth, StandardButton.Save | StandardButton.Cancel)
                    } else {
                        _currentIndex = index
                        valueListView.positionViewAtIndex(_currentIndex, ListView.Center)
                        recalcRelativeIndex()
                        fact.value = value
                    }
107 108 109 110
                }
            }
        }

111 112
        onMovementStarted: valueListView.focus = true

113 114
        onMovementEnded: {
            _currentIndex = firstVisibleIndex() + _currentRelativeIndex
115
            fact.value = _model.valueAtModelIndex(_currentIndex)
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
        }
    }

    Rectangle {
        id:         leftOverlay
        width:      _itemWidth * _prevIncrementSlots
        height:     _itemHeight
        color:      qgcPal.textField
        opacity:    0.5
    }

    Rectangle {
        width:          _itemWidth * _nextIncrementSlots
        height:         _itemHeight
        anchors.right:  parent.right
        color:          qgcPal.textField
        opacity:        0.5
    }

    Rectangle {
        x:              _currentRelativeIndex * _itemWidth - _borderWidth
        y:              -_borderWidth
        width:          _itemWidth + (_borderWidth * 2)
        height:         _itemHeight + (_borderWidth * 2)
        border.width:   _borderWidth
        border.color:   qgcPal.brandingBlue
        color:          "transparent"

        readonly property int _borderWidth: 3
    }
}