SettingTextField.qml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. onFocusReceived:
  13. {
  14. textHasChanged = false;
  15. textBeforeEdit = focusItem.text;
  16. focusItem.selectAll();
  17. }
  18. contents: Rectangle
  19. {
  20. id: control
  21. anchors.fill: parent
  22. border.width: UM.Theme.getSize("default_lining").width
  23. border.color:
  24. {
  25. if(!enabled)
  26. {
  27. return UM.Theme.getColor("setting_control_disabled_border")
  28. }
  29. switch(propertyProvider.properties.validationState)
  30. {
  31. case "ValidatorState.Exception":
  32. case "ValidatorState.MinimumError":
  33. case "ValidatorState.MaximumError":
  34. return UM.Theme.getColor("setting_validation_error");
  35. case "ValidatorState.MinimumWarning":
  36. case "ValidatorState.MaximumWarning":
  37. return UM.Theme.getColor("setting_validation_warning");
  38. }
  39. //Validation is OK.
  40. if(hovered || input.activeFocus)
  41. {
  42. return UM.Theme.getColor("setting_control_border_highlight")
  43. }
  44. return UM.Theme.getColor("setting_control_border")
  45. }
  46. color: {
  47. if(!enabled)
  48. {
  49. return UM.Theme.getColor("setting_control_disabled")
  50. }
  51. switch(propertyProvider.properties.validationState)
  52. {
  53. case "ValidatorState.Exception":
  54. case "ValidatorState.MinimumError":
  55. case "ValidatorState.MaximumError":
  56. return UM.Theme.getColor("setting_validation_error_background")
  57. case "ValidatorState.MinimumWarning":
  58. case "ValidatorState.MaximumWarning":
  59. return UM.Theme.getColor("setting_validation_warning_background")
  60. case "ValidatorState.Valid":
  61. return UM.Theme.getColor("setting_validation_ok")
  62. default:
  63. return UM.Theme.getColor("setting_control")
  64. }
  65. }
  66. Rectangle
  67. {
  68. anchors.fill: parent;
  69. anchors.margins: UM.Theme.getSize("default_lining").width;
  70. color: UM.Theme.getColor("setting_control_highlight")
  71. opacity: !control.hovered ? 0 : propertyProvider.properties.validationState == "ValidatorState.Valid" ? 1.0 : 0.35;
  72. }
  73. Label
  74. {
  75. anchors.right: parent.right;
  76. anchors.rightMargin: UM.Theme.getSize("setting_unit_margin").width
  77. anchors.verticalCenter: parent.verticalCenter;
  78. text: definition.unit;
  79. color: UM.Theme.getColor("setting_unit")
  80. font: UM.Theme.getFont("default")
  81. }
  82. MouseArea
  83. {
  84. id: mouseArea
  85. anchors.fill: parent;
  86. //hoverEnabled: true;
  87. cursorShape: Qt.IBeamCursor
  88. }
  89. TextInput
  90. {
  91. id: input
  92. anchors
  93. {
  94. left: parent.left
  95. leftMargin: UM.Theme.getSize("setting_unit_margin").width
  96. right: parent.right
  97. rightMargin: 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. onEditingFinished:
  121. {
  122. if (textHasChanged)
  123. {
  124. propertyProvider.setPropertyValue("value", text)
  125. }
  126. }
  127. onActiveFocusChanged:
  128. {
  129. if(activeFocus)
  130. {
  131. base.focusReceived();
  132. }
  133. }
  134. color: !enabled ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  135. font: UM.Theme.getFont("default");
  136. selectByMouse: true;
  137. maximumLength: (definition.type == "str" || definition.type == "[int]") ? -1 : 10;
  138. clip: true; //Hide any text that exceeds the width of the text box.
  139. 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
  140. Binding
  141. {
  142. target: input
  143. property: "text"
  144. value: {
  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. // We have a resolve function. Indicates that the setting is not settable per extruder and that
  155. // we have to choose between the resolved value (default) and the global value
  156. // (if user has explicitly set this).
  157. return base.resolve;
  158. } else {
  159. return propertyProvider.properties.value;
  160. }
  161. }
  162. when: !input.activeFocus
  163. }
  164. }
  165. }
  166. }