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 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)
27
28 property real _popupWidth: width
29 property real _largestTextWidth: 0
30 property bool _onCompleted: false
31 property bool _showHighlight: enabled && (pressed || hovered || popup.visible)
32
33 QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
34
35 TextMetrics {
36 id: textMetrics
37 font.family: control.font.family
38 font.pointSize: control.font.pointSize
39 }
40
41 ItemDelegate {
42 id: itemDelegateMetrics
43 visible: false
44 font.family: control.font.family
45 font.pointSize: control.font.pointSize
46 }
47
48 function _calcPopupWidth() {
49 if (_onCompleted && sizeToContents && control.count > 0) {
50 _largestTextWidth = 0
51 for (let i = 0; i < control.count; i++) {
52 textMetrics.text = control.textAt(i)
53 _largestTextWidth = Math.max(textMetrics.width, _largestTextWidth)
54 }
55 _popupWidth = _largestTextWidth + itemDelegateMetrics.leftPadding + itemDelegateMetrics.rightPadding
56 }
57 }
58
59 onModelChanged: _calcPopupWidth()
60 onCountChanged: _calcPopupWidth()
61
62 Component.onCompleted: {
63 _onCompleted = true
64 _calcPopupWidth()
65 }
66
67 // The items in the popup
68 delegate: ItemDelegate {
69 width: _popupWidth
70 height: Math.round(popupItemMetrics.height * 1.75)
71
72 property string _text: control.textRole ?
73 (model.hasOwnProperty(control.textRole) ? model[control.textRole] : modelData[control.textRole]) :
74 modelData
75
76 TextMetrics {
77 id: popupItemMetrics
78 font: control.font
79 text: _text
80 }
81
82 contentItem: Text {
83 text: _text
84 font: control.font
85 color: qgcPal.textPrimary
86 verticalAlignment: Text.AlignVCenter
87 }
88
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
93 }
94
95 highlighted: control.highlightedIndex === index
96 }
97
98 indicator: QGCColoredImage {
99 anchors.rightMargin: control.padding
100 anchors.right: parent.right
101 anchors.verticalCenter: parent.verticalCenter
102 height: ScreenTools.defaultFontPixelWidth
103 width: height
104 source: "/qmlimages/arrow-down.png"
105 color: qgcPal.textPrimary
106 }
107
108 // The label of the button
109 contentItem: QGCLabel {
110 id: text
111 text: control.alternateText === "" ? control.currentText : control.alternateText
112 font: control.font
113 color: qgcPal.textPrimary
114 elide: Text.ElideRight
115 }
116
117 background: Rectangle {
118 color: qgcPal.windowInset
119 border.color: _showHighlight ? qgcPal.accent : qgcPal.borderSubtle
120 border.width: 1
121 radius: ScreenTools.defaultBorderRadius
122 }
123
124 popup: T.Popup {
125 x: Math.max(-_controlPos.x, Math.min(control.width - _popupWidth, control.Window.width - _controlPos.x - _popupWidth))
126 y: _openAbove ? -height : control.height
127 width: _popupWidth
128 height: Math.min(contentItem.implicitHeight, _openAbove ? _spaceAbove : _spaceBelow)
129 topMargin: 6
130 bottomMargin: 6
131
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
136
137 contentItem: ListView {
138 clip: true
139 implicitHeight: contentHeight
140 model: control.delegateModel
141 currentIndex: control.highlightedIndex
142 highlightMoveDuration: 0
143
144 Rectangle {
145 z: 10
146 width: parent.width
147 height: parent.height
148 color: "transparent"
149 border.color: qgcPal.borderSubtle
150 }
151
152 T.ScrollIndicator.vertical: ScrollIndicator { }
153 }
154
155 background: Rectangle {
156 color: qgcPal.windowPanel
157 }
158 }
159}