SingleSettingSlider.qml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // This silder allows changing of a single setting. Only the setting name has to be passed in to "settingName".
  9. // All of the setting updating logic is handled by this component.
  10. // This component allows you to choose values between minValue -> maxValue and rounds them to the nearest 10.
  11. // If the setting is limited to a single extruder or is settable with different values per extruder use "updateAllExtruders: true"
  12. RowLayout
  13. {
  14. height: childrenRect.height
  15. spacing: UM.Theme.getSize("default_margin").width
  16. property alias settingName: propertyProvider.key
  17. property alias enabled: settingSlider.enabled
  18. // If true, all extruders will have "settingName" property updated.
  19. // The displayed value will be read from the extruder with index "defaultExtruderIndex" instead of the machine.
  20. property bool updateAllExtruders: false
  21. // This is only used if updateAllExtruders == true
  22. property int defaultExtruderIndex: 0
  23. property bool roundToNearestTen: false
  24. property int maxValue: 100
  25. property int minValue: 0
  26. property int previousValue: -1
  27. UM.SettingPropertyProvider
  28. {
  29. id: propertyProvider
  30. containerStackId: updateAllExtruders ? Cura.ExtruderManager.globalStackExtruderIds[defaultExtruderIndex] : Cura.MachineManager.activeMachine.id
  31. watchedProperties: ["value"]
  32. storeIndex: 0
  33. }
  34. UM.Label { Layout.fillWidth: false; text: minValue }
  35. UM.Slider
  36. {
  37. id: settingSlider
  38. Layout.fillWidth: true
  39. width: parent.width
  40. from: minValue; to: maxValue; stepSize: 1
  41. // set initial value from stack
  42. value: parseInt(propertyProvider.properties.value)
  43. // When the slider is released trigger an update immediately. This forces the slider to snap to the rounded value.
  44. onPressedChanged: if(!pressed) { roundSliderValueUpdateSetting() }
  45. }
  46. UM.Label { Layout.fillWidth: false; text: maxValue }
  47. Connections
  48. {
  49. target: propertyProvider
  50. function onContainerStackChanged()
  51. {
  52. comboboxModel.updateModel()
  53. }
  54. function onIsValueUsedChanged()
  55. {
  56. comboboxModel.updateModel()
  57. }
  58. }
  59. // Updates to the setting are delayed by interval. This reduces lag by waiting a bit after a setting change to update the slider contents.
  60. Timer
  61. {
  62. id: updateTimer
  63. interval: 100
  64. repeat: false
  65. onTriggered: parseValueUpdateSetting(false)
  66. }
  67. function updateSlider(value)
  68. {
  69. settingSlider.value = value
  70. }
  71. function roundSliderValueUpdateSetting()
  72. {
  73. // If the user interacts with the slider we round the value and update the setting.
  74. const roundedSliderValue = roundToNearestTen ? Math.round(settingSlider.value / 10) * 10 : Math.round(settingSlider.value)
  75. updateSetting(roundedSliderValue)
  76. }
  77. function parseValueUpdateSetting(triggerUpdate)
  78. {
  79. // Only run when the setting value is updated by something other than the slider.
  80. // This sets the slider value based on the setting value, it does not update the setting value.
  81. if (parseInt(propertyProvider.properties.value) == settingSlider.value)
  82. {
  83. return
  84. }
  85. settingSlider.value = propertyProvider.properties.value
  86. }
  87. // Override this function to update a setting differently
  88. function updateSetting(value)
  89. {
  90. if (updateAllExtruders)
  91. {
  92. Cura.MachineManager.setSettingForAllExtruders(propertyProvider.key, "value", value)
  93. }
  94. else
  95. {
  96. propertyProvider.setPropertyValue("value", value)
  97. }
  98. }
  99. }