SettingTextField.qml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2015 Ultimaker B.V.
  2. // Uranium is released under the terms of the AGPLv3 or higher.
  3. import QtQuick 2.2
  4. import QtQuick.Controls 1.2
  5. import UM 1.1 as UM
  6. SettingItem
  7. {
  8. id: base
  9. contents: Rectangle
  10. {
  11. id: control
  12. anchors.fill: parent
  13. property alias hovered: mouseArea.containsMouse;
  14. border.width: UM.Theme.getSize("default_lining").width
  15. border.color: !enabled ? UM.Theme.getColor("setting_control_disabled_border") : hovered ? UM.Theme.getColor("setting_control_border_highlight") : UM.Theme.getColor("setting_control_border")
  16. property variant parentValue: value //From parent loader
  17. function notifyReset() {
  18. input.text = format(parentValue)
  19. }
  20. color: {
  21. if (!enabled)
  22. {
  23. return UM.Theme.getColor("setting_control_disabled")
  24. }
  25. switch(propertyProvider.properties.validationState)
  26. {
  27. case "ValidatorState.Exception":
  28. return UM.Theme.getColor("setting_validation_error")
  29. case "ValidatorState.MinimumError":
  30. return UM.Theme.getColor("setting_validation_error")
  31. case "ValidatorState.MaximumError":
  32. return UM.Theme.getColor("setting_validation_error")
  33. case "ValidatorState.MinimumWarning":
  34. return UM.Theme.getColor("setting_validation_warning")
  35. case "ValidatorState.MaximumWarning":
  36. return UM.Theme.getColor("setting_validation_warning")
  37. case "ValidatorState.Valid":
  38. return UM.Theme.getColor("setting_validation_ok")
  39. default:
  40. return UM.Theme.getColor("setting_control")
  41. }
  42. }
  43. Rectangle
  44. {
  45. anchors.fill: parent;
  46. anchors.margins: UM.Theme.getSize("default_lining").width;
  47. color: UM.Theme.getColor("setting_control_highlight")
  48. opacity: !control.hovered ? 0 : propertyProvider.properties.validationState == "ValidatorState.Valid" ? 1.0 : 0.35;
  49. }
  50. Label
  51. {
  52. anchors.right: parent.right;
  53. anchors.rightMargin: UM.Theme.getSize("setting_unit_margin").width
  54. anchors.verticalCenter: parent.verticalCenter;
  55. text: definition.unit;
  56. color: UM.Theme.getColor("setting_unit")
  57. font: UM.Theme.getFont("default")
  58. }
  59. MouseArea
  60. {
  61. id: mouseArea
  62. anchors.fill: parent;
  63. hoverEnabled: true;
  64. cursorShape: Qt.IBeamCursor
  65. }
  66. TextInput
  67. {
  68. id: input
  69. anchors
  70. {
  71. left: parent.left
  72. leftMargin: UM.Theme.getSize("setting_unit_margin").width
  73. right: parent.right
  74. verticalCenter: parent.verticalCenter
  75. }
  76. Keys.onReleased:
  77. {
  78. // text = text.replace(",", ".") // User convenience. We use dots for decimal values
  79. // if(parseFloat(text) != base.parentValue)
  80. // {
  81. // base.valueChanged(parseFloat(text));
  82. // }
  83. propertyProvider.setPropertyValue("value", text)
  84. }
  85. onEditingFinished:
  86. {
  87. // if(parseFloat(text) != base.parentValue)
  88. // {
  89. // base.valueChanged(parseFloat(text));
  90. // }
  91. propertyProvider.setPropertyValue("value", text)
  92. }
  93. color: !enabled ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  94. font: UM.Theme.getFont("default");
  95. selectByMouse: true;
  96. maximumLength: 10;
  97. validator: RegExpValidator { regExp: /[0-9.,-]{0,10}/ }
  98. Binding
  99. {
  100. target: input
  101. property: "text"
  102. value: control.format(propertyProvider.properties.value)
  103. when: !input.activeFocus
  104. }
  105. }
  106. //Rounds a floating point number to 4 decimals. This prevents floating
  107. //point rounding errors.
  108. //
  109. //input: The number to round.
  110. //decimals: The number of decimals (digits after the radix) to round to.
  111. //return: The rounded number.
  112. function roundFloat(input, decimals)
  113. {
  114. //First convert to fixed-point notation to round the number to 4 decimals and not introduce new floating point errors.
  115. //Then convert to a string (is implicit). The fixed-point notation will be something like "3.200".
  116. //Then remove any trailing zeroes and the radix.
  117. return input.toFixed(decimals).replace(/\.?0*$/, ""); //Match on periods, if any ( \.? ), followed by any number of zeros ( 0* ), then the end of string ( $ ).
  118. }
  119. //Formats a value for display in the text field.
  120. //
  121. //This correctly handles formatting of float values.
  122. //
  123. //input: The string value to format.
  124. //return: The formatted string.
  125. function format(inputValue) {
  126. return parseFloat(inputValue) ? roundFloat(parseFloat(inputValue), 4) : inputValue //If it's a float, round to four decimals.
  127. }
  128. }
  129. }