SettingTextField.qml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. 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.right: parent.right
  84. anchors.rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  85. anchors.verticalCenter: parent.verticalCenter
  86. text: definition.unit
  87. textFormat: Text.PlainText
  88. renderType: Text.NativeRendering
  89. color: UM.Theme.getColor("setting_unit")
  90. font: UM.Theme.getFont("default")
  91. }
  92. TextInput
  93. {
  94. id: input
  95. anchors
  96. {
  97. left: parent.left
  98. leftMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  99. right: parent.right
  100. rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  101. verticalCenter: parent.verticalCenter
  102. }
  103. renderType: Text.NativeRendering
  104. Keys.onTabPressed:
  105. {
  106. base.setActiveFocusToNextSetting(true)
  107. }
  108. Keys.onBacktabPressed:
  109. {
  110. base.setActiveFocusToNextSetting(false)
  111. }
  112. Keys.onReleased:
  113. {
  114. if (text != textBeforeEdit)
  115. {
  116. textHasChanged = true;
  117. }
  118. if (textHasChanged)
  119. {
  120. propertyProvider.setPropertyValue("value", text)
  121. }
  122. }
  123. onActiveFocusChanged:
  124. {
  125. if(activeFocus)
  126. {
  127. base.focusReceived();
  128. }
  129. base.focusGainedByClick = false;
  130. }
  131. color: !enabled ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  132. font: UM.Theme.getFont("default")
  133. selectByMouse: true
  134. maximumLength: (definition.type == "str" || definition.type == "[int]") ? -1 : 10
  135. // Since [int] & str don't have a max length, they need to be clipped (since clipping is expensive, this
  136. // should be done as little as possible)
  137. clip: definition.type == "str" || definition.type == "[int]"
  138. 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
  139. Binding
  140. {
  141. target: input
  142. property: "text"
  143. value:
  144. {
  145. // Stacklevels
  146. // 0: user -> unsaved change
  147. // 1: quality changes -> saved change
  148. // 2: quality
  149. // 3: material -> user changed material in materialspage
  150. // 4: variant
  151. // 5: machine_changes
  152. // 6: machine
  153. if ((base.resolve != "None" && base.resolve) && (stackLevel != 0) && (stackLevel != 1))
  154. {
  155. // We have a resolve function. Indicates that the setting is not settable per extruder and that
  156. // we have to choose between the resolved value (default) and the global value
  157. // (if user has explicitly set this).
  158. return base.resolve
  159. }
  160. else {
  161. return propertyProvider.properties.value
  162. }
  163. }
  164. when: !input.activeFocus
  165. }
  166. MouseArea
  167. {
  168. id: mouseArea
  169. anchors.fill: parent
  170. cursorShape: Qt.IBeamCursor
  171. onPressed: {
  172. if (!input.activeFocus)
  173. {
  174. base.focusGainedByClick = true
  175. input.forceActiveFocus()
  176. }
  177. mouse.accepted = false
  178. }
  179. }
  180. }
  181. }
  182. }