PrintHeadMinMaxTextField.qml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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: machineXMaxField
  24. UM.I18nCatalog { id: catalog; name: "cura" }
  25. containerStackId: Cura.MachineManager.activeMachineId
  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. result = Math.abs(result)
  41. return result
  42. }
  43. valueValidator: RegExpValidator { regExp: /[0-9\.,]{0,6}/ }
  44. valueText: axisValue
  45. editingFinishedFunction: function()
  46. {
  47. var polygon = JSON.parse(propertyProvider.properties.value)
  48. var newValue = parseFloat(valueText.replace(',', '.'))
  49. if (axisName == "x") // x min/x max
  50. {
  51. var start_i1 = (axisMinOrMax == "min") ? 0 : 2
  52. var factor = (axisMinOrMax == "min") ? -1 : 1
  53. polygon[start_i1][0] = newValue * factor
  54. polygon[start_i1 + 1][0] = newValue * factor
  55. }
  56. else // y min/y max
  57. {
  58. var start_i1 = (axisMinOrMax == "min") ? 1 : 0
  59. var factor = (axisMinOrMax == "min") ? -1 : 1
  60. polygon[start_i1][1] = newValue * factor
  61. polygon[start_i1 + 2][1] = newValue * factor
  62. }
  63. var polygon_string = JSON.stringify(polygon)
  64. if (polygon_string != propertyProvider.properties.value)
  65. {
  66. propertyProvider.setPropertyValue("value", polygon_string)
  67. forceUpdateOnChangeFunction()
  68. }
  69. }
  70. // TODO: add forceUpdateOnChangeFunction:
  71. }