MaterialsList.qml 5.5 KB

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