SettingOptionalExtruder.qml 8.3 KB

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