NumericTextFieldWithUnit.qml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2020 Ultimaker B.V.
  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 UM 1.3 as UM
  6. import Cura 1.1 as Cura
  7. //
  8. // TextField widget with validation for editing numeric data in the Machine Settings dialog.
  9. //
  10. UM.TooltipArea
  11. {
  12. id: numericTextFieldWithUnit
  13. UM.I18nCatalog { id: catalog; name: "cura"; }
  14. height: childrenRect.height
  15. width: childrenRect.width
  16. property int controlWidth: UM.Theme.getSize("setting_control").width
  17. property int controlHeight: UM.Theme.getSize("setting_control").height
  18. text: tooltipText
  19. property alias containerStackId: propertyProvider.containerStackId
  20. property alias settingKey: propertyProvider.key
  21. property alias settingStoreIndex: propertyProvider.storeIndex
  22. property alias propertyProvider: propertyProvider
  23. property alias labelText: fieldLabel.text
  24. property alias labelFont: fieldLabel.font
  25. property alias labelWidth: fieldLabel.width
  26. property alias unitText: unitLabel.text
  27. property alias textField: textFieldWithUnit
  28. property alias valueText: textFieldWithUnit.text
  29. property alias editingFinishedFunction: textFieldWithUnit.editingFinishedFunction
  30. property string tooltipText: propertyProvider.properties.description ? propertyProvider.properties.description : ""
  31. property real minimum: 0
  32. property real maximum: Number.POSITIVE_INFINITY
  33. property int decimals: 6
  34. // callback functions
  35. property var afterOnEditingFinishedFunction: dummy_func
  36. property var forceUpdateOnChangeFunction: dummy_func
  37. property var setValueFunction: null
  38. // a dummy function for default property values
  39. function dummy_func() {}
  40. UM.SettingPropertyProvider
  41. {
  42. id: propertyProvider
  43. watchedProperties: [ "value", "description" ]
  44. }
  45. Label
  46. {
  47. id: fieldLabel
  48. anchors.left: parent.left
  49. anchors.verticalCenter: textFieldWithUnit.verticalCenter
  50. visible: text != ""
  51. font: UM.Theme.getFont("default")
  52. color: UM.Theme.getColor("text")
  53. renderType: Text.NativeRendering
  54. }
  55. TextField
  56. {
  57. id: textFieldWithUnit
  58. anchors.left: fieldLabel.right
  59. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  60. verticalAlignment: Text.AlignVCenter
  61. width: numericTextFieldWithUnit.controlWidth
  62. height: numericTextFieldWithUnit.controlHeight
  63. // Background is a rounded-cornered box with filled color as state indication (normal, warning, error, etc.)
  64. background: Rectangle
  65. {
  66. anchors.fill: parent
  67. anchors.margins: Math.round(UM.Theme.getSize("default_lining").width)
  68. radius: UM.Theme.getSize("setting_control_radius").width
  69. border.color:
  70. {
  71. if (!textFieldWithUnit.enabled)
  72. {
  73. return UM.Theme.getColor("setting_control_disabled_border")
  74. }
  75. switch (propertyProvider.properties.validationState)
  76. {
  77. case "ValidatorState.Exception":
  78. case "ValidatorState.MinimumError":
  79. case "ValidatorState.MaximumError":
  80. return UM.Theme.getColor("setting_validation_error")
  81. case "ValidatorState.MinimumWarning":
  82. case "ValidatorState.MaximumWarning":
  83. return UM.Theme.getColor("setting_validation_warning")
  84. }
  85. // Validation is OK.
  86. if (textFieldWithUnit.hovered || textFieldWithUnit.activeFocus)
  87. {
  88. return UM.Theme.getColor("setting_control_border_highlight")
  89. }
  90. return UM.Theme.getColor("setting_control_border")
  91. }
  92. color:
  93. {
  94. if (!textFieldWithUnit.enabled)
  95. {
  96. return UM.Theme.getColor("setting_control_disabled")
  97. }
  98. switch (propertyProvider.properties.validationState)
  99. {
  100. case "ValidatorState.Exception":
  101. case "ValidatorState.MinimumError":
  102. case "ValidatorState.MaximumError":
  103. return UM.Theme.getColor("setting_validation_error_background")
  104. case "ValidatorState.MinimumWarning":
  105. case "ValidatorState.MaximumWarning":
  106. return UM.Theme.getColor("setting_validation_warning_background")
  107. case "ValidatorState.Valid":
  108. return UM.Theme.getColor("setting_validation_ok")
  109. default:
  110. return UM.Theme.getColor("setting_control")
  111. }
  112. }
  113. }
  114. hoverEnabled: true
  115. selectByMouse: true
  116. font: UM.Theme.getFont("default")
  117. color: UM.Theme.getColor("text")
  118. renderType: Text.NativeRendering
  119. // When the textbox gets focused by TAB, select all text
  120. onActiveFocusChanged:
  121. {
  122. if (activeFocus && (focusReason == Qt.TabFocusReason || focusReason == Qt.BacktabFocusReason))
  123. {
  124. selectAll()
  125. }
  126. }
  127. text:
  128. {
  129. const value = propertyProvider.properties.value
  130. return value ? value : ""
  131. }
  132. validator: DoubleValidator
  133. {
  134. bottom: numericTextFieldWithUnit.minimum
  135. top: numericTextFieldWithUnit.maximum
  136. decimals: numericTextFieldWithUnit.decimals
  137. notation: DoubleValidator.StandardNotation
  138. }
  139. //Enforce actual minimum and maximum values.
  140. //The DoubleValidator allows intermediate values, which essentially means that the maximum gets rounded up to the nearest power of 10.
  141. //This is not accurate at all, so here if the value exceeds the maximum or the minimum we disallow it.
  142. property string previousText
  143. onTextChanged:
  144. {
  145. var value = Number(text);
  146. if(value < numericTextFieldWithUnit.minimum || value > numericTextFieldWithUnit.maximum)
  147. {
  148. text = previousText;
  149. }
  150. previousText = text;
  151. }
  152. onEditingFinished: editingFinishedFunction()
  153. property var editingFinishedFunction: defaultEditingFinishedFunction
  154. function defaultEditingFinishedFunction()
  155. {
  156. if (propertyProvider && text != propertyProvider.properties.value)
  157. {
  158. // For some properties like the extruder-compatible material diameter, they need to
  159. // trigger many updates, such as the available materials, the current material may
  160. // need to be switched, etc. Although setting the diameter can be done directly via
  161. // the provider, all the updates that need to be triggered then need to depend on
  162. // the metadata update, a signal that can be fired way too often. The update functions
  163. // can have if-checks to filter out the irrelevant updates, but still it incurs unnecessary
  164. // overhead.
  165. // The ExtruderStack class has a dedicated function for this call "setCompatibleMaterialDiameter()",
  166. // and it triggers the diameter update signals only when it is needed. Here it is optionally
  167. // choose to use setCompatibleMaterialDiameter() or other more specific functions that
  168. // are available.
  169. if (setValueFunction !== null)
  170. {
  171. setValueFunction(text)
  172. }
  173. else
  174. {
  175. propertyProvider.setPropertyValue("value", text)
  176. }
  177. forceUpdateOnChangeFunction()
  178. afterOnEditingFinishedFunction()
  179. }
  180. }
  181. Label
  182. {
  183. id: unitLabel
  184. anchors.right: parent.right
  185. anchors.rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  186. anchors.verticalCenter: parent.verticalCenter
  187. text: unitText
  188. textFormat: Text.PlainText
  189. verticalAlignment: Text.AlignVCenter
  190. renderType: Text.NativeRendering
  191. color: UM.Theme.getColor("setting_unit")
  192. font: UM.Theme.getFont("default")
  193. }
  194. }
  195. }