ComboBox.qml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2022 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.10
  4. import QtQuick.Controls 2.3
  5. import UM 1.5 as UM
  6. import Cura 1.1 as Cura
  7. //
  8. // ComboBox with Cura styling.
  9. //
  10. ComboBox
  11. {
  12. id: control
  13. property var defaultTextOnEmptyModel: catalog.i18nc("@label", "No items to select from") // Text displayed in the combobox when the model is empty
  14. property var defaultTextOnEmptyIndex: "" // Text displayed in the combobox when the model has items but no item is selected
  15. enabled: delegateModel.count > 0
  16. onVisibleChanged: { popup.close() }
  17. states: [
  18. State
  19. {
  20. name: "disabled"
  21. when: !control.enabled
  22. PropertyChanges { target: background; color: UM.Theme.getColor("setting_control_disabled")}
  23. PropertyChanges { target: contentLabel; color: UM.Theme.getColor("setting_control_disabled_text")}
  24. },
  25. State
  26. {
  27. name: "highlighted"
  28. when: control.hovered || control.activeFocus
  29. PropertyChanges { target: background; liningColor: UM.Theme.getColor("border_main")}
  30. }
  31. ]
  32. background: UM.UnderlineBackground{}
  33. indicator: UM.RecolorImage
  34. {
  35. id: downArrow
  36. x: control.width - width - control.rightPadding
  37. y: control.topPadding + Math.round((control.availableHeight - height) / 2)
  38. source: UM.Theme.getIcon("ChevronSingleDown")
  39. width: UM.Theme.getSize("standard_arrow").width
  40. height: UM.Theme.getSize("standard_arrow").height
  41. sourceSize.width: width + 5 * screenScaleFactor
  42. sourceSize.height: width + 5 * screenScaleFactor
  43. color: UM.Theme.getColor("setting_control_button")
  44. }
  45. contentItem: UM.Label
  46. {
  47. id: contentLabel
  48. leftPadding: UM.Theme.getSize("setting_unit_margin").width + UM.Theme.getSize("default_margin").width
  49. anchors.right: downArrow.left
  50. wrapMode: Text.NoWrap
  51. text:
  52. {
  53. if (control.delegateModel.count == 0)
  54. {
  55. return control.defaultTextOnEmptyModel != "" ? control.defaultTextOnEmptyModel : control.defaultTextOnEmptyIndex
  56. }
  57. else
  58. {
  59. return control.currentIndex == -1 ? control.defaultTextOnEmptyIndex : control.currentText
  60. }
  61. }
  62. textFormat: Text.PlainText
  63. color: control.currentIndex == -1 ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  64. elide: Text.ElideRight
  65. }
  66. popup: Popup
  67. {
  68. y: control.height - UM.Theme.getSize("default_lining").height
  69. width: control.width
  70. implicitHeight: contentItem.implicitHeight + 2 * UM.Theme.getSize("default_lining").width
  71. bottomMargin: UM.Theme.getSize("default_margin").height
  72. padding: UM.Theme.getSize("default_lining").width
  73. contentItem: ListView
  74. {
  75. implicitHeight: contentHeight
  76. ScrollBar.vertical: UM.ScrollBar {}
  77. clip: true
  78. model: control.popup.visible ? control.delegateModel : null
  79. currentIndex: control.highlightedIndex
  80. }
  81. background: Rectangle
  82. {
  83. color: UM.Theme.getColor("setting_control")
  84. border.color: UM.Theme.getColor("setting_control_border")
  85. }
  86. }
  87. delegate: ItemDelegate
  88. {
  89. id: delegateItem
  90. width: control.width - 2 * UM.Theme.getSize("default_lining").width
  91. height: control.height
  92. highlighted: control.highlightedIndex == index
  93. text:
  94. // FIXME: Maybe there is a better way to do this. Check model and modelData doc page:
  95. // https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html
  96. {
  97. var _val = undefined
  98. if (typeof _val === 'undefined') // try to get textRole from "model".
  99. {
  100. _val = model[textRole]
  101. }
  102. if (typeof _val === 'undefined') // try to get textRole from "modelData" if it's still undefined.
  103. {
  104. _val = modelData[textRole]
  105. }
  106. return (typeof _val !== 'undefined') ? _val : ""
  107. }
  108. contentItem: UM.Label
  109. {
  110. id: delegateLabel
  111. // FIXME: Somehow the top/bottom anchoring is not correct on Linux and it results in invisible texts.
  112. anchors.fill: parent
  113. anchors.leftMargin: UM.Theme.getSize("setting_unit_margin").width
  114. anchors.rightMargin: UM.Theme.getSize("setting_unit_margin").width
  115. text: delegateItem.text
  116. textFormat: Text.PlainText
  117. color: UM.Theme.getColor("setting_control_text")
  118. elide: Text.ElideRight
  119. wrapMode: Text.NoWrap
  120. }
  121. background: UM.TooltipArea
  122. {
  123. Rectangle
  124. {
  125. color: delegateItem.highlighted ? UM.Theme.getColor("setting_control_highlight") : "transparent"
  126. anchors.fill: parent
  127. }
  128. text: delegateLabel.truncated ? delegateItem.text : ""
  129. }
  130. }
  131. }