SettingTextField.qml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) 2017 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Controls 2.0
  5. import UM 1.1 as UM
  6. SettingItem
  7. {
  8. id: base
  9. property var focusItem: input
  10. property string textBeforeEdit
  11. property bool textHasChanged
  12. onFocusReceived:
  13. {
  14. textHasChanged = false;
  15. textBeforeEdit = focusItem.text;
  16. focusItem.selectAll();
  17. }
  18. contents: Rectangle
  19. {
  20. id: control
  21. anchors.fill: parent
  22. border.width: Math.round(UM.Theme.getSize("default_lining").width)
  23. border.color:
  24. {
  25. if(!enabled)
  26. {
  27. return UM.Theme.getColor("setting_control_disabled_border")
  28. }
  29. switch(propertyProvider.properties.validationState)
  30. {
  31. case "ValidatorState.Exception":
  32. case "ValidatorState.MinimumError":
  33. case "ValidatorState.MaximumError":
  34. return UM.Theme.getColor("setting_validation_error");
  35. case "ValidatorState.MinimumWarning":
  36. case "ValidatorState.MaximumWarning":
  37. return UM.Theme.getColor("setting_validation_warning");
  38. }
  39. //Validation is OK.
  40. if(hovered || input.activeFocus)
  41. {
  42. return UM.Theme.getColor("setting_control_border_highlight")
  43. }
  44. return UM.Theme.getColor("setting_control_border")
  45. }
  46. color: {
  47. if(!enabled)
  48. {
  49. return UM.Theme.getColor("setting_control_disabled")
  50. }
  51. switch(propertyProvider.properties.validationState)
  52. {
  53. case "ValidatorState.Exception":
  54. case "ValidatorState.MinimumError":
  55. case "ValidatorState.MaximumError":
  56. return UM.Theme.getColor("setting_validation_error_background")
  57. case "ValidatorState.MinimumWarning":
  58. case "ValidatorState.MaximumWarning":
  59. return UM.Theme.getColor("setting_validation_warning_background")
  60. case "ValidatorState.Valid":
  61. return UM.Theme.getColor("setting_validation_ok")
  62. default:
  63. return UM.Theme.getColor("setting_control")
  64. }
  65. }
  66. Rectangle
  67. {
  68. anchors.fill: parent;
  69. anchors.margins: Math.round(UM.Theme.getSize("default_lining").width);
  70. color: UM.Theme.getColor("setting_control_highlight")
  71. opacity: !control.hovered ? 0 : propertyProvider.properties.validationState == "ValidatorState.Valid" ? 1.0 : 0.35;
  72. }
  73. Label
  74. {
  75. anchors.right: parent.right
  76. anchors.rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  77. anchors.verticalCenter: parent.verticalCenter
  78. text: definition.unit
  79. renderType: Text.NativeRendering
  80. color: UM.Theme.getColor("setting_unit")
  81. font: UM.Theme.getFont("default")
  82. }
  83. MouseArea
  84. {
  85. id: mouseArea
  86. anchors.fill: parent;
  87. //hoverEnabled: true;
  88. cursorShape: Qt.IBeamCursor
  89. }
  90. TextInput
  91. {
  92. id: input
  93. anchors
  94. {
  95. left: parent.left
  96. leftMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  97. right: parent.right
  98. rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  99. verticalCenter: parent.verticalCenter
  100. }
  101. renderType: Text.NativeRendering
  102. Keys.onTabPressed:
  103. {
  104. base.setActiveFocusToNextSetting(true)
  105. }
  106. Keys.onBacktabPressed:
  107. {
  108. base.setActiveFocusToNextSetting(false)
  109. }
  110. Keys.onReleased:
  111. {
  112. if (text != textBeforeEdit)
  113. {
  114. textHasChanged = true;
  115. }
  116. if (textHasChanged)
  117. {
  118. propertyProvider.setPropertyValue("value", text)
  119. }
  120. }
  121. onActiveFocusChanged:
  122. {
  123. if(activeFocus)
  124. {
  125. base.focusReceived();
  126. }
  127. }
  128. color: !enabled ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  129. font: UM.Theme.getFont("default");
  130. selectByMouse: true;
  131. maximumLength: (definition.type == "str" || definition.type == "[int]") ? -1 : 10;
  132. clip: true; //Hide any text that exceeds the width of the text box.
  133. validator: RegExpValidator { regExp: (definition.type == "[int]") ? /^\[?(\s*-?[0-9]{0,9}\s*,)*(\s*-?[0-9]{0,9})\s*\]?$/ : (definition.type == "int") ? /^-?[0-9]{0,10}$/ : (definition.type == "float") ? /^-?[0-9]{0,9}[.,]?[0-9]{0,10}$/ : /^.*$/ } // definition.type property from parent loader used to disallow fractional number entry
  134. Binding
  135. {
  136. target: input
  137. property: "text"
  138. value: {
  139. // Stacklevels
  140. // 0: user -> unsaved change
  141. // 1: quality changes -> saved change
  142. // 2: quality
  143. // 3: material -> user changed material in materialspage
  144. // 4: variant
  145. // 5: machine_changes
  146. // 6: machine
  147. if ((base.resolve != "None" && base.resolve) && (stackLevel != 0) && (stackLevel != 1)) {
  148. // We have a resolve function. Indicates that the setting is not settable per extruder and that
  149. // we have to choose between the resolved value (default) and the global value
  150. // (if user has explicitly set this).
  151. return base.resolve;
  152. } else {
  153. return propertyProvider.properties.value;
  154. }
  155. }
  156. when: !input.activeFocus
  157. }
  158. }
  159. }
  160. }