PolygonTextField.qml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 for editing polygon data in the Machine Settings dialog.
  10. //
  11. UM.TooltipArea
  12. {
  13. UM.I18nCatalog { id: catalog; name: "cura"; }
  14. height: textField.height
  15. width: textField.width
  16. text: tooltip
  17. property alias containerStackId: propertyProvider.containerStackId
  18. property alias settingKey: propertyProvider.key
  19. property alias settingStoreIndex: propertyProvider.storeIndex
  20. property alias labelText: fieldLabel.text
  21. property alias labelWidth: fieldLabel.width
  22. property string unitText: catalog.i18nc("@label", "mm")
  23. // callback functions
  24. property var forceUpdateOnChangeFunction: dummy_func
  25. // a dummy function for default property values
  26. function dummy_func() {}
  27. property var printHeadPolygon:
  28. {
  29. "x": {
  30. "min": 0,
  31. "max": 0,
  32. },
  33. "y": {
  34. "min": 0,
  35. "max": 0,
  36. },
  37. }
  38. UM.SettingPropertyProvider
  39. {
  40. id: propertyProvider
  41. watchedProperties: [ "value" ]
  42. }
  43. Row
  44. {
  45. spacing: UM.Theme.getSize("default_margin").width
  46. Label
  47. {
  48. id: fieldLabel
  49. anchors.verticalCenter: textFieldWithUnit.verticalCenter
  50. visible: text != ""
  51. elide: Text.ElideRight
  52. //width: Math.max(0, settingsTabs.labelColumnWidth)
  53. }
  54. Item
  55. {
  56. id: textFieldWithUnit
  57. width: textField.width
  58. height: textField.height
  59. TextField
  60. {
  61. id: textField
  62. text:
  63. {
  64. var polygon = JSON.parse(propertyProvider.properties.value)
  65. var item = (axis == "x") ? 0 : 1
  66. var result = polygon[0][item]
  67. for (var i = 1; i < polygon.length; i++) {
  68. result = (side == "min")
  69. ? Math.min(result, polygon[i][item])
  70. : Math.max(result, polygon[i][item])
  71. }
  72. result = Math.abs(result)
  73. printHeadPolygon[axis][side] = result
  74. return result
  75. }
  76. validator: RegExpValidator { regExp: /[0-9\.,]{0,6}/ }
  77. onEditingFinished:
  78. {
  79. printHeadPolygon[axis][side] = parseFloat(textField.text.replace(',','.'))
  80. var polygon = [
  81. [-printHeadPolygon["x"]["min"], printHeadPolygon["y"]["max"]],
  82. [-printHeadPolygon["x"]["min"], -printHeadPolygon["y"]["min"]],
  83. [ printHeadPolygon["x"]["max"], printHeadPolygon["y"]["max"]],
  84. [ printHeadPolygon["x"]["max"], -printHeadPolygon["y"]["min"]]
  85. ]
  86. var polygon_string = JSON.stringify(polygon)
  87. if (polygon_string != propertyProvider.properties.value)
  88. {
  89. propertyProvider.setPropertyValue("value", polygon_string)
  90. forceUpdateOnChangeFunction()
  91. }
  92. }
  93. }
  94. Label
  95. {
  96. id: unitLabel
  97. text: unitText
  98. anchors.right: textField.right
  99. anchors.rightMargin: y - textField.y
  100. anchors.verticalCenter: textField.verticalCenter
  101. }
  102. }
  103. }
  104. }