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