SingleSettingTextField.qml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (c) 2022 UltiMaker
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.10
  4. import QtQuick.Controls 2.3
  5. import QtQuick.Layouts 1.3
  6. import UM 1.7 as UM
  7. import Cura 1.7 as Cura
  8. // This text field allows you to edit a single setting. The setting can be passed by "settingName".
  9. // You must specify a validator with Validator. We store our default setting validators in qml/Validators
  10. // If the setting is limited to a single extruder or is settable with different values per extruder use "updateAllExtruders: true"
  11. UM.TextField
  12. {
  13. id: control
  14. property alias settingName: propertyProvider.key
  15. // If true, all extruders will have "settingName" property updated.
  16. // The displayed value will be read from the extruder with index "defaultExtruderIndex" instead of the machine.
  17. property bool updateAllExtruders: false
  18. // This is only used if updateAllExtruders == true
  19. property int defaultExtruderIndex: Cura.ExtruderManager.activeExtruderIndex
  20. // Resolving the value in the textField.
  21. Binding
  22. {
  23. target: control
  24. property: "text"
  25. value:
  26. {
  27. if (control.activeFocus)
  28. {
  29. // This stops the text being reformatted as you edit. For example "10.1" -Edit-> "10." -Auto Format-> "10.0".
  30. return control.text
  31. }
  32. if (( propertyProvider.properties.resolve != "None" && propertyProvider.properties.resolve) && ( propertyProvider.properties.stackLevels[0] != 0) && ( propertyProvider.properties.stackLevels[0] != 1))
  33. {
  34. // We have a resolve function. Indicates that the setting is not settable per extruder and that
  35. // we have to choose between the resolved value (default) and the global value
  36. // (if user has explicitly set this).
  37. return base.resolve
  38. }
  39. return propertyProvider.properties.value
  40. }
  41. }
  42. property UM.SettingPropertyProvider propertyProvider: UM.SettingPropertyProvider
  43. {
  44. id: propertyProvider
  45. watchedProperties: ["value", "validationState", "resolve"]
  46. containerStackId: updateAllExtruders ? Cura.ExtruderManager.extruderIds[defaultExtruderIndex] : Cura.MachineManager.activeMachine.id
  47. }
  48. Connections
  49. {
  50. target: propertyProvider
  51. function onContainerStackChanged()
  52. {
  53. updateTimer.restart()
  54. }
  55. function onIsValueUsedChanged()
  56. {
  57. updateTimer.restart()
  58. }
  59. }
  60. // Restart update timer right after releasing a key. This stops lag while typing, but you still get warning and error
  61. // textfield styling while typing.
  62. Keys.onReleased: updateTimer.restart()
  63. // Forces formatting when you finish editing "10.1" -Edit-> "10." -Focus Change-> "10"
  64. onActiveFocusChanged: updateTimer.restart()
  65. // Updates to the setting are delayed by interval. This stops lag caused by calling the
  66. // parseValueUpdateSetting() function being called repeatedly while changing the text value.
  67. Timer
  68. {
  69. id: updateTimer
  70. interval: 50
  71. repeat: false
  72. onTriggered: parseValueUpdateSetting()
  73. }
  74. function parseValueUpdateSetting()
  75. {
  76. // User convenience. We use dots for decimal values
  77. const modified_text = text.replace(",", ".");
  78. if (propertyProvider.properties.value === modified_text || (parseFloat(propertyProvider.properties.value) === parseFloat(modified_text)))
  79. {
  80. // Don't set the property value from the control. It already has the same value
  81. return
  82. }
  83. if (propertyProvider && modified_text !== propertyProvider.properties.value)
  84. {
  85. updateSetting(modified_text);
  86. }
  87. }
  88. function updateSetting(value)
  89. {
  90. if (updateAllExtruders)
  91. {
  92. Cura.MachineManager.setSettingForAllExtruders(propertyProvider.key, "value", value)
  93. }
  94. else
  95. {
  96. propertyProvider.setPropertyValue("value", text)
  97. }
  98. }
  99. // Forced to override parent states using overrideState. Otherwise hover in TextField.qml would override the validation states.
  100. // The first state to evaluate true applies styling. States in inheriting components get appended to the state list of their parent.
  101. overrideState: true
  102. states:
  103. [
  104. State
  105. {
  106. name: "validationError"
  107. when: propertyProvider.properties.validationState === "ValidatorState.Exception" || propertyProvider.properties.validationState === "ValidatorState.MinimumError" || propertyProvider.properties.validationState === "ValidatorState.MaximumError"
  108. PropertyChanges
  109. {
  110. target: background
  111. liningColor: UM.Theme.getColor("setting_validation_error")
  112. color: UM.Theme.getColor("setting_validation_error_background")
  113. }
  114. },
  115. State
  116. {
  117. name: "validationWarning"
  118. when: propertyProvider.properties.validationState === "ValidatorState.MinimumWarning" || propertyProvider.properties.validationState === "ValidatorState.MaximumWarning"
  119. PropertyChanges
  120. {
  121. target: background
  122. liningColor: UM.Theme.getColor("setting_validation_warning")
  123. color: UM.Theme.getColor("setting_validation_warning_background")
  124. }
  125. },
  126. State
  127. {
  128. name: "disabled"
  129. when: !control.enabled
  130. PropertyChanges
  131. {
  132. target: control
  133. color: UM.Theme.getColor("text_field_text_disabled")
  134. }
  135. PropertyChanges
  136. {
  137. target: background
  138. liningColor: UM.Theme.getColor("text_field_border_disabled")
  139. }
  140. },
  141. State
  142. {
  143. name: "invalid"
  144. when: !control.acceptableInput
  145. PropertyChanges
  146. {
  147. target: background
  148. color: UM.Theme.getColor("setting_validation_error_background")
  149. }
  150. },
  151. State
  152. {
  153. name: "active"
  154. when: control.activeFocus
  155. PropertyChanges
  156. {
  157. target: background
  158. liningColor: UM.Theme.getColor("text_field_border_active")
  159. borderColor: UM.Theme.getColor("text_field_border_active")
  160. }
  161. },
  162. State
  163. {
  164. name: "hovered"
  165. when: control.hovered && !control.activeFocus
  166. PropertyChanges
  167. {
  168. target: background
  169. liningColor: UM.Theme.getColor("text_field_border_hovered")
  170. }
  171. }
  172. ]
  173. }