SettingTextField.qml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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: Math.round(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.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("setting_control_border_highlight")
  49. }
  50. return UM.Theme.getColor("setting_control_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.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("setting_control")
  70. }
  71. }
  72. Rectangle
  73. {
  74. anchors.fill: parent
  75. anchors.margins: Math.round(UM.Theme.getSize("default_lining").width)
  76. color: UM.Theme.getColor("setting_control_highlight")
  77. opacity: !control.hovered ? 0 : propertyProvider.properties.validationState == "ValidatorState.Valid" ? 1.0 : 0.35
  78. }
  79. Label
  80. {
  81. anchors.right: parent.right
  82. anchors.rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  83. anchors.verticalCenter: parent.verticalCenter
  84. text: definition.unit
  85. textFormat: Text.PlainText
  86. renderType: Text.NativeRendering
  87. color: UM.Theme.getColor("setting_unit")
  88. font: UM.Theme.getFont("default")
  89. }
  90. TextInput
  91. {
  92. id: input
  93. anchors
  94. {
  95. left: parent.left
  96. leftMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  97. right: parent.right
  98. rightMargin: Math.round(UM.Theme.getSize("setting_unit_margin").width)
  99. verticalCenter: parent.verticalCenter
  100. }
  101. renderType: Text.NativeRendering
  102. Keys.onTabPressed:
  103. {
  104. base.setActiveFocusToNextSetting(true)
  105. }
  106. Keys.onBacktabPressed:
  107. {
  108. base.setActiveFocusToNextSetting(false)
  109. }
  110. Keys.onReleased:
  111. {
  112. if (text != textBeforeEdit)
  113. {
  114. textHasChanged = true;
  115. }
  116. if (textHasChanged)
  117. {
  118. propertyProvider.setPropertyValue("value", text)
  119. }
  120. }
  121. onActiveFocusChanged:
  122. {
  123. if(activeFocus)
  124. {
  125. base.focusReceived();
  126. }
  127. base.focusGainedByClick = false;
  128. }
  129. color: !enabled ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  130. font: UM.Theme.getFont("default")
  131. selectByMouse: true
  132. maximumLength: (definition.type == "str" || definition.type == "[int]") ? -1 : 10
  133. clip: true; //Hide any text that exceeds the width of the text box.
  134. 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
  135. Binding
  136. {
  137. target: input
  138. property: "text"
  139. value:
  140. {
  141. // Stacklevels
  142. // 0: user -> unsaved change
  143. // 1: quality changes -> saved change
  144. // 2: quality
  145. // 3: material -> user changed material in materialspage
  146. // 4: variant
  147. // 5: machine_changes
  148. // 6: machine
  149. if ((base.resolve != "None" && base.resolve) && (stackLevel != 0) && (stackLevel != 1))
  150. {
  151. // We have a resolve function. Indicates that the setting is not settable per extruder and that
  152. // we have to choose between the resolved value (default) and the global value
  153. // (if user has explicitly set this).
  154. return base.resolve
  155. }
  156. else {
  157. return propertyProvider.properties.value
  158. }
  159. }
  160. when: !input.activeFocus
  161. }
  162. MouseArea
  163. {
  164. id: mouseArea
  165. anchors.fill: parent
  166. cursorShape: Qt.IBeamCursor
  167. onPressed: {
  168. if (!input.activeFocus)
  169. {
  170. base.focusGainedByClick = true
  171. input.forceActiveFocus()
  172. }
  173. mouse.accepted = false
  174. }
  175. }
  176. }
  177. }
  178. }