From fc718892d94226b29f97ae44801177586c9d61d0 Mon Sep 17 00:00:00 2001 From: Kostas Karmas Date: Mon, 18 Jan 2021 12:10:15 +0100 Subject: [PATCH] Add default text in the ComboBox The Cura.ComboBox component can now display a default text when there are no items in its model and another text when there is no item selected. CURA-7865 --- resources/qml/Widgets/ComboBox.qml | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/resources/qml/Widgets/ComboBox.qml b/resources/qml/Widgets/ComboBox.qml index d4c526e265..26324d7773 100644 --- a/resources/qml/Widgets/ComboBox.qml +++ b/resources/qml/Widgets/ComboBox.qml @@ -15,6 +15,18 @@ ComboBox { id: control + UM.I18nCatalog + { + id: catalog + name: "cura" + } + + property var defaultTextOnEmptyModel: catalog.i18nc("@label", "No items to select from") // Text displayed in the combobox when the model is empty + property var defaultTextOnEmptyIndex: "" // Text displayed in the combobox when the model has items but no item is selected + enabled: model.count > 0 + + onVisibleChanged: { popup.close() } + states: [ State { @@ -67,11 +79,22 @@ ComboBox anchors.verticalCenter: parent.verticalCenter anchors.right: downArrow.left - text: control.currentText + text: + { + if (control.model.count == 0) + { + return control.defaultTextOnEmptyModel != "" ? control.defaultTextOnEmptyModel : control.defaultTextOnEmptyIndex + } + else + { + return control.currentIndex == -1 ? control.defaultTextOnEmptyIndex : control.currentText + } + } + textFormat: Text.PlainText renderType: Text.NativeRendering font: UM.Theme.getFont("default") - color: UM.Theme.getColor("setting_control_text") + color: control.currentIndex == -1 ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text") elide: Text.ElideRight verticalAlignment: Text.AlignVCenter } @@ -81,6 +104,7 @@ ComboBox y: control.height - UM.Theme.getSize("default_lining").height width: control.width implicitHeight: contentItem.implicitHeight + 2 * UM.Theme.getSize("default_lining").width + bottomMargin: UM.Theme.getSize("default_margin").height padding: UM.Theme.getSize("default_lining").width contentItem: ListView @@ -133,7 +157,7 @@ ComboBox text: delegateItem.text textFormat: Text.PlainText renderType: Text.NativeRendering - color: control.contentItem.color + color: UM.Theme.getColor("setting_control_text") font: UM.Theme.getFont("default") elide: Text.ElideRight verticalAlignment: Text.AlignVCenter