SettingTextField.qml 6.3 KB

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