PreFlightCheckButton.qml 6.31 KB
Newer Older
DonLakeFlyer's avatar
DonLakeFlyer committed
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/
9

DonLakeFlyer's avatar
DonLakeFlyer committed
10 11 12 13 14 15 16
import QtQuick                  2.3
import QtQuick.Controls         1.2
import QtQuick.Controls.Styles  1.4

import QGroundControl               1.0
import QGroundControl.Palette       1.0
import QGroundControl.ScreenTools   1.0
17

18 19 20
/// The PreFlightCheckButton supports creating a button which the user then has to verify/click to confirm a check.
/// It also supports failing the check based on values from within the system: telemetry or QGC app values. These
/// controls are normally placed within a PreFlightCheckGroup.
21 22 23 24 25
///
/// Two types of checks may be included on the button:
///     Manual - This is simply a check which the user must verify and confirm. It is not based on any system state.
///     Telemetry - This type of check can fail due to some state within the system. A telemetry check failure can be
///                 a hard stop in that there is no way to pass the checklist until the system state resolves itself.
26
///                 Or it can also optionally be override by the user.
27
/// If a button uses both manual and telemetry checks, the telemetry check takes precendence and must be passed first.
28
QGCButton {
29 30 31 32 33 34
    property string name:                           ""
    property string manualText:                     ""      ///< text to show for a manual check, "" signals no manual check
    property string telemetryTextFailure                    ///< text to show if telemetry check failed (override not allowed)
    property bool   telemetryFailure:               false   ///< true: telemetry check failing, false: telemetry check passing
    property bool   allowTelemetryFailureOverride:  false   ///< true: user can click past telemetry failure
    property bool   passed:                         _manualState === _statePassed && _telemetryState === _statePassed
35 36 37 38 39 40 41

    property int _manualState:          manualText === "" ? _statePassed : _statePending
    property int _telemetryState:       _statePassed
    property int _horizontalPadding:    ScreenTools.defaultFontPixelWidth
    property int _verticalPadding:      Math.round(ScreenTools.defaultFontPixelHeight / 2)
    property real _stateFlagWidth:      ScreenTools.defaultFontPixelWidth * 4

42 43
    readonly property int _statePending:    0   ///< Telemetry check is failing or manual check not yet verified, user can click to make it pass
    readonly property int _stateFailed:     1   ///< Telemetry check is failing, user cannot click to make it pass
44 45 46 47 48 49 50 51
    readonly property int _statePassed:     2   ///< Check has passed

    readonly property color _passedColor:   Qt.rgba(0.27,0.67,0.42,1)
    readonly property color _pendingColor:  Qt.rgba(0.9,0.47,0.2,1)
    readonly property color _failedColor:   Qt.rgba(0.92,0.22,0.22,1)

    property string _text: "<b>" + name +"</b>: " +
                           ((_telemetryState !== _statePassed) ?
52 53
                               telemetryTextFailure :
                               (_manualState !== _statePassed ? manualText : qsTr("Passed")))
54 55 56 57 58 59 60 61
    property color  _color: _telemetryState === _statePassed && _manualState === _statePassed ?
                                _passedColor :
                                (_telemetryState == _stateFailed ?
                                     _failedColor :
                                     (_telemetryState === _statePending || _manualState === _statePending ?
                                          _pendingColor :
                                          _failedColor))

DonLakeFlyer's avatar
DonLakeFlyer committed
62 63 64
    property var    _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle

    width:      40 * ScreenTools.defaultFontPixelWidth
65 66

    style: ButtonStyle {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        padding {
            top:    _verticalPadding
            bottom: _verticalPadding
            left:   (_horizontalPadding * 2) + _stateFlagWidth
            right:  _horizontalPadding
        }

        background: Rectangle {
            color:          qgcPal.button
            border.color:   qgcPal.button;

            Rectangle {
                color:          _color
                anchors.left:   parent.left
                anchors.top:    parent.top
                anchors.bottom: parent.bottom
                width:          _stateFlagWidth
            }
        }

87
        label: Label {
DonLakeFlyer's avatar
DonLakeFlyer committed
88 89 90
            text:                   _text
            wrapMode:               Text.WordWrap
            horizontalAlignment:    Text.AlignHCenter
91
            color:                  qgcPal.buttonText
92 93 94
        }
    }

95
    function _updateTelemetryState() {
96 97
        if (telemetryFailure) {
            // We have a new telemetry failure, reset user pass
98
            _telemetryState = allowTelemetryFailureOverride ? _statePending : _stateFailed
DonLakeFlyer's avatar
DonLakeFlyer committed
99
        } else {
100
            _telemetryState = _statePassed
DonLakeFlyer's avatar
DonLakeFlyer committed
101
        }
102
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
103

104 105 106
    onTelemetryFailureChanged:              _updateTelemetryState()
    onAllowTelemetryFailureOverrideChanged: _updateTelemetryState()

107
    onClicked: {
108
        if (telemetryFailure && !allowTelemetryFailureOverride) {
109 110 111
            // No way to proceed past this failure
            return
        }
112
        if (telemetryFailure && allowTelemetryFailureOverride && _telemetryState !== _statePassed) {
113 114 115 116 117 118 119
            // User is allowed to proceed past this failure
            _telemetryState = _statePassed
            return
        }
        if (manualText !== "" && _manualState !== _statePassed) {
            // User is confirming a manual check
            _manualState = _statePassed
DonLakeFlyer's avatar
DonLakeFlyer committed
120
        }
121
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
122

123 124 125 126 127 128 129 130 131
    onPassedChanged: callButtonPassedChanged()
    onParentChanged: callButtonPassedChanged()

    function callButtonPassedChanged() {
        if (typeof parent.buttonPassedChanged === "function") {
            parent.buttonPassedChanged()
        }
    }

132
    function reset() {
133
        _manualState = manualText === "" ? _statePassed : _statePending
134
        if (telemetryFailure) {
135
            _telemetryState = allowTelemetryFailureOverride ? _statePending : _stateFailed
DonLakeFlyer's avatar
DonLakeFlyer committed
136
        } else {
137
            _telemetryState = _statePassed
DonLakeFlyer's avatar
DonLakeFlyer committed
138
        }
139
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
140

141
    QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
142
}