SettingTextField.qml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (c) 2021 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.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. liningColor:
  28. {
  29. if(!enabled)
  30. {
  31. return UM.Theme.getColor("text_field_border_disabled")
  32. }
  33. switch(propertyProvider.properties.validationState)
  34. {
  35. case "ValidatorState.Invalid":
  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("text_field_border_hovered")
  48. }
  49. return UM.Theme.getColor("text_field_border")
  50. }
  51. color: {
  52. if(!enabled)
  53. {
  54. return UM.Theme.getColor("text_field")
  55. }
  56. switch(propertyProvider.properties.validationState)
  57. {
  58. case "ValidatorState.Invalid":
  59. case "ValidatorState.Exception":
  60. case "ValidatorState.MinimumError":
  61. case "ValidatorState.MaximumError":
  62. return UM.Theme.getColor("setting_validation_error_background")
  63. case "ValidatorState.MinimumWarning":
  64. case "ValidatorState.MaximumWarning":
  65. return UM.Theme.getColor("setting_validation_warning_background")
  66. case "ValidatorState.Valid":
  67. return UM.Theme.getColor("setting_validation_ok")
  68. default:
  69. return UM.Theme.getColor("text_field")
  70. }
  71. }
  72. UM.Label
  73. {
  74. anchors
  75. {
  76. left: parent.left
  77. leftMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  78. right: parent.right
  79. rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  80. verticalCenter: parent.verticalCenter
  81. }
  82. text: definition.unit
  83. //However the setting value is aligned, align the unit opposite. That way it stays readable with right-to-left languages.
  84. horizontalAlignment: (input.effectiveHorizontalAlignment == Text.AlignLeft) ? Text.AlignRight : Text.AlignLeft
  85. textFormat: Text.PlainText
  86. color: UM.Theme.getColor("setting_unit")
  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. selectedTextColor: UM.Theme.getColor("setting_control_text")
  129. font: UM.Theme.getFont("default")
  130. selectionColor: UM.Theme.getColor("text_selection")
  131. selectByMouse: true
  132. maximumLength: (definition.type == "str" || definition.type == "[int]") ? -1 : 10
  133. // Since [int] & str don't have a max length, they need to be clipped (since clipping is expensive, this
  134. // should be done as little as possible)
  135. clip: definition.type == "str" || definition.type == "[int]"
  136. 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,3}$/ : /^.*$/ } // definition.type property from parent loader used to disallow fractional number entry
  137. Binding
  138. {
  139. target: input
  140. property: "text"
  141. value:
  142. {
  143. // Stacklevels
  144. // 0: user -> unsaved change
  145. // 1: quality changes -> saved change
  146. // 2: quality
  147. // 3: material -> user changed material in materialspage
  148. // 4: variant
  149. // 5: machine_changes
  150. // 6: machine
  151. if ((base.resolve != "None" && base.resolve) && (stackLevel != 0) && (stackLevel != 1))
  152. {
  153. // We have a resolve function. Indicates that the setting is not settable per extruder and that
  154. // we have to choose between the resolved value (default) and the global value
  155. // (if user has explicitly set this).
  156. return base.resolve
  157. }
  158. else {
  159. return propertyProvider.properties.value
  160. }
  161. }
  162. when: !input.activeFocus
  163. }
  164. MouseArea
  165. {
  166. id: mouseArea
  167. anchors.fill: parent
  168. cursorShape: Qt.IBeamCursor
  169. onPressed: {
  170. if (!input.activeFocus)
  171. {
  172. base.focusGainedByClick = true
  173. input.forceActiveFocus()
  174. }
  175. mouse.accepted = false
  176. }
  177. }
  178. }
  179. }
  180. }