NumericTextFieldWithUnit.qml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 QtQuick.Layouts 1.3
  6. import UM 1.3 as UM
  7. import Cura 1.1 as Cura
  8. //
  9. // TextField widget with validation for editing numeric data in the Machine Settings dialog.
  10. //
  11. UM.TooltipArea
  12. {
  13. id: numericTextFieldWithUnit
  14. UM.I18nCatalog { id: catalog; name: "cura"; }
  15. height: childrenRect.height
  16. width: childrenRect.width
  17. text: tooltipText
  18. property alias containerStackId: propertyProvider.containerStackId
  19. property alias settingKey: propertyProvider.key
  20. property alias settingStoreIndex: propertyProvider.storeIndex
  21. property alias labelText: fieldLabel.text
  22. property alias labelWidth: fieldLabel.width
  23. property alias unitText: unitLabel.text
  24. property string tooltipText: propertyProvider.properties.description
  25. // whether negative value is allowed. This affects the validation of the input field.
  26. property bool allowNegativeValue: false
  27. // callback functions
  28. property var afterOnEditingFinishedFunction: dummy_func
  29. property var forceUpdateOnChangeFunction: dummy_func
  30. property var setValueFunction: null
  31. // a dummy function for default property values
  32. function dummy_func() {}
  33. UM.SettingPropertyProvider
  34. {
  35. id: propertyProvider
  36. watchedProperties: [ "value", "description" ]
  37. }
  38. Row
  39. {
  40. id: itemRow
  41. spacing: UM.Theme.getSize("default_margin").width
  42. Label
  43. {
  44. id: fieldLabel
  45. anchors.verticalCenter: textFieldWithUnit.verticalCenter
  46. visible: text != ""
  47. elide: Text.ElideRight
  48. renderType: Text.NativeRendering
  49. //width: Math.max(0, settingsTabs.labelColumnWidth)
  50. }
  51. Item
  52. {
  53. id: textFieldWithUnit
  54. width: textField.width
  55. height: textField.height
  56. TextField
  57. {
  58. id: textField
  59. text:
  60. {
  61. const value = propertyProvider.properties.value
  62. return value ? value : ""
  63. }
  64. validator: RegExpValidator { regExp: allowNegativeValue ? /-?[0-9\.,]{0,6}/ : /[0-9\.,]{0,6}/ }
  65. onEditingFinished:
  66. {
  67. if (propertyProvider && text != propertyProvider.properties.value)
  68. {
  69. // For some properties like the extruder-compatible material diameter, they need to
  70. // trigger many updates, such as the available materials, the current material may
  71. // need to be switched, etc. Although setting the diameter can be done directly via
  72. // the provider, all the updates that need to be triggered then need to depend on
  73. // the metadata update, a signal that can be fired way too often. The update functions
  74. // can have if-checks to filter out the irrelevant updates, but still it incurs unnecessary
  75. // overhead.
  76. // The ExtruderStack class has a dedicated function for this call "setCompatibleMaterialDiameter()",
  77. // and it triggers the diameter update signals only when it is needed. Here it is optionally
  78. // choose to use setCompatibleMaterialDiameter() or other more specific functions that
  79. // are available.
  80. if (setValueFunction !== null)
  81. {
  82. setValueFunction(text)
  83. }
  84. else
  85. {
  86. propertyProvider.setPropertyValue("value", text)
  87. }
  88. forceUpdateOnChangeFunction()
  89. afterOnEditingFinished()
  90. }
  91. }
  92. }
  93. Label
  94. {
  95. id: unitLabel
  96. anchors.right: textField.right
  97. anchors.rightMargin: y - textField.y
  98. anchors.verticalCenter: textField.verticalCenter
  99. text: unitText
  100. renderType: Text.NativeRendering
  101. }
  102. }
  103. }
  104. }