SingleSettingSlider.qml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2022 UltiMaker
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Controls 2.15
  5. import UM 1.7 as UM
  6. import Cura 1.7 as Cura
  7. import QtQuick.Layouts 1.3
  8. RowLayout
  9. {
  10. height: childrenRect.height
  11. spacing: UM.Theme.getSize("default_margin").width
  12. property alias settingName: settingPropertyProvider.key
  13. property alias enabled: settingSlider.enabled
  14. property bool roundToNearestTen: false
  15. property int maxValue: 100
  16. property int minValue: 0
  17. anchors
  18. {
  19. left: strengthSection.right
  20. right: parent.right
  21. verticalCenter: strengthSection.verticalCenter
  22. }
  23. UM.SettingPropertyProvider
  24. {
  25. id: settingPropertyProvider
  26. containerStackId: Cura.MachineManager.activeStackId
  27. watchedProperties: [ "value" ]
  28. storeIndex: 0
  29. }
  30. UM.Label { Layout.fillWidth: false; text: minValue }
  31. UM.Slider
  32. {
  33. id: settingSlider
  34. Layout.fillWidth: true
  35. width: parent.width
  36. from: minValue; to: maxValue; stepSize: 1
  37. // set initial value from stack
  38. value: parseInt(settingPropertyProvider.properties.value)
  39. // When the slider is released trigger an update immediately. This forces the slider to snap to the rounded value.
  40. onPressedChanged: if(!pressed) { parseValueUpdateSetting() }
  41. }
  42. UM.Label { Layout.fillWidth: false; text: maxValue }
  43. Connections
  44. {
  45. target: settingPropertyProvider
  46. function onContainerStackChanged() { updateTimer.restart() }
  47. function onIsValueUsedChanged() { updateTimer.restart() }
  48. }
  49. // Updates to the setting are delayed by interval. This stops lag caused by calling the
  50. // parseValueUpdateSetting() function being call repeatedly while dragging the slider.
  51. Timer
  52. {
  53. id: updateTimer
  54. interval: 100
  55. repeat: false
  56. onTriggered:
  57. {
  58. parseValueUpdateSetting()
  59. }
  60. }
  61. function parseValueUpdateSetting()
  62. {
  63. // Work around, the `settingPropertyProvider.properties.value` is initially `undefined`. As
  64. // `parseInt(settingPropertyProvider.properties.value)` is parsed as 0 and is initially set as
  65. // the slider value. By setting this 0 value an update is triggered setting the actual
  66. // sitting value to 0.
  67. if (isNaN(parseInt(settingPropertyProvider.properties.value)))
  68. {
  69. return;
  70. }
  71. // Don't update if the setting value, if the slider has the same value
  72. if (parseInt(settingPropertyProvider.properties.value) == settingSlider.value)
  73. {
  74. return;
  75. }
  76. // Round the slider value to the nearest multiple of 10 (simulate step size of 10)
  77. const roundedSliderValue = roundToNearestTen ? Math.round(settingSlider.value / 10) * 10 : Math.round(settingSlider.value)
  78. // Update the slider value to represent the rounded value
  79. settingSlider.value = roundedSliderValue;
  80. updateSetting(roundedSliderValue);
  81. }
  82. // Override this function to update a setting differently
  83. function updateSetting(value) {
  84. propertyProvider.setPropertyValue("value", value)
  85. }
  86. }