ComboBoxWithOptions.qml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) 2019 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.3 as UM
  7. import Cura 1.1 as Cura
  8. import "../Widgets"
  9. //
  10. // ComboBox with dropdown options in the Machine Settings dialog.
  11. //
  12. UM.TooltipArea
  13. {
  14. id: comboBoxWithOptions
  15. UM.I18nCatalog { id: catalog; name: "cura"; }
  16. height: childrenRect.height
  17. width: childrenRect.width
  18. text: tooltipText
  19. property alias containerStackId: propertyProvider.containerStackId
  20. property alias settingKey: propertyProvider.key
  21. property alias settingStoreIndex: propertyProvider.storeIndex
  22. property alias labelText: fieldLabel.text
  23. property alias labelWidth: fieldLabel.width
  24. property string tooltipText: propertyProvider.properties.description
  25. // callback functions
  26. property var afterOnActivateFunction: dummy_func
  27. property var forceUpdateOnChangeFunction: dummy_func
  28. // a dummy function for default property values
  29. function dummy_func() {}
  30. UM.SettingPropertyProvider
  31. {
  32. id: propertyProvider
  33. watchedProperties: [ "value", "options", "description" ]
  34. }
  35. Row
  36. {
  37. spacing: UM.Theme.getSize("default_margin").width
  38. Label
  39. {
  40. id: fieldLabel
  41. anchors.verticalCenter: comboBox.verticalCenter
  42. visible: text != ""
  43. elide: Text.ElideRight
  44. }
  45. ListModel
  46. {
  47. id: optionsModel
  48. Component.onCompleted:
  49. {
  50. // Options come in as a string-representation of an OrderedDict
  51. var options = propertyProvider.properties.options.match(/^OrderedDict\(\[\((.*)\)\]\)$/)
  52. if (options)
  53. {
  54. options = options[1].split("), (")
  55. for (var i = 0; i < options.length; i++)
  56. {
  57. var option = options[i].substring(1, options[i].length - 1).split("', '")
  58. optionsModel.append({text: option[1], value: option[0]})
  59. }
  60. }
  61. }
  62. }
  63. CuraComboBox
  64. {
  65. id: comboBox
  66. width: 100
  67. height: UM.Theme.getSize("action_button").height
  68. model: optionsModel
  69. textRole: "text"
  70. currentIndex:
  71. {
  72. var currentValue = propertyProvider.properties.value
  73. var index = 0
  74. for (var i = 0; i < model.count; i++)
  75. {
  76. if (model.get(i).value == currentValue)
  77. {
  78. index = i
  79. break
  80. }
  81. }
  82. return index
  83. }
  84. onActivated:
  85. {
  86. if(propertyProvider.properties.value != model.get(index).value)
  87. {
  88. propertyProvider.setPropertyValue("value", model.get(index).value)
  89. forceUpdateOnChangeFunction()
  90. afterOnActivateFunction()
  91. }
  92. }
  93. }
  94. }
  95. }