SettingTextField.qml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright (c) 2021 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.15
  4. import QtQuick.Controls 2.15
  5. import UM 1.5 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: UM.UnderlineBackground
  24. {
  25. id: control
  26. anchors.fill: parent
  27. borderColor: (hovered || input.activeFocus) ? UM.Theme.getColor("text_field_border_hovered") : "transparent"
  28. liningColor:
  29. {
  30. if(!enabled)
  31. {
  32. return UM.Theme.getColor("text_field_border_disabled")
  33. }
  34. switch(propertyProvider.properties.validationState)
  35. {
  36. case "ValidatorState.Invalid":
  37. case "ValidatorState.Exception":
  38. case "ValidatorState.MinimumError":
  39. case "ValidatorState.MaximumError":
  40. return UM.Theme.getColor("setting_validation_error");
  41. case "ValidatorState.MinimumWarning":
  42. case "ValidatorState.MaximumWarning":
  43. return UM.Theme.getColor("setting_validation_warning");
  44. }
  45. //Validation is OK.
  46. if(hovered || input.activeFocus)
  47. {
  48. return UM.Theme.getColor("text_field_border_hovered")
  49. }
  50. return UM.Theme.getColor("text_field_border")
  51. }
  52. color: {
  53. if(!enabled)
  54. {
  55. return UM.Theme.getColor("setting_control_disabled")
  56. }
  57. switch(propertyProvider.properties.validationState)
  58. {
  59. case "ValidatorState.Invalid":
  60. case "ValidatorState.Exception":
  61. case "ValidatorState.MinimumError":
  62. case "ValidatorState.MaximumError":
  63. return UM.Theme.getColor("setting_validation_error_background")
  64. case "ValidatorState.MinimumWarning":
  65. case "ValidatorState.MaximumWarning":
  66. return UM.Theme.getColor("setting_validation_warning_background")
  67. case "ValidatorState.Valid":
  68. return UM.Theme.getColor("setting_validation_ok")
  69. default:
  70. return UM.Theme.getColor("text_field")
  71. }
  72. }
  73. UM.Label
  74. {
  75. anchors
  76. {
  77. left: parent.left
  78. leftMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  79. right: parent.right
  80. rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  81. verticalCenter: parent.verticalCenter
  82. }
  83. text: definition.unit
  84. //However the setting value is aligned, align the unit opposite. That way it stays readable with right-to-left languages.
  85. horizontalAlignment: (input.effectiveHorizontalAlignment == Text.AlignLeft) ? Text.AlignRight : Text.AlignLeft
  86. textFormat: Text.PlainText
  87. color: UM.Theme.getColor("setting_unit")
  88. }
  89. TextInput
  90. {
  91. id: input
  92. anchors
  93. {
  94. left: parent.left
  95. leftMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  96. right: parent.right
  97. rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  98. verticalCenter: parent.verticalCenter
  99. }
  100. renderType: Text.NativeRendering
  101. Keys.onTabPressed:
  102. {
  103. base.setActiveFocusToNextSetting(true)
  104. }
  105. Keys.onBacktabPressed:
  106. {
  107. base.setActiveFocusToNextSetting(false)
  108. }
  109. Keys.onReleased:
  110. {
  111. if (text != textBeforeEdit)
  112. {
  113. textHasChanged = true;
  114. }
  115. if (textHasChanged)
  116. {
  117. propertyProvider.setPropertyValue("value", text)
  118. }
  119. }
  120. onActiveFocusChanged:
  121. {
  122. if(activeFocus)
  123. {
  124. base.focusReceived();
  125. }
  126. base.focusGainedByClick = false;
  127. }
  128. color: !enabled ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  129. selectedTextColor: UM.Theme.getColor("setting_control_text")
  130. font: UM.Theme.getFont("default")
  131. selectionColor: UM.Theme.getColor("text_selection")
  132. selectByMouse: true
  133. maximumLength: (definition.type == "str" || definition.type == "[int]") ? -1 : 10
  134. // Since [int] & str don't have a max length, they need to be clipped (since clipping is expensive, this
  135. // should be done as little as possible)
  136. clip: definition.type == "str" || definition.type == "[int]"
  137. validator: RegularExpressionValidator { regularExpression: (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,3}$/ : /^.*$/ } // definition.type property from parent loader used to disallow fractional number entry
  138. Binding
  139. {
  140. target: input
  141. property: "text"
  142. value:
  143. {
  144. if (input.activeFocus)
  145. {
  146. // In QT6 using "when: !activeFocus" causes the value to be null when activeFocus becomes True
  147. // Since we want the value to stay the same when giving focus to the TextInput this is being used
  148. // in place of "when: !activeFocus"
  149. return input.text
  150. }
  151. // Stacklevels
  152. // 0: user -> unsaved change
  153. // 1: quality changes -> saved change
  154. // 2: quality
  155. // 3: material -> user changed material in materialspage
  156. // 4: variant
  157. // 5: machine_changes
  158. // 6: machine
  159. if ((base.resolve != "None" && base.resolve) && (stackLevel != 0) && (stackLevel != 1))
  160. {
  161. // We have a resolve function. Indicates that the setting is not settable per extruder and that
  162. // we have to choose between the resolved value (default) and the global value
  163. // (if user has explicitly set this).
  164. return base.resolve
  165. }
  166. else {
  167. return propertyProvider.properties.value
  168. }
  169. }
  170. }
  171. MouseArea
  172. {
  173. id: mouseArea
  174. anchors.fill: parent
  175. cursorShape: Qt.IBeamCursor
  176. onPressed: {
  177. if (!input.activeFocus)
  178. {
  179. base.focusGainedByClick = true
  180. input.forceActiveFocus()
  181. }
  182. mouse.accepted = false
  183. }
  184. }
  185. }
  186. }
  187. }