SettingTextField.qml 6.7 KB

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