SettingTextField.qml 6.7 KB

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