SettingExtruder.qml 8.4 KB

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