SingleSettingComboBox.qml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2022 UltiMaker
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.10
  4. import QtQuick.Controls 2.3
  5. import QtQuick.Layouts 1.3
  6. import UM 1.5 as UM
  7. import Cura 1.7 as Cura
  8. // This ComboBox 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 uses the "options" value of a setting to populate the drop down. This will only work for settings with "options"
  11. // If the setting is limited to a single extruder or is settable with different values per extruder use "updateAllExtruders: true"
  12. Cura.ComboBox {
  13. textRole: "text"
  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. model: ListModel
  21. {
  22. id: comboboxModel
  23. // The propertyProvider has not loaded the setting when this components onComplete triggers. Populating the model
  24. // is defered until propertyProvider signals "onIsValueUsedChanged". The defered upate is triggered with this function.
  25. function updateModel()
  26. {
  27. clear()
  28. if(!propertyProvider.properties.options) // No options have been loaded yet to populate combobox
  29. {
  30. return
  31. }
  32. for (var i = 0; i < propertyProvider.properties["options"].keys().length; i++)
  33. {
  34. var key = propertyProvider.properties["options"].keys()[i]
  35. var value = propertyProvider.properties["options"][key]
  36. comboboxModel.append({ text: value, code: key})
  37. if (propertyProvider.properties.value === key)
  38. {
  39. // The combobox is cleared after each value change so the currentIndex must be set each time.
  40. currentIndex = i
  41. }
  42. }
  43. }
  44. }
  45. // Updates to the setting are delayed by interval. The signal onIsValueUsedChanged() is emitted early for some reason.
  46. // This causes the selected value in the combobox to be updated to the previous value. (This issue is present with infill_pattern setting)
  47. // This is a hack. If you see this in the future, try removing it and see if the combobox still works.
  48. Timer
  49. {
  50. id: updateTimer
  51. interval: 100
  52. repeat: false
  53. onTriggered: comboboxModel.updateModel(false)
  54. }
  55. property UM.SettingPropertyProvider propertyProvider: UM.SettingPropertyProvider
  56. {
  57. id: propertyProvider
  58. containerStackId: updateAllExtruders ? Cura.ExtruderManager.extruderIds[defaultExtruderIndex] : Cura.MachineManager.activeMachine.id
  59. watchedProperties: ["value" , "options"]
  60. }
  61. Connections
  62. {
  63. target: propertyProvider
  64. function onContainerStackChanged() { updateTimer.restart() }
  65. function onIsValueUsedChanged() { updateTimer.restart() }
  66. }
  67. onCurrentIndexChanged: parseValueAndUpdateSetting()
  68. function parseValueAndUpdateSetting()
  69. {
  70. if (comboboxModel.get(currentIndex) && comboboxModel.get(currentIndex).code !== propertyProvider.properties.value)
  71. {
  72. updateSetting(comboboxModel.get(currentIndex).code)
  73. }
  74. }
  75. function updateSetting(value)
  76. {
  77. if (updateAllExtruders)
  78. {
  79. Cura.MachineManager.setSettingForAllExtruders(propertyProvider.key, "value", value)
  80. }
  81. else
  82. {
  83. propertyProvider.setPropertyValue("value", value)
  84. }
  85. }
  86. }