MaterialsList.qml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2022 Ultimaker B.V.
  2. // Uranium is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Layouts 1.3
  5. import QtQuick.Dialogs
  6. import UM 1.2 as UM
  7. import Cura 1.0 as Cura
  8. Item
  9. {
  10. id: materialList
  11. height: childrenRect.height
  12. // Children
  13. Cura.MaterialBrandsModel
  14. {
  15. id: materialsModel
  16. extruderPosition: Cura.ExtruderManager.activeExtruderIndex
  17. }
  18. Cura.FavoriteMaterialsModel
  19. {
  20. id: favoriteMaterialsModel
  21. extruderPosition: Cura.ExtruderManager.activeExtruderIndex
  22. }
  23. Cura.GenericMaterialsModel
  24. {
  25. id: genericMaterialsModel
  26. extruderPosition: Cura.ExtruderManager.activeExtruderIndex
  27. }
  28. property var currentType: null
  29. property var currentBrand: null
  30. property var expandedBrands: UM.Preferences.getValue("cura/expanded_brands").split(";")
  31. property var expandedTypes: UM.Preferences.getValue("cura/expanded_types").split(";")
  32. // Store information about which parts of the tree are expanded
  33. function persistExpandedCategories()
  34. {
  35. UM.Preferences.setValue("cura/expanded_brands", materialList.expandedBrands.join(";"))
  36. UM.Preferences.setValue("cura/expanded_types", materialList.expandedTypes.join(";"))
  37. }
  38. // Expand the list of materials in order to select the current material
  39. function expandActiveMaterial(search_root_id)
  40. {
  41. if (search_root_id == "")
  42. {
  43. // When this happens it means that the information of one of the materials has changed, so the model
  44. // was updated and the list has to highlight the current item.
  45. var currentItemId = base.currentItem == null ? "" : base.currentItem.root_material_id
  46. search_root_id = currentItemId
  47. }
  48. for (var material_idx = 0; material_idx < genericMaterialsModel.count; material_idx++)
  49. {
  50. var material = genericMaterialsModel.getItem(material_idx)
  51. if (material.root_material_id == search_root_id)
  52. {
  53. if (materialList.expandedBrands.indexOf("Generic") == -1)
  54. {
  55. materialList.expandedBrands.push("Generic")
  56. }
  57. materialList.currentBrand = "Generic"
  58. base.currentItem = material
  59. persistExpandedCategories()
  60. return true
  61. }
  62. }
  63. for (var brand_idx = 0; brand_idx < materialsModel.count; brand_idx++)
  64. {
  65. var brand = materialsModel.getItem(brand_idx)
  66. var types_model = brand.material_types
  67. for (var type_idx = 0; type_idx < types_model.count; type_idx++)
  68. {
  69. var type = types_model.getItem(type_idx)
  70. var colors_model = type.colors
  71. for (var material_idx = 0; material_idx < colors_model.count; material_idx++)
  72. {
  73. var material = colors_model.getItem(material_idx)
  74. if (material.root_material_id == search_root_id)
  75. {
  76. if (materialList.expandedBrands.indexOf(brand.name) == -1)
  77. {
  78. materialList.expandedBrands.push(brand.name)
  79. }
  80. materialList.currentBrand = brand.name
  81. if (materialList.expandedTypes.indexOf(brand.name + "_" + type.name) == -1)
  82. {
  83. materialList.expandedTypes.push(brand.name + "_" + type.name)
  84. }
  85. materialList.currentType = brand.name + "_" + type.name
  86. base.currentItem = material
  87. persistExpandedCategories()
  88. return true
  89. }
  90. }
  91. }
  92. }
  93. base.currentItem = null
  94. return false
  95. }
  96. function updateAfterModelChanges()
  97. {
  98. var correctlyExpanded = materialList.expandActiveMaterial(base.newRootMaterialIdToSwitchTo)
  99. if (correctlyExpanded)
  100. {
  101. if (base.toActivateNewMaterial)
  102. {
  103. var position = Cura.ExtruderManager.activeExtruderIndex
  104. Cura.MachineManager.setMaterialById(position, base.newRootMaterialIdToSwitchTo)
  105. }
  106. base.newRootMaterialIdToSwitchTo = ""
  107. base.toActivateNewMaterial = false
  108. }
  109. }
  110. Connections
  111. {
  112. target: materialsModel
  113. function onItemsChanged() { updateAfterModelChanges() }
  114. }
  115. Connections
  116. {
  117. target: genericMaterialsModel
  118. function onItemsChanged() { updateAfterModelChanges() }
  119. }
  120. Column
  121. {
  122. width: materialList.width
  123. height: childrenRect.height
  124. MaterialsBrandSection
  125. {
  126. id: favoriteSection
  127. sectionName: "Favorites"
  128. elementsModel: favoriteMaterialsModel
  129. hasMaterialTypes: false
  130. }
  131. MaterialsBrandSection
  132. {
  133. id: genericSection
  134. sectionName: "Generic"
  135. elementsModel: genericMaterialsModel
  136. hasMaterialTypes: false
  137. }
  138. Repeater
  139. {
  140. model: materialsModel
  141. delegate: MaterialsBrandSection
  142. {
  143. id: brandSection
  144. sectionName: model.name
  145. elementsModel: model.material_types
  146. hasMaterialTypes: true
  147. }
  148. }
  149. }
  150. }