MaterialsList.qml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2018 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. return false
  97. }
  98. function updateAfterModelChanges()
  99. {
  100. var correctlyExpanded = materialList.expandActiveMaterial(base.newRootMaterialIdToSwitchTo)
  101. if (correctlyExpanded)
  102. {
  103. if (base.toActivateNewMaterial)
  104. {
  105. var position = Cura.ExtruderManager.activeExtruderIndex
  106. Cura.MachineManager.setMaterial(position, base.currentItem.container_node)
  107. }
  108. base.newRootMaterialIdToSwitchTo = ""
  109. base.toActivateNewMaterial = false
  110. }
  111. }
  112. Connections
  113. {
  114. target: materialsModel
  115. onItemsChanged: updateAfterModelChanges()
  116. }
  117. Connections
  118. {
  119. target: genericMaterialsModel
  120. onItemsChanged: updateAfterModelChanges()
  121. }
  122. Column
  123. {
  124. width: materialList.width
  125. height: childrenRect.height
  126. MaterialsBrandSection
  127. {
  128. id: favoriteSection
  129. sectionName: "Favorites"
  130. elementsModel: favoriteMaterialsModel
  131. hasMaterialTypes: false
  132. }
  133. MaterialsBrandSection
  134. {
  135. id: genericSection
  136. sectionName: "Generic"
  137. elementsModel: genericMaterialsModel
  138. hasMaterialTypes: false
  139. }
  140. Repeater
  141. {
  142. model: materialsModel
  143. delegate: MaterialsBrandSection
  144. {
  145. id: brandSection
  146. sectionName: model.name
  147. elementsModel: model.material_types
  148. hasMaterialTypes: true
  149. }
  150. }
  151. }
  152. }