PrintHeadMinMaxTextField.qml 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2020 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. if(propertyProvider.properties.value === undefined) { //PropertyProvider not initialised yet or there is no global stack.
  33. return 0;
  34. }
  35. var polygon = JSON.parse(propertyProvider.properties.value);
  36. var item = (axisName == "x") ? 0 : 1;
  37. var result = polygon[0][item];
  38. var func = (axisMinOrMax == "min") ? Math.min : Math.max;
  39. for (var i = 1; i < polygon.length; i++)
  40. {
  41. result = func(result, polygon[i][item]);
  42. }
  43. return result;
  44. }
  45. valueText: axisValue
  46. Connections
  47. {
  48. target: textField
  49. function onActiveFocusChanged()
  50. {
  51. // When this text field loses focus and the entered text is not valid, make sure to recreate the binding to
  52. // show the correct value.
  53. if (!textField.activeFocus && !textField.acceptableInput)
  54. {
  55. valueText = Qt.binding(function() { return printerHeadMinMaxField.axisValue })
  56. }
  57. }
  58. }
  59. editingFinishedFunction: function()
  60. {
  61. var polygon = JSON.parse(propertyProvider.properties.value)
  62. var newValue = parseFloat(valueText.replace(',', '.'))
  63. if (axisName == "x") // x min/x max
  64. {
  65. var start_i1 = (axisMinOrMax == "min") ? 0 : 2
  66. polygon[start_i1][0] = newValue
  67. polygon[start_i1 + 1][0] = newValue
  68. }
  69. else // y min/y max
  70. {
  71. var start_i1 = (axisMinOrMax == "min") ? 1 : 0
  72. polygon[start_i1][1] = newValue
  73. polygon[start_i1 + 2][1] = newValue
  74. }
  75. var polygon_string = JSON.stringify(polygon)
  76. if (polygon_string != propertyProvider.properties.value)
  77. {
  78. propertyProvider.setPropertyValue("value", polygon_string)
  79. forceUpdateOnChangeFunction()
  80. }
  81. // Recreate the binding to show the correct value.
  82. valueText = Qt.binding(function() { return axisValue })
  83. }
  84. }