SingleSettingTextField.qml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. removeUnusedValue: false
  47. containerStackId: updateAllExtruders ? Cura.ExtruderManager.extruderIds[defaultExtruderIndex] : Cura.MachineManager.activeMachine.id
  48. }
  49. Connections
  50. {
  51. target: propertyProvider
  52. function onContainerStackChanged()
  53. {
  54. updateTimer.restart()
  55. }
  56. function onIsValueUsedChanged()
  57. {
  58. updateTimer.restart()
  59. }
  60. }
  61. // Restart update timer right after releasing a key. This stops lag while typing, but you still get warning and error
  62. // textfield styling while typing.
  63. Keys.onReleased: updateTimer.restart()
  64. // Forces formatting when you finish editing "10.1" -Edit-> "10." -Focus Change-> "10"
  65. onActiveFocusChanged: updateTimer.restart()
  66. // Updates to the setting are delayed by interval. This stops lag caused by calling the
  67. // parseValueUpdateSetting() function being called repeatedly while changing the text value.
  68. Timer
  69. {
  70. id: updateTimer
  71. interval: 50
  72. repeat: false
  73. onTriggered: parseValueUpdateSetting()
  74. }
  75. function parseValueUpdateSetting()
  76. {
  77. // User convenience. We use dots for decimal values
  78. const modified_text = text.replace(",", ".");
  79. if (propertyProvider.properties.value === modified_text || (parseFloat(propertyProvider.properties.value) === parseFloat(modified_text)))
  80. {
  81. // Don't set the property value from the control. It already has the same value
  82. return
  83. }
  84. if (propertyProvider && modified_text !== propertyProvider.properties.value)
  85. {
  86. updateSetting(modified_text);
  87. }
  88. }
  89. function updateSetting(value)
  90. {
  91. if (updateAllExtruders)
  92. {
  93. Cura.MachineManager.setSettingForAllExtruders(propertyProvider.key, "value", value)
  94. }
  95. else
  96. {
  97. propertyProvider.setPropertyValue("value", text)
  98. }
  99. }
  100. // Forced to override parent states using overrideState. Otherwise hover in TextField.qml would override the validation states.
  101. // The first state to evaluate true applies styling. States in inheriting components get appended to the state list of their parent.
  102. overrideState: true
  103. states:
  104. [
  105. State
  106. {
  107. name: "validationError"
  108. when: propertyProvider.properties.validationState === "ValidatorState.Exception" || propertyProvider.properties.validationState === "ValidatorState.MinimumError" || propertyProvider.properties.validationState === "ValidatorState.MaximumError"
  109. PropertyChanges
  110. {
  111. target: background
  112. liningColor: UM.Theme.getColor("setting_validation_error")
  113. color: UM.Theme.getColor("setting_validation_error_background")
  114. }
  115. },
  116. State
  117. {
  118. name: "validationWarning"
  119. when: propertyProvider.properties.validationState === "ValidatorState.MinimumWarning" || propertyProvider.properties.validationState === "ValidatorState.MaximumWarning"
  120. PropertyChanges
  121. {
  122. target: background
  123. liningColor: UM.Theme.getColor("setting_validation_warning")
  124. color: UM.Theme.getColor("setting_validation_warning_background")
  125. }
  126. },
  127. State
  128. {
  129. name: "disabled"
  130. when: !control.enabled
  131. PropertyChanges
  132. {
  133. target: control
  134. color: UM.Theme.getColor("text_field_text_disabled")
  135. }
  136. PropertyChanges
  137. {
  138. target: background
  139. liningColor: UM.Theme.getColor("text_field_border_disabled")
  140. }
  141. },
  142. State
  143. {
  144. name: "invalid"
  145. when: !control.acceptableInput
  146. PropertyChanges
  147. {
  148. target: background
  149. color: UM.Theme.getColor("setting_validation_error_background")
  150. }
  151. },
  152. State
  153. {
  154. name: "active"
  155. when: control.activeFocus
  156. PropertyChanges
  157. {
  158. target: background
  159. liningColor: UM.Theme.getColor("text_field_border_active")
  160. borderColor: UM.Theme.getColor("text_field_border_active")
  161. }
  162. },
  163. State
  164. {
  165. name: "hovered"
  166. when: control.hovered && !control.activeFocus
  167. PropertyChanges
  168. {
  169. target: background
  170. liningColor: UM.Theme.getColor("text_field_border_hovered")
  171. }
  172. }
  173. ]
  174. }