NumericTextFieldWithUnit.qml 7.4 KB

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