ComboBox.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. states: [
  14. State
  15. {
  16. name: "disabled"
  17. when: !control.enabled
  18. PropertyChanges { target: backgroundRectangle.border; color: UM.Theme.getColor("setting_control_disabled_border")}
  19. PropertyChanges { target: backgroundRectangle; color: UM.Theme.getColor("setting_control_disabled")}
  20. PropertyChanges { target: contentLabel; color: UM.Theme.getColor("setting_control_disabled_text")}
  21. },
  22. State
  23. {
  24. name: "highlighted"
  25. when: control.hovered || control.activeFocus
  26. PropertyChanges { target: backgroundRectangle.border; color: UM.Theme.getColor("setting_control_border_highlight") }
  27. PropertyChanges { target: backgroundRectangle; color: UM.Theme.getColor("setting_control_highlight")}
  28. }
  29. ]
  30. background: Rectangle
  31. {
  32. id: backgroundRectangle
  33. color: UM.Theme.getColor("setting_control")
  34. radius: UM.Theme.getSize("setting_control_radius").width
  35. border.width: UM.Theme.getSize("default_lining").width
  36. border.color: UM.Theme.getColor("setting_control_border")
  37. }
  38. indicator: UM.RecolorImage
  39. {
  40. id: downArrow
  41. x: control.width - width - control.rightPadding
  42. y: control.topPadding + Math.round((control.availableHeight - height) / 2)
  43. source: UM.Theme.getIcon("arrow_bottom")
  44. width: UM.Theme.getSize("standard_arrow").width
  45. height: UM.Theme.getSize("standard_arrow").height
  46. sourceSize.width: width + 5 * screenScaleFactor
  47. sourceSize.height: width + 5 * screenScaleFactor
  48. color: UM.Theme.getColor("setting_control_button")
  49. }
  50. contentItem: Label
  51. {
  52. id: contentLabel
  53. anchors.left: parent.left
  54. anchors.leftMargin: UM.Theme.getSize("setting_unit_margin").width
  55. anchors.verticalCenter: parent.verticalCenter
  56. anchors.right: downArrow.left
  57. text: control.currentText
  58. textFormat: Text.PlainText
  59. renderType: Text.NativeRendering
  60. font: UM.Theme.getFont("default")
  61. color: UM.Theme.getColor("setting_control_text")
  62. elide: Text.ElideRight
  63. verticalAlignment: Text.AlignVCenter
  64. }
  65. popup: Popup
  66. {
  67. y: control.height - UM.Theme.getSize("default_lining").height
  68. width: control.width
  69. implicitHeight: contentItem.implicitHeight + 2 * UM.Theme.getSize("default_lining").width
  70. padding: UM.Theme.getSize("default_lining").width
  71. contentItem: ListView
  72. {
  73. clip: true
  74. implicitHeight: contentHeight
  75. model: control.popup.visible ? control.delegateModel : null
  76. currentIndex: control.highlightedIndex
  77. ScrollIndicator.vertical: ScrollIndicator { }
  78. }
  79. background: Rectangle
  80. {
  81. color: UM.Theme.getColor("setting_control")
  82. border.color: UM.Theme.getColor("setting_control_border")
  83. }
  84. }
  85. delegate: ItemDelegate
  86. {
  87. id: delegateItem
  88. width: control.width - 2 * UM.Theme.getSize("default_lining").width
  89. height: control.height
  90. highlighted: control.highlightedIndex == index
  91. text:
  92. // FIXME: Maybe there is a better way to do this. Check model and modelData doc page:
  93. // https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html
  94. {
  95. var _val = undefined
  96. if (typeof _val === 'undefined') // try to get textRole from "model".
  97. {
  98. _val = model[textRole]
  99. }
  100. if (typeof _val === 'undefined') // try to get textRole from "modelData" if it's still undefined.
  101. {
  102. _val = modelData[textRole]
  103. }
  104. return (typeof _val !== 'undefined') ? _val : ""
  105. }
  106. contentItem: Label
  107. {
  108. // FIXME: Somehow the top/bottom anchoring is not correct on Linux and it results in invisible texts.
  109. anchors.fill: parent
  110. anchors.leftMargin: UM.Theme.getSize("setting_unit_margin").width
  111. anchors.rightMargin: UM.Theme.getSize("setting_unit_margin").width
  112. text: delegateItem.text
  113. textFormat: Text.PlainText
  114. renderType: Text.NativeRendering
  115. color: control.contentItem.color
  116. font: UM.Theme.getFont("default")
  117. elide: Text.ElideRight
  118. verticalAlignment: Text.AlignVCenter
  119. }
  120. background: Rectangle
  121. {
  122. color: parent.highlighted ? UM.Theme.getColor("setting_control_highlight") : "transparent"
  123. border.color: parent.highlighted ? UM.Theme.getColor("setting_control_border_highlight") : "transparent"
  124. }
  125. }
  126. }