QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCComboBox.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Window
3import QtQuick.Controls
4import QtQuick.Templates as T
5
6import QGroundControl
7import QGroundControl.Controls
8
9T.ComboBox {
10 property bool sizeToContents: false
11 property string alternateText: ""
12
13 id: control
14 padding: ScreenTools.comboBoxPadding
15 spacing: ScreenTools.defaultFontPixelWidth
16 font.pointSize: ScreenTools.defaultFontPointSize
17 font.family: ScreenTools.normalFontFamily
18 implicitWidth: Math.max(background.implicitWidth,
19 (control.sizeToContents ? _largestTextWidth : contentItem.implicitWidth) + leftPadding + rightPadding + padding)
20 implicitHeight: Math.max(background.implicitHeight,
21 Math.max(contentItem.implicitHeight, indicator ? indicator.implicitHeight : 0) + topPadding + bottomPadding)
22 baselineOffset: contentItem.y + text.baselineOffset
23 leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
24 rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width)
25
26 property real _popupWidth: width
27 property real _largestTextWidth: 0
28 property bool _onCompleted: false
29 property bool _showBorder: qgcPal.globalTheme === QGCPalette.Light
30 property bool _showHighlight: enabled && pressed
31
32 QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
33
34 TextMetrics {
35 id: textMetrics
36 font.family: control.font.family
37 font.pointSize: control.font.pointSize
38 }
39
40 ItemDelegate {
41 id: itemDelegateMetrics
42 visible: false
43 font.family: control.font.family
44 font.pointSize: control.font.pointSize
45 }
46
47 function _calcPopupWidth() {
48 if (_onCompleted && sizeToContents && control.count > 0) {
49 _largestTextWidth = 0
50 for (let i = 0; i < control.count; i++) {
51 textMetrics.text = control.textAt(i)
52 _largestTextWidth = Math.max(textMetrics.width, _largestTextWidth)
53 }
54 _popupWidth = _largestTextWidth + itemDelegateMetrics.leftPadding + itemDelegateMetrics.rightPadding
55 }
56 }
57
58 onModelChanged: _calcPopupWidth()
59 onCountChanged: _calcPopupWidth()
60
61 Component.onCompleted: {
62 _onCompleted = true
63 _calcPopupWidth()
64 }
65
66 // The items in the popup
67 delegate: ItemDelegate {
68 width: _popupWidth
69 height: Math.round(popupItemMetrics.height * 1.75)
70
71 property string _text: control.textRole ?
72 (model.hasOwnProperty(control.textRole) ? model[control.textRole] : modelData[control.textRole]) :
73 modelData
74
75 TextMetrics {
76 id: popupItemMetrics
77 font: control.font
78 text: _text
79 }
80
81 contentItem: Text {
82 text: _text
83 font: control.font
84 color: control.currentIndex === index ? qgcPal.buttonHighlightText : qgcPal.buttonText
85 verticalAlignment: Text.AlignVCenter
86 }
87
88 background: Rectangle {
89 color: control.currentIndex === index ? qgcPal.buttonHighlight : qgcPal.button
90 }
91
92 highlighted: control.highlightedIndex === index
93 }
94
95 indicator: QGCColoredImage {
96 anchors.rightMargin: control.padding
97 anchors.right: parent.right
98 anchors.verticalCenter: parent.verticalCenter
99 height: ScreenTools.defaultFontPixelWidth
100 width: height
101 source: "/qmlimages/arrow-down.png"
102 color: qgcPal.buttonText
103 }
104
105 // The label of the button
106 contentItem: QGCLabel {
107 id: text
108 text: control.alternateText === "" ? control.currentText : control.alternateText
109 font: control.font
110 color: qgcPal.buttonText
111 elide: Text.ElideRight
112 }
113
114 background: Rectangle {
115 color: qgcPal.button
116 border.color: qgcPal.buttonBorder
117 border.width: _showBorder ? 1 : 0
118 radius: ScreenTools.defaultBorderRadius
119
120 Rectangle {
121 anchors.fill: parent
122 color: qgcPal.buttonHighlight
123 opacity: _showHighlight ? 1 : control.enabled && control.hovered ? .2 : 0
124 radius: parent.radius
125 }
126 }
127
128 popup: T.Popup {
129 x: Math.max(-_controlPos.x, Math.min(control.width - _popupWidth, control.Window.width - _controlPos.x - _popupWidth))
130 y: _openAbove ? -height : control.height
131 width: _popupWidth
132 height: Math.min(contentItem.implicitHeight, _openAbove ? _spaceAbove : _spaceBelow)
133 topMargin: 6
134 bottomMargin: 6
135
136 readonly property point _controlPos: control.mapToItem(null, 0, 0)
137 readonly property real _spaceBelow: Math.max(0, control.Window.height - _controlPos.y - control.height - bottomMargin)
138 readonly property real _spaceAbove: Math.max(0, _controlPos.y - topMargin)
139 readonly property bool _openAbove: contentItem.implicitHeight > _spaceBelow && _spaceAbove > _spaceBelow
140
141 contentItem: ListView {
142 clip: true
143 implicitHeight: contentHeight
144 model: control.delegateModel
145 currentIndex: control.highlightedIndex
146 highlightMoveDuration: 0
147
148 Rectangle {
149 z: 10
150 width: parent.width
151 height: parent.height
152 color: "transparent"
153 border.color: qgcPal.text
154 }
155
156 T.ScrollIndicator.vertical: ScrollIndicator { }
157 }
158
159 background: Rectangle {
160 color: qgcPal.window
161 }
162 }
163}