ComboBox.qml 6.1 KB

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