PrintHeadMinMaxTextField.qml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // This is the widget for editing min and max X and Y for the print head.
  9. // The print head is internally stored as a JSON array or array, representing a polygon of the print head.
  10. // The polygon array is stored in the format illustrated below:
  11. // [ [ -x_min, y_max ],
  12. // [ -x_min, -y_min ],
  13. // [ x_max, y_max ],
  14. // [ x_max, -y_min ],
  15. // ]
  16. //
  17. // In order to modify each field, the widget is configurable via "axisName" and "axisMinOrMax", where
  18. // - axisName is "x" or "y"
  19. // - axisMinOrMax is "min" or "max"
  20. //
  21. NumericTextFieldWithUnit
  22. {
  23. id: printerHeadMinMaxField
  24. UM.I18nCatalog { id: catalog; name: "cura" }
  25. containerStackId: Cura.MachineManager.activeMachine.id
  26. settingKey: "machine_head_with_fans_polygon"
  27. settingStoreIndex: 1
  28. property string axisName: "x"
  29. property string axisMinOrMax: "min"
  30. property var axisValue:
  31. {
  32. var polygon = JSON.parse(propertyProvider.properties.value)
  33. var item = (axisName == "x") ? 0 : 1
  34. var result = polygon[0][item]
  35. var func = (axisMinOrMax == "min") ? Math.min : Math.max
  36. for (var i = 1; i < polygon.length; i++)
  37. {
  38. result = func(result, polygon[i][item])
  39. }
  40. return result
  41. }
  42. valueValidator: DoubleValidator {
  43. bottom: allowNegativeValue ? Number.NEGATIVE_INFINITY : 0
  44. top: allowPositiveValue ? Number.POSITIVE_INFINITY : 0
  45. decimals: 6
  46. notation: DoubleValidator.StandardNotation
  47. }
  48. valueText: axisValue
  49. Connections
  50. {
  51. target: textField
  52. onActiveFocusChanged:
  53. {
  54. // When this text field loses focus and the entered text is not valid, make sure to recreate the binding to
  55. // show the correct value.
  56. if (!textField.activeFocus && !textField.acceptableInput)
  57. {
  58. valueText = Qt.binding(function() { return printerHeadMinMaxField.axisValue })
  59. }
  60. }
  61. }
  62. editingFinishedFunction: function()
  63. {
  64. var polygon = JSON.parse(propertyProvider.properties.value)
  65. var newValue = parseFloat(valueText.replace(',', '.'))
  66. if (axisName == "x") // x min/x max
  67. {
  68. var start_i1 = (axisMinOrMax == "min") ? 0 : 2
  69. polygon[start_i1][0] = newValue
  70. polygon[start_i1 + 1][0] = newValue
  71. }
  72. else // y min/y max
  73. {
  74. var start_i1 = (axisMinOrMax == "min") ? 1 : 0
  75. polygon[start_i1][1] = newValue
  76. polygon[start_i1 + 2][1] = newValue
  77. }
  78. var polygon_string = JSON.stringify(polygon)
  79. if (polygon_string != propertyProvider.properties.value)
  80. {
  81. propertyProvider.setPropertyValue("value", polygon_string)
  82. forceUpdateOnChangeFunction()
  83. }
  84. // Recreate the binding to show the correct value.
  85. valueText = Qt.binding(function() { return axisValue })
  86. }
  87. }