CuraComboBox.qml 4.7 KB

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