NumericTextFieldWithUnit.qml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (c) 2019 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 valueText: textFieldWithUnit.text
  28. property alias valueValidator: textFieldWithUnit.validator
  29. property alias editingFinishedFunction: textFieldWithUnit.editingFinishedFunction
  30. property string tooltipText: propertyProvider.properties.description
  31. // whether negative value is allowed. This affects the validation of the input field.
  32. property bool allowNegativeValue: false
  33. // callback functions
  34. property var afterOnEditingFinishedFunction: dummy_func
  35. property var forceUpdateOnChangeFunction: dummy_func
  36. property var setValueFunction: null
  37. // a dummy function for default property values
  38. function dummy_func() {}
  39. UM.SettingPropertyProvider
  40. {
  41. id: propertyProvider
  42. watchedProperties: [ "value", "description" ]
  43. }
  44. Row
  45. {
  46. id: itemRow
  47. spacing: UM.Theme.getSize("default_margin").width
  48. Label
  49. {
  50. id: fieldLabel
  51. anchors.verticalCenter: textFieldWithUnit.verticalCenter
  52. visible: text != ""
  53. font: UM.Theme.getFont("medium")
  54. renderType: Text.NativeRendering
  55. }
  56. TextField
  57. {
  58. id: textFieldWithUnit
  59. width: numericTextFieldWithUnit.controlWidth
  60. height: numericTextFieldWithUnit.controlHeight
  61. // Background is a rounded-cornered box with filled color as state indication (normal, warning, error, etc.)
  62. background: Rectangle
  63. {
  64. anchors.fill: parent
  65. anchors.margins: Math.round(UM.Theme.getSize("default_lining").width)
  66. radius: UM.Theme.getSize("setting_control_radius").width
  67. border.color:
  68. {
  69. if (!textFieldWithUnit.enabled)
  70. {
  71. return UM.Theme.getColor("setting_control_disabled_border")
  72. }
  73. switch (propertyProvider.properties.validationState)
  74. {
  75. case "ValidatorState.Exception":
  76. case "ValidatorState.MinimumError":
  77. case "ValidatorState.MaximumError":
  78. return UM.Theme.getColor("setting_validation_error")
  79. case "ValidatorState.MinimumWarning":
  80. case "ValidatorState.MaximumWarning":
  81. return UM.Theme.getColor("setting_validation_warning")
  82. }
  83. // Validation is OK.
  84. if (textFieldWithUnit.hovered || textFieldWithUnit.activeFocus)
  85. {
  86. return UM.Theme.getColor("setting_control_border_highlight")
  87. }
  88. return UM.Theme.getColor("setting_control_border")
  89. }
  90. color:
  91. {
  92. if (!textFieldWithUnit.enabled)
  93. {
  94. return UM.Theme.getColor("setting_control_disabled")
  95. }
  96. switch (propertyProvider.properties.validationState)
  97. {
  98. case "ValidatorState.Exception":
  99. case "ValidatorState.MinimumError":
  100. case "ValidatorState.MaximumError":
  101. return UM.Theme.getColor("setting_validation_error_background")
  102. case "ValidatorState.MinimumWarning":
  103. case "ValidatorState.MaximumWarning":
  104. return UM.Theme.getColor("setting_validation_warning_background")
  105. case "ValidatorState.Valid":
  106. return UM.Theme.getColor("setting_validation_ok")
  107. default:
  108. return UM.Theme.getColor("setting_control")
  109. }
  110. }
  111. }
  112. hoverEnabled: true
  113. selectByMouse: true
  114. font: UM.Theme.getFont("default")
  115. renderType: Text.NativeRendering
  116. // When the textbox gets focused by TAB, select all text
  117. onActiveFocusChanged:
  118. {
  119. if (activeFocus && (focusReason == Qt.TabFocusReason || focusReason == Qt.BacktabFocusReason))
  120. {
  121. selectAll()
  122. }
  123. }
  124. text:
  125. {
  126. const value = propertyProvider.properties.value
  127. return value ? value : ""
  128. }
  129. validator: RegExpValidator { regExp: allowNegativeValue ? /-?[0-9\.,]{0,6}/ : /[0-9\.,]{0,6}/ }
  130. onEditingFinished: editingFinishedFunction()
  131. property var editingFinishedFunction: defaultEditingFinishedFunction
  132. function defaultEditingFinishedFunction()
  133. {
  134. if (propertyProvider && text != propertyProvider.properties.value)
  135. {
  136. // For some properties like the extruder-compatible material diameter, they need to
  137. // trigger many updates, such as the available materials, the current material may
  138. // need to be switched, etc. Although setting the diameter can be done directly via
  139. // the provider, all the updates that need to be triggered then need to depend on
  140. // the metadata update, a signal that can be fired way too often. The update functions
  141. // can have if-checks to filter out the irrelevant updates, but still it incurs unnecessary
  142. // overhead.
  143. // The ExtruderStack class has a dedicated function for this call "setCompatibleMaterialDiameter()",
  144. // and it triggers the diameter update signals only when it is needed. Here it is optionally
  145. // choose to use setCompatibleMaterialDiameter() or other more specific functions that
  146. // are available.
  147. if (setValueFunction !== null)
  148. {
  149. setValueFunction(text)
  150. }
  151. else
  152. {
  153. propertyProvider.setPropertyValue("value", text)
  154. }
  155. forceUpdateOnChangeFunction()
  156. afterOnEditingFinished()
  157. }
  158. }
  159. Label
  160. {
  161. id: unitLabel
  162. anchors.right: parent.right
  163. anchors.rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  164. anchors.verticalCenter: parent.verticalCenter
  165. text: unitText
  166. textFormat: Text.PlainText
  167. verticalAlignment: Text.AlignVCenter
  168. renderType: Text.NativeRendering
  169. color: UM.Theme.getColor("setting_unit")
  170. font: UM.Theme.getFont("default")
  171. }
  172. }
  173. }
  174. }