SettingTextField.qml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (c) 2015 Ultimaker B.V.
  2. // Uranium 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. verticalCenter: parent.verticalCenter
  97. }
  98. renderType: Text.NativeRendering
  99. Keys.onTabPressed:
  100. {
  101. base.setActiveFocusToNextSetting(true)
  102. }
  103. Keys.onBacktabPressed:
  104. {
  105. base.setActiveFocusToNextSetting(false)
  106. }
  107. Keys.onReleased:
  108. {
  109. if (text != textBeforeEdit)
  110. {
  111. textHasChanged = true;
  112. }
  113. if (textHasChanged)
  114. {
  115. propertyProvider.setPropertyValue("value", text)
  116. }
  117. }
  118. onEditingFinished:
  119. {
  120. if (textHasChanged)
  121. {
  122. propertyProvider.setPropertyValue("value", text)
  123. }
  124. }
  125. onActiveFocusChanged:
  126. {
  127. if(activeFocus)
  128. {
  129. base.focusReceived();
  130. }
  131. }
  132. color: !enabled ? UM.Theme.getColor("setting_control_disabled_text") : UM.Theme.getColor("setting_control_text")
  133. font: UM.Theme.getFont("default");
  134. selectByMouse: true;
  135. maximumLength: (definition.type == "[int]") ? 20 : (definition.type == "str") ? -1 : 10;
  136. 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
  137. Binding
  138. {
  139. target: input
  140. property: "text"
  141. value: {
  142. // Stacklevels
  143. // 0: user -> unsaved change
  144. // 1: quality changes -> saved change
  145. // 2: quality
  146. // 3: material -> user changed material in materialspage
  147. // 4: variant
  148. // 5: machine_changes
  149. // 6: machine
  150. if ((base.resolve != "None" && base.resolve) && (stackLevel != 0) && (stackLevel != 1)) {
  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. } else {
  156. return propertyProvider.properties.value;
  157. }
  158. }
  159. when: !input.activeFocus
  160. }
  161. }
  162. }
  163. }