NumericTextFieldWithUnit.qml 8.8 KB

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