SettingTextField.qml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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: base.style.controlBorderWidth;
  15. border.color: !enabled ? base.style.controlDisabledBorderColor : hovered ? base.style.controlBorderHighlightColor : base.style.controlBorderColor
  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 base.style.controlDisabledColor
  24. // }
  25. // switch(definition.validationState) //From parent loader
  26. // {
  27. // case 0:
  28. // return base.style.validationErrorColor;
  29. // case 1:
  30. // return base.style.validationErrorColor;
  31. // case 2:
  32. // return base.style.validationErrorColor;
  33. // case 3:
  34. // return base.style.validationWarningColor;
  35. // case 4:
  36. // return base.style.validationWarningColor;
  37. // case 5:
  38. // return base.style.validationOkColor;
  39. //
  40. // default:
  41. // return base.style.controlTextColor;
  42. // }
  43. // }
  44. Rectangle
  45. {
  46. anchors.fill: parent;
  47. anchors.margins: base.style.controlBorderWidth;
  48. color: base.style.controlHighlightColor;
  49. opacity: 0.35
  50. // opacity: !control.hovered ? 0 : valid == 5 ? 1.0 : 0.35;
  51. }
  52. Label
  53. {
  54. anchors.right: parent.right;
  55. anchors.rightMargin: base.style.unitRightMargin;
  56. anchors.verticalCenter: parent.verticalCenter;
  57. text: definition.unit;
  58. color: base.style.unitColor
  59. font: base.style.unitFont;
  60. }
  61. MouseArea
  62. {
  63. id: mouseArea
  64. anchors.fill: parent;
  65. hoverEnabled: true;
  66. cursorShape: Qt.IBeamCursor
  67. }
  68. TextInput
  69. {
  70. id: input
  71. anchors
  72. {
  73. left: parent.left
  74. leftMargin: base.style.unitRightMargin
  75. right: parent.right
  76. verticalCenter: parent.verticalCenter
  77. }
  78. Keys.onReleased:
  79. {
  80. text = text.replace(",", ".") // User convenience. We use dots for decimal values
  81. if(parseFloat(text) != base.parentValue)
  82. {
  83. base.valueChanged(parseFloat(text));
  84. }
  85. }
  86. onEditingFinished:
  87. {
  88. if(parseFloat(text) != base.parentValue)
  89. {
  90. base.valueChanged(parseFloat(text));
  91. }
  92. }
  93. color: !enabled ? base.style.controlDisabledTextColor : base.style.controlTextColor;
  94. font: base.style.controlFont;
  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: format(base.parentValue)
  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. }