SingleSettingSlider.qml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: 0
  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"]
  31. storeIndex: 0
  32. }
  33. // set initial value from stack
  34. value: parseInt(propertyProvider.properties.value)
  35. // When the slider is released trigger an update immediately. This forces the slider to snap to the rounded value.
  36. onPressedChanged: function(pressed)
  37. {
  38. if(!pressed)
  39. {
  40. updateSetting(settingSlider.value);
  41. }
  42. }
  43. Connections
  44. {
  45. target: propertyProvider
  46. function onContainerStackChanged()
  47. {
  48. updateTimer.restart()
  49. }
  50. function onIsValueUsedChanged()
  51. {
  52. updateTimer.restart()
  53. }
  54. }
  55. // Updates to the setting are delayed by interval. This reduces lag by waiting a bit after a setting change to update the slider contents.
  56. Timer
  57. {
  58. id: updateTimer
  59. interval: 100
  60. repeat: false
  61. onTriggered: parseValueUpdateSetting(false)
  62. }
  63. function updateSlider(value)
  64. {
  65. settingSlider.value = value
  66. }
  67. function parseValueUpdateSetting(triggerUpdate)
  68. {
  69. // Only run when the setting value is updated by something other than the slider.
  70. // This sets the slider value based on the setting value, it does not update the setting value.
  71. if (parseInt(propertyProvider.properties.value) == settingSlider.value)
  72. {
  73. return
  74. }
  75. settingSlider.value = propertyProvider.properties.value
  76. }
  77. // Override this function to update a setting differently
  78. function updateSetting(value)
  79. {
  80. if (updateAllExtruders)
  81. {
  82. Cura.MachineManager.setSettingForAllExtruders(propertyProvider.key, "value", value)
  83. }
  84. else
  85. {
  86. propertyProvider.setPropertyValue("value", value)
  87. }
  88. }
  89. }