Skip to content
QGCComboBox.qml 5.22 KiB
Newer Older
/****************************************************************************
 *
Don Gagne's avatar
 
Don Gagne committed
 *   (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.
 *
Don Gagne's avatar
 
Don Gagne committed
 ****************************************************************************/
Don Gagne's avatar
 
Don Gagne committed
import QtQuick                  2.11
DonLakeFlyer's avatar
 
DonLakeFlyer committed
import QtQuick.Window           2.3
import QtQuick.Controls         2.4
import QtQuick.Controls.impl    2.4
import QtQuick.Templates        2.4 as T

import QGroundControl.ScreenTools   1.0
Don Gagne's avatar
 
Don Gagne committed
import QGroundControl.Palette       1.0
DonLakeFlyer's avatar
 
DonLakeFlyer committed
import QGroundControl.Controls      1.0
Don Gagne's avatar
 
Don Gagne committed
T.ComboBox {
    id:             control
    padding:        ScreenTools.comboBoxPadding
    spacing:        ScreenTools.defaultFontPixelWidth
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    implicitWidth:  Math.max(background ? background.implicitWidth : 0,
DonLakeFlyer's avatar
 
DonLakeFlyer committed
                             contentItem.implicitWidth + leftPadding + rightPadding + padding)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    implicitHeight: Math.max(background ? background.implicitHeight : 0,
DonLakeFlyer's avatar
 
DonLakeFlyer committed
                             Math.max(contentItem.implicitHeight, indicator ? indicator.implicitHeight : 0) + topPadding + bottomPadding)
Don Gagne's avatar
 
Don Gagne committed
    leftPadding:    padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
    rightPadding:   padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width)
Don Gagne's avatar
 
Don Gagne committed
    property bool   centeredLabel:  false
    property bool   sizeToContents: false
    property string alternateText:  ""
Don Gagne's avatar
 
Don Gagne committed

Don Gagne's avatar
 
Don Gagne committed
    property var    _qgcPal:           QGCPalette { colorGroupEnabled: enabled }
    property real   _largestTextWidth: 0
    property real   _popupWidth:       sizeToContents ? _largestTextWidth + leftPadding + rightPadding : control.width
Don Gagne's avatar
 
Don Gagne committed

Don Gagne's avatar
 
Don Gagne committed
    TextMetrics {
        id: textMetrics
Yasen's avatar
Yasen committed

Don Gagne's avatar
 
Don Gagne committed
    onModelChanged: {
        if (sizeToContents) {
            _largestTextWidth = 0
            textMetrics.font = control.font
            for (var i = 0; i < model.length; i++){
                textMetrics.text = model[i]
                _largestTextWidth = Math.max(textMetrics.width, _largestTextWidth)
            }
        }
    }
Yasen's avatar
Yasen committed

Don Gagne's avatar
 
Don Gagne committed
    // The items in the popup
    delegate: ItemDelegate {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        width:  _popupWidth
        height: Math.round(popupItemMetrics.height * 1.75)
Don Gagne's avatar
 
Don Gagne committed
        property string _text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
Yasen's avatar
Yasen committed

Don Gagne's avatar
 
Don Gagne committed
        TextMetrics {
            id:    popupItemMetrics
            text:  _text
        }
Don Gagne's avatar
 
Don Gagne committed
        contentItem: Text {
            text:                   _text
            font:                   control.font
            color:                  control.currentIndex === index ? _qgcPal.buttonHighlightText : _qgcPal.buttonText
            verticalAlignment:      Text.AlignVCenter
Don Gagne's avatar
 
Don Gagne committed
        background: Rectangle {
            color:                  control.currentIndex === index ? _qgcPal.buttonHighlight : _qgcPal.button
        }

        highlighted:                control.highlightedIndex === index
    }

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    indicator: QGCColoredImage {
        anchors.rightMargin:    control.padding
        anchors.right:          parent.right
        anchors.verticalCenter: parent.verticalCenter
        height:                 ScreenTools.defaultFontPixelWidth
        width:                  height
        source:                 "/qmlimages/arrow-down.png"
        color:                  _qgcPal.text
Don Gagne's avatar
 
Don Gagne committed
    }

    // The label of the button
    contentItem: Item {
        implicitWidth:                  text.implicitWidth
        implicitHeight:                 text.implicitHeight
Yasen's avatar
Yasen committed

        QGCLabel {
            id:                         text
            anchors.verticalCenter:     parent.verticalCenter
            anchors.horizontalCenter:   centeredLabel ? parent.horizontalCenter : undefined
Don Gagne's avatar
 
Don Gagne committed
            text:                       control.alternateText === "" ? control.currentText : control.alternateText
            font:                       control.font
Don Gagne's avatar
 
Don Gagne committed
            color:                      _qgcPal.text
Don Gagne's avatar
 
Don Gagne committed

    background: Rectangle {
        implicitWidth:  ScreenTools.implicitComboBoxWidth
        implicitHeight: ScreenTools.implicitComboBoxHeight
        color:          _qgcPal.window
        border.width:   enabled ? 1 : 0
        border.color:   "#999"
    }

    popup: T.Popup {
        y:              control.height
        width:          _popupWidth
        height:         Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
        topMargin:      6
        bottomMargin:   6

        contentItem: ListView {
            clip:                   true
            implicitHeight:         contentHeight
            model:                  control.delegateModel
            currentIndex:           control.highlightedIndex
            highlightMoveDuration:  0

            Rectangle {
                z:              10
                width:          parent.width
                height:         parent.height
                color:          "transparent"
                border.color:   _qgcPal.text
            }

            T.ScrollIndicator.vertical: ScrollIndicator { }
        }

        background: Rectangle {
            color: control.palette.window
        }
    }