QGCComboBox.qml 5.86 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
 *
 * 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
8
 ****************************************************************************/
9

Don Gagne's avatar
Don Gagne committed
10
import QtQuick                  2.11
DonLakeFlyer's avatar
DonLakeFlyer committed
11 12 13 14
import QtQuick.Window           2.3
import QtQuick.Controls         2.4
import QtQuick.Controls.impl    2.4
import QtQuick.Templates        2.4 as T
15 16

import QGroundControl.ScreenTools   1.0
Don Gagne's avatar
Don Gagne committed
17
import QGroundControl.Palette       1.0
DonLakeFlyer's avatar
DonLakeFlyer committed
18
import QGroundControl.Controls      1.0
19

Don Gagne's avatar
Don Gagne committed
20 21 22 23
T.ComboBox {
    id:             control
    padding:        ScreenTools.comboBoxPadding
    spacing:        ScreenTools.defaultFontPixelWidth
24 25
    font.pointSize: ScreenTools.defaultFontPointSize
    font.family:    ScreenTools.normalFontFamily
DonLakeFlyer's avatar
DonLakeFlyer committed
26
    implicitWidth:  Math.max(background ? background.implicitWidth : 0,
DonLakeFlyer's avatar
DonLakeFlyer committed
27
                             contentItem.implicitWidth + leftPadding + rightPadding + padding)
DonLakeFlyer's avatar
DonLakeFlyer committed
28
    implicitHeight: Math.max(background ? background.implicitHeight : 0,
DonLakeFlyer's avatar
DonLakeFlyer committed
29
                             Math.max(contentItem.implicitHeight, indicator ? indicator.implicitHeight : 0) + topPadding + bottomPadding)
Don Gagne's avatar
Don Gagne committed
30 31
    leftPadding:    padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
    rightPadding:   padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width)
32

Don Gagne's avatar
Don Gagne committed
33 34 35
    property bool   centeredLabel:  false
    property bool   sizeToContents: false
    property string alternateText:  ""
Don Gagne's avatar
Don Gagne committed
36

37 38 39 40
    property var    _qgcPal:            QGCPalette { colorGroupEnabled: enabled }
    property real   _largestTextWidth:  0
    property real   _popupWidth:        sizeToContents ? _largestTextWidth + itemDelegateMetrics.leftPadding + itemDelegateMetrics.rightPadding : control.width
    property bool   _onCompleted:       false
Don Gagne's avatar
Don Gagne committed
41

Don Gagne's avatar
Don Gagne committed
42
    TextMetrics {
43 44 45
        id:                 textMetrics
        font.family:        control.font.family
        font.pointSize:     control.font.pointSize
46
    }
Yasen's avatar
Yasen committed
47

48 49 50 51 52 53 54 55 56
    ItemDelegate {
        id:             itemDelegateMetrics
        visible:        false
        font.family:    control.font.family
        font.pointSize: control.font.pointSize
    }

    function _adjustSizeToContents() {
        if (_onCompleted && sizeToContents) {
Don Gagne's avatar
Don Gagne committed
57 58 59 60 61 62 63
            _largestTextWidth = 0
            for (var i = 0; i < model.length; i++){
                textMetrics.text = model[i]
                _largestTextWidth = Math.max(textMetrics.width, _largestTextWidth)
            }
        }
    }
Yasen's avatar
Yasen committed
64

65 66 67 68 69 70 71
    onModelChanged: _adjustSizeToContents()

    Component.onCompleted: {
        _onCompleted = true
        _adjustSizeToContents()
    }

Don Gagne's avatar
Don Gagne committed
72
    // The items in the popup
73
    delegate: ItemDelegate {
74 75
        width:  _popupWidth
        height: Math.round(popupItemMetrics.height * 1.75)
76

Don Gagne's avatar
Don Gagne committed
77
        property string _text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
Yasen's avatar
Yasen committed
78

Don Gagne's avatar
Don Gagne committed
79
        TextMetrics {
80 81 82
            id:             popupItemMetrics
            font:           control.font
            text:           _text
Don Gagne's avatar
Don Gagne committed
83
        }
84

Don Gagne's avatar
Don Gagne committed
85 86 87 88 89
        contentItem: Text {
            text:                   _text
            font:                   control.font
            color:                  control.currentIndex === index ? _qgcPal.buttonHighlightText : _qgcPal.buttonText
            verticalAlignment:      Text.AlignVCenter
90
        }
91

Don Gagne's avatar
Don Gagne committed
92 93 94 95 96 97 98
        background: Rectangle {
            color:                  control.currentIndex === index ? _qgcPal.buttonHighlight : _qgcPal.button
        }

        highlighted:                control.highlightedIndex === index
    }

DonLakeFlyer's avatar
DonLakeFlyer committed
99 100 101 102 103 104 105 106
    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
107 108 109
    }

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

114 115 116 117
        QGCLabel {
            id:                         text
            anchors.verticalCenter:     parent.verticalCenter
            anchors.horizontalCenter:   centeredLabel ? parent.horizontalCenter : undefined
Don Gagne's avatar
Don Gagne committed
118 119
            text:                       control.alternateText === "" ? control.currentText : control.alternateText
            font:                       control.font
Don Gagne's avatar
Don Gagne committed
120
            color:                      _qgcPal.text
121 122
        }
    }
Don Gagne's avatar
Don Gagne committed
123 124 125 126 127

    background: Rectangle {
        implicitWidth:  ScreenTools.implicitComboBoxWidth
        implicitHeight: ScreenTools.implicitComboBoxHeight
        color:          _qgcPal.window
128
        border.color:   _qgcPal.text
Don Gagne's avatar
Don Gagne committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    }

    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
        }
    }
160
}