SettingExtruder.qml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright (c) 2022 Ultimaker B.V.
  2. // Uranium is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Controls 2.0
  5. import UM 1.5 as UM
  6. import Cura 1.5 as Cura
  7. SettingItem
  8. {
  9. id: base
  10. property var focusItem: control
  11. contents: Cura.ComboBox
  12. {
  13. id: control
  14. anchors.fill: parent
  15. forceHighlight: base.hovered
  16. property var extrudersModel: CuraApplication.getExtrudersModel()
  17. model: extrudersModel
  18. Connections
  19. {
  20. target: extrudersModel
  21. function onModelChanged()
  22. {
  23. control.color = extrudersModel.getItem(control.currentIndex).color
  24. }
  25. }
  26. textRole: "name"
  27. // knowing the extruder position, try to find the item index in the model
  28. function getIndexByPosition(position)
  29. {
  30. for (var item_index in model.items)
  31. {
  32. var item = model.getItem(item_index)
  33. if (item.index == position)
  34. {
  35. return item_index
  36. }
  37. }
  38. return -1
  39. }
  40. onActivated:
  41. {
  42. if (model.getItem(index).enabled)
  43. {
  44. forceActiveFocus();
  45. propertyProvider.setPropertyValue("value", model.getItem(index).index);
  46. } else
  47. {
  48. currentIndex = propertyProvider.properties.value; // keep the old value
  49. }
  50. }
  51. onActiveFocusChanged:
  52. {
  53. if(activeFocus)
  54. {
  55. base.focusReceived();
  56. }
  57. }
  58. Keys.onTabPressed:
  59. {
  60. base.setActiveFocusToNextSetting(true)
  61. }
  62. Keys.onBacktabPressed:
  63. {
  64. base.setActiveFocusToNextSetting(false)
  65. }
  66. currentIndex: propertyProvider.properties.value !== undefined ? propertyProvider.properties.value : 0
  67. property string color: "transparent"
  68. Binding
  69. {
  70. // We override the color property's value when the ExtruderModel changes. So we need to use an
  71. // explicit binding here otherwise we do not handle value changes after the model changes.
  72. target: control
  73. property: "color"
  74. value: control.currentText != "" ? control.model.getItem(control.currentIndex).color : "transparent"
  75. }
  76. Binding
  77. {
  78. target: control
  79. property: "currentIndex"
  80. value: control.getIndexByPosition(propertyProvider.properties.value)
  81. // Sometimes when the value is already changed, the model is still being built.
  82. // The when clause ensures that the current index is not updated when this happens.
  83. when: control.model.items.length > 0
  84. }
  85. indicator: UM.ColorImage
  86. {
  87. id: downArrow
  88. x: control.width - width - control.rightPadding
  89. y: control.topPadding + Math.round((control.availableHeight - height) / 2)
  90. source: UM.Theme.getIcon("ChevronSingleDown")
  91. width: UM.Theme.getSize("standard_arrow").width
  92. height: UM.Theme.getSize("standard_arrow").height
  93. color: UM.Theme.getColor("setting_control_button");
  94. }
  95. background: UM.UnderlineBackground
  96. {
  97. color:
  98. {
  99. if (!enabled)
  100. {
  101. return UM.Theme.getColor("setting_control_disabled")
  102. }
  103. if (control.hovered || base.activeFocus)
  104. {
  105. return UM.Theme.getColor("setting_control_highlight")
  106. }
  107. return UM.Theme.getColor("setting_control")
  108. }
  109. borderColor: control.activeFocus ? UM.Theme.getSize("text_field_border_active") : "transparent"
  110. liningColor:
  111. {
  112. if(!enabled)
  113. {
  114. return UM.Theme.getColor("setting_control_disabled_border");
  115. }
  116. if(control.activeFocus)
  117. {
  118. return UM.Theme.getColor("text_field_border_active");
  119. }
  120. if(base.hovered)
  121. {
  122. return UM.Theme.getColor("text_field_border_hovered");
  123. }
  124. return UM.Theme.getColor("border_field_light");
  125. }
  126. }
  127. contentItem: UM.Label
  128. {
  129. anchors.verticalCenter: parent.verticalCenter
  130. anchors.left: parent.left
  131. anchors.leftMargin: UM.Theme.getSize("setting_unit_margin").width
  132. anchors.right: downArrow.left
  133. rightPadding: swatch.width + UM.Theme.getSize("setting_unit_margin").width
  134. text: control.currentText
  135. textFormat: Text.PlainText
  136. color: enabled ? UM.Theme.getColor("setting_control_text") : UM.Theme.getColor("setting_control_disabled_text")
  137. background: Rectangle
  138. {
  139. id: swatch
  140. height: UM.Theme.getSize("standard_arrow").width
  141. width: height
  142. radius: Math.round(width / 2)
  143. anchors.right: parent.right
  144. anchors.verticalCenter: parent.verticalCenter
  145. anchors.rightMargin: UM.Theme.getSize("thin_margin").width
  146. color: control.color
  147. }
  148. }
  149. popup: Popup
  150. {
  151. y: control.height - UM.Theme.getSize("default_lining").height
  152. width: control.width
  153. implicitHeight: contentItem.implicitHeight + 2 * UM.Theme.getSize("default_lining").width
  154. padding: UM.Theme.getSize("default_lining").width
  155. contentItem: ListView
  156. {
  157. implicitHeight: contentHeight
  158. ScrollBar.vertical: UM.ScrollBar {}
  159. clip: true
  160. model: control.popup.visible ? control.delegateModel : null
  161. currentIndex: control.highlightedIndex
  162. }
  163. background: Rectangle
  164. {
  165. color: UM.Theme.getColor("setting_control")
  166. border.color: UM.Theme.getColor("setting_control_border")
  167. }
  168. }
  169. delegate: ItemDelegate
  170. {
  171. width: control.width - 2 * UM.Theme.getSize("default_lining").width
  172. height: control.height
  173. highlighted: control.highlightedIndex == index
  174. contentItem: UM.Label
  175. {
  176. anchors.fill: parent
  177. anchors.leftMargin: UM.Theme.getSize("setting_unit_margin").width
  178. anchors.rightMargin: UM.Theme.getSize("setting_unit_margin").width
  179. text: model.name
  180. color: model.enabled ? UM.Theme.getColor("setting_control_text") : UM.Theme.getColor("action_button_disabled_text")
  181. elide: Text.ElideRight
  182. rightPadding: swatch.width + UM.Theme.getSize("setting_unit_margin").width
  183. background: Rectangle
  184. {
  185. id: swatch
  186. height: Math.round(parent.height / 2)
  187. width: height
  188. radius: Math.round(width / 2)
  189. anchors.right: parent.right
  190. anchors.verticalCenter: parent.verticalCenter
  191. anchors.rightMargin: UM.Theme.getSize("thin_margin").width
  192. color: control.model.getItem(index).color
  193. }
  194. }
  195. background: Rectangle
  196. {
  197. color: parent.highlighted ? UM.Theme.getColor("setting_control_highlight") : "transparent"
  198. }
  199. }
  200. }
  201. }