FlightModeDropdown.qml 2.15 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10
import QtQuick 2.3
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

import QGroundControl.Controls      1.0
import QGroundControl.ScreenTools   1.0

Item {
    width:  flightModeLabel.visible ? flightModeLabel.width : flightModeCombo.width
    height: flightModeLabel.visible ? flightModeLabel.height : flightModeCombo.height

    property var activeVehicle  ///< Vehicle to show flight modes for

    property int _maxFMCharLength:  10   ///< Maximum number of chars in a flight mode
    property string flightMode:     activeVehicle ? activeVehicle.flightMode : qsTr("N/A", "No data to display")

    onActiveVehicleChanged: _activeVehicleChanged()

    onFlightModeChanged: {
        if (flightModeCombo.visible) {
            flightModeCombo.currentIndex = flightModeCombo.find(flightMode)
        }
    }

    Component.onCompleted: _activeVehicleChanged()

    function _activeVehicleChanged() {
        if (activeVehicle.flightModeSetAvailable) {
            var maxFMChars = 0
            for (var i=0; i<activeVehicle.flightModes.length; i++) {
                maxFMChars = Math.max(maxFMChars, activeVehicle.flightModes[i].length)
            }
            _maxFMCharLength = maxFMChars
        }
    }

    QGCLabel {
        id:         flightModeLabel
        text:       flightMode
        visible:    !activeVehicle.flightModeSetAvailable
        anchors.verticalCenter: parent.verticalCenter
    }

    QGCComboBox {
        id:         flightModeCombo
        width:      (_maxFMCharLength + 4) * ScreenTools.defaultFontPixelWidth
        model:      activeVehicle ? activeVehicle.flightModes : 0
        visible:    activeVehicle.flightModeSetAvailable

        onModelChanged: {
            if (activeVehicle && visible) {
                currentIndex = find(flightMode)
            }
        }

        onActivated: activeVehicle.flightMode = textAt(index)
    }
}