4import QtQuick.Templates as T
7import QGroundControl.Controls
10 property bool sizeToContents: false
11 property string alternateText: ""
14 padding: ScreenTools.comboBoxPadding
15 topPadding: ScreenTools.controlVerticalPadding
16 bottomPadding: ScreenTools.controlVerticalPadding
17 spacing: ScreenTools.defaultFontPixelWidth
18 font.pointSize: ScreenTools.defaultFontPointSize
19 font.family: ScreenTools.normalFontFamily
20 implicitWidth: Math.max(background.implicitWidth,
21 (control.sizeToContents ? _largestTextWidth : contentItem.implicitWidth) + leftPadding + rightPadding + padding)
22 implicitHeight: Math.max(background.implicitHeight,
23 Math.max(contentItem.implicitHeight, indicator ? indicator.implicitHeight : 0) + topPadding + bottomPadding)
24 baselineOffset: contentItem.y + text.baselineOffset
25 leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
26 rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width)
28 property real _popupWidth: width
29 property real _largestTextWidth: 0
30 property bool _onCompleted: false
31 property bool _showHighlight: enabled && (pressed || hovered || popup.visible)
33 QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
37 font.family: control.font.family
38 font.pointSize: control.font.pointSize
42 id: itemDelegateMetrics
44 font.family: control.font.family
45 font.pointSize: control.font.pointSize
48 function _calcPopupWidth() {
49 if (_onCompleted && sizeToContents && control.count > 0) {
51 for (let i = 0; i < control.count; i++) {
52 textMetrics.text = control.textAt(i)
53 _largestTextWidth = Math.max(textMetrics.width, _largestTextWidth)
55 _popupWidth = _largestTextWidth + itemDelegateMetrics.leftPadding + itemDelegateMetrics.rightPadding
59 onModelChanged: _calcPopupWidth()
60 onCountChanged: _calcPopupWidth()
62 Component.onCompleted: {
67 // The items in the popup
68 delegate: ItemDelegate {
70 height: Math.round(popupItemMetrics.height * 1.75)
72 property string _text: control.textRole ?
73 (model.hasOwnProperty(control.textRole) ? model[control.textRole] : modelData[control.textRole]) :
85 color: qgcPal.textPrimary
86 verticalAlignment: Text.AlignVCenter
89 background: Rectangle {
90 color: control.currentIndex === index ? qgcPal.accentSubtle : (highlighted ? qgcPal.windowCard : qgcPal.windowPanel)
91 border.width: control.currentIndex === index ? 1 : 0
92 border.color: qgcPal.accent
95 highlighted: control.highlightedIndex === index
98 indicator: QGCColoredImage {
99 anchors.rightMargin: control.padding
100 anchors.right: parent.right
101 anchors.verticalCenter: parent.verticalCenter
102 height: ScreenTools.defaultFontPixelWidth
104 source: "/qmlimages/arrow-down.png"
105 color: qgcPal.textPrimary
108 // The label of the button
109 contentItem: QGCLabel {
111 text: control.alternateText === "" ? control.currentText : control.alternateText
113 color: qgcPal.textPrimary
114 elide: Text.ElideRight
117 background: Rectangle {
118 color: qgcPal.windowInset
119 border.color: _showHighlight ? qgcPal.accent : qgcPal.borderSubtle
121 radius: ScreenTools.defaultBorderRadius
125 x: Math.max(-_controlPos.x, Math.min(control.width - _popupWidth, control.Window.width - _controlPos.x - _popupWidth))
126 y: _openAbove ? -height : control.height
128 height: Math.min(contentItem.implicitHeight, _openAbove ? _spaceAbove : _spaceBelow)
132 readonly property point _controlPos: control.mapToItem(null, 0, 0)
133 readonly property real _spaceBelow: Math.max(0, control.Window.height - _controlPos.y - control.height - bottomMargin)
134 readonly property real _spaceAbove: Math.max(0, _controlPos.y - topMargin)
135 readonly property bool _openAbove: contentItem.implicitHeight > _spaceBelow && _spaceAbove > _spaceBelow
137 contentItem: ListView {
139 implicitHeight: contentHeight
140 model: control.delegateModel
141 currentIndex: control.highlightedIndex
142 highlightMoveDuration: 0
147 height: parent.height
149 border.color: qgcPal.borderSubtle
152 T.ScrollIndicator.vertical: ScrollIndicator { }
155 background: Rectangle {
156 color: qgcPal.windowPanel