Commit e5aa03b6 authored by Don Gagne's avatar Don Gagne

New features:
* Less padding to the right of drop indicator
* Ability to set font
* alternateText - Set alternate text for button display
* sizeToContents - Width will adjust to fit all contents
parent 2fc9b3fe
/**************************************************************************** /****************************************************************************
* *
* (c) 2009-2019 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org> * (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
* *
* QGroundControl is licensed according to the terms in the file * QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory. * COPYING.md in the root of the source code directory.
* *
* @file ****************************************************************************/
* @author Gus Grubba <gus@auterion.com>
*/
import QtQuick 2.11 import QtQuick 2.11
import QtQuick.Controls 2.4 import QtQuick.Window 2.11
import QtQuick.Layouts 1.11 import QtQuick.Controls 2.11
import QtQuick.Controls.impl 2.11
import QtQuick.Templates 2.11 as T
import QGroundControl.Controls 1.0
import QGroundControl.Palette 1.0
import QGroundControl.ScreenTools 1.0 import QGroundControl.ScreenTools 1.0
import QGroundControl.Palette 1.0
ComboBox { T.ComboBox {
id: control id: control
padding: ScreenTools.comboBoxPadding padding: ScreenTools.comboBoxPadding
spacing: ScreenTools.defaultFontPixelWidth
implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
implicitContentWidth + leftPadding + rightPadding,
sizeToContents ? _popupWidth : 0)
implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
implicitContentHeight + topPadding + bottomPadding,
implicitIndicatorHeight + topPadding + bottomPadding)
leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width)
property bool centeredLabel: false property bool centeredLabel: false
property bool sizeToContents: false
property string alternateText: ""
property var _qgcPal: QGCPalette { colorGroupEnabled: enabled } property var _qgcPal: QGCPalette { colorGroupEnabled: enabled }
property real _largestTextWidth: 0
property real _popupWidth: sizeToContents ? _largestTextWidth + leftPadding + rightPadding : control.width
Component.onCompleted: indicator.color = Qt.binding(function() { return _qgcPal.text }) TextMetrics {
id: textMetrics
background: Rectangle {
implicitWidth: ScreenTools.implicitComboBoxWidth
implicitHeight: ScreenTools.implicitComboBoxHeight
color: _qgcPal.window
border.width: enabled ? 1 : 0
border.color: "#999"
} }
/*! Adding the Combobox list item to the theme. */ onModelChanged: {
if (sizeToContents) {
_largestTextWidth = 0
textMetrics.font = control.font
for (var i = 0; i < model.length; i++){
textMetrics.text = model[i]
_largestTextWidth = Math.max(textMetrics.width, _largestTextWidth)
}
}
}
// The items in the popup
delegate: ItemDelegate { delegate: ItemDelegate {
width: control.width width: _popupWidth
property string _text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
TextMetrics {
id: popupItemMetrics
text: _text
}
contentItem: Text { contentItem: Text {
text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData text: _text
font: control.font
color: control.currentIndex === index ? _qgcPal.buttonHighlightText : _qgcPal.buttonText color: control.currentIndex === index ? _qgcPal.buttonHighlightText : _qgcPal.buttonText
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
} }
...@@ -53,7 +77,17 @@ ComboBox { ...@@ -53,7 +77,17 @@ ComboBox {
highlighted: control.highlightedIndex === index highlighted: control.highlightedIndex === index
} }
/*! This defines the label of the button. */ // Dropdown indicator
indicator: ColorImage {
x: control.mirrored ? control.padding : control.width - width
y: control.topPadding + (control.availableHeight - height) / 2
color: _qgcPal.text
defaultColor: "#353637"
source: "qrc:/qt-project.org/imports/QtQuick/Controls.2/images/double-arrow.png"
opacity: enabled ? 1 : 0.3
}
// The label of the button
contentItem: Item { contentItem: Item {
implicitWidth: text.implicitWidth implicitWidth: text.implicitWidth
implicitHeight: text.implicitHeight implicitHeight: text.implicitHeight
...@@ -62,8 +96,47 @@ ComboBox { ...@@ -62,8 +96,47 @@ ComboBox {
id: text id: text
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: centeredLabel ? parent.horizontalCenter : undefined anchors.horizontalCenter: centeredLabel ? parent.horizontalCenter : undefined
text: control.currentText text: control.alternateText === "" ? control.currentText : control.alternateText
font: control.font
color: _qgcPal.text color: _qgcPal.text
} }
} }
background: Rectangle {
implicitWidth: ScreenTools.implicitComboBoxWidth
implicitHeight: ScreenTools.implicitComboBoxHeight
color: _qgcPal.window
border.width: enabled ? 1 : 0
border.color: "#999"
}
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
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment