ComboBox.qml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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: "active"
  28. when: control.activeFocus
  29. PropertyChanges
  30. {
  31. target: background
  32. borderColor: UM.Theme.getColor("text_field_border_active")
  33. liningColor: UM.Theme.getColor("text_field_border_active")
  34. }
  35. },
  36. State
  37. {
  38. name: "highlighted"
  39. when: (base.hovered || control.hovered) && !control.activeFocus
  40. PropertyChanges
  41. {
  42. target: background
  43. liningColor: UM.Theme.getColor("text_field_border_hovered")
  44. }
  45. }
  46. ]
  47. background: UM.UnderlineBackground{}
  48. indicator: UM.ColorImage
  49. {
  50. id: downArrow
  51. x: control.width - width - control.rightPadding
  52. y: control.topPadding + Math.round((control.availableHeight - height) / 2)
  53. source: UM.Theme.getIcon("ChevronSingleDown")
  54. width: UM.Theme.getSize("standard_arrow").width
  55. height: UM.Theme.getSize("standard_arrow").height
  56. color: UM.Theme.getColor("setting_control_button")
  57. }
  58. contentItem: UM.Label
  59. {
  60. id: contentLabel
  61. leftPadding: UM.Theme.getSize("setting_unit_margin").width + UM.Theme.getSize("default_margin").width
  62. anchors.right: downArrow.left
  63. wrapMode: Text.NoWrap
  64. text:
  65. {
  66. if (control.delegateModel.count == 0)
  67. {
  68. return control.defaultTextOnEmptyModel != "" ? control.defaultTextOnEmptyModel : control.defaultTextOnEmptyIndex
  69. }
  70. else
  71. {
  72. return control.currentIndex == -1 ? control.defaultTextOnEmptyIndex : control.currentText
  73. }
  74. }
  75. textFormat: Text.PlainText
  76. color: control.currentIndex == -1 ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  77. elide: Text.ElideRight
  78. }
  79. popup: Popup
  80. {
  81. y: control.height - UM.Theme.getSize("default_lining").height
  82. width: control.width
  83. implicitHeight: contentItem.implicitHeight + 2 * UM.Theme.getSize("default_lining").width
  84. bottomMargin: UM.Theme.getSize("default_margin").height
  85. padding: UM.Theme.getSize("default_lining").width
  86. contentItem: ListView
  87. {
  88. implicitHeight: contentHeight
  89. ScrollBar.vertical: UM.ScrollBar {}
  90. clip: true
  91. model: control.popup.visible ? control.delegateModel : null
  92. currentIndex: control.highlightedIndex
  93. }
  94. background: Rectangle
  95. {
  96. color: UM.Theme.getColor("setting_control")
  97. border.color: UM.Theme.getColor("setting_control_border")
  98. }
  99. }
  100. delegate: ItemDelegate
  101. {
  102. id: delegateItem
  103. width: control.width - 2 * UM.Theme.getSize("default_lining").width
  104. height: control.height
  105. highlighted: control.highlightedIndex == index
  106. text:
  107. // FIXME: Maybe there is a better way to do this. Check model and modelData doc page:
  108. // https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html
  109. {
  110. var _val = undefined
  111. if (typeof _val === 'undefined') // try to get textRole from "model".
  112. {
  113. _val = model[textRole]
  114. }
  115. if (typeof _val === 'undefined') // try to get textRole from "modelData" if it's still undefined.
  116. {
  117. _val = modelData[textRole]
  118. }
  119. return (typeof _val !== 'undefined') ? _val : ""
  120. }
  121. contentItem: UM.Label
  122. {
  123. id: delegateLabel
  124. // FIXME: Somehow the top/bottom anchoring is not correct on Linux and it results in invisible texts.
  125. anchors.fill: parent
  126. anchors.leftMargin: UM.Theme.getSize("setting_unit_margin").width
  127. anchors.rightMargin: UM.Theme.getSize("setting_unit_margin").width
  128. text: delegateItem.text
  129. textFormat: Text.PlainText
  130. color: UM.Theme.getColor("setting_control_text")
  131. elide: Text.ElideRight
  132. wrapMode: Text.NoWrap
  133. }
  134. background: UM.TooltipArea
  135. {
  136. Rectangle
  137. {
  138. color: delegateItem.highlighted ? UM.Theme.getColor("setting_control_highlight") : "transparent"
  139. anchors.fill: parent
  140. }
  141. text: delegateLabel.truncated ? delegateItem.text : ""
  142. }
  143. }
  144. }