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