SingleSettingComboBox.qml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. property alias propertyRemoveUnusedValue: propertyProvider.removeUnusedValue
  16. // If true, all extruders will have "settingName" property updated.
  17. // The displayed value will be read from the extruder with index "defaultExtruderIndex" instead of the machine.
  18. property bool updateAllExtruders: false
  19. // This is only used if updateAllExtruders == true
  20. property int defaultExtruderIndex: Cura.ExtruderManager.activeExtruderIndex
  21. model: ListModel
  22. {
  23. id: comboboxModel
  24. // The propertyProvider has not loaded the setting when this components onComplete triggers. Populating the model
  25. // is deferred until propertyProvider signals "onIsValueUsedChanged". The deferred update is triggered with this function.
  26. function updateModel()
  27. {
  28. clear()
  29. if(!propertyProvider.properties.options) // No options have been loaded yet to populate combobox
  30. {
  31. return
  32. }
  33. for (var i = 0; i < propertyProvider.properties["options"].keys().length; i++)
  34. {
  35. var key = propertyProvider.properties["options"].keys()[i]
  36. var value = propertyProvider.properties["options"][key]
  37. comboboxModel.append({ text: value, code: key})
  38. if (propertyProvider.properties.value === key)
  39. {
  40. // The combobox is cleared after each value change so the currentIndex must be set each time.
  41. currentIndex = i
  42. }
  43. }
  44. }
  45. }
  46. // Updates to the setting are delayed by interval. The signal onIsValueUsedChanged() is emitted early for some reason.
  47. // This causes the selected value in the combobox to be updated to the previous value. (This issue is present with infill_pattern setting)
  48. // This is a hack. If you see this in the future, try removing it and see if the combobox still works.
  49. Timer
  50. {
  51. id: updateTimer
  52. interval: 100
  53. repeat: false
  54. onTriggered: comboboxModel.updateModel(false)
  55. }
  56. property UM.SettingPropertyProvider propertyProvider: UM.SettingPropertyProvider
  57. {
  58. id: propertyProvider
  59. containerStackId: updateAllExtruders ? Cura.ExtruderManager.extruderIds[defaultExtruderIndex] : Cura.MachineManager.activeMachine.id
  60. removeUnusedValue: false
  61. watchedProperties: ["value", "validationState", "resolve", "options"]
  62. }
  63. Connections
  64. {
  65. target: propertyProvider
  66. function onContainerStackChanged() { updateTimer.restart() }
  67. function onIsValueUsedChanged() { updateTimer.restart() }
  68. }
  69. onCurrentIndexChanged: parseValueAndUpdateSetting()
  70. function parseValueAndUpdateSetting()
  71. {
  72. if (comboboxModel.get(currentIndex) && comboboxModel.get(currentIndex).code !== propertyProvider.properties.value)
  73. {
  74. updateSetting(comboboxModel.get(currentIndex).code)
  75. }
  76. }
  77. function forceUpdateSettings()
  78. {
  79. comboboxModel.updateModel();
  80. }
  81. function updateSetting(value)
  82. {
  83. if (updateAllExtruders)
  84. {
  85. Cura.MachineManager.setSettingForAllExtruders(propertyProvider.key, "value", value)
  86. }
  87. else
  88. {
  89. propertyProvider.setPropertyValue("value", value)
  90. }
  91. }
  92. }