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 && model) {
49 _largestTextWidth = 0
50 for (var i = 0; i < model.length; i++){
51 textMetrics.text = control.textRole ? model[i][control.textRole] : model[i]
52 _largestTextWidth = Math.max(textMetrics.width, _largestTextWidth)
53 }
54 _popupWidth = _largestTextWidth + itemDelegateMetrics.leftPadding + itemDelegateMetrics.rightPadding
55 }
56 }
57
58 onModelChanged: _calcPopupWidth()
59
60 Component.onCompleted: {
61 _onCompleted = true
62 _calcPopupWidth()
63 }
64
65 // The items in the popup
66 delegate: ItemDelegate {
67 width: _popupWidth
68 height: Math.round(popupItemMetrics.height * 1.75)
69
70 property string _text: control.textRole ?
71 (model.hasOwnProperty(control.textRole) ? model[control.textRole] : modelData[control.textRole]) :
72 modelData
73
74 TextMetrics {
75 id: popupItemMetrics
76 font: control.font
77 text: _text
78 }
79
80 contentItem: Text {
81 text: _text
82 font: control.font
83 color: control.currentIndex === index ? qgcPal.buttonHighlightText : qgcPal.buttonText
84 verticalAlignment: Text.AlignVCenter
85 }
86
87 background: Rectangle {
88 color: control.currentIndex === index ? qgcPal.buttonHighlight : qgcPal.button
89 }
90
91 highlighted: control.highlightedIndex === index
92 }
93
94 indicator: QGCColoredImage {
95 anchors.rightMargin: control.padding
96 anchors.right: parent.right
97 anchors.verticalCenter: parent.verticalCenter
98 height: ScreenTools.defaultFontPixelWidth
99 width: height
100 source: "/qmlimages/arrow-down.png"
101 color: qgcPal.buttonText
102 }
103
104 // The label of the button
105 contentItem: QGCLabel {
106 id: text
107 text: control.alternateText === "" ? control.currentText : control.alternateText
108 font: control.font
109 color: qgcPal.buttonText
110 }
111
112 background: Rectangle {
113 color: qgcPal.button
114 border.color: qgcPal.buttonBorder
115 border.width: _showBorder ? 1 : 0
116 radius: ScreenTools.defaultBorderRadius
117
118 Rectangle {
119 anchors.fill: parent
120 color: qgcPal.buttonHighlight
121 opacity: _showHighlight ? 1 : control.enabled && control.hovered ? .2 : 0
122 radius: parent.radius
123 }
124 }
125
126 popup: T.Popup {
127 x: control.width - _popupWidth
128 y: control.height
129 width: _popupWidth
130 height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
131 topMargin: 6
132 bottomMargin: 6
133
134 contentItem: ListView {
135 clip: true
136 implicitHeight: contentHeight
137 model: control.delegateModel
138 currentIndex: control.highlightedIndex
139 highlightMoveDuration: 0
140
141 Rectangle {
142 z: 10
143 width: parent.width
144 height: parent.height
145 color: "transparent"
146 border.color: qgcPal.text
147 }
148
149 T.ScrollIndicator.vertical: ScrollIndicator { }
150 }
151
152 background: Rectangle {
153 color: qgcPal.window
154 }
155 }
156}