SingleSettingSlider.qml 3.4 KB

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