SettingTextField.qml 7.7 KB

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