SettingOptionalExtruder.qml 8.3 KB

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