ComboBoxWithOptions.qml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //
  9. // ComboBox with dropdown options in the Machine Settings dialog.
  10. //
  11. UM.TooltipArea
  12. {
  13. id: comboBoxWithOptions
  14. UM.I18nCatalog { id: catalog; name: "cura"; }
  15. height: childrenRect.height
  16. width: childrenRect.width
  17. text: tooltipText
  18. property alias containerStackId: propertyProvider.containerStackId
  19. property alias settingKey: propertyProvider.key
  20. property alias settingStoreIndex: propertyProvider.storeIndex
  21. property alias labelText: fieldLabel.text
  22. property alias labelWidth: fieldLabel.width
  23. property string tooltipText: propertyProvider.properties.description
  24. // callback functions
  25. property var afterOnActivateFunction: dummy_func
  26. property var forceUpdateOnChangeFunction: dummy_func
  27. // a dummy function for default property values
  28. function dummy_func() {}
  29. UM.SettingPropertyProvider
  30. {
  31. id: propertyProvider
  32. watchedProperties: [ "value", "options", "description" ]
  33. }
  34. Row
  35. {
  36. spacing: UM.Theme.getSize("default_margin").width
  37. Label
  38. {
  39. id: fieldLabel
  40. anchors.verticalCenter: comboBox.verticalCenter
  41. visible: text != ""
  42. elide: Text.ElideRight
  43. //width: Math.max(0, settingsTabs.labelColumnWidth)
  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. ComboBox
  64. {
  65. id: comboBox
  66. model: optionsModel
  67. textRole: "text"
  68. currentIndex:
  69. {
  70. var currentValue = propertyProvider.properties.value
  71. var index = 0
  72. for (var i = 0; i < model.count; i++)
  73. {
  74. if (model.get(i).value == currentValue)
  75. {
  76. index = i
  77. break
  78. }
  79. }
  80. return index
  81. }
  82. onActivated:
  83. {
  84. if(propertyProvider.properties.value != model.get(index).value)
  85. {
  86. propertyProvider.setPropertyValue("value", model.get(index).value)
  87. forceUpdateOnChangeFunction()
  88. afterOnActivateFunction()
  89. }
  90. }
  91. }
  92. }
  93. }