ComboBoxWithOptions.qml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2020 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 int controlWidth: UM.Theme.getSize("setting_control").width
  20. property int controlHeight: UM.Theme.getSize("setting_control").height
  21. property alias containerStackId: propertyProvider.containerStackId
  22. property alias settingKey: propertyProvider.key
  23. property alias settingStoreIndex: propertyProvider.storeIndex
  24. property alias labelText: fieldLabel.text
  25. property alias labelFont: fieldLabel.font
  26. property alias labelWidth: fieldLabel.width
  27. property alias optionModel: comboBox.model
  28. property string tooltipText: propertyProvider.properties.description ? propertyProvider.properties.description : ""
  29. // callback functions
  30. property var forceUpdateOnChangeFunction: dummy_func
  31. property var afterOnEditingFinishedFunction: dummy_func
  32. property var setValueFunction: null
  33. // a dummy function for default property values
  34. function dummy_func() {}
  35. UM.SettingPropertyProvider
  36. {
  37. id: propertyProvider
  38. watchedProperties: [ "value", "options", "description" ]
  39. }
  40. Label
  41. {
  42. id: fieldLabel
  43. anchors.left: parent.left
  44. anchors.verticalCenter: comboBox.verticalCenter
  45. visible: text != ""
  46. font: UM.Theme.getFont("medium")
  47. color: UM.Theme.getColor("text")
  48. renderType: Text.NativeRendering
  49. }
  50. ListModel
  51. {
  52. id: defaultOptionsModel
  53. function updateModel()
  54. {
  55. clear()
  56. // Options come in as a string-representation of an OrderedDict
  57. if(propertyProvider.properties.options)
  58. {
  59. var options = propertyProvider.properties.options.match(/^OrderedDict\(\[\((.*)\)\]\)$/);
  60. if(options)
  61. {
  62. options = options[1].split("), (");
  63. for(var i = 0; i < options.length; i++)
  64. {
  65. var option = options[i].substring(1, options[i].length - 1).split("', '");
  66. append({ text: option[1], value: option[0] });
  67. }
  68. }
  69. }
  70. }
  71. Component.onCompleted: updateModel()
  72. }
  73. // Remake the model when the model is bound to a different container stack
  74. Connections
  75. {
  76. target: propertyProvider
  77. onContainerStackChanged: defaultOptionsModel.updateModel()
  78. onIsValueUsedChanged: defaultOptionsModel.updateModel()
  79. }
  80. Cura.ComboBox
  81. {
  82. id: comboBox
  83. anchors.left: fieldLabel.right
  84. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  85. width: comboBoxWithOptions.controlWidth
  86. height: comboBoxWithOptions.controlHeight
  87. model: defaultOptionsModel
  88. textRole: "text"
  89. currentIndex:
  90. {
  91. var currentValue = propertyProvider.properties.value
  92. var index = 0
  93. for (var i = 0; i < model.count; i++)
  94. {
  95. if (model.get(i).value == currentValue)
  96. {
  97. index = i
  98. break
  99. }
  100. }
  101. return index
  102. }
  103. onActivated:
  104. {
  105. var newValue = model.get(index).value
  106. if (propertyProvider.properties.value != newValue)
  107. {
  108. if (setValueFunction !== null)
  109. {
  110. setValueFunction(newValue)
  111. }
  112. else
  113. {
  114. propertyProvider.setPropertyValue("value", newValue)
  115. }
  116. forceUpdateOnChangeFunction()
  117. afterOnEditingFinishedFunction()
  118. }
  119. }
  120. }
  121. }