ComboBoxWithOptions.qml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: tooltip
  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 tooltip: 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. ComboBox
  46. {
  47. id: comboBox
  48. model: ListModel
  49. {
  50. id: optionsModel
  51. Component.onCompleted:
  52. {
  53. // Options come in as a string-representation of an OrderedDict
  54. var options = propertyProvider.properties.options.match(/^OrderedDict\(\[\((.*)\)\]\)$/)
  55. if (options)
  56. {
  57. options = options[1].split("), (")
  58. for (var i = 0; i < options.length; i++)
  59. {
  60. var option = options[i].substring(1, options[i].length - 1).split("', '")
  61. optionsModel.append({text: option[1], value: option[0]});
  62. }
  63. }
  64. }
  65. }
  66. currentIndex:
  67. {
  68. var currentValue = propertyProvider.properties.value
  69. var index = 0
  70. for (var i = 0; i < optionsModel.count; i++)
  71. {
  72. if (optionsModel.get(i).value == currentValue)
  73. {
  74. index = i
  75. break
  76. }
  77. }
  78. return index
  79. }
  80. onActivated:
  81. {
  82. if(propertyProvider.properties.value != optionsModel.get(index).value)
  83. {
  84. propertyProvider.setPropertyValue("value", optionsModel.get(index).value);
  85. forceUpdateOnChangeFunction()
  86. afterOnActivateFunction()
  87. }
  88. }
  89. }
  90. }
  91. }