SingleSettingComboBox.qml 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2022 UltiMaker B.V.
  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. Cura.ComboBox {
  12. textRole: "text"
  13. property alias settingName: propertyProvider.key
  14. model: ListModel {
  15. id: comboboxModel
  16. // The propertyProvider has not loaded the setting when this components onComplete triggers. Populating the model
  17. // is defered until propertyProvider signals "onIsValueUsedChanged". The defered upate is triggered with this function.
  18. function updateModel()
  19. {
  20. clear()
  21. if(!propertyProvider.properties.options) // No options have been loaded yet to populate combobox
  22. {
  23. return
  24. }
  25. for (var i = 0; i < propertyProvider.properties["options"].keys().length; i++)
  26. {
  27. var key = propertyProvider.properties["options"].keys()[i]
  28. var value = propertyProvider.properties["options"][key]
  29. comboboxModel.append({ text: value, code: key})
  30. if (propertyProvider.properties.value == key)
  31. {
  32. // The combobox is cleared after each value change so the currentIndex must be set each time.
  33. currentIndex = i
  34. }
  35. }
  36. }
  37. }
  38. property UM.SettingPropertyProvider propertyProvider: UM.SettingPropertyProvider
  39. {
  40. id: propertyProvider
  41. containerStack: Cura.MachineManager.activeMachine
  42. watchedProperties: [ "value" , "options"]
  43. }
  44. Connections
  45. {
  46. target: propertyProvider
  47. function onContainerStackChanged() { comboboxModel.updateModel() }
  48. function onIsValueUsedChanged() { comboboxModel.updateModel() }
  49. }
  50. onCurrentIndexChanged: propertyProvider.setPropertyValue("value", comboboxModel.get(currentIndex).code)
  51. }