NumericTextFieldWithUnit.qml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. color: UM.Theme.getColor("text")
  52. renderType: Text.NativeRendering
  53. }
  54. TextField
  55. {
  56. id: textFieldWithUnit
  57. anchors.left: fieldLabel.right
  58. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  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. color: UM.Theme.getColor("text")
  116. renderType: Text.NativeRendering
  117. // When the textbox gets focused by TAB, select all text
  118. onActiveFocusChanged:
  119. {
  120. if (activeFocus && (focusReason == Qt.TabFocusReason || focusReason == Qt.BacktabFocusReason))
  121. {
  122. selectAll()
  123. }
  124. }
  125. text:
  126. {
  127. const value = propertyProvider.properties.value
  128. return value ? value : ""
  129. }
  130. validator: RegExpValidator { regExp: allowNegativeValue ? /-?[0-9\.,]{0,6}/ : /[0-9\.,]{0,6}/ }
  131. onEditingFinished: editingFinishedFunction()
  132. property var editingFinishedFunction: defaultEditingFinishedFunction
  133. function defaultEditingFinishedFunction()
  134. {
  135. if (propertyProvider && text != propertyProvider.properties.value)
  136. {
  137. // For some properties like the extruder-compatible material diameter, they need to
  138. // trigger many updates, such as the available materials, the current material may
  139. // need to be switched, etc. Although setting the diameter can be done directly via
  140. // the provider, all the updates that need to be triggered then need to depend on
  141. // the metadata update, a signal that can be fired way too often. The update functions
  142. // can have if-checks to filter out the irrelevant updates, but still it incurs unnecessary
  143. // overhead.
  144. // The ExtruderStack class has a dedicated function for this call "setCompatibleMaterialDiameter()",
  145. // and it triggers the diameter update signals only when it is needed. Here it is optionally
  146. // choose to use setCompatibleMaterialDiameter() or other more specific functions that
  147. // are available.
  148. if (setValueFunction !== null)
  149. {
  150. setValueFunction(text)
  151. }
  152. else
  153. {
  154. propertyProvider.setPropertyValue("value", text)
  155. }
  156. forceUpdateOnChangeFunction()
  157. afterOnEditingFinishedFunction()
  158. }
  159. }
  160. Label
  161. {
  162. id: unitLabel
  163. anchors.right: parent.right
  164. anchors.rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  165. anchors.verticalCenter: parent.verticalCenter
  166. text: unitText
  167. textFormat: Text.PlainText
  168. verticalAlignment: Text.AlignVCenter
  169. renderType: Text.NativeRendering
  170. color: UM.Theme.getColor("setting_unit")
  171. font: UM.Theme.getFont("default")
  172. }
  173. }
  174. }